Tscope5
audio02.c
////////////////////////////////////////////////////////////////////////////////
//
// __ ______
// / /_______________ ____ ___ / ____/
// / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
// / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
// \__/____/\___/\____/ .___/\___/ /_____/
// /_/
//
// audio02.c
// - Generate a sine wave, play it and save it to a file.
////////////////////////////////////////////////////////////////////////////////
#include <tscope5.h>
#include <stdio.h>
#include <math.h>
int main()
{
// set some audio parameters
// allocate an audio sample
double seconds = 3.0;
TS5_SAMPLE *test = ts5_alloc_sample(seconds);
// get the data pointer
int16_t *data = (int16_t *)ts5_get_sample_data_pointer(test);
// we need to know the samplerate and n of channels
// and the frequency of the sine wave that we want to generate
int samplerate = ts5_get_audio_samplerate();
int channels = ts5_get_audio_channels();
int Hz = 440;
// temporary values for the sine wave
double angle, sine;
// indices for the samples
int64_t i, nsamples = seconds * samplerate * channels;
// maximum value that a signed 16-bit integer can contain
int max = 32767;
// generate the sine wave
for(i=0; i<nsamples; i++) {
seconds = (double)i / (samplerate * channels);
angle = seconds * 2 * M_PI * Hz;
sine = sin(angle);
data[i] = sine * max;
}
// play, write, free
ts5_wait(seconds);
ts5_write_sample("sine.wav", test);
return 0;
}