cnrg.itx.datax
Class MixerChannel

java.lang.Object
  |
  +--cnrg.itx.datax.Channel
        |
        +--cnrg.itx.datax.MixerChannel

public class MixerChannel
extends Channel
implements java.lang.Runnable

MixerChannel acts as a channel with multiple inputs. Destinations can be set (as in a standard Channel) with the addDestination() method. To use multiple inputs, call the getNewInput method. This returns a Channel object that can be assigned a source. Example:

The following creates a MixerChannel and adds a microphone soruce and a mixer source to it.

 
 Channel ch;
 MixerChannel mc = new MixerChannel();
 
 ch = mc.getNewInput();
 ch.setSource(new MicrophoneSource(ch));
 ch = mc.getNewInput();
 ch.setSource(new NetworkSource(...,ch,...));
 


Field Summary
private  int BUFFER_TIME
          The number of ms we try to hold buffered at our destination.
private  int iMyInstance
          Instance number of the current mixer; used for stat output numbering.
private  java.util.Vector inChannels
          a vector containing the input ChannelBuffers to feed into the mixer.
(package private) static int instances
          Number of instances of the mixer; used for statistics output numbering.
private  java.util.Hashtable ioLinks
          holds correspondence between linked inputs and outputs for source muting in conferences.
private  int mixSampleSize
          The number of bytes to be mixed at a time.
private  long outBytes
          number of bytes the mixerChannel has sent so far
private  Channel outChannel
          A post-mixing buffering channel.
private  int SAMPLE_RATE
          The sample rate of the data to be mixed.
private  float SAMPLE_TIME
          The number of time we have to process each byte in ms.
private  int totalSilence
          Total bytes of silence mixed in to samples we've sent.
private  boolean useOutputChannel
          Determines if we will use a second buffering stage after the mixer.
 
Fields inherited from class cnrg.itx.datax.Channel
closed, destinations, INPUT, mode, OTHER, OUTPUT, pipedInput, pipedOutput, readerThread, running, SAMPLE_SIZE, sampleSize, source
 
Constructor Summary
MixerChannel()
          Creates an mixerChannel with no sources or destinations.
MixerChannel(int sampleSize)
          Creates a new mixerChannel with the specified sample size.
 
Method Summary
 void addDestination(Destination d)
          Method to add a destination for the Channel.
 void close()
          This method closes the MixerChannel.
private  void doSetup()
          Setup steps common to both constructors.
 Channel getNewInput()
          Adds an additional source to the mixerChannel.
 Channel getNewSubtractedInput(Destination dest)
          Adds an additional source to the mixerChannel that will be silent to the given destination.
 PropertiesCollection getProperties()
          Gets the properties for this MixerChannel
 Source getSource()
          Returns the channel's source.
 Stats getStatistics()
          Method to get the statistics from the channel.
 void mute(boolean state)
          Method to mute all the sources and destinations
 void open()
          Method to open the Channel.
 void push(byte[] b)
          This method will push data into the MixerChannel's output channel skipping over the mixer.
 void removeAllDestinations()
          Method to remove all destinations.
 boolean removeDestination(Destination d)
          Method to remove a destination from the list.
 void removeInput(Channel c)
          Closes and removes a channel form the mixer.
 void removeInput(Source s)
          Closes and removes from the mixer the channel corresponding to the given source.
 void run()
          Implements the mixer thread.
 void setPeerProperties(PropertiesCollection pc)
          Interface to set the given properties collection into the device.
 void setProperties(PropertiesCollection pc)
          Sets the properties for this MixerChannel
 void setSource(Source s)
          Sets the source of the Mixer channel to a single source.
private  byte[] subtract(int[] sum, Destination dest, java.util.Vector inputs)
           
private  int[] sum(java.util.Vector v)
          Sum adds together the corresponding elements of the byte[]'s in the specified vector.
 
Methods inherited from class cnrg.itx.datax.Channel
getMode, setMode
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

inChannels

private java.util.Vector inChannels
a vector containing the input ChannelBuffers to feed into the mixer.

outChannel

private Channel outChannel
A post-mixing buffering channel. This stage is enabled by the useOutputChannel variable.

outBytes

private long outBytes
number of bytes the mixerChannel has sent so far

mixSampleSize

private int mixSampleSize
The number of bytes to be mixed at a time. This is also the number of bytes we send at a time to our destination.

BUFFER_TIME

private int BUFFER_TIME
The number of ms we try to hold buffered at our destination.

SAMPLE_RATE

private int SAMPLE_RATE
The sample rate of the data to be mixed.

SAMPLE_TIME

private float SAMPLE_TIME
The number of time we have to process each byte in ms.

useOutputChannel

private boolean useOutputChannel
Determines if we will use a second buffering stage after the mixer. If this is true, we will allocate a sepatate channel with the mixer as it's source. If this is false, things work marginally faster (no extra buffering) but we have to set mixSampleSize to the destination's sample size.

instances

static int instances
Number of instances of the mixer; used for statistics output numbering.

iMyInstance

private int iMyInstance
Instance number of the current mixer; used for stat output numbering.

totalSilence

private int totalSilence
Total bytes of silence mixed in to samples we've sent.

ioLinks

private java.util.Hashtable ioLinks
holds correspondence between linked inputs and outputs for source muting in conferences.
Constructor Detail

MixerChannel

public MixerChannel()
Creates an mixerChannel with no sources or destinations.

MixerChannel

public MixerChannel(int sampleSize)
Creates a new mixerChannel with the specified sample size.
Parameters:
sampleSize - the number of bytes to be sent by the channel in each write.
Method Detail

doSetup

private void doSetup()
Setup steps common to both constructors.

addDestination

public void addDestination(Destination d)
                    throws DuplicateDestinationException
Method to add a destination for the Channel. This will add the destination to the list of destinations.
Parameters:
d - The destination to add to the list of destination for the channel
Throws:
DuplicateDestinationException - thrown when d is already a destination of this channel.
Overrides:
addDestination in class Channel

removeDestination

public boolean removeDestination(Destination d)
Method to remove a destination from the list.
Parameters:
d - The Destination object to remove
Returns:
boolean True if the destination was found and removed
Overrides:
removeDestination in class Channel

removeAllDestinations

public void removeAllDestinations()
Method to remove all destinations.
Overrides:
removeAllDestinations in class Channel

open

public void open()
          throws DataException
Method to open the Channel. This method starts the mixer thread that mixes data from the sources and sends it to all the destinations. It also starts all of the mixer's sources and destinations.
Throws:
DataException - thrown when channel, or components of channel have already been closed.
Overrides:
open in class Channel

mute

public void mute(boolean state)
Method to mute all the sources and destinations
Parameters:
state - The state of the mute. true to mute and false to unmute
Overrides:
mute in class Channel

getProperties

public PropertiesCollection getProperties()
Gets the properties for this MixerChannel
Returns:
the ProertiesCollection
Overrides:
getProperties in class Channel

setProperties

public void setProperties(PropertiesCollection pc)
Sets the properties for this MixerChannel
Parameters:
pc - The new PropertiesCollection for this MixerChannel
Overrides:
setProperties in class Channel

setPeerProperties

public void setPeerProperties(PropertiesCollection pc)
                       throws DataException
Interface to set the given properties collection into the device. Works under the assumption that this is the properties collection of the peer.
Overrides:
setPeerProperties in class Channel

getStatistics

public Stats getStatistics()
Method to get the statistics from the channel. This method gets the statistics from the devices connected to the channel.
Returns:
Stats the statistics for the channel
Overrides:
getStatistics in class Channel

getSource

public Source getSource()
Returns the channel's source. By default, this is a dummy object. This method is included for compatibility. NOTE: This will not return the ultimate source of the audio, since there is no single source.
Returns:
Source the channel's Source
Overrides:
getSource in class Channel

setSource

public void setSource(Source s)
Sets the source of the Mixer channel to a single source. By default, MixerChannels have no real single source. Setting one could produce unpredictable results.
Parameters:
s - the new source for the MixerChannel
Overrides:
setSource in class Channel

getNewInput

public Channel getNewInput()
Adds an additional source to the mixerChannel. If a new input is added after the mixer has been opened, you must open the input's corresponding channel manually by calling its open() method.
Returns:
Channel a channel to which you can assign a new source

getNewSubtractedInput

public Channel getNewSubtractedInput(Destination dest)
Adds an additional source to the mixerChannel that will be silent to the given destination. If the output is not a destination of the mixerChannel, it will be added as one.
Parameters:
dest - the Destination object to be removed from the mix sent to the returned channel
Returns:
Channel the new ChannelBuffer input to the mixer

removeInput

public void removeInput(Channel c)
Closes and removes a channel form the mixer.

Closing this channel also closes its source.

Parameters:
the - channel corresponding to the input to be removed.

removeInput

public void removeInput(Source s)
Closes and removes from the mixer the channel corresponding to the given source.

(closes both channel and source)

NOTE: slower than removeInput(Channel c).

Parameters:
s - the Source corresponding to the input to be removed

run

public void run()
Implements the mixer thread. Reads form all of the inputs, mixes the samples, and writes out the result to the destinations.
Specified by:
run in interface java.lang.Runnable
Overrides:
run in class Channel

close

public void close()
This method closes the MixerChannel. It first closes all of the channels inputs, then brings down the mixer thread. finally, it closes the output buffer (if it exists)
Overrides:
close in class Channel

push

public void push(byte[] b)
This method will push data into the MixerChannel's output channel skipping over the mixer. When sending data through the mixerChannel, data should instead be sent by invoking the push methods of the inChannels. Included for compatibility. May produce unpredictable results.
Parameters:
b - the data to be sent to the output
Overrides:
push in class Channel

sum

private int[] sum(java.util.Vector v)
Sum adds together the corresponding elements of the byte[]'s in the specified vector. If the sum ever exceeds the max byte value, or falls below the min value, it is clipped.
Parameters:
v - a vector containing byte[]'s to be summed.
Returns:
byte[] the element by element summ of the vectors

subtract

private byte[] subtract(int[] sum,
                        Destination dest,
                        java.util.Vector inputs)