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

Thread: Read raw audio file and get sample value

  1. #1
    Senior Member
    Join Date
    Nov 2015
    Posts
    187

    Read raw audio file and get sample value

    Hi, im back with an audio project.
    I need to read one .raw audio file and get the samples value.

    My code now

    Code:
    File dataFile = SD.open("amen.raw");
    int16_t numSamples = (dataFile.size() - (dataFile.size()%256)) >> 1; // return samples_available;
    int16_t buf[numSamples] = {0}; // buffer
    
            
    double audio  = dataFile.read();
    I should create an array (or two if the file is stereo) with the values of the samples for further use.
    right now I don't know how to create the array which I will then use to store the samples values.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,769
    Quote Originally Posted by urbanspaceman View Post
    right now I don't know how to create the array which I will then use to store the samples values.
    This line from your own code will create an array.

    Code:
    int16_t buf[numSamples] = {0}; // buffer
    However, this array will be created as a local variable. That's probably not what you want, because the variable will cease to exist when the surrounding function or code block ends (we can't see more of your code, so the best I can do is the very generic advice). Normally arrays for buffers are created as global or static or C++ member variables, so the data is retained and can be reliably used later. Also, it's common practice to use a constant for the array size.

  3. #3
    Senior Member
    Join Date
    Nov 2015
    Posts
    187
    Ho Paul, i'm working on this project in the spare time.

    the code is this

    Code:
    uint32_t GET_samples_in_file(uint8_t f)
    {
      return Wave.file_samples(name_file[f]);
    }
    
    void plotWave(uint8_t n){
      int16_t numSamples = GET_samples_in_file(n);
      File dataFile = SD.open("amen.raw");
      int16_t buf[numSamples] = {0}; // buffer 
      int audio[numSamples]  = dataFile.read(); 
    
      for(int n=0; n<numSamples; n++){
        ??
      }
    }
    in included file

    Code:
    int file_samples(const char *filename)
        {
        File rawfile = SD.open(filename);
        if (!rawfile)
            return 0;
        return (rawfile.size() - (rawfile.size()%256)) >> 1; // return samples_available;
        }

Posting Permissions

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