00001 00002 package audio; 00003 00004 import java.io.IOException; 00005 00006 /** 00007 * Base class for CODECs that can convert to and from PCM. 00008 * The class wraps underlying AudioInterfacePCM that can find and talk to PCM hardware. 00009 * Derived classes should only implement encoding/decoding part from/to PCM 00010 * (methods AbstractCODEC.convertFromPCM() and AbstractCODEC.convertToPCM()). 00011 */ 00012 public abstract class AbstractCODEC implements AudioInterface 00013 { 00014 /** 00015 * Encodes data to PCM, i.e. converts samples from PCM to CODEC format. 00016 */ 00017 public abstract void convertFromPCM( byte[] in, byte[] out ); 00018 00019 /** 00020 * Decodes data from PCM, i.e. converts samples from CODEC to PCM format. 00021 */ 00022 public abstract void convertToPCM( byte[] in, byte[] out ); 00023 00024 /** 00025 * Gets the VoicePDU subclass attribute of the AbstractAudio object 00026 */ 00027 public abstract int getVoicePduSubclass (); 00028 00029 /** 00030 * Instance of the audio interface that provides access to 00031 * PCM (signed 16-bit linear) samples. 00032 */ 00033 protected AudioInterfacePCM audio; 00034 00035 /** 00036 * Output PCM buffer (converted from CODEC) written to audio interface 00037 */ 00038 protected byte[] outputPcmBuf; 00039 00040 /** 00041 * Input PCM buffer (converted to CODEC) read from audio interface 00042 */ 00043 protected byte[] inputPcmBuf; 00044 00045 /** 00046 * Stops the recorder - but don't throw it away. 00047 */ 00048 public void stopRecording () 00049 { 00050 audio.stopRecording (); 00051 } 00052 00053 /** 00054 * Starts the recorder (returning the time) 00055 */ 00056 public long startRecording () 00057 { 00058 return audio.startRecording (); 00059 } 00060 00061 /** 00062 * Starts the player 00063 */ 00064 public void startPlay () 00065 { 00066 audio.startPlay(); 00067 } 00068 00069 /** 00070 * Stops the player 00071 */ 00072 public void stopPlay () 00073 { 00074 audio.stopPlay(); 00075 } 00076 00077 /** 00078 * Starts ringing signal 00079 */ 00080 public void startRinging () 00081 { 00082 audio.startRinging (); 00083 } 00084 00085 /** 00086 * Stops ringing signal 00087 */ 00088 public void stopRinging () 00089 { 00090 audio.stopRinging (); 00091 } 00092 00093 /** 00094 * Plays the sample given (AudioInterface.getSampleSize() bytes) assuming 00095 * that it's timestamp is long 00096 */ 00097 public void writeBuffered( byte[] buf, long timestamp ) throws IOException 00098 { 00099 convertToPCM( buf, outputPcmBuf ); 00100 audio.writeBuffered( outputPcmBuf, timestamp ); 00101 } 00102 00103 /** 00104 * Reads from the microphone, using the buffer provided, 00105 * but <em>only</em> filling getSampSize() bytes. 00106 * Returns the time-stamp of the sample from the audio clock. 00107 */ 00108 public long readWithTimestamp( byte[] buf ) throws IOException 00109 { 00110 long ret = audio.readWithTimestamp( inputPcmBuf ); 00111 convertFromPCM( inputPcmBuf, buf ); 00112 return ret; 00113 } 00114 00115 /** 00116 * Writes directly to source line without buffering 00117 */ 00118 public void writeDirectly( byte[] f ) 00119 { 00120 byte[] tf = new byte[ 2 * f.length ]; 00121 convertToPCM( f, tf ); 00122 audio.writeDirectly( tf ); 00123 } 00124 00125 /** 00126 * Sets the audioSender attribute of the AbstractAudio object 00127 */ 00128 public void setAudioSender( AudioInterface.Packetizer as ) 00129 { 00130 audio.setAudioSender( as ); 00131 } 00132 00133 /** 00134 * Cleans up resources used by the interface. 00135 */ 00136 public void cleanUp () 00137 { 00138 audio.cleanUp (); 00139 } 00140 00141 /** 00142 * Creates new instance of the interface by choosing specified CODEC format 00143 */ 00144 public AudioInterface getByFormat( Integer format ) 00145 { 00146 return audio.getByFormat(format); 00147 } 00148 }