• Audio
  • Sampling at lower rate than predefined sampling rates (44.1kHz and 22.05kHz)

I want to change the default sampling rates provided in Bela IDE i.e. 44.1kHz and 22.05kHz. The sampling rate I require is 4kHz. How to change it?
I am using Bela IDE (c++) for programming.

Those are sampling rates for the analog I/O and they are actually tied to the audio sampling rate: same sampling rate (44100) when using 4 analog channels, or half sampling rate (22050) when using 8 analog channels.

The audio sampling rate can be changed down to 8000Hz using the hack explained here https://forum.bela.io/d/2358-how-can-i-change-audio-sample-rate-to-22050-hz/28 . Note that the IDE settings and even the BelaContext object in the code won't reflect that change. Again, the analog sampling rate will be the same as the audio if "44100"/4ch is selected in the IDE, or half that if 22050/8ch is selected.

a month later

Hello @giuliomoro !!

I tried following the steps you mentioned in that link. but I am getting some errors.

  • This is the only change (Sampling at 8000Hz) made to the code you have mentioned.
    ((I2c_Codec*)gAudioCodec)->setAudioSamplingRate(8000);

  • The error is mentioned below;
    Running project ...
    I2c_Codec: error, no valid PLL settings found
    pitch down
    Underrun detected: 4 blocks dropped
    1 mode switch detected on the audio thread.
    Bela stopped

  • I can't differentiate between pitch down and before pitch down.

  • Bela version details.

Any help will be appreciated. Thank you.

    ACMLKgp
    I2c_Codec: error, no valid PLL settings found
    pitch down
    Underrun detected: 4 blocks dropped

    That's unexpected, it works for me when using 8000. Anything below that will fail, though. Working example:

    #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(8000);
                    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)
    {}