1
+ //go:build !nolint
2
+
1
3
package audio
2
4
3
5
import (
@@ -54,7 +56,7 @@ int jetkvm_audio_read_encode(void *opus_buf) {
54
56
short pcm_buffer[1920]; // max 2ch*960
55
57
unsigned char *out = (unsigned char*)opus_buf;
56
58
int pcm_rc = snd_pcm_readi(pcm_handle, pcm_buffer, frame_size);
57
-
59
+
58
60
// Handle ALSA errors with recovery
59
61
if (pcm_rc < 0) {
60
62
if (pcm_rc == -EPIPE) {
@@ -70,12 +72,12 @@ int jetkvm_audio_read_encode(void *opus_buf) {
70
72
return -1;
71
73
}
72
74
}
73
-
75
+
74
76
// If we got fewer frames than expected, pad with silence
75
77
if (pcm_rc < frame_size) {
76
78
memset(&pcm_buffer[pcm_rc * channels], 0, (frame_size - pcm_rc) * channels * sizeof(short));
77
79
}
78
-
80
+
79
81
int nb_bytes = opus_encode(encoder, pcm_buffer, frame_size, out, max_packet_size);
80
82
return nb_bytes;
81
83
}
@@ -85,15 +87,15 @@ int jetkvm_audio_playback_init() {
85
87
int err;
86
88
snd_pcm_hw_params_t *params;
87
89
if (pcm_playback_handle) return 0;
88
-
90
+
89
91
// Try to open the USB gadget audio device for playback
90
92
// This should correspond to the capture endpoint of the USB gadget
91
93
if (snd_pcm_open(&pcm_playback_handle, "hw:1,0", SND_PCM_STREAM_PLAYBACK, 0) < 0) {
92
94
// Fallback to default device if hw:1,0 doesn't work for playback
93
95
if (snd_pcm_open(&pcm_playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0)
94
96
return -1;
95
97
}
96
-
98
+
97
99
snd_pcm_hw_params_malloc(¶ms);
98
100
snd_pcm_hw_params_any(pcm_playback_handle, params);
99
101
snd_pcm_hw_params_set_access(pcm_playback_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
@@ -104,23 +106,23 @@ int jetkvm_audio_playback_init() {
104
106
snd_pcm_hw_params(pcm_playback_handle, params);
105
107
snd_pcm_hw_params_free(params);
106
108
snd_pcm_prepare(pcm_playback_handle);
107
-
109
+
108
110
// Initialize Opus decoder
109
111
decoder = opus_decoder_create(sample_rate, channels, &err);
110
112
if (!decoder) return -2;
111
-
113
+
112
114
return 0;
113
115
}
114
116
115
117
// Decode Opus and write PCM to playback device
116
118
int jetkvm_audio_decode_write(void *opus_buf, int opus_size) {
117
119
short pcm_buffer[1920]; // max 2ch*960
118
120
unsigned char *in = (unsigned char*)opus_buf;
119
-
121
+
120
122
// Decode Opus to PCM
121
123
int pcm_frames = opus_decode(decoder, in, opus_size, pcm_buffer, frame_size, 0);
122
124
if (pcm_frames < 0) return -1;
123
-
125
+
124
126
// Write PCM to playback device
125
127
int pcm_rc = snd_pcm_writei(pcm_playback_handle, pcm_buffer, pcm_frames);
126
128
if (pcm_rc < 0) {
@@ -131,7 +133,7 @@ int jetkvm_audio_decode_write(void *opus_buf, int opus_size) {
131
133
}
132
134
if (pcm_rc < 0) return -2;
133
135
}
134
-
136
+
135
137
return pcm_frames;
136
138
}
137
139
@@ -148,8 +150,6 @@ void jetkvm_audio_close() {
148
150
*/
149
151
import "C"
150
152
151
-
152
-
153
153
// Go wrappers for initializing, starting, stopping, and controlling audio
154
154
func cgoAudioInit () error {
155
155
ret := C .jetkvm_audio_init ()
@@ -179,8 +179,6 @@ func cgoAudioReadEncode(buf []byte) (int, error) {
179
179
return int (n ), nil
180
180
}
181
181
182
-
183
-
184
182
// Go wrappers for audio playback (microphone input)
185
183
func cgoAudioPlaybackInit () error {
186
184
ret := C .jetkvm_audio_playback_init ()
@@ -206,8 +204,6 @@ func cgoAudioDecodeWrite(buf []byte) (int, error) {
206
204
return int (n ), nil
207
205
}
208
206
209
-
210
-
211
207
// Wrapper functions for non-blocking audio manager
212
208
func CGOAudioInit () error {
213
209
return cgoAudioInit ()
0 commit comments