Created
February 1, 2019 03:55
-
-
Save brianfay/c3fd1e3ccde42449e7f149e8de52c589 to your computer and use it in GitHub Desktop.
Modified examples to play three sine waves, with the amplitude controlled by three momentary switches connected as digital inputs. Need to do some switch debouncing to get rid of some crackling
This file contains hidden or 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 <Bela.h> | |
#include <stdio.h> | |
#include <cmath> | |
#include <digital_gpio_mapping.h> | |
float gPhase1, gPhase2, gPhase3; | |
float gInverseSampleRate; | |
bool setup(BelaContext *context, void *userData) | |
{ | |
//these should already be set by default but I'm learning | |
gInverseSampleRate = 1.0 / context->audioSampleRate; | |
pinMode(context, 0, P9_12, INPUT); | |
pinMode(context, 0, P9_14, INPUT); | |
pinMode(context, 0, P9_16, INPUT); | |
return true; | |
} | |
void render(BelaContext *context, void *userData) | |
{ | |
int btn1 = digitalRead(context, 0, P9_12); | |
int btn2 = digitalRead(context, 0, P9_14); | |
int btn3 = digitalRead(context, 0, P9_16); | |
for(unsigned int n = 0; n < context->audioFrames; n++){ | |
float out = 0.4 * sinf(gPhase1) * btn1; | |
out += 0.4 * sinf(gPhase2) * btn2; | |
out += 0.4 * sinf(gPhase3) * btn3; | |
gPhase1 += 2.0 * M_PI * 440.0 * gInverseSampleRate; | |
gPhase2 += 2.0 * M_PI * 660.0 * gInverseSampleRate; | |
gPhase3 += 2.0 * M_PI * 770.0 * gInverseSampleRate; | |
if(gPhase1 > 2.0 * M_PI) | |
gPhase1 -= 2.0 * M_PI; | |
if(gPhase2 > 2.0 * M_PI) | |
gPhase2 -= 2.0 * M_PI; | |
if(gPhase3 > 2.0 * M_PI) | |
gPhase3 -= 2.0 * M_PI; | |
for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) { | |
audioWrite(context, n, channel, out); | |
} | |
} | |
} | |
void cleanup(BelaContext *context, void *userData) | |
{ | |
printf("cleaning up\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment