Manipulating audio data with filters

yeti349_at_yahoo.com
Date: 12/31/04


Date: 30 Dec 2004 16:28:28 -0800

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



Relevant Pages

  • Manipulating audio data with filters
    ... I'm trying to capture .wav data and manipulate it using filters, ... private AudioInputStream audioInput; ... int numChannels,frameLengthInt; ... AudioFileFormat.Type targetFileType = ...
    (comp.lang.java.help)
  • AudioInputStream -- skip() method seems not to work
    ... I am using AudioInputStream and I find that the 'skip' method seems not to ... int bufferLengthInFrames = line.getBufferSize/ 8; ... numBytesRemaining -= line.write; ...
    (comp.lang.java.programmer)
  • Re: AudioInputStream does not work properly
    ... I can play the Stream with a SourceDataLine and that stuff, ... the samples in an int[]. ... AudioSystem.getInputStream(AudioFormat, AudioInputStream) with an ...
    (comp.lang.java.help)
  • Re: SQL Query Help
    ... modify it for your needs ... create function dbo.fn_my (@id int) ... > I am try to build an SP that queries two table, Filters and Locations. ...
    (microsoft.public.sqlserver.programming)
  • SQL Query Help
    ... I am try to build an SP that queries two table, Filters and Locations. ... [CustomerID] ...
    (microsoft.public.sqlserver.programming)