Manipulating audio data with filters

Y

yeti349

Hi. I'm trying to capture .wav data and manipulate it using filters,
etc... I've read a million posts on this topic, but I'm still at a loss
as to my next move.

I believe the following code successfully converts audio byte[] data to
a short array. When you run it from the command line you'll get data
like:

-94
2822
-1
-43
-110
-1
-92
-89
74
314

Exactly what does this data represent? Is this data ready to have a FFT
run on it?

How do I go about drawing the waveform of this data?

Please have a look at the code and let me know how I should proceed to
accomplish these tasks. Thanks for the help!


import java.io.*;
import java.nio.*;
import java.net.*;
import java.awt.*;
import java.util.*;
import java.lang.*;
import java.math.*;
import javax.media.*;
import javax.swing.*;
import java.lang.Long.*;
import javax.sound.sampled.*;

public class Filters {

final double pi = Math.PI;
private Player audioPlayer = null;
private int frameRate;

private AudioInputStream audioInput;


public static void main(String args[]) {


int numChannels,frameLengthInt;
int totalFrames = 0;
long frameLengthLong;

byte[] audioData;
double[] doubArray;

SourceDataLine sourceData;
AudioInputStream audioInput;
ByteArrayOutputStream byteOut;

String output = "";


try {

if (args.length == 1) {


File sourceFile = new File(args[0]);
//File targetFile = new File(args[1]);

//audio player for file
Filters player = new Filters(sourceFile);

//put audio data into an input stream
audioInput =
AudioSystem.getAudioInputStream(sourceFile);

//output stream
byteOut = new ByteArrayOutputStream();

//get the file format info.
AudioFileFormat fileFormat =
AudioSystem.getAudioFileFormat(sourceFile);





//get file type (.wav, .au...etc)
AudioFileFormat.Type targetFileType =
fileFormat.getType();

//get the total length of the file
frameLengthLong = audioInput.getFrameLength();
//convert long to int
frameLengthInt = (int)frameLengthLong;

//get channels: mono=(1,2), stereo=(4)
numChannels = audioInput.getFormat().getFrameSize();

//total bytes of file
int numBytes = frameLengthInt * numChannels;

//put total bytes into binary form
audioData = new byte[numBytes];

//play audio file
player.play();




try {

int numBytesRead = 0;
int numFramesRead = 0;

while((numBytesRead = audioInput.read(audioData))
!= -1) {

numFramesRead = numBytesRead/numChannels;
totalFrames += numFramesRead;


short[] shortArray = new short[numBytes/2];


for (int x = 0; x < numBytes/2; x++) {


shortArray[x] = (short)
((audioData[x * numChannels + 1] << 8 ) |
audioData[x * numChannels + 0]);

if (shortArray[x] != 0) {


System.out.println(shortArray[x]);


}


}

}
}
catch (Exception ex) {}



//System.out.println("file format: ");







//create byte array input stream
//ByteArrayInputStream bAIS = new
ByteArrayInputStream(audioData);
//AudioInputStream opAIS = new AudioInputStream(
// bAIS,
// audioFormat,
// audioData.length / audioFormat.getFrameSize());

//write modifed data to target file
//int nWrittenBytes = AudioSystem.write(
// opAIS,
// targetFileType,
// targetFile);

}
else { usage();}

System.exit(0);

}
catch (Exception ex) {}



} //end main



// create media player
public Filters(URL url) throws IOException, NoPlayerException,
CannotRealizeException {
audioPlayer = Manager.createRealizedPlayer(url);
}
public Filters(File file) throws IOException, NoPlayerException,
CannotRealizeException {
this(file.toURL());
}


// play and stop file
public void play() {
audioPlayer.start();
}
public void stop() {
audioPlayer.stop();
audioPlayer.close();
}

// usage display
public static void usage() {
System.out.println("Usage: java Filters inputFile");
}



} //end class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top