I am running this code on the latest master
branch, which I assume is what you are using, too:
#include <Bela.h>
#include <cmath>
#include <I2c_Codec.h>
float gFrequency = 440.0;
float gPhase;
float gInverseSampleRate;
bool setup(BelaContext *context, void *userData)
{
gInverseSampleRate = 1.0 / context->audioSampleRate;
gPhase = 0.0;
return true;
}
#include <I2c_Codec.h>
void render(BelaContext *context, void *userData) {
if(context->audioFramesElapsed == context->audioFrames * 10000)
{
extern AudioCodec* gAudioCodec;
((I2c_Codec*)gAudioCodec)->setAudioSamplingRate(22050);
rt_printf("pitch down\n");
}
for(unsigned int n = 0; n < context->audioFrames; n++) {
float out = 0.8 * sinf(gPhase);
gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
if(gPhase > 2.0 * M_PI)
gPhase -= 2.0 * M_PI;
for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
// Two equivalent ways to write this code
// The long way, using the buffers directly:
// context->audioOut[n * context->audioOutChannels + channel] = out;
// Or using the macros:
audioWrite(context, n, channel, out);
}
}
}
void cleanup(BelaContext *context, void *userData)
{
}
after approx four seconds, "pitch down" is printed to the console, the pitch of the sinewave changes and no further warning is printed. Can you try it?