|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--JavaSound
The JavaSound
class is an Implementation of the
Java Sound API specifically designed for use with the Jython
Environment for Students (JES).
This class allows for easy playback, and manipulation of AU, AIFF, and WAV files.
Note that EVERYTHING in this file is 0-indexed. (Since we want JES to use a 1-based indexing system for sounds, the media.py file contains wrappers for all of these functions that translate between the 0 and 1 based systems). Except error messages, they are 1-indexed so that JES can catch them and make sense of it all.
Nested Class Summary | |
class |
JavaSound.Playback
The nested class Playback extends from Thread
and allows for playback of this sound. |
Constructor Summary | |
JavaSound()
Constructs a JavaSound of 3 seconds long. |
|
JavaSound(int numSeconds)
Constructs a JavaSound of the specified length. |
|
JavaSound(int sampleSizeInBits,
boolean isBigEndian)
|
|
JavaSound(java.lang.String fileName)
Constructs a new JavaSound from the given file. |
Method Summary | |
byte[] |
asArray()
|
void |
blockingPlay()
Creates a new Playback thread, starts it, then waits for the entire sound to finish playing before it returns. |
void |
blockingPlayAtRateDur(double rate,
double durInFrames)
Calls playAtRateInRange((float)rate, 0, (int)durInFrames-1, true)
. |
void |
blockingPlayAtRateInRange(float rate,
int startFrame,
int endFrame)
Calls playAtRateInRange(rate, startFrame, endFrame, true)
. |
javax.sound.sampled.AudioFileFormat |
getAudioFileFormat()
Obtains the AudioFileFormat describing this sound. |
byte[] |
getBuffer()
Obtains the byte array representation of this sound. |
int |
getChannels()
Obtains the number of channels of this sound. |
java.lang.String |
getFileName()
Obtains 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()
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. |
java.util.Vector |
getPlaybacks()
Obtains the collection 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. |
int |
getSample(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. |
static byte |
intToUnsignedByte(int sample)
|
static void |
intToUnsignedBytes16(int sample,
byte[] buffer,
int byteOffset,
boolean bigEndian)
|
static void |
intToUnsignedBytes24(int sample,
byte[] buffer,
int byteOffset,
boolean bigEndian)
|
static void |
intToUnsignedBytes32(int sample,
byte[] buffer,
int byteOffset,
boolean bigEndian)
|
boolean |
isStereo()
|
java.lang.String |
justABufferTaste(byte[] b)
|
java.lang.String |
justATaste()
|
static byte |
linear2alaw(short pcm_val)
|
static byte |
linear2ulaw(int sample)
Converts a linear signed 16bit sample to a uLaw byte. |
void |
loadFromFile(java.lang.String inFileName)
Resets the fields of this sound so that it now represents the sound in the specified file. |
static void |
main(java.lang.String[] args)
|
javax.sound.sampled.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)
Calls playAtRateInRange((float)rate, 0, (int)durInFrames-1, false)
. |
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. |
void |
printError(java.lang.String message)
Invokes printError(message, null) |
void |
printError(java.lang.String message,
java.lang.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 |
setAudioFileFormat(javax.sound.sampled.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 |
setSample(int frameNum,
int sample)
Changes the value of the sample found at the specified frame. |
void |
setSoundView(SoundView soundView)
Changes the view of this object. |
java.lang.String |
toString()
Obtains a string representation of this JavaSound. |
static int |
unsignedByteToInt24(byte[] buffer,
int offset,
boolean isBigEndian)
|
static int |
unsignedByteToInt32(byte[] buffer,
int offset,
boolean isBigEndian)
|
void |
writeToFile(java.lang.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 |
Constructor Detail |
public JavaSound()
JavaSound
of 3 seconds long.
JavaSound(int numSeconds)
public JavaSound(int numSeconds)
JavaSound
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.
numSeconds
- The length, in seconds, for the new soundwriteToFile(String filename)
public JavaSound(int sampleSizeInBits, boolean isBigEndian)
public JavaSound(java.lang.String fileName) throws JavaSoundException
fileName
- The File from which to create this sound.
JavaSoundException
- if there are problems loading the fileloadFromFile(String filename)
Method Detail |
public byte[] getBuffer()
public javax.sound.sampled.AudioFileFormat getAudioFileFormat()
AudioFileFormat
public byte[] asArray()
getBuffer()
public java.util.Vector getPlaybacks()
public java.lang.String getFileName()
loadFromFile(String fileName)
public void setBuffer(byte[] newBuffer)
newBuffer
- a byte array representation of the new sound we
want this to represent.public void setAudioFileFormat(javax.sound.sampled.AudioFileFormat newAudioFileFormat)
newAudioFileFormat
- the new audioFileFormat that describes
this sound.AudioFileFormat
public void setSoundView(SoundView soundView)
#soundView
public javax.sound.sampled.AudioInputStream makeAIS()
AudioInputStream
for this sound from the
buffer
and the audioFileFormat
.
AudioInputStream
public void printError(java.lang.String message) throws JavaSoundException
printError(message, null)
JavaSoundException
- Will throw under every circumstance.
This way we can catch the exception
in JES.printError(String message, Exception e)
public void printError(java.lang.String message, java.lang.Exception e) throws JavaSoundException
message
- A description of the errore
- The exception, if any, that was caught regarding the error
JavaSoundException
- Will throw under every circumstance.
This way we can catch the exception
in JES.public boolean isStereo()
public void writeToFile(java.lang.String outFileName) throws JavaSoundException
AudioFileFormat.Type
of this sound.
outFileName
- The name of the file to write this sound to
JavaSoundException
- if any error is encountered while writing
to the file.public void loadFromFile(java.lang.String inFileName) throws JavaSoundException
inFileName
.
inFileName
- the path and filename of the sound we want to
represent.
JavaSoundException
- 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"
JavaSound.Playback
public void blockingPlay()
JavaSound.Playback
public void playAtRateDur(double rate, double durInFrames) throws JavaSoundException
playAtRateInRange((float)rate, 0, (int)durInFrames-1, false)
. Checks the value of durInFrames to make sure that it is not
larger than Integer.MAX_VALUE to guarrantee safe casting. Also checks
the value of rate to make sure that it is not larger than Float.MAX_VALUE
before casting.
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.
JavaSoundException
- if there are problems playing the sound.playAtRateInRange(float rate, int startFrame,
int endFrame, boolean isBlocking)
public void blockingPlayAtRateDur(double rate, double durInFrames) throws JavaSoundException
playAtRateInRange((float)rate, 0, (int)durInFrames-1, true)
. First, checks the value of durInFrames to make sure that it is
not larger than Integer.MAX_VALUE to guarrantee safe casting.
Simmilarly, checks the value of rate to make sure that it is not larger
than FLoat.MAX_VALUE before casting.
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
JavaSoundException
- 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 JavaSoundException
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
JavaSoundException
- 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 JavaSoundException
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
JavaSoundException
- 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 JavaSoundException
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.
JavaSoundException
- if there are any problems playing the sound.public byte[] getFrame(int frameNum) throws JavaSoundException
frameNum
- the index of the frame to access
frameNum
JavaSoundException
- if the frame number is invalid.public int getLengthInFrames()
public int getSample(int frameNum) throws JavaSoundException
frameNum
- the index of the frame to access
JavaSoundException
- if the frame number is invalid.public int getLeftSample(int frameNum) throws JavaSoundException
frameNum
- the index of the frame to access
JavaSoundException
- if the frameNumber is invalidpublic int getRightSample(int frameNum) throws JavaSoundException
frameNum
- the index of the frame to access
JavaSoundException
- if the frameNumber is invalid, or
the encoding isn't supported.public int getLength()
public int getChannels()
AudioSystem.NOT_SPECIFIED
AudioSystem.NOT_SPECIFIED
public void setFrame(int frameNum, byte[] theFrame) throws JavaSoundException
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.
JavaSoundException
- if the frameNumber is invalid.public void setSample(int frameNum, int sample) throws JavaSoundException
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
JavaSoundException
- if the frameNumber is invalid, or
another problem is encounteredpublic void setLeftSample(int frameNum, int sample) throws JavaSoundException
JavaSoundException
public void setRightSample(int frameNum, int sample) throws JavaSoundException
JavaSoundException
public static byte linear2ulaw(int sample)
public static byte linear2alaw(short pcm_val)
public static int unsignedByteToInt24(byte[] buffer, int offset, boolean isBigEndian)
public static int unsignedByteToInt32(byte[] buffer, int offset, boolean isBigEndian)
public static byte intToUnsignedByte(int sample)
public static void intToUnsignedBytes16(int sample, byte[] buffer, int byteOffset, boolean bigEndian)
public static void intToUnsignedBytes24(int sample, byte[] buffer, int byteOffset, boolean bigEndian)
public static void intToUnsignedBytes32(int sample, byte[] buffer, int byteOffset, boolean bigEndian)
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String justATaste()
public java.lang.String justABufferTaste(byte[] b)
public static void main(java.lang.String[] args)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |