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

Thread: Teensy 3.2 playing RAW file is instable

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    14

    Teensy 3.2 playing RAW file is instable

    Hi,

    I built an audio recording and playing solution using the Teensy libray "Recorder" file. I tested my code several times succesfully, but for an unclear reason the solution is not producing any sound over my speaker or my directly connected headset anymore. I believe the SDCard player is reading the RAW file, because during the reading it is printing the volume on the seriel monitor and the build_in LED is High during the playing time, which is about equal to the recording time.

    When a use the library code "SimultaneousPlay", I get the sound of the WAV files over my speaker / headphone. So. my conclusion it is:
    - my code or
    - the RAW file on the SDCard

    To make sure it isn't a error on the SDcard, I formatted the SDcard and tested it again with different file names, but still no sound.

    I would appriciate it, if someone can check my code on any issue?
    Is this a know issue for the Teensy 3.2 SDcard player to be instable?

    Click image for larger version. 

Name:	IMG_4764.jpg 
Views:	28 
Size:	78.9 KB 
ID:	25473Click image for larger version. 

Name:	IMG_4763.jpg 
Views:	17 
Size:	97.8 KB 
ID:	25474Click image for larger version. 

Name:	IMG_4762.jpg 
Views:	24 
Size:	105.5 KB 
ID:	25475Click image for larger version. 

Name:	IMG_4761.jpg 
Views:	27 
Size:	107.2 KB 
ID:	25476Click image for larger version. 

Name:	IMG_4760.jpg 
Views:	26 
Size:	102.2 KB 
ID:	25477SD_readwrite.ino

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    9,393
    Difficult to answer when we can't see your RAW file or your code. So, almost no information we can use. What would YOU answer in this case?
    No, there are no known instabilities. If we knew one, a fix would be on the way...


    Do you use TD 1.54? It's SD library is way faster.
    Last edited by Frank B; 08-01-2021 at 05:33 PM.

  3. #3
    Junior Member
    Join Date
    Jul 2021
    Posts
    14
    Hi Frank, I added the code as attachment, but now the code is included. I will try to get the RAW file too.
    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Bounce.h>
    
    #define SDCARD_CS_PIN    10
    #define SDCARD_MOSI_PIN  7
    #define SDCARD_SCK_PIN   14
    //#define HWSERIAL Serial1
    
    // GUItool: begin automatically generated code
    AudioInputI2S            i2s2;           //xy=219,163
    AudioPlaySdRaw           playRaw1;     //xy=223,314
    AudioOutputI2S           i2s1;           //xy=559,262
    AudioRecordQueue         queue1;         //xy=563,320
    AudioMixer4              mixer1;         //xy=563,469
    AudioAnalyzePeak         peak1;          //xy=565,376
    AudioConnection          patchCord1(i2s2, 0, queue1, 0);
    AudioConnection          patchCord2(i2s2, 0, peak1, 0);
    AudioConnection          patchCord3(playRaw1, 0, mixer1, 0);
    AudioConnection          patchCord4(mixer1, 0, i2s1, 0);
    AudioConnection          patchCord5(mixer1, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=367,576
    // GUItool: end automatically generated code
    
    Bounce buttonPlay1 = Bounce(2, 8);
    Bounce buttonPlay2 = Bounce(3, 8);
    Bounce buttonRecord1 = Bounce(21, 8);
    Bounce buttonRecord2 = Bounce(20, 8);
    
    int mode = 0; // 0=stopped, 1=recording, 2=playing
    File myFile;
    
    void setup() {
      pinMode(2, INPUT_PULLUP);
      pinMode(3, INPUT_PULLUP);
      pinMode(21, INPUT_PULLUP);
      pinMode(20, INPUT_PULLUP);
      pinMode(13, OUTPUT);
    //  digitalWrite(13, LOW);
      Serial.begin(9600);
    //  HWSERIAL.begin(9600);
    
      // Audio connections require memory, and the record queue
      // uses this memory to buffer incoming audio.
      AudioMemory(60);
    
      // Initialize the SD card
      SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
      if (!(SD.begin(SDCARD_CS_PIN))) {
        while (1) {
          Serial.println("Unable to access the SD card");
          delay(500);
        }
      }
      delay(500);
      Serial.println("initialization done.");
    
      // Enable the audio shield, select input, and enable output
      sgtl5000_1.enable();
      sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
      sgtl5000_1.micGain(47);
      sgtl5000_1.lineOutLevel(29);
      mixer1.gain(1,1.0);
    }
    
    void loop() {
      // First, read the buttons
      buttonRecord1.update();
      buttonRecord2.update();
      buttonPlay1.update();
      buttonPlay2.update();
      // adjust volume with A1
      float a1 = analogRead(A1);
      float vol = a1 / 1280;
      sgtl5000_1.volume(0.5);
      //HWSERIAL.write(0);
      //Serial.println(mode);
      
      // Start and Stop recording
      if (buttonRecord1.fallingEdge()) {
        Serial.println("Record Button 1 Press");
        if (mode == 2) stopPlaying();
        if (mode == 0) startRecording1();
      }
      if (buttonRecord1.risingEdge()) {
        Serial.println("Record Button 1 Release");
        if (mode == 1) stopRecording();
    //    if (mode == 2) stopPlaying();
      }
      if (buttonRecord2.fallingEdge()) {
        Serial.println("Record Button 2 Press");
        if (mode == 2) stopPlaying();
        if (mode == 0) startRecording2();
      }
      if (buttonRecord2.risingEdge()) {
        Serial.println("Record Button 2 Release");
        if (mode == 1) stopRecording();
    //   if (mode == 2) stopPlaying();
      }
    
      // Start playing
      if (buttonPlay1.fallingEdge()) {
        Serial.println("Play command 1");
        if (mode == 1) stopRecording();
        if (mode == 2) stopPlaying();
        if (mode == 0) startPlaying1();
      }
      if (buttonPlay2.fallingEdge()) {
        Serial.println("Play command 2");
        if (mode == 1) stopRecording();
        if (mode == 2) stopPlaying();
        if (mode == 0) startPlaying2();
      }
      
      // If we're playing or recording, carry on...
      if (mode == 1) {
        continueRecording();
        //Serial.println(mode);
      }
      if (mode == 2) {
        continuePlaying(); // Stop SDcard reading when audio is stopped
        Serial.println(vol);
        //Serial.println(mode);
      }  
    }
    
    void startRecording1() {
      Serial.println("startRecording");
      if (SD.exists("RECORD11.RAW")) {
        // The SD library writes new data to the end of the
        // file, so to start a new recording, the old file
        // must be deleted before new data is written.
        SD.remove("RECORD11.RAW");
      }
      myFile = SD.open("RECORD11.RAW", FILE_WRITE);
      if (myFile) {
        queue1.begin();
    //    digitalWrite(13, HIGH);
        mode = 1;
      }
    }  
    
    void startRecording2() {
      Serial.println("startRecording");
      if (SD.exists("RECORD21.RAW")) {
        // The SD library writes new data to the end of the
        // file, so to start a new recording, the old file
        // must be deleted before new data is written.
        SD.remove("RECORD21.RAW");
      }
      myFile = SD.open("RECORD21.RAW", FILE_WRITE);
      if (myFile) {
        queue1.begin();
     //   digitalWrite(13, HIGH);
        mode = 1;
      }
    }  
    
    void continueRecording() {
      if (queue1.available() >= 2) {
        byte buffer[512];
        // Fetch 2 blocks from the audio library and copy
        // into a 512 byte buffer.  The Arduino SD library
        // is most efficient when full 512 byte sector size
        // writes are used.
        memcpy(buffer, queue1.readBuffer(), 256);
        queue1.freeBuffer();
        memcpy(buffer+256, queue1.readBuffer(), 256);
        queue1.freeBuffer();
        // write all 512 bytes to the SD card
        elapsedMicros usec = 0;
        myFile.write(buffer, 512);
        // Uncomment these lines to see how long SD writes
        // are taking.  A pair of audio blocks arrives every
        // 5802 microseconds, so hopefully most of the writes
        // take well under 5802 us.  Some will take more, as
        // the SD library also must write to the FAT tables
        // and the SD card controller manages media erase and
        // wear leveling.  The queue1 object can buffer
        // approximately 301700 us of audio, to allow time
        // for occasional high SD card latency, as long as
        // the average write time is under 5802 us.
        Serial.print("SD write, us=");
        Serial.println(usec);
      }
    }
    
    void stopRecording() {
      Serial.println("stopRecording");
      queue1.end();
      if (mode == 1) {
        while (queue1.available() > 0) {
          myFile.write((byte*)queue1.readBuffer(), 256);
          queue1.freeBuffer();
        }
        myFile.close();
        Serial.println("Closed the file");
      }
      mode = 0;
    //  digitalWrite(13, LOW);
    }
    
    void startPlaying1() {
    //  HWSERIAL.write(1);
      Serial.println("startPlaying Record 1");
      playRaw1.play("RECORD11.RAW");
      mode = 2;
    //  digitalWrite(13, HIGH);
    }
    
    void startPlaying2() {
    //  HWSERIAL.write(2);
      Serial.println("startPlaying Record 2");
      playRaw1.play("RECORD21.RAW");
      mode = 2;
    //  digitalWrite(13, HIGH);
    }
    
    void continuePlaying() {
      if (!playRaw1.isPlaying()) {
        playRaw1.stop();
        Serial.println("Playing stopped");
    //    digitalWrite(13, LOW);
        mode = 0;
      }
    }
    
    void stopPlaying() {
      Serial.println("stopPlaying");
      if (mode == 2) playRaw1.stop();
      mode = 0;
    //  digitalWrite(13, LOW);
    }

  4. #4
    Junior Member
    Join Date
    Jul 2021
    Posts
    14
    Hi Frank, I was able to check the data of the RAW files with Audacity. The signals were flat!! No surpise I can't hear anything. This could mean the mic is not working anymore.

  5. #5
    Junior Member
    Join Date
    Jul 2021
    Posts
    14
    I just replaced the mic with another one. Same result, flat line....

  6. #6
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    9,393
    Yes, you use pin 13, which is in use by the Audio shield.

    https://www.pjrc.com/store/teensy3_audio.html

  7. #7
    Junior Member
    Join Date
    Jul 2021
    Posts
    14
    thanks Frank for your support. I was doubting if I could make it with the current solution, but that was completely misplaced. Great product and great sound quality and great forum

  8. #8
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    9,393
    You're welcome.

Posting Permissions

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