• Audio
  • Reducing Sampling Rate

I wanted to change the analog sampling rate to 8000 Hz. I found a post regarding this here, which says for changing from 44.1 kHz to 8 kHz;

sed -i s/44100/8000/g /root/Bela/core/*_Codec.cpp

I ran the command in the Bela IDE and found this as error so i reverted back to 44.1kHz.

I am attaching the code which i ran. Can anyone help regarding this ?

#include <Bela.h>
#include <cmath>
#include <iostream>
#include <libraries/Scope/Scope.h>
#include <libraries/Biquad/Biquad.h>

Scope scope;
float amplitude;
float gFrequency = 600.0;
float gPhase;
float gInverseSampleRate;
int gAudioFramesPerAnalogFrame = 0;
int InChannel = 0;

Biquad lpFilter;	// Biquad low-pass frequency;
Biquad hpFilter;	// Biquad high-pass frequency;
float gHPfreq = 200.0;	// Cut-off frequency for high-pass filter (Hz)
float gLPfreq = 800.0;	// Cut-off frequency for low-pass filter (Hz)
float gFilterQ = 0.5; // Quality factor for the biquad filters to provide a Butterworth response (0.707 default)

// Variables for DC bias removal
float gDcBias = 0.0;

// DC bias removal
float DcBiasRemove(float rawValue, float &gDcBias) {
    const float gDcBiasAlpha = 0.99;

    // Remove DC bias using a simple low-pass filter
    gDcBias = gDcBiasAlpha * gDcBias + (1.0 - gDcBiasAlpha) * rawValue;
    float amplitude = rawValue - gDcBias;
    return amplitude;
}

bool setup(BelaContext *context, void *userData)
{
	// setup the scope with 1 channel at the audio sample rate
	scope.setup(3, context->audioSampleRate);
	
	// Low pass filter settings
	Biquad::Settings settings{
			.fs = context->audioSampleRate,
			.type = Biquad::lowpass,
			.cutoff = gLPfreq,
			.q = gFilterQ,
			.peakGainDb = 0,
			};
	lpFilter.setup(settings);
	settings.cutoff = gHPfreq;
	settings.type = Biquad::highpass;
	hpFilter.setup(settings);
	
	// Check if analog channels are enabled
	if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) {
		rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
		return false;
	}

	// Useful calculations
	gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
	gInverseSampleRate = 1.0 / context->audioSampleRate;
	gPhase = 0.0;

	return true;
}

void render(BelaContext *context, void *userData)
{
	for(unsigned int n = 0; n < context->audioFrames; n++) {
		float out = sinf(gPhase);
		gPhase += 2.0f * (float)M_PI * gFrequency * gInverseSampleRate;
		if(gPhase > M_PI)
			gPhase -= 2.0f * (float)M_PI;

		for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
			audioWrite(context, n, channel, out);
		}
		
		// static float frequency;
		if(gAudioFramesPerAnalogFrame && !(n % gAudioFramesPerAnalogFrame)) {
			
				amplitude = analogRead(context, n/gAudioFramesPerAnalogFrame, InChannel);
		}
		
		//amplitude = DcBiasRemove(amplitude, gDcBias);
		float amplitude1 = hpFilter.process(amplitude);
		amplitude1 = lpFilter.process(amplitude1);
		// log the sine wave and sensor values on the scope
		scope.log(200*amplitude1, 200*amplitude, out);
	}
}

void cleanup(BelaContext *context, void *userData)
{

}

Try adding --verbose to the user command line options box in the project settings. That should print some additional useful debugging information.

    5 days later

    giuliomoro

    I am getting this response after adding --verbose to user command line options

    Xenomai not explicitly inited
    Xenomai is going to be inited by us
    Bela_initAudio()
    Starting with period size 64 ;analog enabled
    DAC level 999999.000000 dB; ADC level 999999.000000 dB; headphone level 999999.000000 dB
    Detected hardware: BelaRevC
    Hardware specified at the command line: NoHardware
    Hardware specified in the user's belaconfig: NoHardware
    Hardware to be used: BelaRevC
    Codec mode: 0 ()
    gFifoFactor: 1
    core audioFrames: 64
    Project name: dawd
    PRU memory mapped to ARM:
    digital: 0xb62ac000 0xb62ac400
    audio: 0xb62ad000 0xb62ad500 0xb62ada00 0xb62adb00
    analog: 0xb629d000 0xb629d200 0xb629d400 0xb629d600
    analog offset: 0xffff2000 0xffff2200 0xffff2400 0xffff2600
    I2c_Codec: sample rate 8000.000000 out of range
    AuxTaskNonRT WSServer_5432 starting
    AuxTaskNonRT WSClient_scope_data starting
    AuxTaskNonRT WSClient_scope_control starting
    AuxTaskRT scope-trigger-task starting
    Bela_startAudio
    startAudioInline
    AudioCodec parameters:
    slotSize: 32
    startingSlot: 0
    bitDelay: 1
    mclk: 2.4e+07
    samplingRate: 8000
    dualRate: 0
    tdmMode: 2
    bclk: 1
    wclk: 1
    MCLK: 24.0000 MHz, fs(ref): 8000.0000, P: 2, R: 1, J: 6, D: 8267, NCODEC: 5.000000, Achieved Fs: 8000.039063 (err: 0.0005%),
    w 0x98 192 0x03 // //Set GPIO1 (MCLK) pad to input mode, Invert CLKHV phase for better DNR
    w 0x98 193 0xc3 // //Set PLL Bypass, Remove 10k DVDD shunt to ground, set PLL input to MCLK, enable the PLL clock inputs
    w 0x98 202 0x40 // //Set PLL Parameters
    w 0x90 1 0xff // //Enable Interpolation and modulator clocks for all 8 channels
    Stopping audio...
    Bela_cleanupAudio()
    AuxTaskNonRT WSServer_5432 exiting
    AuxTaskNonRT WSClient_scope_data exiting
    Failed to write register 1 on Es9080 codec
    Error writing
    Error: unable to start audio codec
    Error: unable to start real-time audio
    Failed to join audio thread: (3) No such process
    AuxTaskNonRT WSClient_scope_control exiting
    AuxTaskRT scope-trigger-task exiting
    Makefile:613: recipe for target 'runide' failed
    make: *** [runide] Error 1
    Bela stopped

    Can you try some larger value such as 32000 and then go down from there?