Reading and writing audio data

Java provides classes for reading and writing from audio files in the AU, WAVE and AIFF formats. jMusic builds on these classes to provide some simple read and write utilities that deliver and accept audio data as an array of floating point numbers. Specifically, the Read.audio() and Write.audio() methods, in the jm.util package. The reading method operates in two ways, one returns an array of floats, the other accepts an existing array and fills that array. Both methods require the name of the file to be read.

	float[] data = new float[1];
	Read.audio(data, "Welcome.au");

or

	float[] data = Read.audio("Welcome.au");


The writing method has only one argument format, accepting an array of floats and the name for the saved file.

	Write.audio(data, "SavedAudio.aif");

Accessing recorded sound
A program to access an audio file simply needs to wrap the Read.audio() method with a class structure. Here is one of the simplest Java classes that imports and uses the Read.audio() method.

import jm.util.*;

public final class ReadAudio {
    public static void main(String[] args){
        float[] data = Read.audio("Welcome.au");
    }
}


During the reading of the audio file, the Read class prints some feedback out to the terminal window. This includes information about the file.

-------------------- Reading Audio File ---------------------
File 'Welcome.au' read in. Details:
Channels = 1, Samples per channel = 47492, Sample rate = 44100, Bit depth = 16
-------------------------------------------------------------


It should be noted that these methods are designed to enable stand-alone manipulation of audio data and that many additional audio features ion Java and jMusic allow for integration of event-based and audio-based music making processes.

To access the audio samples, simply read from the float array data. For example, addind a for-loop after the Read.audio() line can print out all the sample values to the terminal window.

	for (int i=0; i< data.length; i++) {
	System.out.print(data[i] + " ");
	}


In order to see a more visual (but still textual) display of the sample data, replace the previous for-loop with this one, recompile and run.

	for (int i=0; i< data.length; i+= 10) {
            for(int j=0; j-40<data[i] * 40; j++) {
                System.out.print(" ");
            }
            System.out.println("o");
       }


Saving samples as an audio file
The compliment to the Read class is the Write class, whose audio() method will create an audio file from an array of floats. In the class below, samples are read from one file and immediately written out to another. Notice that the written file is of a different type to the input file. So this program performs a simple file type conversion from an AU to AIFF file.

import jm.util.*;

public final class WriteAudio {
public static void main(String[] args){
        float[] data = Read.audio("Welcome.au");
        Write.audio(data, "SavedAudio.aif");
    }
}


As a final code example in this section, we’ll manipulate the samples before they are written. A simple change is to reverse the order of the samples, thus playing the sound backwards.

import jm.util.*;

public final class WriteReverseAudio {
    public static void main(String[] args){
        float[] data = Read.audio("Welcome.au");
        float[] reversed = new float[data.length];
        for (int i=0; i< data.length; i++) {
            reversed[data.length - i - 1] = data[i];
        }
        Write.audio(reversed, "ReversedAudio.wav");
    }
}


© 2004 Andrew Brown
jMusic Tutorial Index