JeremiahRose
Member
Hi there,
I'm attempting to use an SPI connected MCP3008 ADC as a source of audio data with a Teensy 4.1.
The AudioInputMCP3008 class is taking raw readings direct from the MCP3008 and throwing it straight into the audio buffer, and the DC offset is removed further down the line by a high pass filter.
I believe I have the skeleton of it correct, however I haven't been able to get it to work. The AudioAnalyzePeak object reports peaks of 1.0 to serial for a few seconds, and then the whole patch seems to freeze. I'm not getting any sound out. Any input would be greatly appreciated
I'm attempting to use an SPI connected MCP3008 ADC as a source of audio data with a Teensy 4.1.
The AudioInputMCP3008 class is taking raw readings direct from the MCP3008 and throwing it straight into the audio buffer, and the DC offset is removed further down the line by a high pass filter.
I believe I have the skeleton of it correct, however I haven't been able to get it to work. The AudioAnalyzePeak object reports peaks of 1.0 to serial for a few seconds, and then the whole patch seems to freeze. I'm not getting any sound out. Any input would be greatly appreciated
Code:
/***************************************************
Attempts to use an MCP3008 ADC as an audio source
****************************************************/
#include <Adafruit_MCP3008.h> // MCP3008 analogue to digital converter library
#include <Audio.h> // Teensy audio library
#include "arm_math.h"
#include "AudioStream.h"
const bool DEBUG = true; // Debugging over serial
class AudioInputMCP3008 : public AudioStream
{
public:
Adafruit_MCP3008 adc;
bool started = false;
AudioInputMCP3008() : AudioStream(0, NULL) {}
void begin(){
adc.begin();
started = true;
}
virtual void update(void){
audio_block_t *block;
uint32_t i;
block = allocate();
if (block) {
for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
if (started){
block->data[i] = adc.readADC(3);
} else {
block->data[i] = 0;
}
}
transmit(block);
release(block);
return;
}
}
};
// Initialise audio objects
AudioInputMCP3008 input1;
//AudioSynthWaveformSine testSine1;
AudioAmplifier amp1;
AudioFilterBiquad filter1;
AudioOutputI2S i2s1;
AudioAnalyzePeak peak1;
// Connect audio objects
AudioConnection patchCord4(input1, amp1);
AudioConnection patchCord1(amp1, filter1);
AudioConnection patchCord2(filter1, 0, i2s1, 0);
AudioConnection patchCord3(filter1, 0, i2s1, 1);
AudioConnection patchCord5(filter1, peak1);
void setup() {
if (DEBUG){
Serial.begin(9600);
//while (!Serial);
Serial.println("Debug enabled.");
}
AudioMemory(12);
amp1.gain(1);
filter1.setHighpass(0, 40, 0.707);
input1.begin();
//testSine1.amplitude(1);
//testSine1.frequency(440);
}
void loop() {
if (DEBUG){
Serial.println(peak1.read());
}
AudioNoInterrupts();
AudioInterrupts();
}