Java - Reading, Manipulating and Writing Wav Files

Reading wav file in Java

The official Java Sound Programmer Guide walks through reading and writing audio files.

This article by A Greensted: Reading and Writing Wav Files in java should be helpful. The WavFile class is very useful and it can be tweaked to return the entire data array instead of buffered fragments.

Java Reading/Manipulating Wav File

Will you know this beat in advance? If so, you could cross correlate the two signals and the highest peak in this output would correspond to the time delay.

Other than that, depending on the sound before the beat starts, you could convert to frequency domain (via FFT) and have a look at what frequencies are present and see whether there's a significant change when the beat begins.

Some examples/extra detail would help.

If you're trying to detect the tempo of said beat, please ignore everything most of what I've said.

reading wav/wave file into short[] array

Bring it in via an AudioInputStream as one normally does for sound.

With each buffer load you grab from the stream, iterate through it, two bytes at a time (if it is 16-bit encoding) and use your favorite algorithm to convert the two bytes to a single short int. (Depends if file is Big-Endian or Little-Endian.)

Then, save the results by appending to your preferred form of short array, instead of posting to a SourceDataLine like is done in the more usual playback situations.

Quote from Java Tutorials:

Because the Java Sound API gives you access to the audio data as an
array of bytes, you can alter these bytes in any way you choose.

You might check out the code in this Java sound tutorial: Using Files and Format Converters, the section "Reading Sound Files" and note the comment: "Here, do something useful with the audio data that's now in the audioBytes array..."

For example, that something useful might be:

    myShortArray[i] = (short)(( buffer[i*2] & 0xff )|( buffer[i*2 + 1] << 8 ));

buffer = byte buffer receiving the AudioInputStream.

i = index into the arrays.

Java how to write 128 kpbs wav file

I managed to find an answer to my question.

Maybe later someone also ask the same so I'm giving my solution.

Under the class I wrote these global variables as I wanted my wav

static AudioFormat.Encoding defaultEncoding = AudioFormat.Encoding.PCM_SIGNED;
static float fDefaultSampleRate = 8000;
static int nDefaultSampleSizeInBits = 16;
static int nDefaultChannels = 1;
static int frameSize = 2;
static float frameRate= 8000;
static boolean bDefaultBigEndian = false;

And changed my code like this.

I created a format as I wanted, generated audio from text, changed audio in my format and wrote it.

AudioFormat defaultFormat = new AudioFormat(defaultEncoding,fDefaultSampleRate,nDefaultSampleSizeInBits,nDefaultChannels,frameSize,frameRate,bDefaultBigEndian);
AudioInputStream GeneratedAudio = marytts.generateAudio(text); //generate audio from text
AudioInputStream audio = AudioSystem.getAudioInputStream(defaultFormat, GeneratedAudio);
AudioSystem.write(audio, AudioFileFormat.Type.WAVE, new File("F:\\temp\\" + filename + ".wav"));//save audio as .wav to the static location with filename
return true;//function completed so return true


Related Topics



Leave a reply



Submit