diff options
Diffstat (limited to 'examples/say.c')
-rw-r--r-- | examples/say.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/examples/say.c b/examples/say.c new file mode 100644 index 0000000..92d2b19 --- /dev/null +++ b/examples/say.c @@ -0,0 +1,120 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <sysexits.h> +#include <fcntl.h> +#include <math.h> + +#include <xas/audio.h> +#include <xas/mixer.h> +#include <xas/riff.h> +#include <xas/vox.h> + +static void usage(int argc, char **argv, const char *message, ...) { + va_list args; + + va_start(args, message); + + if (message) { + vfprintf(stderr, message, args); + fputc('\n', stderr); + } + + va_end(args); + + fprintf(stderr, "usage: %s output.wav\n", argv[0]); + + exit(EX_USAGE); +} + +int main(int argc, char **argv) { + xas_mixer *mixer; + + xas_audio_stream *input, + *output; + + xas_vox *vox; + + size_t sample_rate = 44100, + buffer_size = 4096, + duration_s = 60, + i; + + if (argc != 2) { + usage(argc, argv, "No output file provided"); + } + + if ((output = xas_riff_new_file(argv[1], + XAS_AUDIO_STREAM_PCM_16_BIT, + sample_rate, + XAS_AUDIO_STREAM_STEREO, + O_WRONLY | O_CREAT | O_TRUNC)) == NULL) { + goto error_riff_new_file; + } + + if ((vox = xas_vox_new("/usr/bin/text2wave", + XAS_AUDIO_STREAM_PCM_16_BIT, + sample_rate, + buffer_size, + NULL)) == NULL) { + goto error_vox_new; + } + + if ((input = xas_vox_stream_new(vox)) == NULL) { + goto error_vox_stream_new; + } + + if ((mixer = xas_mixer_new(XAS_AUDIO_STREAM_PCM_16_BIT, + sample_rate, + XAS_AUDIO_STREAM_STEREO, + buffer_size)) == NULL) { + goto error_mixer_new; + } + + if (xas_mixer_input_add(mixer, input, 1.0, -1.0) == NULL) { + goto error_mixer_input_add; + } + + xas_vox_say(vox, "I want to eat your soul. You don't understand. I really want to eat your soul.\n"); + + for (i=0; i<duration_s; i++) { + void *buf; + ssize_t readlen; + + if ((readlen = xas_audio_stream_read(mixer->output, + &buf, + buffer_size)) < 0) { + goto error_audio_stream_read; + } + + if (xas_audio_stream_write(output, buf, readlen) < 0) { + goto error_audio_stream_write; + } + } + + xas_audio_stream_flush(output); + + xas_mixer_destroy(mixer); + xas_audio_stream_destroy(input); + xas_audio_stream_destroy(output); + + return EX_OK; + +error_audio_stream_write: +error_audio_stream_read: +error_mixer_input_add: + xas_mixer_destroy(mixer); + +error_mixer_new: + xas_audio_stream_destroy(input); + +error_vox_stream_new: + xas_vox_destroy(vox); + +error_vox_new: + xas_audio_stream_destroy(output); + +error_riff_new_file: + return EX_OSERR; +} |