Very well done ! I had my bit reversed, despite double checking twice.
Don't worry about the values returned by getAudioSamplingRate(), there is probably some difference in how the float division is handled in my code vs the PLL. You can check your P, L, J, D values against Table 1. Typical MCLK Rates in the datasheet.
Regarding CPU usage, there is some overhead there due to converting ints to floats and back to ints in PRU.cpp. If you disable analog and digital channels, the CPU usage when idle should drop substantially.
Also, Pd in general and its implementation of [osc~] are not very efficient.
This is the final patch to get 8kHz sampling rate:
diff --git a/core/I2c_Codec.cpp b/core/I2c_Codec.cpp
index aaa754e..b1af4ae 100644
--- a/core/I2c_Codec.cpp
+++ b/core/I2c_Codec.cpp
@@ -41,7 +41,7 @@ int I2c_Codec::initCodec()
int I2c_Codec::startAudio(int dual_rate)
{
// see datasehet for TLV320AIC3104 from page 44
- if(writeRegister(0x02, 0x00)) // Codec sample rate register: fs_ref / 1
+ if(writeRegister(0x02, 0xAA)) // Codec sample rate register: fs_ref / 6
return 1;
// The sampling frequency is given as f_{S(ref)} = (PLLCLK_IN × K × R)/(2048 × P)
// The master clock PLLCLK_IN is 12MHz
@@ -49,18 +49,18 @@ int I2c_Codec::startAudio(int dual_rate)
// using P=8 and R=1 gives a resolution of 0.0732421875Hz ( 0.000166% at 44.1kHz)
// to obtain Fs=44100 we need to have K=60.2112
- if(setPllP(8))
+ if(setPllP(1))
return 1;
if(setPllR(1))
return 1;
- if(setAudioSamplingRate(44100)) //this will automatically find and set K for the given P and R so that Fs=44100
+ if(setAudioSamplingRate(48000)) //this will automatically find and set K for the given P and R so that Fs=48000
return 1;
if(dual_rate) {
if(writeRegister(0x07, 0xEA)) // Codec datapath register: 44.1kHz; dual rate; standard datapath
return 1;
}
else {
- if(writeRegister(0x07, 0x8A)) // Codec datapath register: 44.1kHz; std rate; standard datapath
+ if(writeRegister(0x07, 0x0A)) // Codec datapath register: 48kHz; std rate; standard datapath
return 1;
}
if(writeRegister(0x08, 0xC0)) // Audio serial control register A: BLCK, WCLK outputs
diff --git a/core/RTAudio.cpp b/core/RTAudio.cpp
index 5e972c8..a7b5675 100644
--- a/core/RTAudio.cpp
+++ b/core/RTAudio.cpp
@@ -183,7 +183,7 @@ int Bela_initAudio(BelaInitSettings *settings, void *userData)
}
// Initialise the rendering environment: sample rates, frame counts, numbers of channels
- gContext.audioSampleRate = 44100.0;
+ gContext.audioSampleRate = 8000.0;