// File:   SoundApplet\SoundApplet.java

// This is a slight modification of the SoundApplet that is part
// of the Sun Java Tutorial on sound...

import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class SoundApplet extends JApplet
                         implements ActionListener, ItemListener {
    AppletSoundList soundList;
    String auFile   = "spacemusic.au";
    String aiffFile = "reverie.aif";
    String midiFile = "trippygaia1.mid";
    String wavFile  = "bottle-open.wav";
    String chosenFile;
    AudioClip onceClip, loopClip;

    JComboBox formats;
    JButton playButton, loopButton, stopButton;
    boolean bLooping = false;

    public void init() {
        String [] fileTypes = { auFile,
                                aiffFile,
                                midiFile,        
                                wavFile };
        formats = new JComboBox(fileTypes);
        formats.setSelectedIndex(0);
        chosenFile = (String) formats.getSelectedItem();
        formats.addItemListener(this);

        playButton = new JButton("Play");
        playButton.addActionListener(this);

        loopButton = new JButton("Loop");
        loopButton.addActionListener(this);

        stopButton = new JButton("Stop");
        stopButton.addActionListener(this);
        stopButton.setEnabled(false);

        JPanel controlPanel = new JPanel();
        controlPanel.add(formats);
        controlPanel.add(playButton);
        controlPanel.add(loopButton);
        controlPanel.add(stopButton);
        getContentPane().add(controlPanel);

        startLoadingSounds(fileTypes.length);   
    }
       
    public void itemStateChanged(ItemEvent e) {
        chosenFile = (String) formats.getSelectedItem();
        soundList.startLoading(chosenFile);
    }


    // Start asynchronous sound loading

    void startLoadingSounds(int nNumSounds) {
        soundList = new AppletSoundList(this, nNumSounds, getCodeBase());
        soundList.startLoading(auFile);
        soundList.startLoading(aiffFile);
        soundList.startLoading(midiFile);
        soundList.startLoading(wavFile);
   }

    public void stop() {
        if (onceClip != null) {
            onceClip.stop();                /* Cut short the one-time sound */
            if (bLooping)
                loopClip.stop();            /* Stop the sound loop */
        }
    }    

    public void start() {
        if (bLooping)
            loopClip.loop();                 /* Restart the sound loop */
    }    

    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();

        // PLAY BUTTON

        if (source == playButton) {
            /* Try to get the AudioClip */
            onceClip = soundList.getClip(chosenFile);
            onceClip.play();                  /* Play it once */
            stopButton.setEnabled(true); 
            showStatus("Playing sound " + chosenFile + ".");
            if (onceClip == null) {
                showStatus("Sound " + chosenFile + " not loaded yet.");
            }
        }

        // START LOOP BUTTON

        else if (source == loopButton) {
            loopClip = soundList.getClip(chosenFile);

            bLooping = true;
            loopClip.loop();                  /* Start the sound loop */
            loopButton.setEnabled(false);     /* Disable loop button */
            stopButton.setEnabled(true); 
            showStatus("Playing sound " + chosenFile + " continuously.");
            if (loopClip == null)
                showStatus("Sound " + chosenFile + " not loaded yet.");
        }

        // STOP LOOP BUTTON

        else if (source == stopButton) {
            if (bLooping) {
                bLooping = false;
	            loopClip.stop();                /* Stop the sound loop */
                loopButton.setEnabled(true);    /* Enable start button */
            }
            else if (onceClip != null)
                onceClip.stop();
            stopButton.setEnabled(false); 
            showStatus("Stopped playing " + chosenFile + ".");
        }
    }

}
