Projects

New Music



Blog Roll



Support This Site

Reading and Writing Wav Files in Java

Earlier this year, we looked at how to read and write wav files in C++ and VB.Net, and C/C++ is really the natural choice for this kind of file IO (for a number of reasons that we won't go into here). Nonetheless, it is important to understand the process in other languages, if you want to be the type of language-agnostic programmer who can approach computer music in any environment.

So today we are looking at reading and writing wav files in Java. Surprisingly, this is the trickiest one of them all.

Java Numbers

The problem is that Java has no unsigned types. In other words, all numerical types in java can hold either positive or negative numbers. There are no types that are designed for only positibe numbers.

This doesn't sound like a big deal unless you know a little bit about how negative numbers are represented in most programming languages. The most important thing to know is that most languages represent negative numbers, in a way that is incompatible with the way they represent only positive numbers. More specifically, wav file headers represent numbers as unsigned bytes, whereas java represents numbers using a format called Two's Complement.

The practical implications of this are simple: you can't use java's built-in IO functions for reading wav headers. You have to read the data as bytes, then do the conversion in a subroutine.

Understanding this problem is hard, but luckily, working around it is relatively easy. In our solution, we made 2 changes. First, we stored all of our numbers with more bytes than are necessary in other languages (to ensure that we are storing the correct numbers). So for each 2-byte number, which would usually be stored by a short, we used an int, and for each 4-byte number, we used a long. Then we added 4 extra subroutines to do the conversions for us. These routines are at the bottom of wavIO.java.

// these two routines convert a byte array to a unsigned short
public static int byteArrayToInt(byte[] b)
{
  int start = 0;
  int low = b[start] & 0xff;
  int high = b[start+1] & 0xff;
  return (int)( high << 8 | low );
}

// these two routines convert a byte array to an unsigned integer
public static long byteArrayToLong(byte[] b)
{
  int start = 0;
  int i = 0;
  int len = 4;
  int cnt = 0;
  byte[] tmp = new byte[len];
  for (i = start; i < (start + len); i++)
  {
    tmp[cnt] = b[i];
    cnt++;
  }
  long accum = 0;
  i = 0;
  for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 )
  {
    accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
    i++;
  }
  return accum;
}

// returns a byte array of length 4
private static byte[] intToByteArray(int i)
{
  byte[] b = new byte[4];
  b[0] = (byte) (i & 0x00FF);
  b[1] = (byte) ((i >> 8) & 0x000000FF);
  b[2] = (byte) ((i >> 16) & 0x000000FF);
  b[3] = (byte) ((i >> 24) & 0x000000FF);
  return b;
}

// convert a short to a byte array
public static byte[] shortToByteArray(short data)
{
  return new byte[]{(byte)(data & 0xff),(byte)((data >>> 8) & 0xff)};
}

WAV File Specification

Again, we are using a VERY basic version of the wav file format for this example. This won't read many wav files, it will only read what are referred to here as canonical wave files. Basically, old fashioned wav files (before the chaos introduced by 24 and 32 bit recording).

If you want to read more complicated wav files, you may want to check out what's written here and here, although you really need to look at the actual specification (and you had better have some free time).

The Source Code

To use the source code, just unzip all the files into the same directory, and then use the batch files I created called compile.bat and run.bat. Of course, I am assuming that you have the latest version of java installed.

Download the source code for reading and writing wav files in java here.


Posted by Evan on August 29, 2008
Sharing:
at 2008-10-09 04:04:32
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.applet.AudioClip; // i import this for an audio clip
import java.net.*; // also for audio


class BlackJack extends JFrame implements ActionListener
{

    //*****ALL MY J VARIABLES**************

    JLabel background = new JLabel (new ImageIcon ("bl2.jpg"));
    //JLabel back2 = new JLabel ("BlackJack");

    JButton but1 = new JButton ("Continue");
    JButton but2 = new JButton ("GameRules");
    //MUSIC//
    AudioClip Music;
    ///////////////

    //**MY BLACKJACK OBJECT***
    public BlackJack ()
    {
        super ("BlackJack, By David Lutrin");
        setSize (700, 600);
        setLocation (180, 60);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setVisible (true);



        //ESTABLISH THE LAYERED PANE TO LAY OUT THE SCREEN
        //************************************************

        JLayeredPane x = getLayeredPane ();
        x.setOpaque (true);


        ////////////////////Music ??????????????/////
        try
        {
            Music = JApplet.newAudioClip (new URL ("File:Stock/2-03 Fort Knox.wav"));
            Music.play ();
        }

        catch (Exception u)
        {
            System.out.println (u);
        }

        ///////////////////////////////////////////


PLEASE HELP ME PLAY THE MUSIC FILE

at 2008-11-09 22:48:29
fgsdfgsfdgdfgdfgdfdfsd dadf dfasdf
test at 2008-11-10 06:15:22test
This is a test
asdw at 2008-11-30 22:07:01qwe
asdas
at 2009-03-02 11:49:56
fgdfgfdg
Eben at 2009-03-18 10:12:54Nice!
Thank you very much! This helped me quite a lot!!! The recording part was also tricky, maybe you could touch that as well please.
Eben Bekker

rts at 2009-04-29 00:22:20
welcome to the world of programming!!
at 2009-05-29 15:48:38
walk
at 2009-06-04 21:25:25

Shri Kamakshi Devi Pranam


at 2009-06-04 21:27:33

Shri Kamakshi Devi Pranam


Speak
Handle:
Title:
Comment:
Enter this text:
username: password: