|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object SimpleSound
public class SimpleSound
The SimpleSound
class is an implementation of the
Java Sound API specifically designed for use with students.
http://java.sun.com/products/java-media/sound/index.html
This class allows for easy playback, and manipulation of AU, AIFF, and WAV files.
Code & ideas for this class related to playing and viewing the sound were borrowed from the Java Sound Demo: http://java.sun.com/products/java-media/sound/ samples/JavaSoundDemo/ Also, some code borrowed from Tritonus as noted. Copyright Georgia Institute of Technology 2004
Field Summary | |
---|---|
static int |
MAX_NEG
Constant for max negative value |
static int |
MAX_POS
Constant for max positive value |
Constructor Summary | |
---|---|
SimpleSound()
Constructs a SimpleSound of 3 seconds long. |
|
SimpleSound(int numFrames)
Constructs a SimpleSound of the specified length. |
|
SimpleSound(int sampleSizeInBits,
boolean isBigEndian)
Constructs a simple sound with the given sample size in bits and type of endian (big or little) |
|
SimpleSound(int numFrames,
int sampleRate)
Constructs a SimpleSound of the specified length. |
|
SimpleSound(SimpleSound sound)
Constructor that creates a new SimpleSound by copying a passed SimpleSound |
|
SimpleSound(String fileName)
Constructs a new SimpleSound from the given file. |
Method Summary | |
---|---|
byte[] |
asArray()
Method to return the byte array |
void |
blockingPlay()
Plays the sound, then sleeps for how long the sound SHOULD last. |
void |
blockingPlayAtRateDur(double rate,
double durInFrames)
First, checks the value of durInFrames to make sure that it is not larger than Integer.MAX_VALUE to guarrantee safe casting. |
void |
blockingPlayAtRateInRange(float rate,
int startFrame,
int endFrame)
Calls playAtRateInRange(rate, startFrame, endFrame, true)
. |
void |
blockingPlayOld()
Creates a new Playback thread, starts it, then waits for the entire sound to finish playing before it returns. |
static void |
convert(String mp3File,
String wavFile)
Method to convert a mp3 sound into a wav sound |
void |
explore()
Method to open a sound viewer on a copy of this sound |
AudioFileFormat |
getAudioFileFormat()
Method that returns the AudioFileFormat describing this simple sound. |
byte[] |
getBuffer()
Method that returns the byte array representation of this simple sound. |
int |
getChannels()
Obtains the number of channels of this sound. |
boolean |
getDEBUG()
Method to get the value of the debug flag |
String |
getFileName()
Method that returns the name of the file this sound came from. |
byte[] |
getFrame(int frameNum)
Returns an array containing all of the bytes in the specified frame. |
int |
getLeftSample(int frameNum)
Obtains the left sample of the audio data contained at the specified frame. |
int |
getLength()
Method to return the length of the sound as the number of samples |
int |
getLengthInBytes()
Obtains the length of this sound in bytes. |
int |
getLengthInFrames()
Obtains the length of the audio data contained in the file, expressed in sample frames. |
int |
getNumSamples()
Returns the number of samples in this sound |
Vector |
getPlaybacks()
Method that returns the vector of playback threads currently active on this sound. |
int |
getRightSample(int frameNum)
Obtains the right sample of the audio data contained at the specified frame. |
SoundSample |
getSample(int frameNum)
Method to create and return a SoundSample object for the given frame number |
SoundSample[] |
getSamples()
Method to create and return an array of SoundSample objects |
int |
getSampleValue(int frameNum)
If this is a mono sound, obtains the single sample contained within this frame, else obtains the first (left) sample contained in the specified frame. |
int |
getSampleValueAt(int index)
Method to get the sample at the passed index and handle any SoundExceptions |
double |
getSamplingRate()
Method to get the sampling rate of this sound |
SoundExplorer |
getSoundExplorer()
Method that returns the SoundExplorer |
boolean |
isStereo()
Method to check if a sound is stereo (2 channels) or not |
void |
loadFromFile(String inFileName)
Resets the fields of this sound so that it now represents the sound in the specified file. |
AudioInputStream |
makeAIS()
Creates an AudioInputStream for this sound from the
buffer and the audioFileFormat . |
void |
play()
Creates a new Playback thread and starts it. |
void |
playAtRateDur(double rate,
double durInFrames)
Checks the value of durInFrames to make sure that it is not larger than Integer.MAX_VALUE to guarrantee safe casting. |
void |
playAtRateInRange(float rate,
int startFrame,
int endFrame)
Calls playAtRateInRange(rate, startFrame, endFrame,
false) . |
void |
playAtRateInRange(float rate,
int startFrame,
int endFrame,
boolean isBlocking)
Plays the specified segment of this sound at the given sample rate. |
static void |
playNote(int key,
int duration,
int intensity)
Method to play a note using MIDI |
void |
printError(String message)
Invokes printError(message, null) |
void |
printError(String message,
Exception e)
Prints the given String to the "standard" error output stream, then prints a stack trace on the exception, and then exits the program. |
void |
removePlayback(Playback playbackToRemove)
Deletes the specified playback object from the Vector. |
void |
setAudioFileFormat(AudioFileFormat newAudioFileFormat)
Changes the AudioFileFormat of this sound. |
void |
setBuffer(byte[] newBuffer)
Changes the byte array that represents this sound. |
void |
setFrame(int frameNum,
byte[] theFrame)
Changes the value of each byte of the specified frame. |
void |
setLeftSample(int frameNum,
int sample)
|
void |
setRightSample(int frameNum,
int sample)
|
void |
setSampleValue(int frameNum,
int sample)
Changes the value of the sample found at the specified frame. |
void |
setSampleValueAt(int index,
int value)
Method to set the sample value at the passed index to the passed value |
void |
setSoundExplorer(SoundExplorer soundExplorer)
Changes the explorer of this object. |
String |
toString()
Obtains a string representation of this JavaSound. |
void |
write(String fileName)
Method to write this sound to a file |
void |
writeToFile(String outFileName)
Creates an audioInputStream from this sound, and then writes this stream out to the file with the specified name. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final int MAX_NEG
public static final int MAX_POS
Constructor Detail |
---|
public SimpleSound()
SimpleSound
of 3 seconds long.
public SimpleSound(int numFrames)
SimpleSound
of the specified length.
This sound will simply consist of an empty byte array, and an
AudioFileFormat
with the following values:
AudioFileFormat.Type.WAVE
writeToFile(String filename)
must be called on this newly created sound.
numFrames
- the number of samples in the soundwrite(String filename)
public SimpleSound(int numFrames, int sampleRate)
SimpleSound
of the specified length.
This sound will simply consist of an empty byte array, and an
AudioFileFormat
with the following values:
AudioFileFormat.Type.WAVE
writeToFile(String filename)
must be called on this newly created sound.
numFrames
- the number of samples in the soundwrite(String filename)
public SimpleSound(int sampleSizeInBits, boolean isBigEndian)
public SimpleSound(String fileName)
fileName
- The File from which to create this sound.loadFromFile(String filename)
public SimpleSound(SimpleSound sound)
sound
- the sound to copyMethod Detail |
---|
public byte[] getBuffer()
public AudioFileFormat getAudioFileFormat()
AudioFileFormat
public double getSamplingRate()
public SoundExplorer getSoundExplorer()
public byte[] asArray()
getBuffer()
public Vector getPlaybacks()
public String getFileName()
loadFromFile(String fileName)
public boolean getDEBUG()
public void setBuffer(byte[] newBuffer)
newBuffer
- a byte array representation of the new sound we
want this to represent.public void setAudioFileFormat(AudioFileFormat newAudioFileFormat)
newAudioFileFormat
- the new audioFileFormat that describes
this sound.AudioFileFormat
public void setSoundExplorer(SoundExplorer soundExplorer)
soundExplorer
- the new SoundExplorer to useSoundExplorer
public AudioInputStream makeAIS()
AudioInputStream
for this sound from the
buffer
and the audioFileFormat
.
AudioInputStream
public void printError(String message)
printError(message, null)
SoundException
- Will throw under every circumstance.
This way we can catch the exception
in JES.printError(String message, Exception e)
public void printError(String message, Exception e)
message
- A description of the errore
- The exception, if any, that was caught regarding the error
SoundException
- Will throw under every circumstance.
This way we can catch the exception
in JES.public boolean isStereo()
public void write(String fileName)
fileName
- the name of the file to write topublic void writeToFile(String outFileName) throws SoundException
AudioFileFormat.Type
of this sound.
outFileName
- The name of the file to write this sound to
SoundException
- if any error is encountered while
writing to the file.public void loadFromFile(String inFileName) throws SoundException
inFileName
.
inFileName
- the path and filename of the sound we want to
represent.
SoundException
- if any problem is encountered while
reading in from the file.public void play()
play()
multiple times
in a row, sounds will simply play on
top of eachother - "accidental mixing"
Playback
public void blockingPlayOld()
Playback
public void blockingPlay()
public void playAtRateDur(double rate, double durInFrames) throws SoundException
rate
- a double representing the change in sampleRate
(==frameRate) for playing back this sounddurInFrames
- a double representing how much of this
sound we want to play.
SoundException
- if there are problems playing the sound.playAtRateInRange(float rate,
int startFrame,
int endFrame,
boolean isBlocking)
public void blockingPlayAtRateDur(double rate, double durInFrames) throws SoundException
rate
- a double representing the change in sampleRate
(==frameRate) for playing back this sounddurInFrames
- a double representing how much of this sound
we want to play
SoundException
- if there are problems playing the sound.playAtRateInRange(float range,
int startFrame,
int endFrame,
boolean isBlocking)
public void playAtRateInRange(float rate, int startFrame, int endFrame) throws SoundException
playAtRateInRange(rate, startFrame, endFrame,
false)
.
rate
- a float representing the change in sampleRate
(==frameRate) for playing back this soundstartFrame
- an int representing the frame at which we
want to begin playing the soundendFrame
- an int representing the frame at which want
to stop playing the sound
SoundException
- if there are problems playing the sound.playAtRateInRange(float range,
int startFrame,
int endFrame,
boolean isBlocking)
public void blockingPlayAtRateInRange(float rate, int startFrame, int endFrame) throws SoundException
playAtRateInRange(rate, startFrame, endFrame, true)
.
rate
- a float representing the change in sampleRate
(==frameRate) for playing back this soundstartFrame
- an int representing the frame at which we want
to begin playing the soundendFrame
- an int representing the frame at which want
to stop playing the sound
SoundException
- if there are problems playing the sound.playAtRateInRange(float range,
int startFrame,
int endFrame,
boolean isBlocking)
public void playAtRateInRange(float rate, int startFrame, int endFrame, boolean isBlocking) throws SoundException
rate
- The change in the sampleRate (==frameRate) for
playing back this sound. The old SampleRate is multiplied by
this value. So, if rate = 2, the sound will play twice as fast
(half the length), and if rate = .5, the sound will play half as
fast (twice the length).startFrame
- The index of the frame where we want to begin
playendFrame
- The index of the frame where we want to end playisBlocking
- If true, this method waits until the thread is
done playing the sound before returning. If false, it
simply starts the thread and then returns.
SoundException
- if there are any problems playing the
sound.public void removePlayback(Playback playbackToRemove)
Playback.run()
public byte[] getFrame(int frameNum) throws SoundException
frameNum
- the index of the frame to access
frameNum
SoundException
- if the frame number is invalid.public int getLengthInFrames()
public int getNumSamples()
public SoundSample getSample(int frameNum)
public SoundSample[] getSamples()
public int getSampleValueAt(int index)
index
- the desired index
public int getSampleValue(int frameNum) throws SoundException
frameNum
- the index of the frame to access
SoundException
- if the frame number is invalid.public int getLeftSample(int frameNum) throws SoundException
frameNum
- the index of the frame to access
SoundException
- if the frameNumber is invalidpublic int getRightSample(int frameNum) throws SoundException
frameNum
- the index of the frame to access
SoundException
- if the frameNumber is invalid, or
the encoding isn't supported.public int getLengthInBytes()
public int getLength()
public int getChannels()
AudioSystem.NOT_SPECIFIED
AudioSystem.NOT_SPECIFIED
public void setFrame(int frameNum, byte[] theFrame) throws SoundException
frameNum
- the index of the frame to changetheFrame
- the byte array that will be copied into this sound's
buffer in place of the specified frame.
SoundException
- if the frameNumber is invalid.public void setSampleValueAt(int index, int value)
index
- the indexvalue
- the new valuepublic void setSampleValue(int frameNum, int sample) throws SoundException
frameNum
- the index of the frame where the sample should be changedsample
- an int representation of the new sample to put in this
sound's buffer at the specified frame
SoundException
- if the frameNumber is invalid, or
another problem is encounteredpublic void setLeftSample(int frameNum, int sample) throws SoundException
SoundException
public void setRightSample(int frameNum, int sample) throws SoundException
SoundException
public void explore()
public static void playNote(int key, int duration, int intensity)
key
- the piano key to playduration
- how long to play the noteintensity
- how hard to strike the note from (0-127)public static void convert(String mp3File, String wavFile)
mp3File
- wavFile
- public String toString()
toString
in class Object
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |