Created
May 19, 2021 04:19
-
-
Save brbrr/9155763905f54b3ceb66c838c3db1e4b to your computer and use it in GitHub Desktop.
Infinite Decay state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "portaudio.h" | |
#include "daisysp.h" | |
using namespace daisysp; | |
#define NUM_SECONDS (20) | |
#define SAMPLE_RATE (48000) | |
Adsr metroEnv; | |
Oscillator metro; | |
bool beatGate = true; | |
typedef struct | |
{ | |
float left_phase; | |
float right_phase; | |
} paTestData; | |
static paTestData data; | |
void initStuff(float sampleRate) | |
{ | |
// Metronome init | |
metro.Init(sampleRate); | |
metro.SetWaveform(metro.WAVE_SIN); | |
// metro.SetFreq(523); | |
metroEnv.Init(sampleRate); | |
metroEnv.SetTime(ADSR_SEG_ATTACK, 0.01f); | |
metroEnv.SetTime(ADSR_SEG_DECAY, 0.05f); | |
metroEnv.SetTime(ADSR_SEG_RELEASE, 0.f); | |
metroEnv.SetSustainLevel(0.f); | |
} | |
void runPerCallBack(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer) | |
{ | |
float *out = (float *)outputBuffer; | |
(void)inputBuffer; /* Prevent unused variable warning. */ | |
for (unsigned int i = 0; i < framesPerBuffer; i++) | |
{ | |
float metroAmp = metroEnv.Process(beatGate); | |
metro.SetAmp(metroAmp); | |
int segment = metroEnv.GetCurrentSegment(); | |
printf("METRO TICK: q: %d\n", segment); | |
float sig = metro.Process(); | |
*out++ = sig; /* left */ | |
*out++ = sig; /* right */ | |
} | |
} | |
static int patestCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData) | |
{ | |
runPerCallBack(inputBuffer, outputBuffer, framesPerBuffer); | |
return 0; | |
} | |
/*******************************************************************/ | |
void startAudio(int patestCallback(const void *inputBuffer, void *outputBuffer, | |
unsigned long framesPerBuffer, | |
const PaStreamCallbackTimeInfo *timeInfo, | |
PaStreamCallbackFlags statusFlags, | |
void *userData)) | |
{ | |
PaStream *stream; | |
PaError err; | |
/* Initialize our data for use by callback. */ | |
data.left_phase = data.right_phase = 0.0; | |
/* Initialize library before making any other calls. */ | |
err = Pa_Initialize(); | |
if (err != paNoError) | |
{ | |
printf("Error here 0.\n"); | |
goto error; | |
} | |
/* Open an audio I/O stream. */ | |
err = Pa_OpenDefaultStream(&stream, | |
0, /* no input channels */ | |
2, /* stereo output */ | |
paFloat32, /* 32 bit floating point output */ | |
SAMPLE_RATE, | |
256, /* frames per buffer */ | |
patestCallback, | |
&data); | |
if (err != paNoError) | |
{ | |
printf("Error here 1.\n"); | |
goto error; | |
} | |
err = Pa_StartStream(stream); | |
if (err != paNoError) | |
{ | |
printf("Error here 2.\n"); | |
goto error; | |
} | |
/* Sleep for several seconds. */ | |
Pa_Sleep(NUM_SECONDS * 1000); | |
// whatever(); | |
err = Pa_StopStream(stream); | |
if (err != paNoError) | |
{ | |
printf("Error here 3.\n"); | |
goto error; | |
} | |
err = Pa_CloseStream(stream); | |
if (err != paNoError) | |
{ | |
printf("Error here 4.\n"); | |
goto error; | |
} | |
Pa_Terminate(); | |
error: | |
Pa_Terminate(); | |
fprintf(stderr, "An error occurred while using the portaudio stream\n"); | |
fprintf(stderr, "Error number: %d\n", err); | |
fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(err)); | |
} | |
int main(void) | |
{ | |
printf("PortAudio Test: output sawtooth wave.\n"); | |
initStuff(SAMPLE_RATE); | |
startAudio(patestCallback); | |
printf("Test finished.\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment