public CallHelper(string id)
        {
            this.isCallEstablished = true;
            this.callId            = id;

            this.codec = new NarrowBandSpeexCodec();
        }
Example #2
0
    public static short[] GetDecodePcm(byte[] data)
    {
        int          offset = 0;
        var          codec  = new SpeexCodec();
        List <short> buffer = new List <short>();

        while (offset < data.Length)
        {
            USpeakFrameContainer cont = default(USpeakFrameContainer);
            var     l   = cont.LoadFrom(data, offset);
            short[] pcm = codec.Decode(cont.encodedData, BandMode.Narrow);
            offset += l;
            buffer.AddRange(pcm.ToArray());
        }
        return(buffer.ToArray());
    }
            ///<summary>
            ///    Initialize a new microphone instance, creating the
            ///    FMOD sound object, optionally opening a recording
            ///    stream, and creating and starting a timer instance
            ///    to copy the PCM samples.
            ///</summary>
            public void InitMicrophone(VoiceParmSet parmSet, bool reconfigure)
            {
                log.DebugFormat(voiceMgr, "MicrophoneChannel.InitMicrophone called with recording WAV({0}), Speex({1})", recordingWAV, recordingSpeex);
                if (channelCodec != null)
                    return;
                try {
                    log.Debug(voiceMgr, "MicrophoneChannel.InitMicrophone: Creating codec");
                    encoderOutputBuffer = new byte[maxBytesPerEncodedFrame];
                    if (voiceMgr.runningVoiceBot) {
                        ApplyVoiceBotParms(parmSet, reconfigure);
                        SetTimeOfNextPlayback();
                    }
                    else {
                        encoderInputBuffer = new short[maxSamplesPerFrame];
                        channelCodec = new SpeexCodec();
                        int encodecFrameSize = channelCodec.InitEncoder(maxSamplesPerFrame, samplesPerFrame, samplesPerSecond);
                        if (encodecFrameSize != samplesPerFrame)
                            log.ErrorFormat(voiceMgr, "MicrophoneChannel.InitMicrophone: The encoder frame size {0} != samplesPerFrame " + samplesPerFrame);
                        ApplyMicrophoneSettings(parmSet, reconfigure);
                        // create a sound object
                        FMOD.RESULT result;
                        FMOD.MODE mode = FMOD.MODE._2D | FMOD.MODE.OPENUSER | FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL;
                        lock(voiceMgr.usingFmod) {
                            FMOD.CREATESOUNDEXINFO  exinfo = new FMOD.CREATESOUNDEXINFO();
                            exinfo.cbsize = Marshal.SizeOf(exinfo);
                            exinfo.decodebuffersize = 0;
                            exinfo.length = (uint)(micQueueSize * 2);
                            exinfo.numchannels = 1;
                            exinfo.defaultfrequency = samplesPerSecond;
                            exinfo.format = FMOD.SOUND_FORMAT.PCM16;

                            log.Debug(voiceMgr, "MicrophoneChannel.InitMicrophone: Creating sound");
                            result = voiceMgr.fmod.createSound((string)null, mode, ref exinfo, ref channelSound);
                        }
                        try {
                            ERRCHECK(result);
                        }
                        catch (Exception e) {
                            log.ErrorFormat(voiceMgr, "MicrophoneChannel.InitMicrophone: Error creating microphone sound: {0}; stack trace\n {1}",
                                e.Message, e.StackTrace);
                        }
                        SetMicDevice();
                        micSamples = new short[micQueueSize];
                        if (recordingWAV || recordingSpeex) {
                            log.Debug(voiceMgr, "MicrophoneChannel.InitMicrophone: Calling StartRecording");
                            StartRecording(channelSound, voiceMgr.LogFolderPath("RecordMic"), recordingWAV, recordingSpeex);
                        }
                        log.Debug(voiceMgr, "MicrophoneChannel.InitMicrophone: Calling StartFmodMicUpdates");
                        StartFmodMicUpdates(channelSound);
                        log.Debug(voiceMgr, "MicrophoneChannel.InitMicrophone: Calling StartChannel");
                        StartChannel(false);
                    }
                    micHandlingTimer = new System.Timers.Timer();
                    micHandlingTimer.Elapsed += MicHandlingTimerTick;
                    micHandlingTimer.Interval = 10; // ms
                    micHandlingTimer.Enabled = true;
                    if (usingDataFrameAggregation) {
                        dataFrameAggregator = new DataFrameAggregator(voiceMgr, deviceNumber, this);
                        dataFrameAggregator.Start();
                    }
                    sourceReady = true;
                }
                catch (Exception e) {
                    log.Error(voiceMgr, "MicrophoneChannel.InitMicrophone: Exception " + e.Message + ", stack trace\n" + e.StackTrace);
                }
            }
 ///<summary>
 ///    Make sure to free the sound and the codec
 ///</summary>
 public void Dispose()
 {
     disposing = true;
     lock(voiceMgr.usingFmod) {
         sourceReady = false;
         StopRecording(channelSound);
         if (playbackStream != null) {
             playbackStream.Close();
             playbackStream = null;
             playbackFile = "";
         }
         log.Debug(voiceMgr, "BasicChannel.Dispose called");
         if (channel != null) {
             CheckRetCode("Stopping the FMOD channel in BasicChannel.Dispose()", channel.stop());
             channel = null;
         }
         if (channelSound != null) {
             CheckRetCode("Releasing the FMOD sound in BasicChannel.Dispose()", channelSound.release());
             channelSound = null;
         }
         if (channelCodec != null) {
             channelCodec.ResetEncoder();
             channelCodec = null;
         }
     }
 }