diff options
author | XANTRONIX Development | 2022-02-09 00:00:51 -0500 |
---|---|---|
committer | XANTRONIX Development | 2022-02-09 00:56:29 -0500 |
commit | 77976d03699f95ea28278b5fdbba392f198b5f52 (patch) | |
tree | f540fc87d774e4e7b3b99fc2d4611a963d86bfaf /examples | |
parent | a5c70a6adfaac0088e992caed5db5e3df2744ee0 (diff) | |
download | xas-77976d03699f95ea28278b5fdbba392f198b5f52.tar.gz xas-77976d03699f95ea28278b5fdbba392f198b5f52.tar.bz2 xas-77976d03699f95ea28278b5fdbba392f198b5f52.zip |
Implement xas_audio_format type
Changes:
* Implement xas_audio_format struct to provide sample size,
channels, and sample rate
* Refactor argument order for most functions to allow a reasonable
reading of argument types, when replacing three separate size_t
arguments with one xas_audio_format object
Diffstat (limited to 'examples')
-rw-r--r-- | examples/open.c | 6 | ||||
-rw-r--r-- | examples/say.c | 50 | ||||
-rw-r--r-- | examples/test.c | 36 |
3 files changed, 42 insertions, 50 deletions
diff --git a/examples/open.c b/examples/open.c index 4bf036b..27ededd 100644 --- a/examples/open.c +++ b/examples/open.c @@ -37,9 +37,9 @@ int main(int argc, char **argv) { } printf("channels %zu sample rate %zu sample size %zu\n", - wave->channels, - wave->sample_rate, - wave->sample_size); + wave->format.channels, + wave->format.sample_rate, + wave->format.sample_size); xas_audio_stream_destroy(wave); diff --git a/examples/say.c b/examples/say.c index 19dcb08..e0aea9a 100644 --- a/examples/say.c +++ b/examples/say.c @@ -41,13 +41,14 @@ static void usage(int argc, char **argv, const char *message, ...) { } static int16_t sine_sample(xas_synth *synth, synth_sine *sine) { - int16_t ret; static float tau = 2.0f * M_PI; + int16_t ret; + if (sine->flags & SYNTH_STATUS_ON) { ret = (int16_t)roundf((INT16_MAX >> 2) * sinf(sine->phase)); - sine->phase += tau / (synth->sample_rate / sine->frequency); + sine->phase += tau / (synth->format.sample_rate / sine->frequency); if (sine->phase > tau) { sine->phase -= tau; @@ -80,9 +81,14 @@ int main(int argc, char **argv) { *synth_r, *output; - size_t sample_rate = 44100, - buffer_size = 735, - duration_frames = 3600, + xas_audio_format format = { + .channels = XAS_AUDIO_STEREO, + .sample_size = XAS_AUDIO_PCM_16_BIT, + .sample_rate = 44100 + }; + + size_t buffer_size = 735, + duration_frames = 3600, i; if (argc != 2) { @@ -90,24 +96,17 @@ int main(int argc, char **argv) { } if ((vox = xas_vox_new("/usr/bin/text2wave", - XAS_AUDIO_PCM_16_BIT, - sample_rate, - buffer_size, - NULL)) == NULL) { + format, + buffer_size)) == NULL) { goto error_vox_new; } - if ((bank = xas_bank_new(XAS_AUDIO_PCM_16_BIT, - 44100, - 2646000, - 4)) == NULL) { + if ((bank = xas_bank_new(format, 2646000, 4)) == NULL) { goto error_bank_new; } if ((output = xas_riff_new_file(argv[1], - XAS_AUDIO_STEREO, - XAS_AUDIO_PCM_16_BIT, - sample_rate, + format, O_WRONLY | O_CREAT | O_TRUNC)) == NULL) { goto error_riff_new_file; } @@ -120,28 +119,23 @@ int main(int argc, char **argv) { goto error_bank_stream_new; } - if ((synth_l = xas_synth_new(XAS_AUDIO_PCM_16_BIT, - sample_rate, - buffer_size, - (xas_synth_callback_sample)sine_sample, + if ((synth_l = xas_synth_new((xas_synth_callback_sample)sine_sample, (xas_synth_callback_cleanup)sine_cleanup, + format, + buffer_size, &sine_channels[0])) == NULL) { goto error_synth_new_l; } - if ((synth_r = xas_synth_new(XAS_AUDIO_PCM_16_BIT, - sample_rate, - buffer_size, - (xas_synth_callback_sample)sine_sample, + if ((synth_r = xas_synth_new((xas_synth_callback_sample)sine_sample, (xas_synth_callback_cleanup)sine_cleanup, + format, + buffer_size, &sine_channels[1])) == NULL) { goto error_synth_new_r; } - if ((mixer = xas_mixer_new(XAS_AUDIO_STEREO, - XAS_AUDIO_PCM_16_BIT, - sample_rate, - buffer_size)) == NULL) { + if ((mixer = xas_mixer_new(format, buffer_size)) == NULL) { goto error_mixer_new; } diff --git a/examples/test.c b/examples/test.c index 24e6ba1..1fbd8c4 100644 --- a/examples/test.c +++ b/examples/test.c @@ -45,7 +45,7 @@ static int16_t sine_sample(xas_synth *synth, synth_sine *sine) { if (sine->flags & SYNTH_STATUS_ON) { ret = (int16_t)roundf((INT16_MAX >> 2) * sinf(sine->phase)); - sine->phase += tau / (synth->sample_rate / sine->frequency); + sine->phase += tau / (synth->format.sample_rate / sine->frequency); if (sine->phase > tau) { sine->phase -= tau; @@ -73,8 +73,13 @@ int main(int argc, char **argv) { { SYNTH_STATUS_ON, 0.0f, 420 }, }; - size_t sample_rate = 44100, - buffer_size = 4096, + xas_audio_format format = { + .channels = XAS_AUDIO_STEREO, + .sample_size = XAS_AUDIO_PCM_16_BIT, + .sample_rate = 44100 + }; + + size_t buffer_size = 4096, duration_s = 60, i; @@ -83,35 +88,28 @@ int main(int argc, char **argv) { } if ((wave = xas_riff_new_file(argv[1], - XAS_AUDIO_STEREO, - XAS_AUDIO_PCM_16_BIT, - sample_rate, + format, O_WRONLY | O_CREAT | O_TRUNC)) == NULL) { goto error_riff_new_file; } - if ((synth_l = xas_synth_new(XAS_AUDIO_PCM_16_BIT, - sample_rate, - buffer_size, - (xas_synth_callback_sample)sine_sample, + if ((synth_l = xas_synth_new((xas_synth_callback_sample)sine_sample, (xas_synth_callback_cleanup)sine_cleanup, + format, + buffer_size, &sine_channels[0])) == NULL) { goto error_synth_new_l; } - if ((synth_r = xas_synth_new(XAS_AUDIO_PCM_16_BIT, - sample_rate, - buffer_size, - (xas_synth_callback_sample)sine_sample, + if ((synth_r = xas_synth_new((xas_synth_callback_sample)sine_sample, (xas_synth_callback_cleanup)sine_cleanup, + format, + buffer_size, &sine_channels[1])) == NULL) { goto error_synth_new_r; } - if ((mixer = xas_mixer_new(XAS_AUDIO_STEREO, - XAS_AUDIO_PCM_16_BIT, - sample_rate, - buffer_size)) == NULL) { + if ((mixer = xas_mixer_new(format, buffer_size)) == NULL) { goto error_mixer_new; } @@ -129,7 +127,7 @@ int main(int argc, char **argv) { if ((readlen = xas_audio_stream_read(mixer->output, &buf, - sample_rate)) < 0) { + buffer_size)) < 0) { goto error_audio_stream_read; } |