Manipulating Sounds in Java

Before reading or using example of this article. You must have to learn to setup eclipse environment or use Dr. Java IDE. Here are the instructions to configure and prepare eclipse environment “customize-use-bookclasses-eclipse“.

This article will help you to understand how to digitize and manipulate (e.g. splicing, reversing and blending ) sounds. BookClass library contains SoundSimple class having flexible methods to manipulate sounds. Available methods are listed below.
SimpleSound Class

Method Description
SimpleSound(String)  Create  a new sound from given sound
SimpleSound(SimpleSound)  Create a sound by passing a simplesound instance
SimpleSound(int, int)  Create sound with a buffer and the AudioFileFormat, no sound file created
SimpleSound(int, Boolean)  Create sound with given sample size in bits and title
SimpleSound(int)  Create sound with a buffer and the AudioFileFormat
SimpleSound()  Create a simplesound 3 second long
writeToFile(String)  Create an audioInputStream from this sound, and write stream out to file with specified name
write(String)  Write sound to a file
toString()  Return string representation of sound
setSoundExplorer(SoundExplorer)  Changing the explorer of object
setSampleValueAt(int, int) Set sample value at given index
setSampleValue(int, int)  Changes the value of the sample found at the specified frame
setRightSample(int, int)
setLeftSample
setFrame  Changes the value of each byte of the specified frame
setBuffer  Changes the byte array that represents this sound
setAudioFileFormat(AudioFileFormat)
reportIndexException
removePlayback(Playback)
playAtRateInRange(float, int, int, Boolean)
playAtRateInRange(float, int, int)
playAtRateDur(double, double)  Checks the value of durInFrames
play()  New Playback thread and starts it
makeAIS()  An AudioInputStream representing this sound
loadFromFile(String)  Resets the fields of this sound
isStereo()  Check if a sound is stereo (2 channels) or not
getSoundExplorer()  The sound explorer
getSamplingRate()  The sampling rate in number of samples per second
getSampleValueAt(int)  Method to get the sample at the passed index and handle
getSampleValue(int)  Obtains the single sample contained
getSamples()  Return an array of SoundSample objects
getSample(int)  Return a SoundSample object
getRightSample(int)  Right sample of the audio data contained at the specified frame
getPlaybacks()  Returns the vector of playback threads currently active on this sound
getNumSamples()  Returns the number of samples in this sound
getLengthInFrames()  Length of the audio data contained in the file
getLengthInBytes()  Length of this sound in bytes
getLength()  Returns the length of sound as number of samples
getLeftSample(int)  Returns left sample of the audio data contained at the specified
getFrame(int)  Returns an array containing all of the bytes in the specified frame
getDEBUG()  Returns value of debug flag
getFileName()  Returns the name of file
getChannels()  Returns number of channels for this sound
getBuffer()  Returns byte array
getAudioFileFormat()  Returns audio file format
explore()  Open a sound viewer
blockingPlayOld()  Play the entire sound, and does not allow for any accidental mixing
blockingPlayAtRateInRange(float, int, int)
blockingPlayAtRateDur(double, double)  Check duration in frames
blockingPlay()  Play sounds, then sleeps as long sound last
asArray()  Method to return byte array

Here is the first example to open a sound file and play.

package Sounds;

import bookClasses.FileChooser;
import bookClasses.SimpleSound;
import bookClasses.SoundSample;

public class MySound extends SimpleSound {
  
  /**
   * Constructor that takes a file name
   * @param fileName the name of the file
   */
  public MySound(String fileName)
  {
    // let the parent class handle setting the file name
    super(fileName);
  }
  
   

	public static void main(String[] args) {
		//open a sound file e.g. WAV file
		String fileName = FileChooser.pickAFile();
		//Create instance of sound 
		MySound sound = new MySound(fileName);
		
		//Get sound samples
		SoundSample[] sampleArray = sound.getSamples();
		
		//Print lenght or size of sound sample
		System.out.println(sampleArray.length);
		//play sound
		sound.play();
	}
}

Example Audio used in example. Download it from here.
 

Video with code in eclipse and showing result.

No Responses