From 630a4f78038b256068ecd3dcee2deb6e9643737e Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 24 Nov 2024 04:14:54 +0400 Subject: [PATCH] Updated audio module. --- src/audio.c | 25 +++++++++++++------------ src/audio.h | 13 +++++++------ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/audio.c b/src/audio.c index ca09ccd..e6c18ad 100644 --- a/src/audio.c +++ b/src/audio.c @@ -1,6 +1,5 @@ #include "audio.h" -#include #include #include @@ -26,10 +25,12 @@ int audio_terminate_soundsystem(void) { return 0; } -int audio_init_default(audio_t *aud, int channels, int sample_rate, int frame_size) { +int audio_init_default(audio_t *const aud, int channels, int sample_rate, int frame_size) { PaError pa_err; int opus_err; + aud->frame_size = frame_size; + if ((pa_err = Pa_OpenDefaultStream(&aud->stream_in, channels, 0, AUDIO_SAMPLE_FORMAT, sample_rate, frame_size, NULL, NULL)) != paNoError) { fprintf(stderr, "Cannot open an input PortAudio stream: %s\n", Pa_GetErrorText(pa_err)); @@ -38,14 +39,16 @@ int audio_init_default(audio_t *aud, int channels, int sample_rate, int frame_si if ((pa_err = Pa_OpenDefaultStream(&aud->stream_out, 0, channels, AUDIO_SAMPLE_FORMAT, sample_rate, frame_size, NULL, NULL)) != paNoError) { - fprintf(stderr, "Cannot open an input PortAudio stream: %s\n", Pa_GetErrorText(pa_err)); + fprintf(stderr, "Cannot open an output PortAudio stream: %s\n", Pa_GetErrorText(pa_err)); return -1; } aud->buffer_size = frame_size * channels; aud->buffer = (audio_sample_t *)malloc(aud->buffer_size * sizeof(audio_sample_t)); - if (aud->buffer == NULL) + if (aud->buffer == NULL) { + fprintf(stderr, "Failed to allocate an audio buffer.\n"); return -1; + } aud->opus_enc = opus_encoder_create(sample_rate, channels, OPUS_APPLICATION_VOIP, &opus_err); if (opus_err != OPUS_OK) { @@ -62,7 +65,7 @@ int audio_init_default(audio_t *aud, int channels, int sample_rate, int frame_si return 0; } -int audio_destroy(audio_t *aud) { +int audio_destroy(audio_t *const aud) { PaError pa_err; if (aud->stream_in != NULL) { @@ -112,7 +115,6 @@ int audio_stream_input_toggle(audio_t *aud) { if ((pa_err = Pa_StartStream(aud->stream_in)) != paNoError) { fprintf(stderr, "Cannot start an input PortAudio stream: %s\n", Pa_GetErrorText(pa_err)); - free(aud->buffer); return -1; } } else { @@ -136,7 +138,6 @@ int audio_stream_output_toggle(audio_t *aud) { if ((pa_err = Pa_StartStream(aud->stream_out)) != paNoError) { fprintf(stderr, "Cannot start an output PortAudio stream: %s\n", Pa_GetErrorText(pa_err)); - free(aud->buffer); return -1; } } else { @@ -149,7 +150,7 @@ int audio_stream_output_toggle(audio_t *aud) { return 0; } -int audio_read(audio_t *aud, unsigned char *output, int output_len) { +int audio_read(audio_t *const aud, void *const output, int output_len) { PaError pa_err; if ((pa_err = Pa_ReadStream(aud->stream_in, aud->buffer, aud->buffer_size)) != paNoError) { @@ -157,7 +158,7 @@ int audio_read(audio_t *aud, unsigned char *output, int output_len) { return -1; } - int encodedLen = opus_encode_float(aud->opus_enc, aud->buffer, aud->buffer_size, output, output_len); + int encodedLen = opus_encode_float(aud->opus_enc, aud->buffer, aud->frame_size, output, output_len); if (encodedLen < 0) { fprintf(stderr, "Opus failed to encode: %s\n", opus_strerror(encodedLen)); return -1; @@ -166,16 +167,16 @@ int audio_read(audio_t *aud, unsigned char *output, int output_len) { return encodedLen; } -int audio_write(audio_t *aud, const unsigned char *input, int input_len) { +int audio_write(audio_t *const aud, const void *const input, int input_len) { PaError pa_err; - int frame_size = opus_decode_float(aud->opus_dec, input, input_len, aud->buffer, aud->buffer_size, 0); + int frame_size = opus_decode_float(aud->opus_dec, input, input_len, aud->buffer, aud->frame_size, 0); if (frame_size < 0) { fprintf(stderr, "Opus failed to decode: %s\n", opus_strerror(frame_size)); return -1; } - if ((pa_err = Pa_WriteStream(aud->stream_out, aud->buffer, aud->buffer_size)) != paNoError) { + if ((pa_err = Pa_WriteStream(aud->stream_out, aud->buffer, aud->frame_size)) != paNoError) { fprintf(stderr, "Cannot write to a PortAudio stream: %s\n", Pa_GetErrorText(pa_err)); return -1; } diff --git a/src/audio.h b/src/audio.h index f3a34c3..3182a5b 100644 --- a/src/audio.h +++ b/src/audio.h @@ -17,15 +17,16 @@ typedef struct audio_t { audio_sample_t *buffer; int buffer_size; + int frame_size; } audio_t; -int audio_init_default(audio_t *aud, int channels, int sample_rate, int frame_size); -int audio_destroy(audio_t *aud); +int audio_init_default(audio_t *const aud, int channels, int sample_rate, int frame_size); +int audio_destroy(audio_t *const aud); -int audio_stream_input_toggle(audio_t *aud); -int audio_stream_output_toggle(audio_t *aud); +int audio_stream_input_toggle(audio_t *const aud); +int audio_stream_output_toggle(audio_t *const aud); -int audio_read(audio_t *aud, unsigned char *output, int output_len); -int audio_write(audio_t *aud, const unsigned char *input, int input_len); +int audio_read(audio_t *aud, void *const output, int output_len); +int audio_write(audio_t *aud, const void *const input, int input_len); #endif /* _AUDIO_H_ */