#include #include #include #include #include #include #include #include #include #define SYNTH_STATUS_ON (1 << 0) typedef struct _synth_sine { int flags; float phase; size_t sample_rate, frequency; } synth_sine; 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); } static ssize_t synth_sine_fill(synth_sine *synth, int16_t *samples, size_t count, xas_audio_stream *stream) { size_t i; for (i=0; iflags & SYNTH_STATUS_ON) { samples[i] = (int16_t)roundf((INT16_MAX >> 2) * sinf(synth->phase)); synth->phase += (2.0f * M_PI) / (stream->sample_rate / synth->frequency); } else { samples[i] = 0; } } return count; } int main(int argc, char **argv) { xas_audio_stream *stream; int duration_s = 60, frequency = 220, sample_rate = 44100; int i; if (argc != 2) { usage(argc, argv, "No output file provided"); } if ((stream = xas_riff_file_open(argv[1], 2, sample_rate, 1, O_WRONLY | O_CREAT | O_TRUNC)) == NULL) { goto error_riff_file_open; } for (i=0; i < duration_s * frequency; i++) { float phase; for (phase = 0.0; phase < 2 * M_PI; phase += (2 * M_PI) / (sample_rate / frequency)) { int16_t value = (int16_t)round((INT16_MAX >> 2) * sin(phase)); xas_audio_stream_write(stream, &value, 1); } } xas_audio_stream_flush(stream); xas_audio_stream_destroy(stream); return EX_OK; error_riff_file_open: return EX_OSERR; }