public PlayAudio(VoiceService voices, int frameSize, RemoteVoice user) { Voices = voices; User = user; CallbackHandler = new WinMM.WaveDelegate(WaveCallback); FrameSize = frameSize; int sampleRate = 0; // if 20ms, at high quality (16khz) is 320 samples at 2 bytes each if (FrameSize == 320) { sampleRate = 16000; BufferSize = 320 * 2 * 2; // 2 bytes each frame, 2 channels SpeexMode = Speex.SPEEX_MODEID_WB; } else if (FrameSize == 160) { sampleRate = 8000; BufferSize = 160 * 2 * 2; SpeexMode = Speex.SPEEX_MODEID_NB; } else { Dispose(); return; } try { // init speex Speex.speex_bits_init(ref DecodeBits); IntPtr modePtr = Speex.speex_lib_get_mode(SpeexMode); SpeexDecoder = Speex.speex_decoder_init(modePtr); int tmp = 1; Speex.speex_decoder_ctl(SpeexDecoder, Speex.SPEEX_SET_ENH, ref tmp); // init wave Format = new WinMM.WaveFormat(sampleRate, 16, 2); WinMM.ErrorCheck(WinMM.waveOutOpen(out WaveHandle, Voices.PlaybackDevice, Format, CallbackHandler, 0, WinMM.CALLBACK_FUNCTION)); for (int i = 0; i < BufferCount; i++) { Buffers[i] = new PlayBuffer(i, WaveHandle, BufferSize); } } catch (Exception ex) { Dispose(); Voices.Core.Network.UpdateLog("Voice", "Error starting playing: " + ex.Message); } }
private void InitSpeexEncoder() { // init Speex.speex_bits_init(ref EncodeBits); // get narrow band mode IntPtr modePtr = Speex.speex_lib_get_mode(SpeexMode); SpeexEncoder = Speex.speex_encoder_init(modePtr); //int zeroNoError = 0; int tmp = 0; // no variable bit rate Speex.speex_encoder_ctl(SpeexEncoder, Speex.SPEEX_SET_VBR, ref tmp); tmp = 4; // ok quality Speex.speex_encoder_ctl(SpeexEncoder, Speex.SPEEX_SET_QUALITY, ref tmp); tmp = 1; // uses a little more cpu for better processing Speex.speex_encoder_ctl(SpeexEncoder, Speex.SPEEX_SET_COMPLEXITY, ref tmp); tmp = 1; // voice activated, deadspace is not encoded Speex.speex_encoder_ctl(SpeexEncoder, Speex.SPEEX_SET_VAD, ref tmp); tmp = 1; // dead space no transmission Speex.speex_encoder_ctl(SpeexEncoder, Speex.SPEEX_SET_DTX, ref tmp); Speex.speex_encoder_ctl(SpeexEncoder, Speex.SPEEX_GET_FRAME_SIZE, ref FrameSize); Debug.Assert(FrameSize == BufferSize / 2); if (FrameSize != BufferSize / 2) { throw new Exception("Frame size " + FrameSize + " did agree with buffer " + BufferSize); } /* Turn this off if you want to measure SNR (on by default) */ tmp = 1; Speex.speex_encoder_ctl(SpeexEncoder, Speex.SPEEX_SET_HIGHPASS, ref tmp); // pre-processor PreProcessor = Speex.speex_preprocess_state_init(FrameSize, SampleRate); tmp = 1; Speex.speex_preprocess_ctl(PreProcessor, Speex.SPEEX_PREPROCESS_SET_DENOISE, ref tmp); // echo cancelation //EchoState = Speex.speex_echo_state_init(FrameSize, FrameSize * 3); // 100ms tail length suggested //Speex.speex_preprocess_ctl(PreProcessor, Speex.SPEEX_PREPROCESS_SET_ECHO_STATE, EchoState); EncodedBytes = new byte[BufferSize]; }