1+ //go:build !nolint
2+
13package audio
24
35import (
@@ -54,7 +56,7 @@ int jetkvm_audio_read_encode(void *opus_buf) {
5456 short pcm_buffer[1920]; // max 2ch*960
5557 unsigned char *out = (unsigned char*)opus_buf;
5658 int pcm_rc = snd_pcm_readi(pcm_handle, pcm_buffer, frame_size);
57-
59+
5860 // Handle ALSA errors with recovery
5961 if (pcm_rc < 0) {
6062 if (pcm_rc == -EPIPE) {
@@ -70,12 +72,12 @@ int jetkvm_audio_read_encode(void *opus_buf) {
7072 return -1;
7173 }
7274 }
73-
75+
7476 // If we got fewer frames than expected, pad with silence
7577 if (pcm_rc < frame_size) {
7678 memset(&pcm_buffer[pcm_rc * channels], 0, (frame_size - pcm_rc) * channels * sizeof(short));
7779 }
78-
80+
7981 int nb_bytes = opus_encode(encoder, pcm_buffer, frame_size, out, max_packet_size);
8082 return nb_bytes;
8183}
@@ -85,15 +87,15 @@ int jetkvm_audio_playback_init() {
8587 int err;
8688 snd_pcm_hw_params_t *params;
8789 if (pcm_playback_handle) return 0;
88-
90+
8991 // Try to open the USB gadget audio device for playback
9092 // This should correspond to the capture endpoint of the USB gadget
9193 if (snd_pcm_open(&pcm_playback_handle, "hw:1,0", SND_PCM_STREAM_PLAYBACK, 0) < 0) {
9294 // Fallback to default device if hw:1,0 doesn't work for playback
9395 if (snd_pcm_open(&pcm_playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0)
9496 return -1;
9597 }
96-
98+
9799 snd_pcm_hw_params_malloc(¶ms);
98100 snd_pcm_hw_params_any(pcm_playback_handle, params);
99101 snd_pcm_hw_params_set_access(pcm_playback_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
@@ -104,23 +106,23 @@ int jetkvm_audio_playback_init() {
104106 snd_pcm_hw_params(pcm_playback_handle, params);
105107 snd_pcm_hw_params_free(params);
106108 snd_pcm_prepare(pcm_playback_handle);
107-
109+
108110 // Initialize Opus decoder
109111 decoder = opus_decoder_create(sample_rate, channels, &err);
110112 if (!decoder) return -2;
111-
113+
112114 return 0;
113115}
114116
115117// Decode Opus and write PCM to playback device
116118int jetkvm_audio_decode_write(void *opus_buf, int opus_size) {
117119 short pcm_buffer[1920]; // max 2ch*960
118120 unsigned char *in = (unsigned char*)opus_buf;
119-
121+
120122 // Decode Opus to PCM
121123 int pcm_frames = opus_decode(decoder, in, opus_size, pcm_buffer, frame_size, 0);
122124 if (pcm_frames < 0) return -1;
123-
125+
124126 // Write PCM to playback device
125127 int pcm_rc = snd_pcm_writei(pcm_playback_handle, pcm_buffer, pcm_frames);
126128 if (pcm_rc < 0) {
@@ -131,7 +133,7 @@ int jetkvm_audio_decode_write(void *opus_buf, int opus_size) {
131133 }
132134 if (pcm_rc < 0) return -2;
133135 }
134-
136+
135137 return pcm_frames;
136138}
137139
@@ -148,8 +150,6 @@ void jetkvm_audio_close() {
148150*/
149151import "C"
150152
151-
152-
153153// Go wrappers for initializing, starting, stopping, and controlling audio
154154func cgoAudioInit () error {
155155 ret := C .jetkvm_audio_init ()
@@ -179,8 +179,6 @@ func cgoAudioReadEncode(buf []byte) (int, error) {
179179 return int (n ), nil
180180}
181181
182-
183-
184182// Go wrappers for audio playback (microphone input)
185183func cgoAudioPlaybackInit () error {
186184 ret := C .jetkvm_audio_playback_init ()
@@ -206,8 +204,6 @@ func cgoAudioDecodeWrite(buf []byte) (int, error) {
206204 return int (n ), nil
207205}
208206
209-
210-
211207// Wrapper functions for non-blocking audio manager
212208func CGOAudioInit () error {
213209 return cgoAudioInit ()
0 commit comments