Package ptolemy.media.javasound
Class LiveSoundCommon
- java.lang.Object
-
- ptolemy.media.javasound.LiveSoundCommon
-
- Direct Known Subclasses:
LiveSoundJavaSE
public class LiveSoundCommon extends java.lang.Object
Class containing the platform independent code of LiveSound actor.- Since:
- Ptolemy II 10.0
- Version:
- $Id$
- Author:
- Brian K. Vogel and Neil E. Turner and Steve Neuendorffer, Edward A. Lee, Contributor: Dennis Geurts, Ishwinder Singh
- Pt.AcceptedRating:
- Red (ishwinde)
- Pt.ProposedRating:
- Red (ishwinde)
-
-
Field Summary
Fields Modifier and Type Field Description protected double[][]
_audioInDoubleArray
Array of audio samples in double format.protected int
_bitsPerSample
The number of bits per sample.protected int
_bufferSize
The requested buffer size in samples per channel.protected int
_bytesPerSample
The number of bytes per sample, default 2.protected byte[]
_captureData
Array of audio samples in byte format.protected boolean
_captureIsActive
true is audio capture is currently active.protected int
_channels
The number of channels.protected java.util.List
_liveSoundListeners
The list of listeners.protected double
_maxSample
Cached value of the maximum integer value, default for 16 bits.protected double
_maxSampleReciprocal
Cached value of the maximum value scaling factor, default for 16 bits.protected byte[]
_playbackData
Byte buffer used for playback data.protected boolean
_playbackIsActive
true is audio playback is currently active.protected float
_sampleRate
The sample rate.protected java.util.List
_soundConsumers
The list of sound consumers.protected int
_transferSize
The number of audio samples to transfer per channel when getSamples() is invoked.
-
Constructor Summary
Constructors Constructor Description LiveSoundCommon()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
_byteArrayToDoubleArray(double[][] doubleArray, byte[] byteArray)
Convert a byte array of audio samples in linear signed PCM big endian format into a double array of audio samples (-1, 1) range.protected byte[]
_doubleArrayToByteArray(double[][] doubleArray)
Convert a double array of audio samples into a byte array of audio samples in linear signed PCM big endian format.protected void
_notifyLiveSoundListeners(int parameter)
Notify the live sound listeners about a change in an audio parameter.void
addLiveSoundListener(LiveSoundListener listener)
Add a live sound listener.int
getBitsPerSample()
Return the number of bits per sample.int
getBufferSize()
Return the suggested size of the internal capture and playback audio buffers, in samples per channel.int
getChannels()
Return the number of audio channels, which is set by the setChannels() method.int
getSampleRate()
Return the current sampling rate in Hz, which is set by the setSampleRate() method.int
getTransferSize()
Get the array length (in samples per channel) to use for capturing and playing samples via the putSamples() and getSamples() methods.boolean
isCaptureActive()
Return true if audio capture is currently active.boolean
isPlaybackActive()
Return true if audio playback is currently active.void
removeLiveSoundListener(LiveSoundListener listener)
Remove a live sound listener.void
setTransferSize(int transferSize)
Set the array length (in samples per channel) to use for capturing and playing samples via the putSamples() and getSamples() methods.
-
-
-
Field Detail
-
_audioInDoubleArray
protected double[][] _audioInDoubleArray
Array of audio samples in double format.
-
_bitsPerSample
protected int _bitsPerSample
The number of bits per sample. Default is 16.
-
_bufferSize
protected int _bufferSize
The requested buffer size in samples per channel.
-
_bytesPerSample
protected int _bytesPerSample
The number of bytes per sample, default 2.
-
_captureIsActive
protected boolean _captureIsActive
true is audio capture is currently active.
-
_channels
protected int _channels
The number of channels. Deafult is 1.
-
_captureData
protected byte[] _captureData
Array of audio samples in byte format.
-
_playbackData
protected byte[] _playbackData
Byte buffer used for playback data.
-
_liveSoundListeners
protected java.util.List _liveSoundListeners
The list of listeners.
-
_maxSampleReciprocal
protected double _maxSampleReciprocal
Cached value of the maximum value scaling factor, default for 16 bits.
-
_maxSample
protected double _maxSample
Cached value of the maximum integer value, default for 16 bits.
-
_playbackIsActive
protected boolean _playbackIsActive
true is audio playback is currently active.
-
_sampleRate
protected float _sampleRate
The sample rate.
-
_soundConsumers
protected java.util.List _soundConsumers
The list of sound consumers.
-
_transferSize
protected int _transferSize
The number of audio samples to transfer per channel when getSamples() is invoked.
-
-
Method Detail
-
addLiveSoundListener
public void addLiveSoundListener(LiveSoundListener listener)
Add a live sound listener. The listener will be notified of all changes in live audio parameters. If the listener is already listening, then do nothing.- Parameters:
listener
- The LiveSoundListener to add.- See Also:
removeLiveSoundListener(LiveSoundListener)
-
getBitsPerSample
public int getBitsPerSample()
Return the number of bits per sample.- Returns:
- The number of bits per sample, which in this base class defaults to 16.
-
getBufferSize
public int getBufferSize()
Return the suggested size of the internal capture and playback audio buffers, in samples per channel. This parameter is set by the setBufferSize() method. There is no guarantee that the value returned is the actual buffer size used for capture and playback. Furthermore, the buffers used for capture and playback may have different sizes. The default value of this parameter is 4096.- Returns:
- The suggested internal buffer size in samples per channel.
-
getChannels
public int getChannels()
Return the number of audio channels, which is set by the setChannels() method. The default value of this parameter is 1 (for mono audio).- Returns:
- The number of audio channels.
-
getSampleRate
public int getSampleRate()
Return the current sampling rate in Hz, which is set by the setSampleRate() method. The default value of this parameter is 8000 Hz.- Returns:
- The sample rate in Hz.
-
getTransferSize
public int getTransferSize()
Get the array length (in samples per channel) to use for capturing and playing samples via the putSamples() and getSamples() methods. This method gets the size of the 2nd dimension of the 2-dimensional array used by the putSamples() and getSamples() methods. This method returns the value that was set by the setTransferSize(). If setTransferSize() was not invoked, the default value of 128 is returned.- Returns:
- The size of the 2nd dimension of the 2-dimensional array used by the putSamples() and getSamples() methods.
- See Also:
setTransferSize(int)
-
isCaptureActive
public boolean isCaptureActive()
Return true if audio capture is currently active. Otherwise return false.- Returns:
- True If audio capture is currently active. Otherwise return false.
-
isPlaybackActive
public boolean isPlaybackActive()
Return true if audio playback is currently active. Otherwise return false.- Returns:
- True If audio playback is currently active. Otherwise return false.
-
removeLiveSoundListener
public void removeLiveSoundListener(LiveSoundListener listener)
Remove a live sound listener. If the listener is is not listening, then do nothing.- Parameters:
listener
- The LiveSoundListener to remove.- See Also:
addLiveSoundListener(LiveSoundListener)
-
setTransferSize
public void setTransferSize(int transferSize) throws java.lang.IllegalStateException
Set the array length (in samples per channel) to use for capturing and playing samples via the putSamples() and getSamples() methods. This method sets the size of the 2nd dimension of the 2-dimensional array used by the putSamples() and getSamples() methods. If this method is not invoked, the default value of 128 is used.This method should only be called while audio capture and playback are inactive. Otherwise an exception will occur.
- Parameters:
transferSize
- The size of the 2nd dimension of the 2-dimensional array used by the putSamples() and getSamples() methods- Throws:
java.lang.IllegalStateException
- If this method is called while audio capture or playback are active.- See Also:
getTransferSize()
-
_byteArrayToDoubleArray
protected void _byteArrayToDoubleArray(double[][] doubleArray, byte[] byteArray)
Convert a byte array of audio samples in linear signed PCM big endian format into a double array of audio samples (-1, 1) range.- Parameters:
doubleArray
- The resulting array of doubles.byteArray
- The linear signed pcm big endian byte array formatted array representation of audio data.
-
_doubleArrayToByteArray
protected byte[] _doubleArrayToByteArray(double[][] doubleArray)
Convert a double array of audio samples into a byte array of audio samples in linear signed PCM big endian format. The samples contained in doubleArray should be in the range (-1, 1). Samples outside this range will be hard clipped to the range (-1, 1).- Parameters:
doubleArray
- Two dimensional array holding audio samples. For each channel, m, doubleArray[m] is a single dimensional array containing samples for channel m. All channels are required to have the same number of samples, but this is not checked.- Returns:
- The linear signed PCM big endian byte array formatted array representation of doubleArray. The length of the returned array is (doubleArray[i].length*bytesPerSample*channels).
-
_notifyLiveSoundListeners
protected void _notifyLiveSoundListeners(int parameter)
Notify the live sound listeners about a change in an audio parameter.- Parameters:
parameter
- The audio parameter of LiveSound that has changed. The value of parameter should be one of LiveSoundEvent.SAMPLE_RATE, LiveSoundEvent.CHANNELS, LiveSoundEvent.BUFFER_SIZE, or LiveSoundEvent.BITS_PER_SAMPLE.
-
-