Pergunta

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,

Foi útil?

Solução 3

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);
          }

Outras dicas

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.

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.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top