Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed AudioFormat matching code; it now uses matches() instead of equ…

…als(). Modified README.
  • Loading branch information...
commit c9763fc6569e30a5ec9060ea8059cd776cd12e37 1 parent a7ec0f5
@finnkuusisto authored
View
3  README
@@ -49,8 +49,7 @@ update thread and clears resources in use.
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.
+data from the system via its unload() method. See the Javadocs for more detail.
-Sound-
You load Sound objects via the TinySound loadSound() functions. Sound objects
View
37 src/kuusisto/tinysound/TinySound.java
@@ -86,13 +86,12 @@ public static void init() {
//try to open a line to the speakers
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
TinySound.FORMAT);
- if (!javax.sound.sampled.AudioSystem.isLineSupported(info)) {
+ if (!AudioSystem.isLineSupported(info)) {
System.err.println("Unsupported output format!");
return;
}
try {
- TinySound.outLine =
- (SourceDataLine)javax.sound.sampled.AudioSystem.getLine(info);
+ TinySound.outLine = (SourceDataLine)AudioSystem.getLine(info);
TinySound.outLine.open(TinySound.FORMAT);
}
catch (LineUnavailableException e) {
@@ -393,12 +392,12 @@ else if (numChannels == 2) {
* failure
*/
private static AudioInputStream getValidAudioStream(URL url) {
- System.out.println(url);
AudioInputStream audioStream = null;
try {
audioStream = AudioSystem.getAudioInputStream(url);
+ AudioFormat streamFormat = audioStream.getFormat();
//1-channel can also be treated as stereo
- AudioFormat mono = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
+ AudioFormat mono16 = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
44100, 16, 1, 2, 44100, false);
//1 or 2 channel 8-bit may be easy to convert
AudioFormat mono8 = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
@@ -407,35 +406,32 @@ private static AudioInputStream getValidAudioStream(URL url) {
new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 8, 2, 2,
44100, false);
//now check formats (attempt conversion as needed)
- if (audioStream.getFormat().equals(TinySound.FORMAT) ||
- audioStream.getFormat().equals(mono)) {
+ if (streamFormat.matches(TinySound.FORMAT) ||
+ streamFormat.matches(mono16)) {
return audioStream;
} //check conversion to TinySound format
else if (AudioSystem.isConversionSupported(TinySound.FORMAT,
- audioStream.getFormat())) {
+ streamFormat)) {
audioStream = AudioSystem.getAudioInputStream(TinySound.FORMAT,
audioStream);
} //check conversion to mono alternate
- else if (AudioSystem.isConversionSupported(mono,
- audioStream.getFormat())) {
- audioStream = AudioSystem.getAudioInputStream(mono,
+ else if (AudioSystem.isConversionSupported(mono16, streamFormat)) {
+ audioStream = AudioSystem.getAudioInputStream(mono16,
audioStream);
} //try convert from 8-bit, 2-channel
- else if (audioStream.getFormat().equals(stereo8) ||
- AudioSystem.isConversionSupported(stereo8,
- audioStream.getFormat())) {
+ else if (streamFormat.matches(stereo8) ||
+ AudioSystem.isConversionSupported(stereo8, streamFormat)) {
//convert to 8-bit stereo first?
- if (!audioStream.getFormat().equals(stereo8)) {
+ if (!streamFormat.matches(stereo8)) {
audioStream = AudioSystem.getAudioInputStream(stereo8,
audioStream);
}
audioStream = TinySound.convertStereo8Bit(audioStream);
} //try convert from 8-bit, 1-channel
- else if (audioStream.getFormat().equals(mono8) ||
- AudioSystem.isConversionSupported(mono8,
- audioStream.getFormat())) {
+ else if (streamFormat.matches(mono8) ||
+ AudioSystem.isConversionSupported(mono8, streamFormat)) {
//convert to 8-bit mono first?
- if (!audioStream.getFormat().equals(mono8)) {
+ if (!streamFormat.matches(mono8)) {
audioStream = AudioSystem.getAudioInputStream(mono8,
audioStream);
}
@@ -443,7 +439,8 @@ else if (audioStream.getFormat().equals(mono8) ||
} //it's time to give up
else {
System.err.println("Unable to convert audio resource!");
- System.err.println(audioStream.getFormat());
+ System.err.println(url);
+ System.err.println(streamFormat);
audioStream.close();
return null;
}
View
BIN  tinysound.zip
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.