1 2 Previous Next 25 Replies Latest reply: Dec 29, 2010 11:10 AM by captfoss Go to original post RSS
      • 15. Re: Display sound data value from ByteArrayOutputStream
        captfoss
        cielle wrote:
        May i know what is the different between

        audioInputStream.getFrameLength()
        Number of frames in the stream, ie, length of the file
        audioFormat.getFrameSize() ?
        Number of bytes in a frame, ie, sample size / 8.
        • 16. Re: Display sound data value from ByteArrayOutputStream
          819932
          Hi, can i make a simple example here to get myself more understand to do the conversion?

          Says that my audio format is as 8000Hz, 16 bit, monostero channel, signed data, and using little Endian.

          I will will have
          - 8000 sample per second
          - 16 bit per one sample (2 byte per one sample)

          By assuming, my 2seconds audio wave file have sampled data (in Hexa format for easier description):
          0A00, 1A31, 2A33, 3A33 .............. 3A33,114A, 33fA (total, 8000*2 = 16000 sample)

          I firstly declare an array with 16000 size to store the decoded byte value of sound data.
          Then i carry on a for loop to decode the sound data
          int soundData[] = new int [16000];
          byte abData[] = 'my sound data as above' ;           // 2 byte array required to store a single sample , 32000byte required
          
          for (int i = 0; i < abData.length ; i+=2){
                  int b1 = abData; // The 1st byte of sample
               int b2 = abData[i + 1]; // The 2nd byte of sample
               
               int value;

               //Store the data based on the original Endian encoding format
               if (!isBigEndian) value = (b1 >> 8) + b2;
               else value = b1 + (b2 >> 8);
               soundData[i] = value;

          }
          But i can't get the correct sound data value. I compared the result from my code with Java and result in MATLAB.
          Please tell me what is the mistake i have done.
          
          Thank you.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
          • 17. Re: Display sound data value from ByteArrayOutputStream
            captfoss
                 if (!isBigEndian) value = (b1 >> 8) + b2;
            (b1 >> 8) is zero + b2 = b2
                 else value = b1 + (b2 >> 8);
            b1 + (b2 >> 8) is zero = b1

            You need to left-shift the MSB rather than right-shifting the LSB...
            • 18. Re: Display sound data value from ByteArrayOutputStream
              819932
              Hi i have made changes , but still not accurate...
              I have made a case. Please correct my concept and code where i have done wrong that cause the error in my code.

              If
              - my sound data of one sample is 1234H (in hexa for easier representation)
              - i have byte array abData[] that hold my sound data
              - Data is stored as arrangement below: abData[i+1] ... abData[i]

              In big Endian: 34 12
              Left-shift abData[i] by 8 bits

              In little Endian: 12 34
              Left-shift abData[i+1] by 8 bits

              The sound data should be obtained after abData[i+1] + abData[i]
              boolean isBigEndian = audioFormat.isBigEndian();
              
              //this array is the value of the signal at time, assume that i have n sample
              int x[] = new int[n];
              
              //convert each pair of byte values from the byte array to an Endian value
              for (int i = 0; i < n*2; i+=2) {
                   int b1 = abData;
                   int b2 = abData[i + 1];

                   int value;

                   //Store the data based on the original Endian encoding format
                   if (isBigEndian) value = (b1 << 8) + b2;
                   else value = b1 + (b2 << 8);
                   x[i/2] = value;
              }
              I have do according my concept... but the result still wrong...
              
              Below is the 1st 10th results i got from both Java and MATLAB after simulate the program with a 300 Hz sine wave audio file:
              
              Java:
              -173
              -779
              -1128
              -1219
              -1819
              -2161
              -2244
              -2835
              -3165
              -3233
              
              MATLAB: (I use wavread function to retrieve the sound data directly, the data is reliable.)
              -0.1272
              -0.1363
              -0.1451
              -0.1537
              -0.1620
              -0.1699
              -0.1776
              -0.1850
              -0.1920
              
              Please advise again.
              
              Thanks a lot!!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
              • 19. Re: Display sound data value from ByteArrayOutputStream
                captfoss
                I would have written...
                if (isBigEndian) value = (b1 << 8) + b2;
                else value = b1 + (b2 << 8);
                as
                if (isBigEndian) {
                    value = ((b1 << 8) & 0x0000ff00) | (b2 & 0x000000ff);
                }
                else {
                    value = ((b2 << 8) & 0x0000ff00) | (b1 & 0x000000ff);
                }
                And bear in mind that you're producing integer values while MATLAB is producing floating-point values... so your numbers aren't going to be the same because you're not producing the same kind of numbers.
                • 20. Re: Display sound data value from ByteArrayOutputStream
                  819932
                  Message noted.

                  I have confirmed that the result from MATLAB is mapped to the range of -1, +1..

                  I have tried to displays the native sound value in MATLAB ... noticed that the result is mostly the same between Java and MATLAB...

                  The difference is about plus minus 0, 1, 2...

                  Is that acceptable? I tried to retrieve the frequency value of audio file with Native value in Matlab... it works fine...

                  Now going to work out the same thing with Java... But get stucked with FFT ... FFT work with number of sample that is power of 2. But number of sample isn't power of 2...

                  I have started another tread regarding on FFT in java under Java Sound ... Hope to have your advise how can i use the ddf package for FFT and Hamming...

                  Many thanks for your replies all the way :)

                  Edited by: cielle on Dec 27, 2010 9:27 AM
                  • 21. Re: Display sound data value from ByteArrayOutputStream
                    captfoss
                    cielle wrote:
                    The difference is about plus minus 0, 1, 2...

                    Is that acceptable?
                    Really depends on what you're using it for, there really shouldn't be a difference between them at all... but most likely it's a rounding error and I wouldn't worry about it.
                    • 22. Re: Display sound data value from ByteArrayOutputStream
                      819932
                      I am going to use the sound data for windowing and fft....

                      From your option, would it give a big difference on the result?

                      I am now looking for the ddf package that provide windowing and FFT method...

                      From my research, class FFT is provided in package ddf..

                      I tried to import tis package in Netbean IDE.. but it give me error stated package is not available...

                      I will issue this problem in my other tread under Java Sound.. Hope to have advise again...

                      Thank you.
                      • 23. Re: Display sound data value from ByteArrayOutputStream
                        captfoss
                        cielle wrote:
                        I am going to use the sound data for windowing and fft....

                        From your option, would it give a big difference on the result?
                        Depends on the dynamic range of the sound. If you've got values going up to 10, you're talking about a difference of 30% (which isn't acceptable)... if you've got values going up to 10,000, you're talking about a difference of 0.03% (which is acceptable)
                        • 24. Re: Display sound data value from ByteArrayOutputStream
                          819932
                          Yea..

                          You are right.

                          Anyway, may i know

                          1)How usually the sampled sound data are mapped to the range of [-1, +1]? I should divide the data by what??

                          2)What is the purpose of mapping the data? Would it really make the calculation faster in FFT? I can't google the correct result to study bout it...

                          3)Is it more encouraged to map the data for FFT procedure?

                          Thank you.
                          • 25. Re: Display sound data value from ByteArrayOutputStream
                            captfoss
                            诸葛 wrote:
                            1)How usually the sampled sound data are mapped to the range of [-1, +1]? I should divide the data by what??
                            There isn't a mapping, you just convert the sample bitstream to doubles instead of ints...
                            2)What is the purpose of mapping the data? Would it really make the calculation faster in FFT? I can't google the correct result to study bout it...
                            Again, there's no mapping, just a different decoding of the data... and I'm unfamiliar with FFT, but if it's doing any floating-point operations, it'd be faster to do those on floating point values rather than integer values because it'll skip having to convert the ints to doubles, and so on...
                            3)Is it more encouraged to map the data for FFT procedure?
                            Again, not a mapping ;-)
                            1 2 Previous Next