Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
finnkuusisto committed Feb 25, 2012
0 parents commit 349ff84
Show file tree
Hide file tree
Showing 10 changed files with 1,445 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
############
# Compiled #
############
*.class
*.jar

#################
# IDE Generated #
#################
.classpath
.project
.settings

########
# Misc #
########
*~
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright (c) 2012, Finn Kuusisto
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 changes: 52 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
About
-----
TinySound is a simple sound system that wraps the standard Java sound libraries.
It is "tiny" in that it is intended to have a small, easy-to-use interface with
everything that you need in a simple sound system and nothing that you don't.

License
-------
TinySound is licensed under the BSD 2-Clause license. A copy of the license can
be found in the header of every source file as well as in the LICENSE file
included with the TinySound system.

Using TinySound
---------------
There are 3 classes that you need to know when using TinySound: TinySound, Music
and Sound. TinySound is the main system class, Music is an abstraction for
music, and Sound is an abstraction for Sound. Simple.

-TinySound-
There are really only 2 steps you need to worry about with the TinySound class.
1. Initialization
2. Shutdown

1. Initialization is accomplished via one of the two init() functions:
-The first one (and probably the only one you will care about) takes no
arguments and sets up the system for you to automatically write any sound data
to the speakers 40 times per second. TinySound creates another thread to
perform updates. This should be sufficient in almost all use cases.
-The second one allows you to specify at what rate you would like sound data to
be written to the speakers and whether or not TinySound should write the data
automatically or if you would like to handle those updates yourself.
Note: If you choose to not have TinySound automatically update, you must call
one of the update() methods yourself at the rate specified. See the Javadocs
for more detail.

2. Shutdown is accomplished via the shutdown() function. It is especially
important that you shutdown TinySound if the system has been initialized to
perform automatic updates so it can stop the update thread.

-Music-
You load Music objects via the TinySound loadMusic() functions. Music objects
can be started, stopped, paused, resumed, and looped from specified positions.
If you are done using a particular Music object, you can also unload its sound
data from the system via its unload() function. See the Javadocs for more
detail.

-Sound-
You load Sound objects via the TinySound loadSound() functions. Sound objects
work differently from Music objects as you can only play them (no pausing etc.).
When a Sound is played it is queued to be played from the speakers once. Of
course, you can play a Sound multiple times in an overlapping fashion so it is
generally useful for sound effects.
206 changes: 206 additions & 0 deletions src/kuusisto/tinysound/Music.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Copyright (c) 2012, Finn Kuusisto
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package kuusisto.tinysound;
import kuusisto.tinysound.internal.Mixer;
import kuusisto.tinysound.internal.MusicReference;

/**
* The Music class is an abstraction for music. Music objects should only be
* loaded via the TinySound <code>loadMusic()</code> functions. Music can be
* played, paused, resumed, stopped and looped from specified positions.
*
* @author Finn Kuusisto
*/
public class Music {

private byte[] data;
private Mixer mixer;
private MusicReference reference;

/**
* Construct a new Music with the given music data and the Mixer with which
* to register this Music.
* @param data music data
* @param Mixer with which this Music is registered
*/
public Music(byte[] data, Mixer mixer) {
this.data = data;
this.mixer = mixer;
this.reference = new MusicReference(this.data, false, false, 0, 0, 1.0);
this.mixer.registerMusicReference(this.reference);
}

/**
* Play this Music and loop if specified.
* @param loop if this Music should loop
*/
public void play(boolean loop) {
this.reference.setPlaying(true);
this.reference.setLoop(loop);
}

/**
* Play this Music at the specified volume and loop if specified.
* @param loop if this Music should loop
* @param volume the volume to play the this Music
*/
public void play(boolean loop, double volume) {
this.reference.setPlaying(true);
this.setLoop(loop);
this.setVolume(volume);
}

/**
* Stop playing this Music and set its position to the beginning.
*/
public void stop() {
this.reference.setPlaying(false);
this.rewind();
}

/**
* Stop playing this Music and keep its current position.
*/
public void pause() {
this.reference.setPlaying(false);
}

/**
* Play this Music from its current position.
*/
public void resume() {
this.reference.setPlaying(true);
}

/**
* Set this Music's position to the beginning.
*/
public void rewind() {
this.reference.setPosition(0);
}

/**
* Set this Music's position to the loop position.
*/
public void rewindToLoopPosition() {
int byteIndex = this.reference.getLoopPosition();
this.reference.setPosition(byteIndex);
}

/**
* Determine if this Music is playing.
* @return true if this Music is playing
*/
public boolean playing() {
return this.reference.getPlaying();
}

/**
* Determine if this Music will loop.
* @return true if this Music will loop
*/
public boolean loop() {
return this.reference.getLoop();
}

/**
* Set whether this Music will loop.
* @param loop whether this Music will loop
*/
public void setLoop(boolean loop) {
this.reference.setLoop(loop);
}

/**
* Get the loop position of this Music by sample frame.
* @return loop position by sample frame
*/
public int getLoopPositionByFrame() {
int byteIndex = this.reference.getLoopPosition();
return (byteIndex / TinySound.FORMAT.getFrameSize());
}

/**
* Get the loop position of this Music by seconds.
* @return loop position by seconds
*/
public double getLoopPositionBySeconds() {
int byteIndex = this.reference.getLoopPosition();
return (int)(byteIndex / (TinySound.FORMAT.getFrameRate() *
TinySound.FORMAT.getFrameSize()));
}

/**
* Set the loop position of this Music by sample frame.
* @param frameIndex sample frame loop position to set
*/
public void setLoopPositionByFrame(int frameIndex) {
int byteIndex = frameIndex * TinySound.FORMAT.getFrameSize();
this.reference.setLoopPosition(byteIndex);
}

/**
* Set the loop position of this Music by seconds.
* @param seconds loop position to set by seconds
*/
public void setLoopPositionBySeconds(double seconds) {
int byteIndex = (int)(seconds * TinySound.FORMAT.getFrameRate() *
TinySound.FORMAT.getFrameSize());
this.reference.setLoopPosition(byteIndex);
}

/**
* Get the volume of this Music.
* @return volume of this Music
*/
public double getVolume() {
return this.reference.getVolume();
}

/**
* Set the volume of this Music.
* @param volume the desired volume of this Music
*/
public void setVolume(double volume) {
if (volume >= 0.0) {
this.reference.setVolume(volume);
}
}

/**
* Unload this Music from the system. Attempts to use this Music after
* unloading will result in error.
*/
public void unload() {
//unregister the reference
this.mixer.unRegisterMusicReference(this.reference);
this.mixer = null;
this.data = null;
this.reference = null;
}

}
87 changes: 87 additions & 0 deletions src/kuusisto/tinysound/Sound.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2012, Finn Kuusisto
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package kuusisto.tinysound;
import kuusisto.tinysound.internal.Mixer;
import kuusisto.tinysound.internal.SoundReference;

/**
* The Sound class is an abstraction for sound effects. Sound objects should
* only be loaded via the TinySound <code>loadSound()</code> functions. Sound
* can be played repeatedly in an overlapping fashion.
*
* @author Finn Kuusisto
*/
public class Sound {

private static int soundCount = 0;

private byte[] data;
private Mixer mixer;
private final int ID; //unique ID to match references

/**
* Construct a new Sound with the given data and Mixer which will handle
* handle this Sound.
* @param data sound data
* @param mixer Mixer that will handle this Sound
*/
public Sound(byte[] data, Mixer mixer) {
this.data = data;
this.mixer = mixer;
//get the next ID
this.ID = Sound.soundCount;
Sound.soundCount++;
}

/**
* Plays this Sound.
*/
public void play() {
this.play(1.0);
}

/**
* Plays this Sound with a specified volume.
* @param volume the volume at which to play this Sound
*/
public void play(double volume) {
//dispatch a SoundReference to the mixer
SoundReference ref = new SoundReference(this.data, volume, this.ID);
this.mixer.registerSoundReference(ref);
}

/**
* Unloads this Sound from the system. Attempts to use this Sound after
* unloading will result in error.
*/
public void unload() {
this.mixer.unRegisterSoundReference(this.ID);
this.mixer = null;
this.data = null;
}

}
Loading

0 comments on commit 349ff84

Please sign in to comment.