On systems that support it, sound can be an important part of many applications. Sound can be used to notify the user that her attention is required, to add the extra dimension of aural feedback to visual GUIs, or for entertainment purposes. Other applications such as media players, recorders, and editors are sound-centric and therefore require advanced sound capabilities. This chapter illustrates Java's sound capabilities, from the simple to the complex: Example 17-1 shows how to ring the terminal bell. A simple beep is all that some applications need to attract the user's attention in case of errors or urgent conditions. Example 17-2 shows how to play simple audio clips with the java.applet.AudioClip class. This example illustrates a very simple technique that is sufficient to add sounds to many applications, but does not give any control over the playback of the sound. Example 17-3 shows how to use the javax.sound.sampled and javax.sound.midi packages to load and play sound clips, how to monitor and change the playback position within the clip, and how to set audio parameters such as volume, balance, and tempo for the clips. Example 17-4 shows how to play streaming sounds in both sampled audio and MIDI formats. Example 17-5 reads a simple musical score and converts it into a MIDI Sequence, which is then either played on a Sequencer or saved to a file. Example 17-6 shows how to make music by directly controlling a MidiChannel of a Synthesizer, bypassing the Sequence and Sequencer used in Example 17-5. The Java Sound API is a low-level interface to sound, and the default implementation does not support compressed formats such as MP3. In this chapter we'll be working with simple audio files, with types such as .wav, .aiff, and .au. Note that, although Java Sound supports the recording of sounds, there is no recording example in this chapter: setup and configuration problems on different platforms make sound recording a topic that is beyond the scope of this chapter. |