Class Sound

  • All Implemented Interfaces:
    Sounds

    public class Sound
    extends java.lang.Object
    implements Sounds
    Class that provides access methods for the local audio device
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void beep()
      Beeps once.
      static void beepSequence()
      Downward tones.
      static void beepSequenceUp()
      Upward tones.
      static void buzz()
      Low buzz
      static int getTime()
      Returns the number of milliseconds remaining of the current tone or sample.
      static int getVolume()
      Get the current master volume level
      static void loadSettings()
      Load the current system settings associated with this class.
      static void pause​(int t)  
      static void playNote​(int[] inst, int freq, int len)
      Play a note with attack, decay, sustain and release shape, which is known as a ADSR envelope.
      static int playSample​(byte[] data, int offset, int len, int freq, int vol)
      Queue a series of PCM samples to play at the specified volume and sample rate.
      static int playSample​(java.io.File file)
      Play a wav file.
      static int playSample​(java.io.File file, int vol)
      Play a wav file.
      static void playTone​(int freq, int duration)
      Plays a tone, given its frequency and duration.
      static void playTone​(int aFrequency, int aDuration, int aVolume)
      Plays a tone, given its frequency and duration.
      static void setVolume​(int vol)
      Set the master volume level
      static void systemSound​(boolean aQueued, int aCode)
      Play a system sound.
      static void twoBeeps()
      Beeps twice.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • audio

        protected static final Audio audio
      • C2

        public static int C2
    • Method Detail

      • systemSound

        public static void systemSound​(boolean aQueued,
                                       int aCode)
        Play a system sound.
        aCodeResulting Sound
        0short beep
        1double beep
        2descending arpeggio
        3ascending arpeggio
        4long, low buzz
      • beep

        public static void beep()
        Beeps once.
      • twoBeeps

        public static void twoBeeps()
        Beeps twice.
      • beepSequence

        public static void beepSequence()
        Downward tones.
      • beepSequenceUp

        public static void beepSequenceUp()
        Upward tones.
      • buzz

        public static void buzz()
        Low buzz
      • pause

        public static void pause​(int t)
      • getTime

        public static int getTime()
        Returns the number of milliseconds remaining of the current tone or sample.
        Returns:
        milliseconds remaining
      • playTone

        public static void playTone​(int aFrequency,
                                    int aDuration,
                                    int aVolume)
        Plays a tone, given its frequency and duration.
        Parameters:
        aFrequency - The frequency of the tone in Hertz (Hz).
        aDuration - The duration of the tone, in milliseconds.
        aVolume - The volume of the playback 100 corresponds to 100%
      • playTone

        public static void playTone​(int freq,
                                    int duration)
        Plays a tone, given its frequency and duration.
        Parameters:
        freq - The frequency of the tone in Hertz (Hz).
        duration - The duration of the tone, in milliseconds.
      • playSample

        public static int playSample​(java.io.File file,
                                     int vol)
        Play a wav file. Must be mono, from 8kHz to 48kHz, and 8-bit or 16-bit.
        Parameters:
        file - the 8-bit or 16-bit PWM (WAV) sample file
        vol - the volume percentage 0 - 100
        Returns:
        The number of milliseconds the sample will play for or < 0 if there is an error.
        Throws:
        java.io.FileNotFoundException
      • playSample

        public static int playSample​(java.io.File file)
        Play a wav file. Must be mono, from 8kHz to 48kHz, and 8-bit or 16-bit.
        Parameters:
        file - the 8-bit or 16-bit PWM (WAV) sample file
        Returns:
        The number of milliseconds the sample will play for or < 0 if there is an error.
        Throws:
        java.io.FileNotFoundException
      • playSample

        public static int playSample​(byte[] data,
                                     int offset,
                                     int len,
                                     int freq,
                                     int vol)
        Queue a series of PCM samples to play at the specified volume and sample rate.
        Parameters:
        data - Buffer containing the samples
        offset - Offset of the first sample in the buffer
        len - Number of samples to queue
        freq - Sample rate
        vol - playback volume
        Returns:
        Number of samples actually queued
      • playNote

        public static void playNote​(int[] inst,
                                    int freq,
                                    int len)

        Play a note with attack, decay, sustain and release shape, which is known as a ADSR envelope. This function plays a more musical sounding note compared to playTone(). It uses a set of supplied "instrument" parameters to define the shape of the note's envelope.

        Instruments are defined in the Sounds interface, which is inherited by this class. For example, the piano instrument array looks like this:

        public final static int[] PIANO = new int[]{4, 25, 500, 7000, 5};

        The parameter len is the total time of the note to be played val 0 is the attack time the time for the volume to go from near zero to 100% val 1 is the decay time during this period the volume decreases by val[2]/100 to the sustain level. the sustain period is (len - val[0] - val[1] - val[4]) during this period the volume decreases by val[3]/100. the final decay period is val[4] and the volume reduces to zero. All of the times are units of 2mS (except for len which is in mS).

        Because values equal 2ms, the piano array numbers are doubled:
        1. take 8mS to increase the volume from zero to 100%
        2. take 50mS to decrese the volume by 5%
        3. take len - 8 - 25 - 10 mS to decrease the volume by 70%
        4. take 10mS to decrease the volume to 0

        Parameters:
        inst - Instrument definition (5 ints in an array).
        freq - The note to play (in Hz)
        len - The duration of the note (in ms)
      • setVolume

        public static void setVolume​(int vol)
        Set the master volume level
        Parameters:
        vol - 0-100
      • getVolume

        public static int getVolume()
        Get the current master volume level
        Returns:
        the current master volume 0-100
      • loadSettings

        public static void loadSettings()
        Load the current system settings associated with this class. Called automatically to initialize the class. May be called if it is required to reload any settings.