Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Creating an AudioInputMCP3008 object

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Location
    Melbourne, Australia
    Posts
    12

    Creating an AudioInputMCP3008 object

    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

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

  2. #2
    Junior Member
    Join Date
    Oct 2020
    Location
    Melbourne, Australia
    Posts
    12
    Adding non-blocking timers to the peak reporting, and amplifying the signal before and after filtering got it working!

    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
    elapsedMillis timeSincePeak;
    
    class AudioInputMCP3008 : public AudioStream
    {
    public:
      Adafruit_MCP3008 adc;
      bool started = false;
      AudioInputMCP3008() : AudioStream(0, NULL) {}
      void begin(){
        adc.begin();
        started = true;
        if (DEBUG){
          Serial.print("Started!!\n");
        }
      }
      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;
    AudioAmplifier  amp2;
    AudioOutputI2S  i2s1;
    AudioAnalyzePeak peak1;
    
    // Connect audio objects
    AudioConnection patchCord4(input1, amp1);
    AudioConnection patchCord6(amp1, filter1);
    AudioConnection patchCord1(filter1, amp2);
    AudioConnection patchCord2(amp2, 0, i2s1, 0);
    AudioConnection patchCord3(amp2, 0, i2s1, 1);
    AudioConnection patchCord5(amp2, peak1);
    
    void setup() {
      if (DEBUG){
        Serial.begin(9600);
        //while (!Serial);
        Serial.println("Debug enabled.");
      }
    
      AudioMemory(12);
      amp1.gain(30);
      amp2.gain(30);
      filter1.setHighpass(0, 40, 0.707);
      input1.begin();
      //testSine1.amplitude(1);
      //testSine1.frequency(440);
    }
    
    void loop() {
      if (DEBUG && timeSincePeak > 1000){
        Serial.println(peak1.read());
        timeSincePeak = 0;
      }
      AudioNoInterrupts();
      AudioInterrupts();
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •