chipaudette
Well-known member
Hi All,
Previously, I made an extension of the audio library to enable float32 audio processing for the Teensy 3.5/3.6 (see Tympan library here). With Teensy 4, a bunch of stuff has changed, which I'm working through.
One troublesome area that I've hit is how to enable the user (me!) to change the sample rate of the audio system. Does anyone have suggestions on how to change the I2S bus rates for Teensy 4?
For Teensy 3, I used the many helpful posts from @FrankB, including:
https://forum.pjrc.com/threads/3875...he-sample-rate?p=121365&viewfull=1#post121365
and
https://forum.pjrc.com/threads/3875...he-sample-rate?p=188812&viewfull=1#post188812
I shamelessly copied the examples above (with very modest additions) and turned it into the code below (again, for Teensy 3):
Looking at Teensy 4, I'm not even quite sure where to begin. I looks like the Teensy 4 setup has some important differences...for example, the Teensy 4 compatible AudioOutputI2S class uses a function called set_audioClock(). If I want to change the sample rate, do I only need to call this function, or is this an unrelated function?
Any guidance would be greatly appreciated!
Chip
Previously, I made an extension of the audio library to enable float32 audio processing for the Teensy 3.5/3.6 (see Tympan library here). With Teensy 4, a bunch of stuff has changed, which I'm working through.
One troublesome area that I've hit is how to enable the user (me!) to change the sample rate of the audio system. Does anyone have suggestions on how to change the I2S bus rates for Teensy 4?
For Teensy 3, I used the many helpful posts from @FrankB, including:
https://forum.pjrc.com/threads/3875...he-sample-rate?p=121365&viewfull=1#post121365
and
https://forum.pjrc.com/threads/3875...he-sample-rate?p=188812&viewfull=1#post188812
I shamelessly copied the examples above (with very modest additions) and turned it into the code below (again, for Teensy 3):
Code:
float AudioOutputI2S_F32::setI2SFreq(const float freq_Hz) {
#if defined(KINETISK)
int freq = (int)freq_Hz;
typedef struct {
uint8_t mult;
uint16_t div;
} __attribute__((__packed__)) tmclk;
const int numfreqs = 16;
const int samplefreqs[numfreqs] = { 2000, 8000, 11025, 16000, 22050, 24000, 32000, 44100, (int)44117.64706 , 48000, 88200, (int)(44117.64706 * 2), 96000, 176400, (int)(44117.64706 * 4), 192000};
#if (F_PLL==16000000)
const tmclk clkArr[numfreqs] = {{4, 125}, {16, 125}, {148, 839}, {32, 125}, {145, 411}, {48, 125}, {64, 125}, {151, 214}, {12, 17}, {96, 125}, {151, 107}, {24, 17}, {192, 125}, {127, 45}, {48, 17}, {255, 83} };
#elif (F_PLL==72000000)
const tmclk clkArr[numfreqs] = {{832, 1125}, {32, 1125}, {49, 1250}, {64, 1125}, {49, 625}, {32, 375}, {128, 1125}, {98, 625}, {8, 51}, {64, 375}, {196, 625}, {16, 51}, {128, 375}, {249, 397}, {32, 51}, {185, 271} };
#elif (F_PLL==96000000)
const tmclk clkArr[numfreqs] = {{2, 375},{8, 375}, {73, 2483}, {16, 375}, {147, 2500}, {8, 125}, {32, 375}, {147, 1250}, {2, 17}, {16, 125}, {147, 625}, {4, 17}, {32, 125}, {151, 321}, {8, 17}, {64, 125} };
#elif (F_PLL==120000000)
const tmclk clkArr[numfreqs] = {{8, 1875},{32, 1875}, {89, 3784}, {64, 1875}, {147, 3125}, {32, 625}, {128, 1875}, {205, 2179}, {8, 85}, {64, 625}, {89, 473}, {16, 85}, {128, 625}, {178, 473}, {32, 85}, {145, 354} };
#elif (F_PLL==144000000)
const tmclk clkArr[numfreqs] = {{4, 1125},{16, 1125}, {49, 2500}, {32, 1125}, {49, 1250}, {16, 375}, {64, 1125}, {49, 625}, {4, 51}, {32, 375}, {98, 625}, {8, 51}, {64, 375}, {196, 625}, {16, 51}, {128, 375} };
#elif (F_PLL==180000000)
const tmclk clkArr[numfreqs] = {{23, 8086}, {46, 4043}, {49, 3125}, {73, 3208}, {98, 3125}, {37, 1084}, {183, 4021}, {196, 3125}, {16, 255}, {128, 1875}, {107, 853}, {32, 255}, {219, 1604}, {214, 853}, {64, 255}, {219, 802} };
#elif (F_PLL==192000000)
const tmclk clkArr[numfreqs] = {{1, 375}, {4, 375}, {37, 2517}, {8, 375}, {73, 2483}, {4, 125}, {16, 375}, {147, 2500}, {1, 17}, {8, 125}, {147, 1250}, {2, 17}, {16, 125}, {147, 625}, {4, 17}, {32, 125} };
#elif (F_PLL==216000000)
const tmclk clkArr[numfreqs] = {{8, 3375}, {32, 3375}, {49, 3750}, {64, 3375}, {49, 1875}, {32, 1125}, {128, 3375}, {98, 1875}, {8, 153}, {64, 1125}, {196, 1875}, {16, 153}, {128, 1125}, {226, 1081}, {32, 153}, {147, 646} };
#elif (F_PLL==240000000)
const tmclk clkArr[numfreqs] = {{4, 1875}, {16, 1875}, {29, 2466}, {32, 1875}, {89, 3784}, {16, 625}, {64, 1875}, {147, 3125}, {4, 85}, {32, 625}, {205, 2179}, {8, 85}, {64, 625}, {89, 473}, {16, 85}, {128, 625} };
#endif
for (int f = 0; f < numfreqs; f++) {
if ( freq == samplefreqs[f] ) {
while (I2S0_MCR & I2S_MCR_DUF) ;
I2S0_MDR = I2S_MDR_FRACT((clkArr[f].mult - 1)) | I2S_MDR_DIVIDE((clkArr[f].div - 1));
return (float)(F_PLL / 256 * clkArr[f].mult / clkArr[f].div);
}
}
#endif
return 0.0f;
}
Looking at Teensy 4, I'm not even quite sure where to begin. I looks like the Teensy 4 setup has some important differences...for example, the Teensy 4 compatible AudioOutputI2S class uses a function called set_audioClock(). If I want to change the sample rate, do I only need to call this function, or is this an unrelated function?
Any guidance would be greatly appreciated!
Chip