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

Thread: play raw audio files from flash memory

  1. #1

    play raw audio files from flash memory

    Hello,

    I try to play raw samples from a flash memory soldered to the audio shield. I use a Teensy 3.2.
    I have transferred successfully raw files on the flash memory.
    I can list these files with the ListFiles example sketch.

    But I can't get no sound...
    I need some guidance. A very basic working sketch would help me a lot.

    Here is my code.
    play_raw_from_flash.ino

    Thanks for any help.

  2. #2
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,542

    Cool

    I went down this rabbit hole in the December time frame.

    I have two Audio adapters, one revision B for the Teensy 3.x processors, and one revision D for the Teensy 4.0. Both boards have a flash memory card soldered on to it. I was able to use the 'teensytransfer' tool on Teensy 3.2/3.5 to update the flash memory, and list out the files. On the Teensy 4.0 processor with the revision D board, it works fine, using an amplifier and speakers. On the Teensy 3.1 and 3.5 processors, I cannot get it to read the flash memory with the raw player.

    Here are some old threads. I think Sandro in the 2nd thread might have gotten it to work:


    This was the code I was using (similar to the code I posted in December):
    Code:
    // Simple WAV file player example
    //
    // Three types of output may be used, by configuring the code below.
    //
    //   1: Digital I2S - Normally used with the audio shield:
    //         http://www.pjrc.com/store/teensy3_audio.html
    //
    //   2: Digital S/PDIF - Connect pin 22 to a S/PDIF transmitter
    //         https://www.oshpark.com/shared_projects/KcDBKHta
    //
    //   3: Analog DAC - Connect the DAC pin to an amplified speaker
    //         http://www.pjrc.com/teensy/gui/?info=AudioOutputAnalog
    //
    // To configure the output type, first uncomment one of the three
    // output objects.  If not using the audio shield, comment out
    // the sgtl5000_1 lines in setup(), so it does not wait forever
    // trying to configure the SGTL5000 codec chip.
    //
    // The SD card may connect to different pins, depending on the
    // hardware you are using.  Uncomment or configure the SD card
    // pins to match your hardware.
    //
    // Data files to put on your SD card can be downloaded here:
    //   http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html
    //
    // This example code is in the public domain.
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    AudioPlaySerialflashRaw		playRaw;
    
    #define USE_PROP_SHIELD		0
    #define USE_ANALOG_DAC		0
    #define USE_ANALOG_DACS		0
    #define USE_SPDIF		0
    #define USE_AUDIO_SHIELD	1
    #define USE_I2S			1
    #define USE_AUDIO_SPI		0
    
    // Output devices
    #if USE_I2S || USE_AUDIO_SHIELD
    AudioOutputI2S			audioOutput;
    #if USE_AUDIO_SHIELD
    AudioControlSGTL5000		sgtl5000;
    #endif
    #endif
    
    #if USE_SPDIF
    AudioOutputSPDIF		audioOutput;
    #endif
    
    #if USE_ANALOG_DAC || USE_PROP_SHIELD
    AudioOutputAnalog		audioOutput;
    #endif
    
    #if USE_ANALOG_DACS
    AudioOutputAnalogStereo		audioOutput;
    #endif
    
    AudioConnection			patchCord1 (playRaw, 0, audioOutput, 0);
    
    // Use these with the Teensy Audio Shield
    #if (USE_AUDIO_SHIELD || USE_AUDIO_SPI) && !defined(__IMXRT1062__)
    #define MOSI_PIN		 7
    #define SCK_PIN			14
    
    #else
    #define MOSI_PIN		11
    #define SCK_PIN			13
    #endif
    
    //#define FLASH_CHIP_SELECT	 4
    #define FLASH_CHIP_SELECT	 6
    #define PROP_SHIELD_AUDIO	 5
    
    void setup ()
    {
      while (!Serial && millis () < 3000UL)
        ;
    
      Serial.begin (9600);
    
      //uncomment these if you have other SPI chips connected
      //to keep them disabled while using only SerialFlash
      pinMode (10, INPUT_PULLUP);
    
      if (MOSI_PIN != 7)
        SPI.setMOSI (MOSI_PIN);
    
      if (SCK_PIN != 13)
        SPI.setSCK (SCK_PIN);
    
      SPI.begin ();
    
      eeprom_initialize ();
    
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory (8);
    
      // Comment these out if not using the audio adaptor board.
      // This may wait forever if the SDA & SCL pins lack
      // pullup resistors
    #if USE_AUDIO_SHIELD
      sgtl5000.enable ();
      sgtl5000.volume (0.5f);
    #endif
    
      // Enable the amplifier on the prop shield
    #if USE_PROP_SHIELD
      pinMode (PROP_SHIELD_AUDIO, OUTPUT);
      digitalWrite (PROP_SHIELD_AUDIO, HIGH);
    #endif
    
      Serial.printf ("MOSI = %d, SCK = %d, CS = %d%s%s%s%s%s%s%s\n",
    		 MOSI_PIN,
    		 SCK_PIN,
    		 FLASH_CHIP_SELECT,
    		 (USE_I2S || USE_AUDIO_SHIELD)		? ", i2s"		: "",
    		 (USE_ANALOG_DAC || USE_PROP_SHIELD)	? ", dac"		: "",
    		 USE_ANALOG_DACS			? ", dacs"		: "",
    		 USE_SPDIF				? ", S/PDIF"		: "",
    		 USE_AUDIO_SHIELD			? ", audio shield"	: "",
    		 USE_PROP_SHIELD			? ", prop shield"	: "",
    		 USE_AUDIO_SPI				? ", audio spi"		: "");
    
      // Start SerialFlash
      if (!SerialFlash.begin (FLASH_CHIP_SELECT)) {
        Serial.println ("Cannot access SPI Flash chip");
        while (1)
          {
    	delay (1000);
          }
      }
    }
    
    void playFile (const char *filename)
    {
      Serial.printf ("Playing file: %s\n", filename);
    
      // Start playing the file.  This sketch continues to
      // run while the file plays.
      playRaw.play (filename);
    
      // A brief delay for the library read WAV info
      delay (5);
    
      // Simply wait for the file to finish playing.
      while (playRaw.isPlaying ()) {
        // uncomment these lines if you audio shield
        // has the optional volume pot soldered
        //float vol = analogRead(15);
        //vol = vol / 1024;
        // sgtl5000_1.volume(vol);
      }
    }
    
    
    void loop() {
      playFile ("SDTEST1.RAW");	// filenames are always uppercase 8.3 format
      delay (500);
      playFile ("SDTEST2.RAW");
      delay (500);
      playFile ("SDTEST3.RAW");
      delay (500);
      playFile ("SDTEST4.RAW");
      delay (1500);
    }

  3. #3
    Hi Michael,
    Thanks for your answer.
    I manage to make it work with the following code.
    I am now going to test the performances compare to SDcard.
    Emmanuel

    Code:
    #include <Audio.h>
    #include <SerialFlash.h>
    
    AudioPlaySerialflashRaw  sound0;
    AudioMixer4        mix;
    AudioOutputI2S     headphones;
    
    AudioConnection patchCord1( sound0, 0, mix, 0 );
    AudioConnection patchCord2( mix, 0, headphones, 0 );
    AudioConnection patchCord3( mix, 0, headphones, 1 );
    
    AudioControlSGTL5000     sgtl5000;
    
    
    void setup()
    {
      Serial.begin( 9600 );
      // Audio connections require memory to work
      AudioMemory( 8 );
    
      SPI.setMOSI( 7 );   // MOSI_PIN for audio board
      SPI.setSCK( 14 );   // SCK_PIN for audio board
    
      if ( !SerialFlash.begin() )
        Serial.println( "Unable to access SPI Flash chip" );
    
      sgtl5000.enable();
      sgtl5000.volume( 0.3 );
    }
    
    
    void playFile(const char* filename) {
      sound0.play( filename );
    
      // Simply wait for the file to finish playing.
      while ( sound0.isPlaying() );
    }
    
    
    void loop()
    {
      playFile( "TEST0" );
    }

  4. #4
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,542

    Cool

    Quote Originally Posted by emmanuel63 View Post
    Hi Michael,
    Thanks for your answer.
    I manage to make it work with the following code.
    I am now going to test the performances compare to SDcard.
    Emmanuel
    I did these tests in January:


    And here is FrankB's TeensyTransfer for Teensy LC, 3.2, 3.5, and 3.6 that can be used to list and update the flash memory card:

  5. #5
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,542
    Quote Originally Posted by emmanuel63 View Post
    Hi Michael,
    Thanks for your answer.
    I manage to make it work with the following code.
    Thanks for the working example. I was doing SPI.begin after setting MOSI/SCLK, and eliminating the SPI.begin caused it to work.

  6. #6
    By the way, I don't understand why SPI.begin causes some problems...
    I am not a guru with coding...

  7. #7
    Ok then, I run some tests too. And flash memory is MUCH faster.
    I made a little sampler. I can play simultaneously 13 samples. Very reactive, no problems. My samples are around 4 seconds each, 44 kHz 16bits mono. I have only 13 pads on my project, but I think it is possible to add even more. Flash memory is certainly a good solution for sampler-like projects. I use the CopyFromSD sketch to copy my samples to flash memory. It's fast and easy.

    Emmanuel

Posting Permissions

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