audio
Class AudioInterfacePCM

java.lang.Object
  extended by audio.AudioInterfacePCM
All Implemented Interfaces:
AudioInterface

public class AudioInterfacePCM
extends java.lang.Object
implements AudioInterface

Implements the audio interface for 16-bit signed linear audio (PCM_SIGNED). It also provides support for CODECs that can convert to and from Signed LIN16.

Author:
Mikica B Kocic, based on hevaily modified and re-documented Tim Panton's (thp@westhawk.co.uk) code from org.asteriskjava.iax.audio.javasound.Audio8k

Nested Class Summary
 
Nested classes/interfaces inherited from interface audio.AudioInterface
AudioInterface.Packetizer
 
Field Summary
private  java.lang.Thread audioPlayerThread
           
private  AudioInterface.Packetizer audioSender
           
private  java.lang.Thread audioSenderThread
           
private  long callLength
          Measured call length in milliseconds
private  long deltaTimePlayerMinusMic
           
private static int FRAME_COUNT
          Audio buffering depth in number of frames
private static int FRAME_INTERVAL
          Frame interval in milliseconds
private  boolean jitBufFirst
           
private  long jitBufFudge
           
private  int jitBufGet
           
private  int jitBufPut
           
private  long lastMicTimestamp
           
private static int LLBS
          Low-level water mark used for de-jittering
private  int micBufGet
           
private  int micBufPut
           
private  java.lang.Thread micRecorderThread
           
private  javax.sound.sampled.AudioFormat mono44k
           
private  javax.sound.sampled.AudioFormat mono8k
           
private  AudioBuffer[] playBuffer
           
private  boolean playerIsEnabled
           
private  boolean propertyBigBuff
          Big buffers
private  java.lang.String propertyInputDeviceName
          Input device name
private  java.lang.String propertyOutputDeviceName
          Output device name
private  boolean propertyStereoRec
          Stereo recording
private  boolean providingRingBack
           
private  AudioBuffer[] recordBuffer
           
private  java.lang.Thread ringerThread
           
private  byte[] ringSamples
           
private  long ringTimer
           
private  byte[] silenceSamples
           
private  javax.sound.sampled.SourceDataLine sourceDataLine
           
private  javax.sound.sampled.AudioFormat stereo8k
           
private  javax.sound.sampled.TargetDataLine targetDataLine
           
 
Constructor Summary
AudioInterfacePCM()
          Constructor for the AudioInterfacePCM object
 
Method Summary
private  void audioPlayerWorker()
          Writes frames to audio output i.e. source data line
 void cleanUp()
          Stops threads and cleans-up the instance.
private  void concealMissingDataForAudioOutput(int n)
          Conceals missing data in the audio output buffer by averaging from samples taken from the the previous and next buffer.
private  javax.sound.sampled.DataLine findDataLineByPref(java.lang.String pref, javax.sound.sampled.AudioFormat af, java.lang.String name, int sbuffsz, java.lang.Class<?> lineClass, java.lang.String debugInfo)
          Searches for data line of either sort (source/targe) based on the pref string.
private  javax.sound.sampled.SourceDataLine findSourceDataLineByPref(java.lang.String pref, javax.sound.sampled.AudioFormat af, java.lang.String name, int sbuffsz)
          Searches for source data line according to preferences.
private  javax.sound.sampled.TargetDataLine fintTargetDataLineByPref(java.lang.String pref, javax.sound.sampled.AudioFormat af, java.lang.String name, int sbuffsz)
          Searches for target data line according to preferences.
private  boolean getAudioIn()
          Returns audio input (target data line)
private  boolean getAudioOut()
          Get audio output.
 AudioInterface getByFormat(java.lang.Integer format)
          Gets audio interface by VoicePDU format
 int getSampleSize()
          Returns preferred the minimum sample size for use in creating buffers etc.
 int getVoicePduSubclass()
          Returns our VoicePDU format
private  void initializeRingerSamples()
          Initializes ringer samples (ring singnal and silecce) samples
private  void micDataRead()
          Called from micRecorder to record audio samples from microphone.
private  void micRecorderWorker()
          Records audio samples from the microphone
private  void pduSenderWorker()
          Sends audio frames to UDP channel at regular intervals (ticks)
 long readWithTimestamp(byte[] buff)
          Read from the Microphone, into the buffer provided, but only filling getSampSize() bytes.
private  void resample(byte[] src, byte[] dest)
          Simple PCM down sampler.
private  void ringerWorker()
          Writes ring signal samples to audio output
private  void sendAudioFrame(long set)
          Called every FRAMEINTERVAL ms to send audio frame
 void setAudioSender(AudioInterface.Packetizer as)
          Sets the active audio sender for the recorder
 void startPlay()
          Starts the audio output worker thread
 long startRecording()
          Start the audio recording worker thread
 void startRinging()
          Starts ringing signal
 void stopPlay()
          Stops the audio output worker thread
 void stopRecording()
          Stops the audio recording worker thread
 void stopRinging()
          Stops ringing singnal
 void writeBuffered(byte[] buff, long timestamp)
          Enqueue packet for playing into de-jitter buffer.
private  long writeBuffersToAudioOutput()
          Writes de-jittered audio frames to audio output
private  long writeDirectIfAvail(byte[] samples)
          Writes audio samples to audio output directly (without using jitter buffer).
 void writeDirectly(byte[] buff)
          Writes directly to source line without buffering
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FRAME_COUNT

private static final int FRAME_COUNT
Audio buffering depth in number of frames

See Also:
Constant Field Values

LLBS

private static final int LLBS
Low-level water mark used for de-jittering

See Also:
Constant Field Values

FRAME_INTERVAL

private static final int FRAME_INTERVAL
Frame interval in milliseconds

See Also:
Constant Field Values

propertyStereoRec

private boolean propertyStereoRec
Stereo recording


propertyBigBuff

private boolean propertyBigBuff
Big buffers


propertyInputDeviceName

private java.lang.String propertyInputDeviceName
Input device name


propertyOutputDeviceName

private java.lang.String propertyOutputDeviceName
Output device name


mono8k

private javax.sound.sampled.AudioFormat mono8k

stereo8k

private javax.sound.sampled.AudioFormat stereo8k

mono44k

private javax.sound.sampled.AudioFormat mono44k

targetDataLine

private javax.sound.sampled.TargetDataLine targetDataLine

audioSenderThread

private volatile java.lang.Thread audioSenderThread

micRecorderThread

private volatile java.lang.Thread micRecorderThread

audioSender

private volatile AudioInterface.Packetizer audioSender

recordBuffer

private AudioBuffer[] recordBuffer

micBufPut

private int micBufPut

micBufGet

private int micBufGet

lastMicTimestamp

private long lastMicTimestamp

sourceDataLine

private javax.sound.sampled.SourceDataLine sourceDataLine

audioPlayerThread

private volatile java.lang.Thread audioPlayerThread

playBuffer

private AudioBuffer[] playBuffer

jitBufPut

private int jitBufPut

jitBufGet

private int jitBufGet

jitBufFudge

private long jitBufFudge

jitBufFirst

private boolean jitBufFirst

playerIsEnabled

private boolean playerIsEnabled

deltaTimePlayerMinusMic

private long deltaTimePlayerMinusMic

callLength

private long callLength
Measured call length in milliseconds


ringerThread

private volatile java.lang.Thread ringerThread

ringSamples

private byte[] ringSamples

silenceSamples

private byte[] silenceSamples

providingRingBack

private boolean providingRingBack

ringTimer

private long ringTimer
Constructor Detail

AudioInterfacePCM

public AudioInterfacePCM()
Constructor for the AudioInterfacePCM object

Method Detail

getSampleSize

public int getSampleSize()
Returns preferred the minimum sample size for use in creating buffers etc.

Specified by:
getSampleSize in interface AudioInterface

getVoicePduSubclass

public int getVoicePduSubclass()
Returns our VoicePDU format

Specified by:
getVoicePduSubclass in interface AudioInterface

setAudioSender

public void setAudioSender(AudioInterface.Packetizer as)
Sets the active audio sender for the recorder

Specified by:
setAudioSender in interface AudioInterface

cleanUp

public void cleanUp()
Stops threads and cleans-up the instance.

Specified by:
cleanUp in interface AudioInterface

audioPlayerWorker

private void audioPlayerWorker()
Writes frames to audio output i.e. source data line


writeBuffersToAudioOutput

private long writeBuffersToAudioOutput()
Writes de-jittered audio frames to audio output


concealMissingDataForAudioOutput

private void concealMissingDataForAudioOutput(int n)
Conceals missing data in the audio output buffer by averaging from samples taken from the the previous and next buffer.


writeDirectly

public void writeDirectly(byte[] buff)
Writes directly to source line without buffering

Specified by:
writeDirectly in interface AudioInterface

writeBuffered

public void writeBuffered(byte[] buff,
                          long timestamp)
                   throws java.io.IOException
Enqueue packet for playing into de-jitter buffer.

Specified by:
writeBuffered in interface AudioInterface
Throws:
java.io.IOException

pduSenderWorker

private void pduSenderWorker()
Sends audio frames to UDP channel at regular intervals (ticks)


sendAudioFrame

private void sendAudioFrame(long set)
Called every FRAMEINTERVAL ms to send audio frame


micRecorderWorker

private void micRecorderWorker()
Records audio samples from the microphone


micDataRead

private void micDataRead()
Called from micRecorder to record audio samples from microphone. Blocks as needed.


readWithTimestamp

public long readWithTimestamp(byte[] buff)
                       throws java.io.IOException
Read from the Microphone, into the buffer provided, but only filling getSampSize() bytes. Returns the timestamp of the sample from the audio clock.

Specified by:
readWithTimestamp in interface AudioInterface
Parameters:
buff - audio samples
Returns:
the timestamp of the sample from the audio clock.
Throws:
java.io.IOException - Description of Exception

resample

private void resample(byte[] src,
                      byte[] dest)
Simple PCM down sampler.

Parameters:
src - source buffer with audio samples
dest - destination buffer with audio samples

ringerWorker

private void ringerWorker()
Writes ring signal samples to audio output


writeDirectIfAvail

private long writeDirectIfAvail(byte[] samples)
Writes audio samples to audio output directly (without using jitter buffer).

Returns:
milliseconds to sleep (after which time next write should occur)

initializeRingerSamples

private void initializeRingerSamples()
Initializes ringer samples (ring singnal and silecce) samples


getAudioOut

private boolean getAudioOut()
Get audio output. Initializes source data line.


getAudioIn

private boolean getAudioIn()
Returns audio input (target data line)


findDataLineByPref

private javax.sound.sampled.DataLine findDataLineByPref(java.lang.String pref,
                                                        javax.sound.sampled.AudioFormat af,
                                                        java.lang.String name,
                                                        int sbuffsz,
                                                        java.lang.Class<?> lineClass,
                                                        java.lang.String debugInfo)
Searches for data line of either sort (source/targe) based on the pref string. Uses type to determine the sort ie Target or Source. debugInfo parameter is only used in debug printouts to set the context.


fintTargetDataLineByPref

private javax.sound.sampled.TargetDataLine fintTargetDataLineByPref(java.lang.String pref,
                                                                    javax.sound.sampled.AudioFormat af,
                                                                    java.lang.String name,
                                                                    int sbuffsz)
Searches for target data line according to preferences.


findSourceDataLineByPref

private javax.sound.sampled.SourceDataLine findSourceDataLineByPref(java.lang.String pref,
                                                                    javax.sound.sampled.AudioFormat af,
                                                                    java.lang.String name,
                                                                    int sbuffsz)
Searches for source data line according to preferences.


stopRecording

public void stopRecording()
Stops the audio recording worker thread

Specified by:
stopRecording in interface AudioInterface

startRecording

public long startRecording()
Start the audio recording worker thread

Specified by:
startRecording in interface AudioInterface

startPlay

public void startPlay()
Starts the audio output worker thread

Specified by:
startPlay in interface AudioInterface

stopPlay

public void stopPlay()
Stops the audio output worker thread

Specified by:
stopPlay in interface AudioInterface

getByFormat

public AudioInterface getByFormat(java.lang.Integer format)
Gets audio interface by VoicePDU format

Specified by:
getByFormat in interface AudioInterface
Returns:
AudioInterfacePCM

startRinging

public void startRinging()
Starts ringing signal

Specified by:
startRinging in interface AudioInterface

stopRinging

public void stopRinging()
Stops ringing singnal

Specified by:
stopRinging in interface AudioInterface