0

I am trying to read a wav file into an array using Android. In order to validate the results I read the same wav file using Matlab. The problem is that the values are different. Your help is highly appreciated in solving this problem. Kindly, find below the Matlab and Android code with the associated results:

Matlab Code:

fName = 'C:\Users\me\Desktop\audioText.txt';         
fid = fopen(fName,'w');           
dlmwrite(fName,y_sub,'-append','delimiter','\t','newline','pc');

Matlab Results:

0.00097656 0.00045776 0.0010681 0.00073242 0.00054932 -0.00064087 0.0010376 -0.00027466 -0.00036621 -9.1553e-05 0.00015259 0.0021362 -0.00024414 -3.0518e-05 -0.00021362

Android Code:

String filePath;
    private static DataOutputStream fout;
    ByteArrayOutputStream out;
    BufferedInputStream in;

    filePath = "mnt/sdcard/audio.wav";


            out = new ByteArrayOutputStream();
            try {
                in = new BufferedInputStream(new FileInputStream(filePath));
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            int read;
            byte[] buff = new byte[2000000];
            try {
                while ((read = in.read(buff)) > 0)
                {
                    out.write(buff, 0, read);
                }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                out.flush();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            byte[] audioBytes = out.toByteArray();

            }

Android Results:

82, 73, 70, 70, 92, 108, 40, 0, 87, 65, 86, 69, 102, 109

Thanks,

Mamoun
  • 1
  • 4
  • are you testing with the sensor data?? – Sree Mar 27 '14 at 11:45
  • the wav file is a recording of sensor data, but does this matter? – Mamoun Mar 27 '14 at 11:48
  • No I don't. Do you have any clue what is the problem? – Mamoun Mar 27 '14 at 11:51
  • Do you know how the WAV file is encoded? 16-bit, 24-bit, etc? Make sure you use the proper bit width when printing values directly from the WAV file as text. Also, make sure you use the proper print format. I'd say float or hexadecimal work best for comparison like this. – bjornruffians Mar 27 '14 at 11:57
  • go thorough this http://www.labbookpages.co.uk/audio/javaWavFiles.html and this http://www.mathworks.in/help/matlab/ref/wavread.html some times it will help you – Sree Mar 27 '14 at 11:58
  • Thank you for your comment. I didn't print the values into a text file. These values are copied while I am debugging the code. unfortunately, I don't know the encoding. – Mamoun Mar 27 '14 at 12:00
  • @Sreekanthss I tried the code in the link before, but "WavFile" is not defined in Android. – Mamoun Mar 27 '14 at 12:06
  • do you see this http://stackoverflow.com/a/11018368? – Sree Mar 27 '14 at 12:10
  • @Sreekanthss I tried the solution posted in the thread, and I got different values, but still not as Matlab. – Mamoun Mar 27 '14 at 12:38

3 Answers3

0

In Android you're reading the file header, not the actual values of the sound samples. Your values in Android are ASCII for

RIFF\l( WAVEfm

In Matlab I'm not sure what you're doing... looks like you're writing, not reading a file.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • I am writing on a textfile, and the values are not the header information, since they are very larg – Mamoun Mar 28 '14 at 21:16
0

The dir command is quite helpful here. It either displays the whole content of a directory but you can also specify a glob to just return a sub-set of files, e.g. dir('*.wav'). This returns an struct-array containing file information such as name, date, bytes, isdir and so on.

To get started, try the following:

filelist = dir('*.wav');
for file = filelist
    fprintf('Processing %s\n', file.name);
    fid = fopen(file.name);
    % Do something here with your file.
    fclose(fid);
end

If a processing result has to be stored per file, I often use the following pattern. I usually pre-allocate an array, a struct array or a cell array of the same size as the filelist. Then I use an integer index to iterate over the file list, which I can also use to write the output. If the information to be stored is homogeneous (e.g. one scalar per file), use an array or a struct array. However, if the information differs from file to file (e.g. vectors or matrices of different size) use a cell array instead.

An example using an ordinary array:

filelist = dir('*.wav');
% Pre-allocate an array to store some per-file information.
result = zeros(size(filelist));
for index = 1 : length(filelist)
    fprintf('Processing %s\n', filelist(index).name);
    % Read the sample rate Fs and store it.
    [y, Fs] = wavread(filelist(index).name);
    result(index) = Fs;
end
% result(1) .. result(N) contain the sample rates of each file.

An example using a cell array:

filelist = dir('*.wav');
% Pre-allocate a cell array to store some per-file information.
result = cell(size(filelist));
for index = 1 : length(filelist)
    fprintf('Processing %s\n', filelist(index).name);
    % Read the data of the WAV file and store it.
    y = wavread(filelist(index).name);
    result{index} = y;
end
% result{1} .. result{N} contain the data of the WAV files.
Sree
  • 3,136
  • 2
  • 31
  • 39
  • Thank you for your effort, maybe I didn't get your point clearly, but what is the reason of the difference in values between Matlab and Android? As I understood, you are trying to get into the file through indexing rather than absolute path, then you read the wav file into a vector. In the code I posted, I read the values of the wav file and save them into a text file instead. So it is the same concept I guess. – Mamoun Mar 28 '14 at 21:23
0

I am not sure what is the problem exactly, but I got the correct readings when I used the following code:

File filein = new File(filePath, "audio.wav");


         try
          {
             // Open the wav file specified as the first argument
             WavFile wavFile = WavFile.openWavFile(filein);

             // Display information about the wav file
             wavFile.display();

             // Get the number of audio channels in the wav file
             int numChannels = wavFile.getNumChannels();

             // Create a buffer of 100 frames
             double[] buffer = new double[20000 * numChannels];

             int framesRead;
             double min = Double.MAX_VALUE;
             double max = Double.MIN_VALUE;

             do
             {
                // Read frames into buffer
                framesRead = wavFile.readFrames(buffer, 20000);

                // Loop through frames and look for minimum and maximum value
                for (int s=0 ; s<framesRead * numChannels ; s++)
                {
                   if (buffer[s] > max) max = buffer[s];
                   if (buffer[s] < min) min = buffer[s];
                }
             }
             while (framesRead != 0);

             // Close the wavFile
             wavFile.close();

             // Output the minimum and maximum value
             System.out.printf("Min: %f, Max: %f\n", min, max);
          }
          catch (Exception e)
          {
             System.err.println(e);
          }
Mamoun
  • 1
  • 4
  • Hi, I am experiencing the same problem as you. Trying to read the values of my .wav file as well. I tried to implement the above code but it WavFile is not supported in Android. How do you overcome this? Also, does the min and max values output refers to the valley and peak? Thank You! – user1730935 Mar 13 '16 at 11:52