1
0

In audio.c/.h arg/var naming style was changed.

This commit is contained in:
Alexander Andreev 2024-03-22 03:30:12 +04:00
parent b233d8bc43
commit e075a99a28
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
2 changed files with 50 additions and 50 deletions

View File

@ -4,38 +4,38 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
int audio_init(audio_t *aud, int channels, int sampleRate, int frameSize) { int audio_init(audio_t *aud, int channels, int sample_rate, int frame_size) {
PaError paErr; PaError pa_err;
int opusErr; int opus_err;
if ((paErr = Pa_Initialize()) != paNoError) { if ((pa_err = Pa_Initialize()) != paNoError) {
fprintf(stderr, "Cannot initialise PortAudio: %s\n", Pa_GetErrorText(paErr)); fprintf(stderr, "Cannot initialise PortAudio: %s\n", Pa_GetErrorText(pa_err));
return -1; return -1;
} }
aud->opusEnc = opus_encoder_create(sampleRate, channels, OPUS_APPLICATION_VOIP, &opusErr); aud->opus_enc = opus_encoder_create(sample_rate, channels, OPUS_APPLICATION_VOIP, &opus_err);
if (opusErr != OPUS_OK) { if (opus_err != OPUS_OK) {
fprintf(stderr, "An Opus encoder cannot be created: %s\n", opus_strerror(opusErr)); fprintf(stderr, "An Opus encoder cannot be created: %s\n", opus_strerror(opus_err));
return -1; return -1;
} }
aud->opusDec = opus_decoder_create(sampleRate, channels, &opusErr); aud->opus_dec = opus_decoder_create(sample_rate, channels, &opus_err);
if (opusErr != OPUS_OK) { if (opus_err != OPUS_OK) {
fprintf(stderr, "An Opus decoder cannot be created: %s\n", opus_strerror(opusErr)); fprintf(stderr, "An Opus decoder cannot be created: %s\n", opus_strerror(opus_err));
return -1; return -1;
} }
if ((paErr = Pa_OpenDefaultStream(&aud->stream, channels, channels, AUDIO_SAMPLE_FORMAT, if ((pa_err = Pa_OpenDefaultStream(&aud->stream, channels, channels, AUDIO_SAMPLE_FORMAT,
sampleRate, frameSize, NULL, NULL)) != paNoError) { sample_rate, frame_size, NULL, NULL)) != paNoError) {
fprintf(stderr, "Cannot open a PortAudio stream: %s\n", Pa_GetErrorText(paErr)); fprintf(stderr, "Cannot open a PortAudio stream: %s\n", Pa_GetErrorText(pa_err));
return -1; return -1;
} }
aud->bufferSz = frameSize * channels; aud->buffer_size = frame_size * channels;
aud->buffer = (audio_sample_t *)malloc(aud->bufferSz * sizeof(audio_sample_t)); aud->buffer = (audio_sample_t *)malloc(aud->buffer_size * sizeof(audio_sample_t));
if ((paErr = Pa_StartStream(aud->stream)) != paNoError) { if ((pa_err = Pa_StartStream(aud->stream)) != paNoError) {
fprintf(stderr, "Cannot start a PortAudio stream: %s\n", Pa_GetErrorText(paErr)); fprintf(stderr, "Cannot start a PortAudio stream: %s\n", Pa_GetErrorText(pa_err));
return -1; return -1;
} }
@ -43,53 +43,53 @@ int audio_init(audio_t *aud, int channels, int sampleRate, int frameSize) {
} }
int audio_destroy(audio_t *aud) { int audio_destroy(audio_t *aud) {
PaError paErr; PaError pa_err;
if (aud->stream) { if (aud->stream) {
if ((paErr = Pa_StopStream(aud->stream)) != paNoError) { if ((pa_err = Pa_StopStream(aud->stream)) != paNoError) {
fprintf(stderr, "Cannot stop a PortAudio stream: %s\n", Pa_GetErrorText(paErr)); fprintf(stderr, "Cannot stop a PortAudio stream: %s\n", Pa_GetErrorText(pa_err));
return -1; return -1;
} }
if ((paErr = Pa_CloseStream(aud->stream)) != paNoError) { if ((pa_err = Pa_CloseStream(aud->stream)) != paNoError) {
fprintf(stderr, "Cannot close a PortAudio stream: %s\n", Pa_GetErrorText(paErr)); fprintf(stderr, "Cannot close a PortAudio stream: %s\n", Pa_GetErrorText(pa_err));
return -1; return -1;
} }
} }
if ((paErr = Pa_Terminate()) != paNoError) { if ((pa_err = Pa_Terminate()) != paNoError) {
fprintf(stderr, "Cannot terminate PortAudio: %s\n", Pa_GetErrorText(paErr)); fprintf(stderr, "Cannot terminate PortAudio: %s\n", Pa_GetErrorText(pa_err));
return -1; return -1;
} }
if (aud->buffer) if (aud->buffer)
free(aud->buffer); free(aud->buffer);
if (aud->opusEnc) if (aud->opus_enc)
opus_encoder_destroy(aud->opusEnc); opus_encoder_destroy(aud->opus_enc);
if (aud->opusDec) if (aud->opus_dec)
opus_decoder_destroy(aud->opusDec); opus_decoder_destroy(aud->opus_dec);
return 0; return 0;
} }
/* /*
int encLen = opus_encode_float(aud->opusEnc, (float *)input, frameCount, aud->opusBuf, aud->opusBufSz); int encLen = opus_encode_float(aud->opus_enc, (float *)input, frameCount, aud->opusBuf, aud->opusBufSz);
(void)encLen; (void)encLen;
int frameSize = opus_decode_float(aud->opusDec, aud->opusBuf, aud->opusBufSz, (float *)output, frameCount, 0); int frame_size = opus_decode_float(aud->opus_dec, aud->opusBuf, aud->opusBufSz, (float *)output, frameCount, 0);
(void)frameSize; (void)frame_size;
*/ */
int audio_read(audio_t *aud, uint8_t *outputData, size_t outputLen) { int audio_read(audio_t *aud, uint8_t *output, size_t output_len) {
PaError paErr; PaError pa_err;
if ((paErr = Pa_ReadStream(aud->stream, aud->buffer, aud->bufferSz)) != paNoError) { if ((pa_err = Pa_ReadStream(aud->stream, aud->buffer, aud->buffer_size)) != paNoError) {
fprintf(stderr, "Cannot read from a PortAudio stream: %s\n", Pa_GetErrorText(paErr)); fprintf(stderr, "Cannot read from a PortAudio stream: %s\n", Pa_GetErrorText(pa_err));
return -1; return -1;
} }
int encodedLen = opus_encode_float(aud->opusEnc, aud->buffer, aud->bufferSz, outputData, outputLen); int encodedLen = opus_encode_float(aud->opus_enc, aud->buffer, aud->buffer_size, output, output_len);
if (encodedLen < 0) { if (encodedLen < 0) {
fprintf(stderr, "Opus failed to encode: %s\n", opus_strerror(encodedLen)); fprintf(stderr, "Opus failed to encode: %s\n", opus_strerror(encodedLen));
return -1; return -1;
@ -98,17 +98,17 @@ int audio_read(audio_t *aud, uint8_t *outputData, size_t outputLen) {
return 0; return 0;
} }
int audio_write(audio_t *aud, const uint8_t *inputData, size_t inputLen) { int audio_write(audio_t *aud, const uint8_t *input, size_t input_len) {
PaError paErr; PaError pa_err;
int frameSize = opus_decode_float(aud->opusDec, inputData, inputLen, aud->buffer, aud->bufferSz, 0); int frame_size = opus_decode_float(aud->opus_dec, input, input_len, aud->buffer, aud->buffer_size, 0);
if (frameSize < 0) { if (frame_size < 0) {
fprintf(stderr, "Opus failed to decode: %s\n", opus_strerror(frameSize)); fprintf(stderr, "Opus failed to decode: %s\n", opus_strerror(frame_size));
return -1; return -1;
} }
if ((paErr = Pa_WriteStream(aud->stream, aud->buffer, aud->bufferSz)) != paNoError) { if ((pa_err = Pa_WriteStream(aud->stream, aud->buffer, aud->buffer_size)) != paNoError) {
fprintf(stderr, "Cannot write to a PortAudio stream: %s\n", Pa_GetErrorText(paErr)); fprintf(stderr, "Cannot write to a PortAudio stream: %s\n", Pa_GetErrorText(pa_err));
return -1; return -1;
} }

View File

@ -12,17 +12,17 @@ typedef float audio_sample_t;
typedef struct audio_t { typedef struct audio_t {
PaStream *stream; PaStream *stream;
OpusEncoder *opusEnc; OpusEncoder *opus_enc;
OpusDecoder *opusDec; OpusDecoder *opus_dec;
audio_sample_t *buffer; audio_sample_t *buffer;
size_t bufferSz; size_t buffer_size;
} audio_t; } audio_t;
int audio_init(audio_t *aud, int channels, int sampleRate, int frameSize); int audio_init(audio_t *aud, int channels, int sample_rate, int frame_size);
int audio_destroy(audio_t *aud); int audio_destroy(audio_t *aud);
int audio_read(audio_t *aud, uint8_t *outputData, size_t outputLen); int audio_read(audio_t *aud, uint8_t *output, size_t output_len);
int audio_write(audio_t *aud, const uint8_t *inputData, size_t inputLen); int audio_write(audio_t *aud, const uint8_t *input, size_t input_len);
#endif /* _AUDIO_H_ */ #endif /* _AUDIO_H_ */