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

Thread: Reading Samples from Mic Input

  1. #1
    Junior Member
    Join Date
    Feb 2016
    Posts
    17

    Reading Samples from Mic Input

    I'm trying to read samples from mic with audio shield. I'm using readBuffer() to do so, but the data that is displayed for each sample of 128 bit array doesnt make any sense to me. If i'm reading from microphone then shouldn't the sampled data be random? I've posted my code below and the Serial monitor capture of what i'm getting. Any help would be appreciated on what i'm doing wrong. Thank you!

    serial output:
    Click image for larger version. 

Name:	dataOut.JPG 
Views:	102 
Size:	30.0 KB 
ID:	6336

    my code:

    //
    // Microphone to Speaker output

    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>


    // GUItool: begin automatically generated code
    AudioInputI2S Mic; //microphone
    AudioRecordQueue MicMem; //queue for microphone
    AudioOutputI2S headphones; //output
    AudioConnection patchCord1(Mic, 0, MicMem, 0);
    AudioConnection patchCord2(Mic, 0, headphones,0);
    AudioConnection patchCord3(Mic, 0, headphones,1);
    AudioControlSGTL5000 sgtl5000_1; //xy=339.6666564941406,285.6666488647461
    // GUItool: end automatically generated code
    int16_t data1[128];


    const int micInput = AUDIO_INPUT_MIC;

    void setup() {
    Serial.begin(9600);
    AudioMemory(20);
    sgtl5000_1.enable();
    sgtl5000_1.volume(0.5);
    sgtl5000_1.inputSelect(micInput);
    sgtl5000_1.micGain(36);
    delay(1000);
    }

    void loop() {
    MicMem.begin();
    int16_t *data = MicMem.readBuffer();

    for(int i = 0; i < 128; i++) {
    Serial.println(data[i]);
    }
    MicMem.freeBuffer(); // do not use "data" after this!
    MicMem.end();
    delay(500);
    }

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,566
    - Please use code tags next time, without indention your code is difficult to read.
    - There is an example "Audio/Recorder" under "Examples" which shows you the correct usage of AudioRecordQueue.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,928
    Your code isn't checking for readBuffer() to return NULL, which it does until data is actually available.

    Then your code is reading whatever data happens to be present at address zero, which is the ARM exception and interrupt vector table in flash memory. Since your program is fairly small, the 32 addresses are all zeros in the upper 16 bits, which is why you're seeing half the data as zeros. Most of the vector table is populated with the address of an "unused" default handler function, which is why you're seeing the same number repeated.

  4. #4
    Junior Member
    Join Date
    Feb 2016
    Posts
    17
    Thank you for the input; it really helped. I was able to get the data that I need and manipulate it. So, now I filled the sampled data into 512 int16_t array, how can I take this array and play it on speaker, or send it to DAC. I've been looking though examples and "simpleplayer" has a "play(data)" object, but that wont play the array that I have. How can I accomplish that? Thank you!

Posting Permissions

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