Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved all update code to UpdateRunner. Updated README. Added example.…

… Added jar.
  • Loading branch information...
commit 8d31431e0d552d77febbcdfa407b23a3f0c59222 1 parent 8a1ba02
@finnkuusisto authored
Showing with 128 additions and 300 deletions.
  1. +0 −1  .gitignore
  2. +14 −16 README
  3. +1 −1  doc/allclasses-frame.html
  4. +1 −1  doc/allclasses-noframe.html
  5. +1 −1  doc/constant-values.html
  6. +1 −1  doc/deprecated-list.html
  7. +1 −1  doc/help-doc.html
  8. +4 −15 doc/index-all.html
  9. +1 −1  doc/index.html
  10. +1 −1  doc/kuusisto/tinysound/Music.html
  11. +1 −1  doc/kuusisto/tinysound/Sound.html
  12. +3 −74 doc/kuusisto/tinysound/TinySound.html
  13. +1 −1  doc/kuusisto/tinysound/internal/Mixer.html
  14. +1 −1  doc/kuusisto/tinysound/internal/MusicReference.html
  15. +1 −1  doc/kuusisto/tinysound/internal/SoundReference.html
  16. +9 −10 doc/kuusisto/tinysound/internal/UpdateRunner.html
  17. +1 −1  doc/kuusisto/tinysound/internal/package-frame.html
  18. +1 −1  doc/kuusisto/tinysound/internal/package-summary.html
  19. +1 −1  doc/kuusisto/tinysound/internal/package-tree.html
  20. +1 −1  doc/kuusisto/tinysound/package-frame.html
  21. +1 −1  doc/kuusisto/tinysound/package-summary.html
  22. +1 −1  doc/kuusisto/tinysound/package-tree.html
  23. +1 −1  doc/overview-frame.html
  24. +1 −1  doc/overview-summary.html
  25. +1 −1  doc/overview-tree.html
  26. +27 −0 example/TinySoundExample.java
  27. +4 −150 src/kuusisto/tinysound/TinySound.java
  28. +47 −14 src/kuusisto/tinysound/internal/UpdateRunner.java
  29. BIN  tinysound.jar
View
1  .gitignore
@@ -2,7 +2,6 @@
# Compiled #
############
*.class
-*.jar
#################
# IDE Generated #
View
30 README
@@ -21,21 +21,12 @@ 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.
+1. Initialization is accomplished via the init() function. It takes no
+arguments and sets up the system for you to play audio data. TinySound creates
+a daemon thread to automatically write audio data to the speakers.
+
+2. Shutdown is accomplished via the shutdown() function. This shuts down the
+update thread and clears resources in use.
-Music-
You load Music objects via the TinySound loadMusic() functions. Music objects
@@ -49,4 +40,11 @@ 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.
+generally useful for sound effects.
+
+Example
+-------
+There is a very simple example provided in the example directory. You'll need
+sound resources with the specified names on the classpath if you want to try the
+example without modifying it. Note that the example does not demonstrate all of
+TinySound's features. See the Javadocs for more detail.
View
2  doc/allclasses-frame.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
All Classes
</TITLE>
View
2  doc/allclasses-noframe.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
All Classes
</TITLE>
View
2  doc/constant-values.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
Constant Field Values
</TITLE>
View
2  doc/deprecated-list.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
Deprecated List
</TITLE>
View
2  doc/help-doc.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
API Help
</TITLE>
View
19 doc/index-all.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
Index
</TITLE>
@@ -129,9 +129,6 @@
<DT><A HREF="./kuusisto/tinysound/internal/MusicReference.html#getPosition()"><B>getPosition()</B></A> -
Method in class kuusisto.tinysound.internal.<A HREF="./kuusisto/tinysound/internal/MusicReference.html" title="class in kuusisto.tinysound.internal">MusicReference</A>
<DD>Get the byte index of this MusicReference.
-<DT><A HREF="./kuusisto/tinysound/TinySound.html#getUpdateRate()"><B>getUpdateRate()</B></A> -
-Static method in class kuusisto.tinysound.<A HREF="./kuusisto/tinysound/TinySound.html" title="class in kuusisto.tinysound">TinySound</A>
-<DD>Get the current TinySound audio update rate.
<DT><A HREF="./kuusisto/tinysound/internal/MusicReference.html#getVolume()"><B>getVolume()</B></A> -
Method in class kuusisto.tinysound.internal.<A HREF="./kuusisto/tinysound/internal/MusicReference.html" title="class in kuusisto.tinysound.internal">MusicReference</A>
<DD>Get the volume of this MusicReference.
@@ -148,10 +145,7 @@
<DL>
<DT><A HREF="./kuusisto/tinysound/TinySound.html#init()"><B>init()</B></A> -
Static method in class kuusisto.tinysound.<A HREF="./kuusisto/tinysound/TinySound.html" title="class in kuusisto.tinysound">TinySound</A>
-<DD>Initialize the AudioSystem with the default update rate of 100Hz.
-<DT><A HREF="./kuusisto/tinysound/TinySound.html#init(int)"><B>init(int)</B></A> -
-Static method in class kuusisto.tinysound.<A HREF="./kuusisto/tinysound/TinySound.html" title="class in kuusisto.tinysound">TinySound</A>
-<DD>Initialize the AudioSystem with a desired update rate.
+<DD>Initialize the AudioSystem.
</DL>
<HR>
<A NAME="_K_"><!-- --></A><H2>
@@ -348,15 +342,10 @@
<DT><A HREF="./kuusisto/tinysound/internal/Mixer.html#unRegisterSoundReference(int)"><B>unRegisterSoundReference(int)</B></A> -
Method in class kuusisto.tinysound.internal.<A HREF="./kuusisto/tinysound/internal/Mixer.html" title="class in kuusisto.tinysound.internal">Mixer</A>
<DD>Unregisters all SoundReferences with a given soundID.
-<DT><A HREF="./kuusisto/tinysound/TinySound.html#update()"><B>update()</B></A> -
-Static method in class kuusisto.tinysound.<A HREF="./kuusisto/tinysound/TinySound.html" title="class in kuusisto.tinysound">TinySound</A>
-<DD>Write a buffer of audio data to the speakers and fill the audio buffer
- for the next update.
<DT><A HREF="./kuusisto/tinysound/internal/UpdateRunner.html" title="class in kuusisto.tinysound.internal"><B>UpdateRunner</B></A> - Class in <A HREF="./kuusisto/tinysound/internal/package-summary.html">kuusisto.tinysound.internal</A><DD>The UpdateRunner class implements Runnable and is what performs automatic
- updates of the TinySound system.<DT><A HREF="./kuusisto/tinysound/internal/UpdateRunner.html#UpdateRunner(int)"><B>UpdateRunner(int)</B></A> -
+ updates of the TinySound system.<DT><A HREF="./kuusisto/tinysound/internal/UpdateRunner.html#UpdateRunner(kuusisto.tinysound.internal.Mixer, javax.sound.sampled.SourceDataLine)"><B>UpdateRunner(Mixer, SourceDataLine)</B></A> -
Constructor for class kuusisto.tinysound.internal.<A HREF="./kuusisto/tinysound/internal/UpdateRunner.html" title="class in kuusisto.tinysound.internal">UpdateRunner</A>
-<DD>Constructs a new UpdateRunner to update the TinySound system at the
- specified rate.
+<DD>Constructs a new UpdateRunner to update the TinySound system.
</DL>
<HR>
<A HREF="#_B_">B</A> <A HREF="#_C_">C</A> <A HREF="#_F_">F</A> <A HREF="#_G_">G</A> <A HREF="#_I_">I</A> <A HREF="#_K_">K</A> <A HREF="#_L_">L</A> <A HREF="#_M_">M</A> <A HREF="#_N_">N</A> <A HREF="#_P_">P</A> <A HREF="#_R_">R</A> <A HREF="#_S_">S</A> <A HREF="#_T_">T</A> <A HREF="#_U_">U</A>
View
2  doc/index.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc on Sat Mar 03 12:10:31 CST 2012-->
+<!-- Generated by javadoc on Sat Mar 03 23:27:54 CST 2012-->
<TITLE>
Generated Documentation (Untitled)
</TITLE>
View
2  doc/kuusisto/tinysound/Music.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:30 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
Music
</TITLE>
View
2  doc/kuusisto/tinysound/Sound.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
Sound
</TITLE>
View
77 doc/kuusisto/tinysound/TinySound.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
TinySound
</TITLE>
@@ -158,27 +158,11 @@
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
-<CODE>static&nbsp;int</CODE></FONT></TD>
-<TD><CODE><B><A HREF="../../kuusisto/tinysound/TinySound.html#getUpdateRate()">getUpdateRate</A></B>()</CODE>
-
-<BR>
-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Get the current TinySound audio update rate.</TD>
-</TR>
-<TR BGCOLOR="white" CLASS="TableRowColor">
-<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../kuusisto/tinysound/TinySound.html#init()">init</A></B>()</CODE>
<BR>
-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Initialize the AudioSystem with the default update rate of 100Hz.</TD>
-</TR>
-<TR BGCOLOR="white" CLASS="TableRowColor">
-<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
-<CODE>static&nbsp;void</CODE></FONT></TD>
-<TD><CODE><B><A HREF="../../kuusisto/tinysound/TinySound.html#init(int)">init</A></B>(int&nbsp;updateRate)</CODE>
-
-<BR>
-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Initialize the AudioSystem with a desired update rate.</TD>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Initialize the AudioSystem.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
@@ -252,15 +236,6 @@
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Shutdown the AudioSystem.</TD>
</TR>
-<TR BGCOLOR="white" CLASS="TableRowColor">
-<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
-<CODE>static&nbsp;void</CODE></FONT></TD>
-<TD><CODE><B><A HREF="../../kuusisto/tinysound/TinySound.html#update()">update</A></B>()</CODE>
-
-<BR>
-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Write a buffer of audio data to the speakers and fill the audio buffer
- for the next update.</TD>
-</TR>
</TABLE>
&nbsp;<A NAME="methods_inherited_from_class_java.lang.Object"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
@@ -327,8 +302,7 @@
<PRE>
public static void <B>init</B>()</PRE>
<DL>
-<DD>Initialize the AudioSystem with the default update rate of 100Hz. This
- is probably sufficient for most users.
+<DD>Initialize the AudioSystem. This must be called before loading audio.
<P>
<DD><DL>
</DL>
@@ -336,22 +310,6 @@
</DL>
<HR>
-<A NAME="init(int)"><!-- --></A><H3>
-init</H3>
-<PRE>
-public static void <B>init</B>(int&nbsp;updateRate)</PRE>
-<DL>
-<DD>Initialize the AudioSystem with a desired update rate. The AudioSystem
- will attempt to allocate an audio buffer that is appropriate for the
- specified update rate.
-<P>
-<DD><DL>
-<DT><B>Parameters:</B><DD><CODE>updateRate</CODE> - the desired update rate of the AudioSystem, in updates
- per second</DL>
-</DD>
-</DL>
-<HR>
-
<A NAME="shutdown()"><!-- --></A><H3>
shutdown</H3>
<PRE>
@@ -365,35 +323,6 @@
</DL>
<HR>
-<A NAME="getUpdateRate()"><!-- --></A><H3>
-getUpdateRate</H3>
-<PRE>
-public static int <B>getUpdateRate</B>()</PRE>
-<DL>
-<DD>Get the current TinySound audio update rate.
-<P>
-<DD><DL>
-
-<DT><B>Returns:</B><DD>the current audio update rate</DL>
-</DD>
-</DL>
-<HR>
-
-<A NAME="update()"><!-- --></A><H3>
-update</H3>
-<PRE>
-public static void <B>update</B>()</PRE>
-<DL>
-<DD>Write a buffer of audio data to the speakers and fill the audio buffer
- for the next update. TinySound users shouldn't concern themselves with
- this method.
-<P>
-<DD><DL>
-</DL>
-</DD>
-</DL>
-<HR>
-
<A NAME="loadMusic(java.lang.String)"><!-- --></A><H3>
loadMusic</H3>
<PRE>
View
2  doc/kuusisto/tinysound/internal/Mixer.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
Mixer
</TITLE>
View
2  doc/kuusisto/tinysound/internal/MusicReference.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
MusicReference
</TITLE>
View
2  doc/kuusisto/tinysound/internal/SoundReference.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
SoundReference
</TITLE>
View
19 doc/kuusisto/tinysound/internal/UpdateRunner.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
UpdateRunner
</TITLE>
@@ -127,11 +127,11 @@
<B>Constructor Summary</B></FONT></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
-<TD><CODE><B><A HREF="../../../kuusisto/tinysound/internal/UpdateRunner.html#UpdateRunner(int)">UpdateRunner</A></B>(int&nbsp;updateRate)</CODE>
+<TD><CODE><B><A HREF="../../../kuusisto/tinysound/internal/UpdateRunner.html#UpdateRunner(kuusisto.tinysound.internal.Mixer, javax.sound.sampled.SourceDataLine)">UpdateRunner</A></B>(<A HREF="../../../kuusisto/tinysound/internal/Mixer.html" title="class in kuusisto.tinysound.internal">Mixer</A>&nbsp;mixer,
+ <A HREF="http://docs.oracle.com/javase/6/docs/api/javax/sound/sampled/SourceDataLine.html?is-external=true" title="class or interface in javax.sound.sampled">SourceDataLine</A>&nbsp;outLine)</CODE>
<BR>
-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructs a new UpdateRunner to update the TinySound system at the
- specified rate.</TD>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructs a new UpdateRunner to update the TinySound system.</TD>
</TR>
</TABLE>
&nbsp;
@@ -182,17 +182,16 @@
</TR>
</TABLE>
-<A NAME="UpdateRunner(int)"><!-- --></A><H3>
+<A NAME="UpdateRunner(kuusisto.tinysound.internal.Mixer, javax.sound.sampled.SourceDataLine)"><!-- --></A><H3>
UpdateRunner</H3>
<PRE>
-public <B>UpdateRunner</B>(int&nbsp;updateRate)</PRE>
+public <B>UpdateRunner</B>(<A HREF="../../../kuusisto/tinysound/internal/Mixer.html" title="class in kuusisto.tinysound.internal">Mixer</A>&nbsp;mixer,
+ <A HREF="http://docs.oracle.com/javase/6/docs/api/javax/sound/sampled/SourceDataLine.html?is-external=true" title="class or interface in javax.sound.sampled">SourceDataLine</A>&nbsp;outLine)</PRE>
<DL>
-<DD>Constructs a new UpdateRunner to update the TinySound system at the
- specified rate.
+<DD>Constructs a new UpdateRunner to update the TinySound system.
<P>
<DL>
-<DT><B>Parameters:</B><DD><CODE>updateRate</CODE> - number of times to update the TinySound system
- every second</DL>
+<DT><B>Parameters:</B><DD><CODE>mixer</CODE> - the mixer to read audio data from<DD><CODE>outLine</CODE> - the line to write audio data to</DL>
</DL>
<!-- ============ METHOD DETAIL ========== -->
View
2  doc/kuusisto/tinysound/internal/package-frame.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
kuusisto.tinysound.internal
</TITLE>
View
2  doc/kuusisto/tinysound/internal/package-summary.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
kuusisto.tinysound.internal
</TITLE>
View
2  doc/kuusisto/tinysound/internal/package-tree.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
kuusisto.tinysound.internal Class Hierarchy
</TITLE>
View
2  doc/kuusisto/tinysound/package-frame.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
kuusisto.tinysound
</TITLE>
View
2  doc/kuusisto/tinysound/package-summary.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
kuusisto.tinysound
</TITLE>
View
2  doc/kuusisto/tinysound/package-tree.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
kuusisto.tinysound Class Hierarchy
</TITLE>
View
2  doc/overview-frame.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
Overview List
</TITLE>
View
2  doc/overview-summary.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
Overview
</TITLE>
View
2  doc/overview-tree.html
@@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
-<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 12:10:31 CST 2012 -->
+<!-- Generated by javadoc (build 1.6.0_11) on Sat Mar 03 23:27:54 CST 2012 -->
<TITLE>
Class Hierarchy
</TITLE>
View
27 example/TinySoundExample.java
@@ -0,0 +1,27 @@
+import kuusisto.tinysound.Music;
+import kuusisto.tinysound.Sound;
+import kuusisto.tinysound.TinySound;
+
+public class TinySoundExample {
+
+ public static void main(String[] args) {
+ //initialize TinySound
+ TinySound.init();
+ //load a sound and music
+ //note: you can also load with Files, URLs and InputStreams
+ Music song = TinySound.loadMusic("song.wav");
+ Sound coin = TinySound.loadSound("coin.wav");
+ //start playing the music on loop
+ song.play(true);
+ //play the sound a few times in a loop
+ for (int i = 0; i < 20; i++) {
+ coin.play();
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {}
+ }
+ //be sure to shutdown TinySound when done
+ TinySound.shutdown();
+ }
+
+}
View
154 src/kuusisto/tinysound/TinySound.java
@@ -71,37 +71,17 @@
//the system has only one mixer for both music and sounds
private static Mixer mixer;
- //the system has a buffer size based on a specified update rate
- private static int updateRate;
- private static byte[] audioBuffer;
- private static int numBytesRead;
- private static long lastUpdate;
- private static boolean isFirstUpdate;
//need a line to the speakers
private static SourceDataLine outLine;
//see if the system has been initialized
private static boolean inited = false;
//auto-updater for the system
private static UpdateRunner autoUpdater;
- private static double driftFramesAccrued;
/**
- * Initialize the AudioSystem with the default update rate of 100Hz. This
- * is probably sufficient for most users.
+ * Initialize the AudioSystem. This must be called before loading audio.
*/
public static void init() {
- //default to 100Hz, auto-update
- TinySound.init(100);
- }
-
- /**
- * Initialize the AudioSystem with a desired update rate. The AudioSystem
- * will attempt to allocate an audio buffer that is appropriate for the
- * specified update rate.
- * @param updateRate the desired update rate of the AudioSystem, in updates
- * per second
- */
- public static void init(int updateRate) {
if (TinySound.inited) {
return;
}
@@ -122,22 +102,11 @@ public static void init(int updateRate) {
return;
}
TinySound.outLine.start();
- //now initialize the other stuff
+ //now initialize the mixer
TinySound.mixer = new Mixer();
- TinySound.updateRate = updateRate;
- //1/2 second + 1 update
- int halfSecFrames = (int)(TinySound.FORMAT.getFrameRate() / 2);
- int updateFrames = (int)(TinySound.FORMAT.getFrameRate() / updateRate);
- int bufSize = (halfSecFrames * TinySound.FORMAT.getFrameSize()) +
- (updateFrames * TinySound.FORMAT.getFrameSize());
- TinySound.audioBuffer = new byte[bufSize];
- TinySound.numBytesRead = 0;
- TinySound.isFirstUpdate = true;
- //buffer 10ms of audio
- int tenMSFrames = (int)TinySound.FORMAT.getFrameRate() / 100;
- TinySound.preFillAudioBuffer(tenMSFrames);
//initialize and start the updater
- TinySound.autoUpdater = new UpdateRunner(updateRate);
+ TinySound.autoUpdater = new UpdateRunner(TinySound.mixer,
+ TinySound.outLine);
Thread updateThread = new Thread(TinySound.autoUpdater);
updateThread.setDaemon(true);
TinySound.inited = true;
@@ -160,121 +129,6 @@ public static void shutdown() {
TinySound.mixer.clearMusic();
TinySound.mixer.clearSounds();
TinySound.mixer = null;
- TinySound.updateRate = 0;
- TinySound.lastUpdate = 0;
- TinySound.isFirstUpdate = true;
- TinySound.audioBuffer = null;
- TinySound.numBytesRead = 0;
- TinySound.driftFramesAccrued = 0.0;
- }
-
- /**
- * Get the current TinySound audio update rate.
- * @return the current audio update rate
- */
- public static int getUpdateRate() {
- return TinySound.updateRate;
- }
-
- /**
- * Write a buffer of audio data to the speakers and fill the audio buffer
- * for the next update. TinySound users shouldn't concern themselves with
- * this method.
- */
- public static void update() {
- if (!TinySound.inited) {
- System.err.println("TinySound not initialized!");
- return;
- }
- TinySound.update(true);
- }
-
- /**
- * Write a buffer of audio data to the speakers and fill the audio buffer
- * for the next update as specified.
- * @param fillNextBuffer true if the next audio buffer should be filled
- */
- private static void update(boolean fillNextBuffer) {
- if (!TinySound.inited) {
- System.err.println("TinySound not initialized!");
- return;
- }
- //check for first update
- if (TinySound.isFirstUpdate) {
- TinySound.lastUpdate = System.nanoTime();
- TinySound.isFirstUpdate = false;
- }
- //drain the buffer if there is any
- if (TinySound.numBytesRead <= 0) {
- TinySound.fillAudioBuffer();
- }
- if (TinySound.numBytesRead > 0) {
- //and write to the speakers
- TinySound.outLine.write(TinySound.audioBuffer, 0,
- TinySound.numBytesRead);
- TinySound.numBytesRead = 0;
- }
- //now fill the buffer for the next update if desired
- if (fillNextBuffer) {
- TinySound.fillAudioBuffer();
- }
- }
-
- /**
- * Fills the audio buffer with audio data. Also handles drift compensation.
- */
- private static void fillAudioBuffer() {
- if (!TinySound.inited) {
- return;
- }
- long currTime = System.nanoTime();
- double secDelta = (currTime - TinySound.lastUpdate) / 1000000000.0;
- double framesToRead = secDelta * TinySound.FORMAT.getFrameRate();
- if (framesToRead > 0.0) {
- //update accrued drift
- TinySound.driftFramesAccrued += (framesToRead - (int)framesToRead);
- //check drift and update accrued drift
- int driftFramesToAdd = (int)TinySound.driftFramesAccrued;
- TinySound.driftFramesAccrued -= driftFramesToAdd;
- //calculate bytes to read
- int bytesToRead = ((int)framesToRead + driftFramesToAdd) *
- TinySound.FORMAT.getFrameSize();
- //now read the bytes
- int tmpBytesRead = TinySound.mixer.read(TinySound.audioBuffer,
- TinySound.numBytesRead, bytesToRead);
- TinySound.numBytesRead += tmpBytesRead;
- //fill remainder with zeroes
- int remainder = bytesToRead - tmpBytesRead;
- for (int i = 0; i < remainder; i++) {
- TinySound.audioBuffer[TinySound.numBytesRead + i] = 0;
- }
- TinySound.numBytesRead += remainder;
- //mark last update
- TinySound.lastUpdate = currTime;
- }
- }
-
- /**
- * Fills the audio buffer with a specified number of frames of audio data
- * without recording the last update time.
- */
- private static void preFillAudioBuffer(int numFrames) {
- if (!TinySound.inited) {
- return;
- }
- if (numFrames > 0) {
- int bytesToRead = numFrames * TinySound.FORMAT.getFrameSize();
- //now read the bytes
- int tmpBytesRead = TinySound.mixer.read(TinySound.audioBuffer,
- TinySound.numBytesRead, bytesToRead);
- TinySound.numBytesRead += tmpBytesRead;
- //fill remainder with zeroes
- int remainder = bytesToRead - tmpBytesRead;
- for (int i = 0; i < remainder; i++) {
- TinySound.audioBuffer[TinySound.numBytesRead + i] = 0;
- }
- TinySound.numBytesRead += remainder;
- }
}
/**
View
61 src/kuusisto/tinysound/internal/UpdateRunner.java
@@ -27,6 +27,8 @@
package kuusisto.tinysound.internal;
import java.util.concurrent.atomic.AtomicBoolean;
+import javax.sound.sampled.SourceDataLine;
+
import kuusisto.tinysound.TinySound;
/**
@@ -40,17 +42,18 @@
public class UpdateRunner implements Runnable {
private AtomicBoolean running;
- private int updateRate;
+ private SourceDataLine outLine;
+ private Mixer mixer;
/**
- * Constructs a new UpdateRunner to update the TinySound system at the
- * specified rate.
- * @param updateRate number of times to update the TinySound system
- * every second
+ * Constructs a new UpdateRunner to update the TinySound system.
+ * @param mixer the mixer to read audio data from
+ * @param outLine the line to write audio data to
*/
- public UpdateRunner(int updateRate) {
+ public UpdateRunner(Mixer mixer, SourceDataLine outLine) {
this.running = new AtomicBoolean();
- this.updateRate = updateRate;
+ this.mixer = mixer;
+ this.outLine = outLine;
}
/**
@@ -62,20 +65,50 @@ public void stop() {
@Override
public void run() {
+ //mark the updater as running
this.running.set(true);
- long nanosPerUpdate = 1000000000L / this.updateRate;
- long lastUpdate = 0;
+ //2-sec buffer
+ int bufSize = (int)TinySound.FORMAT.getFrameRate() *
+ TinySound.FORMAT.getFrameSize() * 2;
+ byte[] audioBuffer = new byte[bufSize];
+ int numBytesRead = 0;
+ double framesAccrued = 0;
+ long lastUpdate = System.nanoTime();
//keep running until told to stop
while (this.running.get()) {
+ //check the time
long currTime = System.nanoTime();
- long delta = currTime - lastUpdate;
- if (delta >= nanosPerUpdate) {
- TinySound.update();
- lastUpdate = currTime;
+ //accrue frames
+ double delta = currTime - lastUpdate;
+ double secDelta = (delta / 1000000000L);
+ framesAccrued += secDelta * TinySound.FORMAT.getFrameRate();
+ //read frames if needed
+ int framesToRead = (int)framesAccrued;
+ if (framesToRead > 0) {
+ //read from the mixer
+ int bytesToRead = framesToRead *
+ TinySound.FORMAT.getFrameSize();
+ int tmpBytesRead = this.mixer.read(audioBuffer,
+ numBytesRead, bytesToRead);
+ numBytesRead += tmpBytesRead; //mark how many read
+ //fill rest with zeroes
+ int remaining = bytesToRead - tmpBytesRead;
+ for (int i = 0; i < remaining; i++) {
+ audioBuffer[numBytesRead + i] = 0;
+ }
+ numBytesRead += remaining; //mark zeroes read
+ }
+ framesAccrued -= framesToRead; //mark frames read
+ //write to speakers
+ if (numBytesRead > 0) {
+ this.outLine.write(audioBuffer, 0, numBytesRead);
+ numBytesRead = 0;
}
+ //mark last update
+ lastUpdate = currTime;
//give the CPU back to the OS for a bit
try {
- Thread.sleep(0, 100000);
+ Thread.sleep(1);
} catch (InterruptedException e) {}
}
}
View
BIN  tinysound.jar
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.