Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:42:15+00:00 2026-05-29T09:42:15+00:00

I am trying to read data of a .wav file both in java and

  • 0

I am trying to read data of a .wav file both in java and matlab and save as an array of bytes.

In java the code looks as follows:

 public byte[] readWav2(File file) throws UnsupportedAudioFileException, IOException {

     AudioFormat audioFormat;

     AudioInputStream inputAIS = AudioSystem.getAudioInputStream(file);
     audioFormat = inputAIS.getFormat();
     ByteArrayOutputStream baos = new ByteArrayOutputStream();

     // Read the audio data into a memory buffer.
     int nBufferSize = BUFFER_LENGTH * audioFormat.getFrameSize();


        byte[] abBuffer = new byte[nBufferSize];
        while (true) {

            int nBytesRead = inputAIS.read(abBuffer);

            if (nBytesRead == -1) {
                break;
            }
            baos.write(abBuffer, 0, nBytesRead);
        }

        byte[] abAudioData = baos.toByteArray();


        return abAudioData;
    }

In matlab I am using the wavread function:

[Y, FS] = wavread('sound.wav', 'native');

But the results I am getting are different.

In java the first 20 bytes:

53, 0, 19, 0, -71, -1, -80, -1, -99, -1, 10, 0, 87, 0, -69, -1, 123, -1, -77, -1

In matlab:

53, 19, -71, -80, -99, 10, 87, -69, -133, -77, 38, 143, 13, -100, 39, 45, -52, -83, -82, 56

Why every second byte in java is 0 or -1 where in matlab there isn’t? Even though I skip the 0’s and -1’s where in java there is 123 for matlab there is -133? Why is it different?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:42:15+00:00Added an answer on May 29, 2026 at 9:42 am

    Java is returning you 16-bit signed PCM data. Since each sample is 16 bits and a byte holds 8 bits, each sample spans two bytes in Java. What Matlab returns you is an array of the 16-bit samples directly.

    Basically, the data is the same. It’s just laid out differently in memory.

    To access the samples in an easier way from Java you could do some bitwise arithmetic, like this:

    int firstSample = (abAudioData[0]&0xFF) | (abAudioData[1]<<8);
    

    Another way to read the samples is with the java.nio buffers:

    ByteBuffer bb = ByteBuffer.wrap(abAudioData);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    ShortBuffer sb = bb.asShortBuffer();
    
    int firstSample = sb.get();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read data from the given web page using Java. public
I am trying to read the .wav file and give the raw data as
I'm trying to read data from a.csv file to ouput it on a webpage
I am trying to read data from a file stream as shown below: fileStream.Read(byteArray,
I'm trying to read data from a text file and assign it to arrays.
I am trying to read data from excel file in asp.net. I have added
I am trying to read data (which is actually an array) in Lisp from
In my program, I am trying to read data from binary file, and then
I was trying to read data from some huge file and write them back,
I have created new class to read the data from xml file, which looks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.