Exemplo n.º 1
1
        public Call(IPEndPoint Address)
        {
            UdpSender = new UdpClient();
            UdpSender.Connect(Address);
            this.Address = Address;

            _encoder = new SpeexEncoder(BandMode.Wide);
            SpeexProvider = new JitterBufferWaveProvider();

            SoundOut = new WaveOutEvent();
            SoundOut.Init(SpeexProvider);
            SoundOut.Play();
        }
Exemplo n.º 2
0
        // Token: 0x06004C3F RID: 19519 RVA: 0x00197D4C File Offset: 0x0019614C
        private byte[] SpeexEncode(short[] input, global::BandMode mode)
        {
            SpeexEncoder speexEncoder = null;
            int          num          = 320;

            if (mode != global::BandMode.Narrow)
            {
                if (mode != global::BandMode.Wide)
                {
                    if (mode == global::BandMode.UltraWide)
                    {
                        speexEncoder = this.m_ultrawide_enc;
                        num          = 1280;
                    }
                }
                else
                {
                    speexEncoder = this.m_wide_enc;
                    num          = 640;
                }
            }
            else
            {
                speexEncoder = this.m_narrow_enc;
                num          = 320;
            }
            byte[] @byte = USpeakPoolUtils.GetByte(num + 4);
            int    value = speexEncoder.Encode(input, 0, input.Length, @byte, 4, @byte.Length);

            byte[] bytes = BitConverter.GetBytes(value);
            Array.Copy(bytes, @byte, 4);
            return(@byte);
        }
Exemplo n.º 3
0
    public byte[] GetClipData()
    {
        float[] samples = new float[clip.samples];


        SpeexDecoder decoder = new SpeexDecoder(BandMode.Wide);

        clip.GetData(samples, 0);


        short[] outData = new short[samples.Length];



        for (int i = 0; i < samples.Length; i++)
        {
            outData[i] = (short)(samples[i] * rescaleFactor);
        }


        SpeexEncoder encoder = new SpeexEncoder(BandMode.Wide);


        byte[] encodedData = new byte[samples.Length * 2];

        encoder.Encode(outData, 0, outData.Length, encodedData, 0, encodedData.Length);

        testText = "length:" + encodedData.Length;

        return(encodedData);
    }
Exemplo n.º 4
0
    public void RecordStart()
    {
        if (playAudio.isPlaying)
        {
            this.mainDevice.log("---RecordStop---");
            playAudio.Stop();
        }

        encodeBufList   = new ArrayList();
        lastSamplePos   = 0;
        sampleIndex     = 0;
        totalSampleSize = 0;

        ShowMicrophoneList();
        recordAudio.clip = Microphone.Start(null, true, loopTIme, samplingRate);
        this.mainDevice.log("" + recordAudio.clip.length);

        ShowMicrophoneDeviceCaps(null);


        // speex
        speexEncoder     = new SpeexEncoder(BandMode.Narrow);
        speexDecoder     = new SpeexDecoder(BandMode.Narrow);
        recordSampleSize = samplingRate / (targetSamplingRate / targetSamplingSize);
        sampleBuffer     = new float[recordSampleSize];

        this.mainDevice.log("---RecordStart---");
    }
Exemplo n.º 5
0
 public SpeexChatCodec(BandMode bandMode, int sampleRate, string description)
 {
     decoder = new SpeexDecoder(bandMode);
     encoder = new SpeexEncoder(bandMode);
     recordingFormat = new WaveFormat(sampleRate, 16, 2);
     this.description = description;
     encoderInputBuffer = new WaveBuffer(recordingFormat.AverageBytesPerSecond); // more than enough
 }
Exemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Speex"/> class.
 /// </summary>
 public Speex(BandMode bandMode, int sampleRate, string description)
 {
     Decoder         = new SpeexDecoder(bandMode);
     Encoder         = new SpeexEncoder(bandMode);
     RecordingFormat = new WaveFormat(sampleRate, 16, 1);
     Description     = description;
     EncoderBuffer   = new WaveBuffer(RecordingFormat.AverageBytesPerSecond);
 }
Exemplo n.º 7
0
 protected SpeexChatCodec(BandMode bandMode, int sampleRate, string description)
 {
     decoder            = new SpeexDecoder(bandMode);
     encoder            = new SpeexEncoder(bandMode);
     recordingFormat    = new WaveFormat(sampleRate, 16, 1);
     this.description   = description;
     encoderInputBuffer = new WaveBuffer(recordingFormat.AverageBytesPerSecond); // more than enough
 }
Exemplo n.º 8
0
 public SpeexChatCodec(BandMode bandMode, int sampleRate, string description)
 {
     _decoder            = new SpeexDecoder(bandMode);
     _encoder            = new SpeexEncoder(bandMode);
     _recordingFormat    = new WaveFormat(sampleRate, 16, 1);
     Name                = description;
     _encoderInputBuffer = new WaveBuffer(_recordingFormat.AverageBytesPerSecond); // more than enough
 }
Exemplo n.º 9
0
 public SpeexCodec(BandMode mode)
     : base("Speex")
 {
     Mode = mode;
     Encoder = new SpeexEncoder(mode);
     Encoder.VBR = false;
     Encoder.Quality = 10;
     Decoder = new SpeexDecoder(mode);
 }
Exemplo n.º 10
0
 public SpeexCodec(BandMode mode)
     : base("Speex")
 {
     Mode            = mode;
     Encoder         = new SpeexEncoder(mode);
     Encoder.VBR     = false;
     Encoder.Quality = 10;
     Decoder         = new SpeexDecoder(mode);
 }
Exemplo n.º 11
0
        public Speex32kHz()
        {
            _encoder = new SpeexEncoder(BandMode.UltraWide);
            _encoder.Quality = 10;

            _decoder = new SpeexDecoder(BandMode.UltraWide);

            _buffer = new byte[SampleRate];
            _bufferLength = 0;
        }
Exemplo n.º 12
0
        private byte[] EncodeSpeech(byte[] buf, int len)
        {
            SpeexEncoder encoder = new SpeexEncoder(BandMode.Wide);

            //// convert to short array
            //short[] data = new short[len / 2];
            //int sampleIndex = 0;
            //for (int index = 0; index < len; index += 2, sampleIndex++)
            //{
            //    data[sampleIndex] = BitConverter.ToInt16(buf, index);
            //}

            //var encodedData = new byte[len];
            //// note: the number of samples per frame must be a multiple of encoder.FrameSize
            //int encodedBytes = encoder.Encode(data, 0, sampleIndex, encodedData, 0, len);
            //if (encodedBytes != 0)
            //{
            //    byte[] sizeBuf = BitConverter.GetBytes(encodedBytes);
            //    byte[] returnBuf = new byte[encodedBytes + sizeBuf.Length];
            //    sizeBuf.CopyTo(returnBuf, 0);
            //    Array.Copy(encodedData, 0, returnBuf, sizeBuf.Length, encodedBytes);
            //    return returnBuf;
            //}
            //else
            //    return buf;

            int inDataSize = len / 2;
            // convert to short array
            short[] data = new short[inDataSize];
            int sampleIndex = 0;
            for (int index = 0; index < len; index += 2, sampleIndex++)
            {
                data[sampleIndex] = BitConverter.ToInt16(buf, index);
            }

            // note: the number of samples per frame must be a multiple of encoder.FrameSize
            inDataSize = inDataSize - inDataSize % encoder.FrameSize;

            var encodedData = new byte[len];
            int encodedBytes = encoder.Encode(data, 0, inDataSize, encodedData, 0, len);
            if (encodedBytes != 0)
            {
                // each chunk is laid out as follows:
                // | 4-byte total chunk size | 4-byte encoded buffer size | <encoded-bytes> |
                byte[] inDataSizeBuf = BitConverter.GetBytes(inDataSize);
                byte[] sizeBuf = BitConverter.GetBytes(encodedBytes + inDataSizeBuf.Length);
                byte[] returnBuf = new byte[encodedBytes + sizeBuf.Length + inDataSizeBuf.Length];
                sizeBuf.CopyTo(returnBuf, 0);
                inDataSizeBuf.CopyTo(returnBuf, sizeBuf.Length);
                Array.Copy(encodedData, 0, returnBuf, sizeBuf.Length + inDataSizeBuf.Length, encodedBytes);
                return returnBuf;
            }
            else
                return buf;
        }
Exemplo n.º 13
0
        public NewCall(string CallID)
        {
            this.CallID   = CallID;
            UdpConnection = new UdpClient();
            UdpConnection.Connect("209.141.53.112", 25000);

            _encoder      = new SpeexEncoder(BandMode.Wide);
            SpeexProvider = new JitterBufferWaveProvider();

            SoundOut = new WaveOutEvent();
            SoundOut.Init(SpeexProvider);
            SoundOut.Play();
        }
Exemplo n.º 14
0
        public NewCall(string CallID)
        {
            this.CallID = CallID;
            UdpConnection = new UdpClient();
            UdpConnection.Connect("209.141.53.112", 25000);

            _encoder = new SpeexEncoder(BandMode.Wide);
            SpeexProvider = new JitterBufferWaveProvider();

            SoundOut = new WaveOutEvent();
            SoundOut.Init(SpeexProvider);
            SoundOut.Play();
        }
Exemplo n.º 15
0
        public Call(IPEndPoint Address)
        {
            UdpSender = new UdpClient();
            UdpSender.Connect(Address);
            this.Address = Address;

            _encoder      = new SpeexEncoder(BandMode.Wide);
            SpeexProvider = new JitterBufferWaveProvider();



            SoundOut = new WaveOutEvent();
            SoundOut.Init(SpeexProvider);
            SoundOut.Play();
        }
        public VoipCodecSpeex(BandMode bandMode, int sampleRate, string description, VoipCodecMode mode = VoipCodecMode.Both)
        {
            _bandMode = bandMode;
            _recordingFormat = new WaveFormat(sampleRate, 16, 1);
            _description = description;

            if (mode.HasFlag(VoipCodecMode.Decode))
                _decoder = new SpeexDecoder(bandMode);
            if (mode.HasFlag(VoipCodecMode.Encode))
            {
                _encoder = new SpeexEncoder(bandMode);
                _outputBufferTemp = new byte[_recordingFormat.AverageBytesPerSecond];
                _encoderInputBuffer = new WaveBuffer(_recordingFormat.AverageBytesPerSecond); // more than enough
            }
        }
Exemplo n.º 17
0
        public void Open(Stream targetStream, int channelCount, int sampleFrequency)
        {
            this.targetStream   = targetStream;
            speexEncoder        = new SpeexEncoder(bandMode) { Quality = quality,VBR = isVbr};

            Debug.Log("FrameSize: " + speexEncoder.FrameSize);

            audioFrameBuffer = new AudioFrameBuffer(speexEncoder.FrameSize,1,WriteCore);

            if ( encodeOgg )
            {
                oggWriter = new OggSpeexWriter(GetModeAsInt(),sampleFrequency,channelCount,1,isVbr);
                oggWriter.Open(targetStream);
                oggWriter.WriteHeader("Unity3d");
            }
        }
Exemplo n.º 18
0
        public void Open(Stream targetStream, int channelCount, int sampleFrequency)
        {
            this.targetStream = targetStream;
            speexEncoder      = new SpeexEncoder(bandMode)
            {
                Quality = quality, VBR = isVbr
            };

            Debug.Log("FrameSize: " + speexEncoder.FrameSize);

            audioFrameBuffer = new AudioFrameBuffer(speexEncoder.FrameSize, 1, WriteCore);

            if (encodeOgg)
            {
                oggWriter = new OggSpeexWriter(GetModeAsInt(), sampleFrequency, channelCount, 1, isVbr);
                oggWriter.Open(targetStream);
                oggWriter.WriteHeader("Unity3d");
            }
        }
Exemplo n.º 19
0
        private static byte[] EncodeSpeech(byte[] buf, int len)
        {
            BandMode     mode    = GetBandMode(mic.SampleRate);
            SpeexEncoder encoder = new SpeexEncoder(mode);

            // set encoding quality to lowest (which will generate the smallest size in the fastest time)
            encoder.Quality = 1;

            int inDataSize = len / 2;

            // convert to short array
            short[] data        = new short[inDataSize];
            int     sampleIndex = 0;

            for (int index = 0; index < len; index += 2, sampleIndex++)
            {
                data[sampleIndex] = BitConverter.ToInt16(buf, index);
            }

            // note: the number of samples per frame must be a multiple of encoder.FrameSize
            inDataSize = inDataSize - inDataSize % encoder.FrameSize;

            var encodedData  = new byte[len];
            int encodedBytes = encoder.Encode(data, 0, inDataSize, encodedData, 0, len);

            if (encodedBytes != 0)
            {
                // each chunk is laid out as follows:
                // | 4-byte total chunk size | 4-byte encoded buffer size | <encoded-bytes> |
                byte[] inDataSizeBuf = BitConverter.GetBytes(inDataSize);
                byte[] sizeBuf       = BitConverter.GetBytes(encodedBytes + inDataSizeBuf.Length);
                byte[] returnBuf     = new byte[encodedBytes + sizeBuf.Length + inDataSizeBuf.Length];
                sizeBuf.CopyTo(returnBuf, 0);
                inDataSizeBuf.CopyTo(returnBuf, sizeBuf.Length);
                Array.Copy(encodedData, 0, returnBuf, sizeBuf.Length + inDataSizeBuf.Length, encodedBytes);
                return(returnBuf);
            }
            else
            {
                return(buf);
            }
        }
Exemplo n.º 20
0
        public AudioContext GetAudioContext()
        {
            var resampler     = new ResampleFilter(rawAudioFormat, transmittedAudioFormat);
            var conferenceDtx = new DtxFilter(transmittedAudioFormat);

            IAudioTwoWayFilter enhancer = null;

            switch (enhancementStack)
            {
            case SpeechEnhancementStack.None:
                enhancer = new NullEchoCancelFilter(mediaConfig.ExpectedAudioLatency, mediaConfig.FilterLength, transmittedAudioFormat, AudioFormat.Default);
                break;

            case SpeechEnhancementStack.Speex:
                enhancer = new SpeexEchoCanceller2(mediaConfig, transmittedAudioFormat, AudioFormat.Default);
                break;

            case SpeechEnhancementStack.WebRtc:
                enhancer = new WebRtcFilter(mediaConfig.ExpectedAudioLatency, mediaConfig.FilterLength, transmittedAudioFormat, AudioFormat.Default, mediaConfig.EnableAec, mediaConfig.EnableDenoise, mediaConfig.EnableAgc);
                break;
            }

            IAudioEncoder encoder = null;

            switch (codecType)
            {
            case AudioCodecType.G711M:
                encoder = new G711MuLawEncoder(transmittedAudioFormat);
                break;

            case AudioCodecType.Speex:
                encoder = new SpeexEncoder(transmittedAudioFormat);
                break;
            }

            var ctx = new AudioContext(transmittedAudioFormat, resampler, conferenceDtx, enhancer, encoder);

            return(ctx);
        }
Exemplo n.º 21
0
        public MainViewModel()
        {
            listenAddress = "net.tcp://localhost:8001/SpeexStreamer";

            encoder = new SpeexEncoder(BandMode.Narrow);
            //encoder.VBR = true;
            //encoder.DTX = true;
            //todo: preProcessor = new NativePreprocessor(encoder.FrameSize, 16000);// { AGC = true, AGCIncrement = 1, MaxAGCGain = 10, Denoise = true, VAD = true };

            //encoder = new NativeEncoder((EncodingMode) 1);
            //encoder.VBR = false;
            //encoder.DTX = false;

            waveFormat = new WaveFormat(encoder.FrameSize * 50, 16, 1);
            waveIn     = new WaveIn {
                WaveFormat = waveFormat, BufferMillisconds = 40, NumberOfBuffers = 2
            };
            //waveIn = new WasapiCapture();

            waveIn.DataAvailable    += waveIn_DataAvailable;
            waveIn.RecordingStopped += waveIn_RecordingStopped;

            sender = new DuplexChannelFactory <ISpeexStreamer>(this, "NetTcpBinding_ISpeexStreamer").CreateChannel();

            waveProvider = new JitterBufferWaveProvider();
            waveProvider.VolumeUpdated += HandleVolumeUpdated;

            //waveOut = new DirectSoundOut(40);
            //waveOut = new WaveOut();
            waveOut = new WasapiOut(AudioClientShareMode.Shared, 20);

            waveOut.Init(waveProvider);
            waveOut.PlaybackStopped += waveOut_PlaybackStopped;
            waveOut.Volume           = 1.0f;

            StartCommand  = new DelegateCommand <object>(DoStartRecording, obj => !IsRecording);
            ListenCommand = new DelegateCommand <object>(DoListen, obj => !IsPlaying);
        }
Exemplo n.º 22
0
        /// <summary>
        /// Creates a new instance of the AudioContextFactory.
        /// </summary>
        /// <param name="rawAudioFormat">The format in which the audio coming directly from the microphone is recorded</param>
        /// <param name="playedAudioFormat">The format in which the audio will be played back on the far end (typically 16Khz)</param>
        /// <param name="config">The currently active MediaConfig instance</param>
        /// <param name="mediaEnvironment">An IMediaEnvironment instance which can be used to make decisions about which context to return, for instance,
        /// if the CPU is running too hot, or multiple people have joined the conference.</param>
        public AudioContextFactory(AudioFormat rawAudioFormat, AudioFormat playedAudioFormat, MediaConfig config, IMediaEnvironment mediaEnvironment)
        {
            RawAudioFormat    = rawAudioFormat;
            PlayedAudioFormat = playedAudioFormat;
            MediaConfig       = config;
            MediaEnvironment  = mediaEnvironment;

            // What we should use when there's only one other person, and CPU is OK:
            // 16Khz, Speex, WebRtc at full strength
            var directAudioFormat = new AudioFormat();
            var directResampler   = new ResampleFilter(rawAudioFormat, directAudioFormat);

            directResampler.InstanceName = "High Quality Direct Resampler";
            var directEnhancer = new WebRtcFilter(config.ExpectedAudioLatency, config.FilterLength, directAudioFormat, playedAudioFormat, config.EnableAec, config.EnableDenoise, config.EnableAgc);

            directEnhancer.InstanceName = "High";
            var directDtx     = new DtxFilter(directAudioFormat);
            var directEncoder = new SpeexEncoder(directAudioFormat);

            HighQualityDirectCtx             = new AudioContext(directAudioFormat, directResampler, directDtx, directEnhancer, directEncoder);
            HighQualityDirectCtx.Description = "High Quality Direct";

            // What we should use when there are multiple people (and hence the audio will need to be decoded and mixed), but CPU is OK:
            // 8Khz, G711, WebRtc at full strength
            var conferenceAudioFormat = new AudioFormat(AudioConstants.NarrowbandSamplesPerSecond);
            var conferenceResampler   = new ResampleFilter(rawAudioFormat, conferenceAudioFormat);

            conferenceResampler.InstanceName = "High Quality Conference Resampler";
            var conferenceEnhancer = new WebRtcFilter(config.ExpectedAudioLatency, config.FilterLength, conferenceAudioFormat, playedAudioFormat, config.EnableAec, config.EnableDenoise, config.EnableAgc);

            conferenceEnhancer.InstanceName = "Medium";
            var conferenceDtx     = new DtxFilter(conferenceAudioFormat);
            var conferenceEncoder = new G711MuLawEncoder(conferenceAudioFormat);

            HighQualityConferenceCtx             = new AudioContext(conferenceAudioFormat, conferenceResampler, conferenceDtx, conferenceEnhancer, conferenceEncoder);
            HighQualityConferenceCtx.Description = "High Quality Conference";

            // What we should use when one or more remote CPU's isn't keeping up (regardless of how many people are in the room):
            // 8Khz, G711, WebRtc at full-strength
            var remoteFallbackAudioFormat = new AudioFormat(AudioConstants.NarrowbandSamplesPerSecond);
            var remoteFallbackResampler   = new ResampleFilter(rawAudioFormat, remoteFallbackAudioFormat);

            remoteFallbackResampler.InstanceName = "Low Quality Remote CPU Resampler";
            var remoteFallbackEnhancer = new WebRtcFilter(config.ExpectedAudioLatency, config.FilterLength, remoteFallbackAudioFormat, playedAudioFormat, config.EnableAec, config.EnableDenoise, config.EnableAgc);

            remoteFallbackEnhancer.InstanceName = "Medium";
            var remoteFallbackDtx     = new DtxFilter(remoteFallbackAudioFormat);
            var remoteFallbackEncoder = new G711MuLawEncoder(remoteFallbackAudioFormat);

            LowQualityForRemoteCpuCtx             = new AudioContext(remoteFallbackAudioFormat, remoteFallbackResampler, remoteFallbackDtx, remoteFallbackEnhancer, remoteFallbackEncoder);
            LowQualityForRemoteCpuCtx.Description = "Fallback for remote high CPU";

            // What we should use when the local CPU isn't keeping up (regardless of how many people are in the room):
            // 8Khz, G711, WebRtc at half-strength
            var fallbackAudioFormat = new AudioFormat(AudioConstants.NarrowbandSamplesPerSecond);
            var fallbackResampler   = new ResampleFilter(rawAudioFormat, fallbackAudioFormat);

            fallbackResampler.InstanceName = "Low Quality Local CPU Resampler";
            var fallbackEnhancer = new WebRtcFilter(config.ExpectedAudioLatencyFallback, config.FilterLengthFallback, fallbackAudioFormat, playedAudioFormat, config.EnableAec, false, false);

            fallbackEnhancer.InstanceName = "Low";
            var fallbackDtx     = new DtxFilter(fallbackAudioFormat);
            var fallbackEncoder = new G711MuLawEncoder(fallbackAudioFormat);

            LowQualityForLocalCpuCtx             = new AudioContext(fallbackAudioFormat, fallbackResampler, fallbackDtx, fallbackEnhancer, fallbackEncoder);
            LowQualityForLocalCpuCtx.Description = "Fallback for local high CPU";

            _audioContextAdapter = new EnvironmentAdapter <AudioContext>(mediaEnvironment,
                                                                         HighQualityDirectCtx,
                                                                         HighQualityConferenceCtx,
                                                                         LowQualityForRemoteCpuCtx,
                                                                         LowQualityForLocalCpuCtx);
        }
Exemplo n.º 23
0
        private static byte[] EncodeSpeech(byte[] buf, int len)
        {
            BandMode mode = GetBandMode(mic.SampleRate);
            SpeexEncoder encoder = new SpeexEncoder(mode);

            // set encoding quality to lowest (which will generate the smallest size in the fastest time)
            encoder.Quality = 1;

            int inDataSize = len / 2;
            // convert to short array
            short[] data = new short[inDataSize];
            int sampleIndex = 0;
            for (int index = 0; index < len; index += 2, sampleIndex++)
            {
                data[sampleIndex] = BitConverter.ToInt16(buf, index);
            }

            // note: the number of samples per frame must be a multiple of encoder.FrameSize
            inDataSize = inDataSize - inDataSize % encoder.FrameSize;

            var encodedData = new byte[len];
            int encodedBytes = encoder.Encode(data, 0, inDataSize, encodedData, 0, len);
            if (encodedBytes != 0)
            {
                // each chunk is laid out as follows:
                // | 4-byte total chunk size | 4-byte encoded buffer size | <encoded-bytes> |
                byte[] inDataSizeBuf = BitConverter.GetBytes(inDataSize);
                byte[] sizeBuf = BitConverter.GetBytes(encodedBytes + inDataSizeBuf.Length);
                byte[] returnBuf = new byte[encodedBytes + sizeBuf.Length + inDataSizeBuf.Length];
                sizeBuf.CopyTo(returnBuf, 0);
                inDataSizeBuf.CopyTo(returnBuf, sizeBuf.Length);
                Array.Copy(encodedData, 0, returnBuf, sizeBuf.Length + inDataSizeBuf.Length, encodedBytes);
                return returnBuf;
            }
            else
                return buf;
        }
Exemplo n.º 24
0
 void Awake()
 {
     this.encoder = new SpeexEncoder(bandMode);
     this.decoder = new SpeexDecoder(bandMode);
 }
Exemplo n.º 25
0
 public ObservableSpeexEncoder(int bufferSize, BandMode mode)
 {
     _encoder            = new SpeexEncoder(mode);
     _encoderInputBuffer = new WaveBuffer(bufferSize);
 }
Exemplo n.º 26
0
 public EncodeSubject(BandMode mode)
 {
     _encoder = new SpeexEncoder(mode);
 }
Exemplo n.º 27
0
        public void CreateAudioContexts()
        {
            _captureSource.VideoCaptureDevice = null;
            if (_captureSource.AudioCaptureDevice == null)
            {
                _captureSource.AudioCaptureDevice = CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice();
                if (_captureSource.AudioCaptureDevice == null)
                {
                    throw new InvalidOperationException("No suitable audio capture device was found");
                }
            }
            MediaDeviceConfig.SelectBestAudioFormat(_captureSource.AudioCaptureDevice);
            _captureSource.AudioCaptureDevice.AudioFrameSize = AudioFormat.Default.MillisecondsPerFrame;             // 20 milliseconds
            var desiredFormat  = _captureSource.AudioCaptureDevice.DesiredFormat;
            var rawAudioFormat = new AudioFormat(desiredFormat.SamplesPerSecond, AudioFormat.Default.MillisecondsPerFrame, desiredFormat.Channels, desiredFormat.BitsPerSample);

            var playedAudioFormat = new AudioFormat();
            var config            = MediaConfig.Default;

            // Absolutely bare minimum processing - doesn't process sound at all.
            var nullAudioFormat = new AudioFormat();
            var nullResampler   = new ResampleFilter(rawAudioFormat, nullAudioFormat);

            nullResampler.InstanceName = "Null resample filter";
            var nullEnhancer = new NullEchoCancelFilter(config.ExpectedAudioLatency, config.FilterLength, nullAudioFormat, playedAudioFormat);

            nullEnhancer.InstanceName = "Null";
            var nullDtx          = new NullAudioInplaceFilter();
            var nullEncoder      = new NullAudioEncoder();
            var nullAudioContext = new AudioContext(nullAudioFormat, nullResampler, nullDtx, nullEnhancer, nullEncoder);

            nullAudioContext.Description = "Null";

            // What we should use when there's only one other person, and CPU is OK:
            // 16Khz, Speex, WebRtc at full strength
            var directAudioFormat = new AudioFormat();
            var directResampler   = new ResampleFilter(rawAudioFormat, directAudioFormat);

            directResampler.InstanceName = "Direct high quality resample filter";
            var directEnhancer = new WebRtcFilter(config.ExpectedAudioLatency, config.FilterLength, directAudioFormat, playedAudioFormat, config.EnableAec, config.EnableDenoise, config.EnableAgc);

            directEnhancer.InstanceName = "High";
            var directDtx          = new DtxFilter(directAudioFormat);
            var directEncoder      = new SpeexEncoder(directAudioFormat);
            var directAudioContext = new AudioContext(directAudioFormat, directResampler, directDtx, directEnhancer, directEncoder);

            directAudioContext.Description = "High Quality Direct";

            // What we should use when there are multiple people (and hence the audio will need to be decoded and mixed), but CPU is OK:
            // 8Khz, G711, WebRtc at full strength
            var conferenceAudioFormat = new AudioFormat(AudioConstants.NarrowbandSamplesPerSecond);
            var conferenceResampler   = new ResampleFilter(rawAudioFormat, conferenceAudioFormat);

            conferenceResampler.InstanceName = "Conference high quality resample filter";
            var conferenceEnhancer = new WebRtcFilter(config.ExpectedAudioLatency, config.FilterLength, conferenceAudioFormat, playedAudioFormat, config.EnableAec, config.EnableDenoise, config.EnableAgc);

            conferenceEnhancer.InstanceName = "Medium";
            var conferenceDtx          = new DtxFilter(conferenceAudioFormat);
            var conferenceEncoder      = new G711MuLawEncoder(conferenceAudioFormat);
            var conferenceAudioContext = new AudioContext(conferenceAudioFormat, conferenceResampler, conferenceDtx, conferenceEnhancer, conferenceEncoder);

            conferenceAudioContext.Description = "High Quality Conference";

            // What we should use when one or more remote CPU's isn't keeping up (regardless of how many people are in the room):
            // 8Khz, G711, WebRtc at full-strength
            var remoteFallbackAudioFormat = new AudioFormat(AudioConstants.NarrowbandSamplesPerSecond);
            var remoteFallbackResampler   = new ResampleFilter(rawAudioFormat, remoteFallbackAudioFormat);

            remoteFallbackResampler.InstanceName = "Fallback remote high cpu resample filter";
            var remoteFallbackEnhancer = new WebRtcFilter(config.ExpectedAudioLatency, config.FilterLength, remoteFallbackAudioFormat, playedAudioFormat, config.EnableAec, config.EnableDenoise, config.EnableAgc);

            remoteFallbackEnhancer.InstanceName = "Medium";
            var remoteFallbackDtx          = new DtxFilter(remoteFallbackAudioFormat);
            var remoteFallbackEncoder      = new G711MuLawEncoder(remoteFallbackAudioFormat);
            var remoteFallbackAudioContext = new AudioContext(remoteFallbackAudioFormat, remoteFallbackResampler, remoteFallbackDtx, remoteFallbackEnhancer, remoteFallbackEncoder);

            remoteFallbackAudioContext.Description = "Fallback for remote high CPU";

            // What we should use when the local CPU isn't keeping up (regardless of how many people are in the room):
            // 8Khz, G711, WebRtc at half-strength
            var fallbackAudioFormat = new AudioFormat(AudioConstants.NarrowbandSamplesPerSecond);
            var fallbackResampler   = new ResampleFilter(rawAudioFormat, fallbackAudioFormat);

            fallbackResampler.InstanceName = "Fallback resample filter";
            var fallbackEnhancer = new WebRtcFilter(config.ExpectedAudioLatencyFallback, config.FilterLengthFallback, fallbackAudioFormat, playedAudioFormat, config.EnableAec, false, false);

            fallbackEnhancer.InstanceName = "Low";
            var fallbackDtx          = new DtxFilter(fallbackAudioFormat);
            var fallbackEncoder      = new G711MuLawEncoder(fallbackAudioFormat);
            var fallbackAudioContext = new AudioContext(fallbackAudioFormat, fallbackResampler, fallbackDtx, fallbackEnhancer, fallbackEncoder);

            fallbackAudioContext.Description = "Fallback for local high CPU";

            AudioContextCollection.Clear();
            AudioContextCollection.Add(nullAudioContext);
            AudioContextCollection.Add(directAudioContext);
            AudioContextCollection.Add(conferenceAudioContext);
            AudioContextCollection.Add(remoteFallbackAudioContext);
            AudioContextCollection.Add(fallbackAudioContext);

            CurrentAudioContext = nullAudioContext;
        }
Exemplo n.º 28
0
        public void FileInfoTest()
        {
            const string name       = "male.wav";
            const int    HEAD_SIZE  = 8;
            const string CHUNK_DATA = "data";

            FileInfo file = GetWavFile(name);

            Console.WriteLine("file name:" + file.FullName);
            FileStream fs = File.OpenRead(file.FullName);

            byte[] tmp = new byte[2560];
            fs.Read(tmp, 0, HEAD_SIZE + 4);

            String ckID = LittleEndian.ReadString(tmp, 0, 4);

            Console.WriteLine("ckID,0-3:" + ckID);
            int cksize = LittleEndian.ReadInt(tmp, 4);

            Console.WriteLine("cksize,4-7:" + cksize);
            String WAVEID = LittleEndian.ReadString(tmp, 8, 4);

            Console.WriteLine("WAVEID,8-11:" + WAVEID);

            fs.Read(tmp, 0, HEAD_SIZE);
            String chunk = LittleEndian.ReadString(tmp, 0, 4);
            int    size  = LittleEndian.ReadInt(tmp, 4);

            //
            short format          = 0;
            short nChannels       = 0;
            int   nSamplesPerSec  = 0;
            int   nAvgBytesPerSec = 0;
            short nBlockAlign     = 0;
            short wBitsPerSample  = 0;

            while (!chunk.Equals(CHUNK_DATA))
            {
                Console.WriteLine("chunk:" + chunk);
                Console.WriteLine("size:" + size);
                // read size bytes
                fs.Read(tmp, 0, size);
                format = LittleEndian.ReadShort(tmp, 0);
                Console.WriteLine("format,0-1:" + format);

                nChannels = LittleEndian.ReadShort(tmp, 2);
                Console.WriteLine("nChannels,2-3:" + nChannels);

                nSamplesPerSec = LittleEndian.ReadInt(tmp, 4);
                Console.WriteLine("nSamplesPerSec,4-7:" + nSamplesPerSec);

                nAvgBytesPerSec = LittleEndian.ReadInt(tmp, 8);
                Console.WriteLine("nAvgBytesPerSec:" + nAvgBytesPerSec);

                nBlockAlign = LittleEndian.ReadShort(tmp, 12);
                Console.WriteLine("nBlockAlign:" + nBlockAlign);

                wBitsPerSample = LittleEndian.ReadShort(tmp, 14);
                Console.WriteLine("wBitsPerSample:" + wBitsPerSample);

                fs.Read(tmp, 0, HEAD_SIZE);
                chunk = LittleEndian.ReadString(tmp, 0, 4);
                size  = LittleEndian.ReadInt(tmp, 4);
                Console.WriteLine("chunk:" + chunk);
                Console.WriteLine("size:" + size);
            }

            BandMode mode;

            if (nSamplesPerSec < 12000)
            {
                // Narrowband
                mode = BandMode.Narrow;
            }
            else if (nSamplesPerSec < 24000)
            {
                // Wideband
                mode = BandMode.Wide;
            }
            else
            {
                //Ultra-wideband
                mode = BandMode.UltraWide;
            }

            SpeexEncoder speexEncoder     = new SpeexEncoder(mode);
            int          quality          = 2; // modify your self
            int          framesPerPackage = 4; // one frame one package
            bool         vbr = false;



            //            speexEncoder.getEncoder().setComplexity(5);
            //            speexEncoder.getEncoder().setBitRate();
            //            speexEncoder.getEncoder().setVbr();
            //            speexEncoder.getEncoder().setVbrQuality();
            //            speexEncoder.getEncoder().setVad();
            //            speexEncoder.getEncoder().setDtx();

            OggSpeexWriter2 writer = new OggSpeexWriter2(mode, nSamplesPerSec, nChannels, framesPerPackage, vbr, nAvgBytesPerSec);

            writer.Open("result.spx");
            writer.WriteHeader("alking");
            int pcmPacketSize = 2 * nChannels * speexEncoder.FrameSize;
            int bytesCount    = 0;



            while (bytesCount < size)
            {
                int read = pcmPacketSize * framesPerPackage;
                fs.Read(tmp, 0, read);
                short[] data = new short[read / 2];
                Buffer.BlockCopy(tmp, 0, data, 0, read);

                int encSize = speexEncoder.Encode(data, 0, data.Length, tmp, 0, tmp.Length);
                if (encSize > 0)
                {
                    writer.WritePackage(tmp, 0, encSize);
                }
                bytesCount += read;
            }
            writer.Close();
            fs.Close();
        }
Exemplo n.º 29
0
 void Awake()
 {
     this.encoder = new SpeexEncoder(bandMode);
     this.decoder = new SpeexDecoder(bandMode);
 }
Exemplo n.º 30
0
 /// <summary>
 /// 初始化。
 /// </summary>
 public Speex()
 {
     _encoder = new SpeexEncoder(BandMode.Narrow);
     _decoder = new SpeexDecoder(BandMode.Narrow);
 }