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

Thread: Creating Raw Files for AudioPlaySdRaw Object

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    19

    Creating Raw Files for AudioPlaySdRaw Object

    Hi, I'm working on a project that requires playing back a bunch of audio files at once, wanted to try out using AudioPlaySdRaw in place of AudioPlaySdWav in order to reduce overhead. However the documentation on AudioPlaySdRaw is a little sparse.

    Couple of questions:
    1. Should the raw files be mono? Is this the only option for raw files?
    2. Is there a program I can use to easily convert wav files to raw files? I have tried audicity and whatever raw format it outputs does not work with the teensy. It doesn't expose information as to big and little endian and stuff like that so it's pretty hard to tell whats going wrong or if it worked correctly.

    Please advise. Thanks!

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,068
    Should the raw files be mono? Is this the only option for raw files?
    Yes and yes. And they should be 16 bit unsigned little-endian integer sampled at 44100Hz.

    Is there a program I can use to easily convert wav files to raw files?
    There was one called wav2raw posted on the forum. I tried it and couldn't get it to work the way I expected. Perhaps it was doing something extra - like resampling the rate. I wrote my own C program which basically strips the header from a file and writes it as a .raw file.

    But I suggest that you try playing WAV files first. Whatever overhead exists is primarily due to opening the file. There's very little overhead in parsing the header once the file is open.

    Pete

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Posts
    19
    Quote Originally Posted by el_supremo View Post
    Yes and yes. And they should be 16 bit unsigned little-endian integer sampled at 44100Hz.


    There was one called wav2raw posted on the forum. I tried it and couldn't get it to work the way I expected. Perhaps it was doing something extra - like resampling the rate. I wrote my own C program which basically strips the header from a file and writes it as a .raw file.

    But I suggest that you try playing WAV files first. Whatever overhead exists is primarily due to opening the file. There's very little overhead in parsing the header once the file is open.

    Pete
    Okay, do you know of any way to check if the conversion went correctly? Like what the contents of the raw file should look like? I tried using a program called switch which had the option: 16-Bit PCM Intel Endian Raw. I get something that looks like a bunch of Hex numerals. Is this correct. Unfortunately stuff like what PCM means is not clearly documented at all.

    I've already tried using Wav files, I'm trying to eliminate the overhead from reading the header because my program sometimes freezes for a long time in opening the files.

  4. #4
    Junior Member
    Join Date
    Mar 2019
    Posts
    19
    Quote Originally Posted by el_supremo View Post
    they should be 16 bit unsigned little-endian integer sampled at 44100Hz.
    Also, the documentation in the audio design tool says otherwise:

    "The data file must be RAW 16 bit signed integers in LSB-first format."

  5. #5
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,068
    Yeah, my bad. I don't know how I came up with "unsigned". It should definitely be signed.

    "LSB-first format" = "little-endian"

    Pete

  6. #6
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,068
    I get something that looks like a bunch of Hex numerals
    What were you using to "look" at the file?

    Post your code so I can see what you're trying to do.

    Pete

  7. #7
    Junior Member
    Join Date
    Mar 2019
    Posts
    19
    Quote Originally Posted by el_supremo View Post
    What were you using to "look" at the file?

    Post your code so I can see what you're trying to do.

    Pete
    Opening it with sublime text. Here is the code, I am trying to loop playback of raw files:

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    AudioPlaySdRaw              playWav[16];
    AudioOutputAnalogStereo     audioOutput;
    AudioMixer4                 mixer[5];
    
    AudioConnection             patchCord1(playWav[0], 0, mixer[0], 0);
    AudioConnection             patchCord2(playWav[1], 0, mixer[0], 1);
    AudioConnection             patchCord3(playWav[2], 0, mixer[0], 2);
    AudioConnection             patchCord4(playWav[3], 0, mixer[0], 3);
    AudioConnection             patchCord5(playWav[4], 0, mixer[1], 0);
    AudioConnection             patchCord6(playWav[5], 0, mixer[1], 1);
    AudioConnection             patchCord7(playWav[6], 0, mixer[1], 2);
    AudioConnection             patchCord8(playWav[7], 0, mixer[1], 3);
    AudioConnection             patchCord9(playWav[8], 0, mixer[2], 0);
    AudioConnection             patchCord10(playWav[9], 0, mixer[2], 1);
    AudioConnection             patchCord11(playWav[10], 0, mixer[2], 2);
    AudioConnection             patchCord12(playWav[11], 0, mixer[2], 3);
    AudioConnection             patchCord13(playWav[12], 0, mixer[3], 0);
    AudioConnection             patchCord14(playWav[13], 0, mixer[3], 1);
    AudioConnection             patchCord15(playWav[14], 0, mixer[3], 2);
    AudioConnection             patchCord16(playWav[15], 0, mixer[3], 3);
    
    AudioConnection             patchCord17(mixer[0], 0, mixer[4], 0);
    AudioConnection             patchCord18(mixer[1], 0, mixer[4], 1);
    AudioConnection             patchCord19(mixer[2], 0, mixer[4], 2);
    AudioConnection             patchCord20(mixer[3], 0, mixer[4], 3);
    
    AudioConnection             patchCord21(mixer[4], 0, audioOutput, 0);
    AudioConnection             patchCord22(mixer[4], 0, audioOutput, 1);
    
    
    // Use these with the Teensy 3.5 & 3.6 SD card
    #define SDCARD_CS_PIN    BUILTIN_SDCARD
    #define SDCARD_MOSI_PIN  11  // not actually used
    #define SDCARD_SCK_PIN   13  // not actually used
    
    const char *files[] = {"FIRE1.raw", "FIRE2.raw", "FIRE3.raw", "FIRE4.raw", "RAIN1.raw", "RAIN2.raw", "RAIN3.raw", "RAIN4.raw", 
                          "WIND1.raw", "WIND2.raw", "WIND3.raw", "WIND4.raw", "ANIMAL1.raw", "ANIMAL2.raw", "ANIMAL3.raw", "ANIMAL4.raw"};
    
    float  mix[4] = {};
    
    void setup() {
      delay(1000);
      Serial.begin(9600);
      Serial.println("Starting!");
    
      AudioMemory(256);
      audioOutput.analogReference(INTERNAL);
    
      for(int i = 0; i < 5; ++i){
        for(int j = 0; j < 4; ++j){
          mixer[i].gain(j, 1);
        }
      }
    
      pinMode(13, OUTPUT);
      digitalWrite(13, HIGH);
      
      if (!(SD.begin(SDCARD_CS_PIN))) {
        while (1) {
          Serial.println("Unable to access the SD card");
          delay(500);
        }
      }
    
    }
    
    
    void playFileFromPlayer(const char *filename, int player)
    {
      Serial.print("Playing file: ");
      Serial.println(filename);
    
      // Start playing the file.  This sketch continues to
      // run while the file plays.
      playWav[player].play(filename);
    
      // A brief delay for the library read WAV info
      delay(5);
      Serial.println("Check!");
    }
    
    int counter = 0;
    
    void loop() {
    
      int one = millis();
      for(int i = 0; i < 4; ++i){
        if(!(playWav[i].isPlaying())){
          playFileFromPlayer(files[i], i);
        }
      }
    
      if(millis() - counter > 1000){
        counter = millis();
        digitalWrite(13, !digitalRead(13));
      }
    }
    Note: AudioPlaySDRaw objects are named named playWav, since this is adapted from my previous code and I didn't want to have to do a bunch of typing.

  8. #8
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,566
    Quote Originally Posted by grinch View Post
    Note: AudioPlaySDRaw objects are named named playWav, since this is adapted from my previous code and I didn't want to have to do a bunch of typing.
    Look for "Search and replace" in your editor

  9. #9
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,068
    I found that turning off audio interrupts when starting to play a raw file allowed more files to be played without problems.
    In playFileFromPlayer try:
    Code:
    AudioNoInterrupts();
      playWav[player].play(filename);
    AudioInterrupts();
    It might also help if you start all of them (with audio interrupts off) at first in the setup function.

    Does that code work with the first four files? If it does, how many does it take before it croaks?
    If it doesn't work with 4, can you post them?

    Pete

Posting Permissions

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