Download presentation
Presentation is loading. Please wait.
Published byMaude Shannon Turner Modified over 9 years ago
1
© M. Winter COSC 3P40 – Advanced Object-Oriented Programming 10.1 Sound effects and music Java Sound API: javax.sound.sampled 8- or 16-bit samples from 8,000Hz to 48,000Hz mono or stereo sound file formats: AIFF, AU, WAV (Examples: 16-bit, mono, 44,100Hz, WAV)
2
© M. Winter COSC 3P40 – Advanced Object-Oriented Programming 10.2 Loading and playing a sound try { File file = new File(“…”); AudioInputStream stream = AudioSystem.getAudioInputStream(file); AudioFormat format = stream.getFormat(); } catch(Exception ex) { // IOException or UnsupportedAudioFileException } try { DataLine info = new DataLine.Info(SourceDataLine,format); line = (SourceDataLine) AudioSystem.getLine(info); line.open(format,bufferSize); } catch(Exception ex) { // LineUnavailableException }
3
© M. Winter COSC 3P40 – Advanced Object-Oriented Programming 10.3 try { int numBytesRead = 0; while (numBytesRead != -1) { numBytesRead = stream.read(buffer, 0, buffer.length); if (numBytesRead != -1) { line.write(buffer, 0, numBytesRead); } } catch (Exception ex) { // IOException }; line.drain(); line.close();
4
© M. Winter COSC 3P40 – Advanced Object-Oriented Programming 10.4 Sound filter Examples: echo filter simulated 3D sound filter SoundFilter class (abstract class): filter(byte[] buffer, int offset, int length) – Filters an array of samples. getRemainingSize() – Gets the remaining size, in bytes, that this filter can play after the sound is finished. reset() – Resets the filter so that it can be used again on a different sound.
5
© M. Winter COSC 3P40 – Advanced Object-Oriented Programming 10.5 FilteredSoundStream public class FilteredSoundStream extends FilterInputStream { … public int read(byte[] samples, int offset, int length) throws IOException { int bytesRead = super.read(samples, offset, length); if (bytesRead > 0) { soundFilter.filter(samples, offset, bytesRead); return bytesRead; }; if (remainingSize == REMAINING_SIZE_UNKNOWN) { remainingSize = soundFilter.getRemainingSize(); remainingSize = remainingSize / 4 * 4; };
6
© M. Winter COSC 3P40 – Advanced Object-Oriented Programming 10.6 if (remainingSize > 0) { length = Math.min(length, remainingSize); for (int i=offset; i<offset+length; i++) { samples[i] = 0; }; soundFilter.filter(samples, offset, length); remainingSize-=length; return length; } else { return -1; }; }
7
© M. Winter COSC 3P40 – Advanced Object-Oriented Programming 10.7 Echo filter delay : numbers of samples to delay (44,100Hz sound, 1 sec delay = 44,100 samples) decay : value from 0 to 1 –0 means no echo –1 means the echo is the same volume as the original sound Delay Original sound First echo Second echo
8
© M. Winter COSC 3P40 – Advanced Object-Oriented Programming 10.8 Emulating 3D sound Many different effects are used to create 3D sounds. Make sound diminish with distance so the farther away a sound source is, the quieter it is. Pan sounds to the appropriate speaker. Apply room effects so sound waves bounce off walls, creating echoes and reverberation. Apply the Doppler effect so a sound source movement affect its pitch.
9
© M. Winter COSC 3P40 – Advanced Object-Oriented Programming 10.9 SoundManager The SoundManager class has the following features: provides a common interface for loading and playing sounds (including filters), extends the ThreadPool class, each thread in the thread pool has its own buffer and line object (thread-local variables).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.