/* $Id$
 *
 * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
 *
 * This software is protected by copyright. You are hereby notified from
 * now by reading this message. This software is also the confidential 
 * and proprietary information of Xenonsoft Limited.  ("Confidential
 * Information"). 
 *
 * This software is distributed under the Xenonsoft Public end user 
 * License ("XPeL"), where the machine-readable source code is provided 
 * under the "Open Source" model.
 * For more information, please read the file "LICENSE-XPL.txt" 
 */

// PPMCAudioPlayer.java

//
// Description:
//	Peter Pilgrim's Multi-Channel AudioPlayer
//	  
//	  
// Author:
//	  Peter Pilgrim
//	  
// RCS HEADER
// 
// $Author$
// $Date$
// $Source$
// $Revision$    $State$    $Locker$
//
// ================================================================================
// History
//	  $Log$
// 
// 
//

package xenon.soundkit;

import sun.audio.*;

public class PPMCAudioPlayer {
    /** number of channels */
    public final static int	NUMBER_CHANNELS=32;

    private AudioDataStream []	audioDataStream = new AudioDataStream[NUMBER_CHANNELS];
    private boolean		muted;

    private static PPMCAudioPlayer mcAudioPlayer = new PPMCAudioPlayer(); // SINGLETON OBJECT
    
    private PPMCAudioPlayer() 
    {
	muted = false;
    }

    /** Get multi channel audio player instance. */
    public static PPMCAudioPlayer getInstance() 
    {
	return (mcAudioPlayer);
    }
    
    /** Get the number of channels */
    public int  getNumChannels() 
    {
	return (NUMBER_CHANNELS);
    }
    
    /** Returns true if the audio player is muted */
    public boolean isMuted() 
    {
	return (muted);
    }
    
    /** Sets the flag if the audio player is muted or not 
     * @param flag true if the sound is muted.
     */
    public void setMute( boolean flag )
    {
	if ( flag && !muted ) 
	    stopAllChannels();
	muted = flag;
    }


    /** Stops all the audio channels effectively killing any audio data 
     * being playing at the moment of the call.
     */
    public void stopAllChannels()
    {
	for (int j=0; j<NUMBER_CHANNELS; ++j) 
	    stop(j);
    }
    
    
    /** Play a sound on specific channel with mu law data */
    public void play( int channel, byte[] ulaw ) 
    {
	if ( muted ) return;
	
	stop( channel );

	channel = channel % NUMBER_CHANNELS;
	AudioData audioData = new AudioData(ulaw);
	audioDataStream[channel] = new AudioDataStream(audioData);
	AudioPlayer.player.start( audioDataStream[channel] );
    }

    /** Plays a sound specific channel with mu law data */
    public void loop( int channel, byte[] ulaw ) 
    {
	stop( channel );

	channel = channel % NUMBER_CHANNELS;
	AudioData audioData = new AudioData(ulaw);
	audioDataStream[channel] = new ContinuousAudioDataStream(audioData);
	AudioPlayer.player.start( audioDataStream[channel] );
    }

    /** Stop a sound on a particular channel */
    public void stop( int channel ) 
    {
	channel = channel % NUMBER_CHANNELS;
	if (audioDataStream[channel] != null) {
	    AudioPlayer.player.stop( audioDataStream[channel] );
	    audioDataStream[channel] = null;
	}
    }

    /** Returns a boolean if a particular channel is playing a sound */
    public boolean isPlaying( int channel ) 
    {
	channel = channel % NUMBER_CHANNELS;
	return (audioDataStream[channel] != null);
    }

    /** Returns a boolean if particular channel is playing a looping sound */
    public boolean isLooping( int channel ) 
    {
	channel = channel % NUMBER_CHANNELS;
	return (audioDataStream[channel] != null &&
		audioDataStream[channel] instanceof ContinuousAudioDataStream );
    }

}

// fini
