diff --git a/audio/10kHz_44100Hz_16bit_05sec.wav b/audio/10kHz_44100Hz_16bit_05sec.wav new file mode 100755 index 0000000..ea9b46a Binary files /dev/null and b/audio/10kHz_44100Hz_16bit_05sec.wav differ diff --git a/audio/440Hz_44100Hz_16bit_05sec.wav b/audio/440Hz_44100Hz_16bit_05sec.wav new file mode 100755 index 0000000..268c97c Binary files /dev/null and b/audio/440Hz_44100Hz_16bit_05sec.wav differ diff --git a/audio/PlaySound.java b/audio/PlaySound.java new file mode 100755 index 0000000..cb86e4e --- /dev/null +++ b/audio/PlaySound.java @@ -0,0 +1,86 @@ +package org.wikijava.sound.playWave; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; + +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.SourceDataLine; +import javax.sound.sampled.UnsupportedAudioFileException; +import javax.sound.sampled.DataLine.Info; + +/** + * + * + * + * @author Giulio + */ +public class PlaySound { + + private InputStream waveStream; + + private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb + + /** + * CONSTRUCTOR + */ + public PlaySound(InputStream waveStream) { + this.waveStream = waveStream; + } + + public void play() throws PlayWaveException { + + AudioInputStream audioInputStream = null; + try { + //audioInputStream = AudioSystem.getAudioInputStream(this.waveStream); + + //add buffer for mark/reset support, modified by Jian + InputStream bufferedIn = new BufferedInputStream(this.waveStream); + audioInputStream = AudioSystem.getAudioInputStream(bufferedIn); + + } catch (UnsupportedAudioFileException e1) { + throw new PlayWaveException(e1); + } catch (IOException e1) { + throw new PlayWaveException(e1); + } + + // Obtain the information about the AudioInputStream + AudioFormat audioFormat = audioInputStream.getFormat(); + Info info = new Info(SourceDataLine.class, audioFormat); + + // opens the audio channel + SourceDataLine dataLine = null; + try { + dataLine = (SourceDataLine) AudioSystem.getLine(info); + dataLine.open(audioFormat, this.EXTERNAL_BUFFER_SIZE); + } catch (LineUnavailableException e1) { + throw new PlayWaveException(e1); + } + + // Starts the music :P + dataLine.start(); + + int readBytes = 0; + byte[] audioBuffer = new byte[this.EXTERNAL_BUFFER_SIZE]; + + try { + while (readBytes != -1) { + readBytes = audioInputStream.read(audioBuffer, 0, + audioBuffer.length); + if (readBytes >= 0){ + dataLine.write(audioBuffer, 0, readBytes); + } + } + } catch (IOException e1) { + throw new PlayWaveException(e1); + } finally { + // plays what's left and and closes the audioChannel + dataLine.drain(); + dataLine.close(); + } + + } +} diff --git a/audio/PlayWaveException.java b/audio/PlayWaveException.java new file mode 100755 index 0000000..f882fa3 --- /dev/null +++ b/audio/PlayWaveException.java @@ -0,0 +1,20 @@ +package org.wikijava.sound.playWave; + +/** + * @author Giulio + */ +public class PlayWaveException extends Exception { + + public PlayWaveException(String message) { + super(message); + } + + public PlayWaveException(Throwable cause) { + super(cause); + } + + public PlayWaveException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/audio/PlayWaveFile.java b/audio/PlayWaveFile.java new file mode 100755 index 0000000..4921f49 --- /dev/null +++ b/audio/PlayWaveFile.java @@ -0,0 +1,50 @@ +package org.wikijava.sound.playWave; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +/** + * plays a wave file using PlaySound class + * + * @author Giulio + */ +public class PlayWaveFile { + + /** + * + * + * @param args + * the name of the wave file to play + */ + public static void main(String[] args) { + + // get the command line parameters + if (args.length < 1) { + System.err.println("usage: java -jar PlayWaveFile.jar [filename]"); + return; + } + String filename = args[0]; + + // opens the inputStream + FileInputStream inputStream; + try { + inputStream = new FileInputStream(filename); + //inputStream = this.getClass().getResourceAsStream(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return; + } + + // initializes the playSound Object + PlaySound playSound = new PlaySound(inputStream); + + // plays the sound + try { + playSound.play(); + } catch (PlayWaveException e) { + e.printStackTrace(); + return; + } + } + +} diff --git a/audio/readme.txt b/audio/readme.txt new file mode 100755 index 0000000..a2f381b --- /dev/null +++ b/audio/readme.txt @@ -0,0 +1 @@ +the two .wav files are only for testing of Java PlayWavFile class. There are no corresponding video for them. \ No newline at end of file