📄 voicemicrophoneinput.java
字号:
for(int i = 0 ; i < chunks ; i++) { byte[] preEncodeBuff = new byte[rawChunkSize]; System.arraycopy (buff,startPos[i], preEncodeBuff, 0, rawChunkSize ); long in = System.currentTimeMillis (); byte[] postEncodeBuff = encoder.encode (preEncodeBuff); long out = System.currentTimeMillis (); int roundTrip = (int)(out - in); if(averageEncodeTime != 0) { averageEncodeTime = (int) (averageEncodeTime + roundTrip) / 2; }else{ averageEncodeTime = roundTrip; } sendBuffer.append (postEncodeBuff); } } /** * Returns the current size in bytes of data stored in the send buffer */ public int getBufferSize() { return sendBuffer.size(); } /** * Returns the size in bytes of the capacity of the send buffer */ public int getBufferCapacity() { return sendBuffer.getCapacity(); } public int getAverageEncodeTime () { return averageEncodeTime; } public int getEncodeQuality () { return this.encodeQuality; } protected VoJxtaCallControl getCallControl () { return this.vojxtaCallControl; } public int getAudioBlockSize () { return this.rawChunkSize; } /** * Encapsulates audio data and session command into a dialogMessage. * Diapatches to remote peer. */ public void dispatchVoiceData (byte[] speexData) { // for testing purposes // check if encode/decode process is stable if(firstSentMessage) { if (LOG.isEnabledFor (Level.INFO)) { LOG.info ("dispatchVoiceData : firstSentMessage"); } if(speexData!=null) { firstSentBytes = new byte[speexData.length]; System.arraycopy (speexData, 0 , firstSentBytes, 0 , speexData.length); firstSentMessage = false; }else{ if (LOG.isEnabledFor (Level.INFO)) { LOG.info ("dispatchVoiceData : voice data is null"); } } } DialogMessage msg = (DialogMessage) getCallControl ().getTemplateMessage ().clone (); StringMessageElement commandElement = new StringMessageElement ( VoJxtaCallControl.TAG_SESSION_COMMAND, VoJxtaCallControl.COMMAND_VOJXTA_DATA, null); msg.addMessageElement (VoJxtaCallControl.TAG_SESSION_COMMAND, commandElement); ByteArrayMessageElement voiceElement = new ByteArrayMessageElement ( VoJxtaCallControl.TAG_VOICE_DATA, null, speexData, 0, null); msg.addMessageElement (VoJxtaCallControl.TAG_VOICE_DATA, voiceElement); this.vojxtaDialog.dispatch (msg); this.sentMessages=this.sentMessages+1; this.sentBytes=this.sentBytes+speexData.length; } public long getNumberOfBytesSent () { return this.sentBytes; } public long getNumberOfMessagesSent () { return sentMessages; } private int getMicState () { return this.micState; } private void setMicState (int state) { this.micState = state; } /** * Starts the tagetLine. sets up the buffer and message size. */ public void beginMic () { encodeQuality = getCallControl ().getMinimumVoiceQuality (); encoder = new Encoder (getCallControl ().getMinimumVoiceQuality ()); speexChunkSize = ((Integer)VoJxtaCallControl.qualityByteMap.get (new Integer(getCallControl ().getMinimumVoiceQuality ()))).intValue (); LOG.info("mic chunk size "+speexChunkSize); if ( speexMessageSize == 0) { speexMessageSize = speexChunkSize * DEFAULT_MESSAGE_SIZE_MULTIPLIER; } LOG.info("mic message size "+speexMessageSize); if (speexBufferSize == 0) { speexBufferSize = speexChunkSize * DEFAULT_BUFFER_SIZE_MULTIPLIER; } LOG.info("mic buffer size "+speexBufferSize); this.sendBuffer = new VoiceDataBuffer (speexBufferSize, this, "MicControl"); if(getMicState () == STATE_PAUSE) { setMicState (this.STATE_ON); synchronized(pauseLock) { pauseLock.notify (); } synchronized(messageThreadLock) { messageThreadLock.notify (); } } this.targetLine.start (); super.start (); } /** * Disallows reading to the target line (mic). sets thread state to * off */ public void endMic () { setMicState (this.STATE_OFF); if(targetLine != null) { this.targetLine.stop (); } } /** * Halts the reading of data to this targetline. pauses audio line read * thread. */ public void pauseMic () { setMicState (this.STATE_PAUSE); this.targetLine.stop (); } /** * Restarts the target line and message send thread. */ public void resumeMic () { this.targetLine.start (); setMicState (this.STATE_ON); synchronized(pauseLock) { pauseLock.notify (); } synchronized(messageThreadLock) { messageThreadLock.notify (); } } /** * Releases hardware resources. tagetline, mixers and controls. */ public void releaseHardware () { if(targetLine != null) { targetLine.flush(); targetLine.stop (); targetLine.close (); } } /** * Return true if gain is supported in line target line. */ public boolean isGainControlSupported () { return gainControl != null; } public float getMaxGainValue () { return (gainControl!=null) ? gainControl.getMaximum () : 0.0f; } public float getMinGainValue () { return (gainControl!=null) ? gainControl.getMinimum () : 0.0f; } public String getGainUnits () { return (gainControl!=null) ? gainControl.getUnits () : ""; } public String getMaxLabel () { return (gainControl!=null) ? gainControl.getMaxLabel () : ""; } public String getMinLabel () { return (gainControl!=null) ? gainControl.getMinLabel () : ""; } public float getGainValue () { return (gainControl!=null) ? gainControl.getValue () : 0.0f; } public void adjustGainControl (float newValue) { if(gainControl!=null) { gainControl.setValue (newValue); } } private void printMicState () { String s = null; if(this.micState == STATE_PAUSE) { s = "PAUSE "; } if(this.micState == STATE_ON) { s = "ON "; } if(this.micState == STATE_OFF) { s = "OFF "; } } private void printMixers () { System.out.println ("\n\rMixers : "); Mixer.Info[] info = AudioSystem.getMixerInfo (); for(int i = 0; i< info.length; i++) { System.out.println ("\tName: "+info[i].getName ()+ " Description: "+info[i].getDescription ()); } } private void printControls () { System.out.println ("\n\rControls : "); Control[] controls = targetLine.getControls (); for(int i = 0; i< controls.length; i++) { System.out.println ("\tName: "+controls[i].toString ()); } } protected void printSupportedControls () { LOG.info ("Mic SupportedControls"); LOG.info ("mute "+targetLine.isControlSupported (BooleanControl.Type.MUTE)); LOG.info ("Balance "+targetLine.isControlSupported (FloatControl.Type.BALANCE)); LOG.info ("MasterGain "+targetLine.isControlSupported (FloatControl.Type.MASTER_GAIN)); LOG.info ("Pan "+targetLine.isControlSupported (FloatControl.Type.PAN)); LOG.info ("SampleRate "+targetLine.isControlSupported (FloatControl.Type.SAMPLE_RATE)); LOG.info ("Volume "+targetLine.isControlSupported (FloatControl.Type.VOLUME)); Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo (); for(int i = 0; i< mixerInfo.length; i++) { micMixer = AudioSystem.getMixer (mixerInfo[i]); LOG.info ("MicMixer SupportedControls for mixer"+mixerInfo[i].toString ()); LOG.info ("mute "+micMixer.isControlSupported (BooleanControl.Type.MUTE)); LOG.info ("Balance "+micMixer.isControlSupported (FloatControl.Type.BALANCE)); LOG.info ("MasterGain "+micMixer.isControlSupported (FloatControl.Type.MASTER_GAIN)); LOG.info ("Pan "+micMixer.isControlSupported (FloatControl.Type.PAN)); LOG.info ("SampleRate "+micMixer.isControlSupported (FloatControl.Type.SAMPLE_RATE)); LOG.info ("Volume "+micMixer.isControlSupported (FloatControl.Type.VOLUME)); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -