diff options
Diffstat (limited to 'examples/test.c')
-rw-r--r-- | examples/test.c | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/examples/test.c b/examples/test.c new file mode 100644 index 0000000..05c5114 --- /dev/null +++ b/examples/test.c @@ -0,0 +1,134 @@ +#include "mmband/api/adsr.h" +#include <stdio.h> +#include <stdlib.h> + +#include <portaudio.h> +#include <mmband/api/instrument.h> + +#define SAMPLE_RATE 44100 +#define FRAMES_PER_BUFFER 512 + +// TODO: make adsr depend on sample rate + +int main (void) { + PaStream *stream; + PaError err = Pa_Initialize(); + if (err != paNoError) { + printf("PortAudio error: %s\n", Pa_GetErrorText(err)); + return 1; + } + + PaStreamParameters output_params; + output_params.device = Pa_GetDefaultOutputDevice(); + output_params.channelCount = 2; + output_params.sampleFormat = paFloat32; + output_params.suggestedLatency = Pa_GetDeviceInfo(output_params.device)->defaultHighInputLatency; + output_params.hostApiSpecificStreamInfo = NULL; + + printf("Opening stream.\n"); + err = Pa_OpenStream( + &stream, + NULL, + &output_params, + SAMPLE_RATE, + FRAMES_PER_BUFFER, + paClipOff, + NULL, + NULL + ); + if (err != paNoError) { + printf("PortAudio error: %s\n", Pa_GetErrorText(err)); + return 1; + } + + instrument_ref instrument = ins_new(); + + ins_set_carrier(instrument, OSC_SINE); + ins_set_modulator(instrument, OSC_SINE); + ins_set_carrier_amplitude(instrument, 0.25); + ins_set_modulator_amplitude(instrument, 0.25); + ins_set_frequency_mult(instrument, 3.0); + + adsr_ref adsr = adsr_new(); + + adsr_set_attack(adsr, 0.2); + adsr_set_decay(adsr, 0.2); + adsr_set_sustain(adsr, 0.85); + adsr_set_release(adsr, 0.5); + + int switch_frames = (60.0 / 120.0) * SAMPLE_RATE; + int iteration = 0; + int state = 0; + + printf("Starting stream.\n"); + err = Pa_StartStream(stream); + if (err != paNoError) { + printf("PortAudio error: %s\n", Pa_GetErrorText(err)); + return 1; + } + + ins_set_frequency(instrument, 246.9); + for (int i = 0; i < (60 * SAMPLE_RATE) / FRAMES_PER_BUFFER; ++i) { + float buffer[FRAMES_PER_BUFFER][2]; + for (int j = 0; j < FRAMES_PER_BUFFER; j++) { + iteration++; + + if (iteration >= switch_frames) { + switch (state) { + case 0: + ins_set_frequency(instrument, 329.6); + //adsr_reset(adsr); + state++; + break; + case 1: + ins_set_frequency(instrument, 370.0); + //adsr_reset(adsr); + state++; + break; + case 2: + ins_set_frequency(instrument, 440.0); + //adsr_reset(adsr); + state++; + break; + case 3: + ins_set_frequency(instrument, 370.0); + //adsr_reset(adsr); + state++; + break; + case 4: + ins_set_frequency(instrument, 329.6); + //adsr_reset(adsr); + state++; + break; + default: + ins_set_frequency(instrument, 246.9); + //adsr_reset(adsr); + state = 0; + break; + } + + iteration = 0; + } + + //float env = adsr_tick(adsr, SAMPLE_RATE); + float ins = ins_tick(instrument, SAMPLE_RATE); + buffer[j][0] = ins;// * env; + buffer[j][1] = ins;// * env; + } + + err = Pa_WriteStream(stream, buffer, FRAMES_PER_BUFFER); + if (err != paNoError) { + printf("PortAudio error: %s\n", Pa_GetErrorText(err)); + return 1; + } + } + + Pa_StopStream(stream); + Pa_CloseStream(stream); + + adsr_free(adsr); + ins_free(instrument); + + Pa_Terminate(); + return 0; +} |