Пример #1
0
        public Opus()
        {
            decoder = OpusDecoder.Create(48000, 1);

            encoder         = OpusEncoder.Create(48000, 1, OpusApplication.OPUS_APPLICATION_VOIP);
            encoder.Bitrate = 12000;
        }
Пример #2
0
        public byte[] Encode(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (this.encoder == null)
            {
                lock (this.codecLock) {
                    if (this.encoder == null)
                    {
                        this.encoder         = OpusEncoder.Create(this.settings.SampleRate, this.settings.Channels, Application.Voip);
                        this.encoder.Bitrate = this.settings.Bitrate;
                    }
                }
            }

            int encodedLength;

            byte[] encoded = this.encoder.Encode(data, data.Length, out encodedLength);
            if (encoded.Length != encodedLength)
            {
                byte[] final = new byte[encodedLength];
                Buffer.BlockCopy(encoded, 0, final, 0, encodedLength);
                encoded = final;
            }

            return(encoded);
        }
Пример #3
0
 public Opus()
 {
     _decoder                = OpusDecoder.Create(SAMPLE_RATE, CHANNEL_COUNT);
     _encoder                = OpusEncoder.Create(SAMPLE_RATE, CHANNEL_COUNT, OpusPInvoke.Application.Voip);
     _encoder.InbandFec      = true;
     _encoder.LossPercentage = 30;
 }
Пример #4
0
        void StartEncoding()
        {
            _startTime       = DateTime.Now;
            _bytesSent       = 0;
            _segmentFrames   = 960;
            _encoder         = OpusEncoder.Create(48000, 1, FragLabs.Audio.Codecs.Opus.Application.Voip);
            _encoder.Bitrate = 8192;
            _decoder         = OpusDecoder.Create(48000, 1);
            _bytesPerSegment = _encoder.FrameByteCount(_segmentFrames);

            _waveIn = new WaveIn(WaveCallbackInfo.FunctionCallback());
            _waveIn.BufferMilliseconds = 50;
            _waveIn.DeviceNumber       = comboBox1.SelectedIndex;
            _waveIn.DataAvailable     += _waveIn_DataAvailable;
            _waveIn.WaveFormat         = new WaveFormat(48000, 16, 1);

            _playBuffer = new BufferedWaveProvider(new WaveFormat(48000, 16, 1));

            _waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());
            _waveOut.DeviceNumber = comboBox2.SelectedIndex;
            _waveOut.Init(_playBuffer);

            _waveOut.Play();
            _waveIn.StartRecording();

            if (_timer == null)
            {
                _timer          = new Timer();
                _timer.Interval = 1000;
                _timer.Tick    += _timer_Tick;
            }
            _timer.Start();
        }
Пример #5
0
        public void StartPreview(int mic, MMDevice speakers)
        {
            try
            {
                _waveOut = new WasapiOut(speakers, AudioClientShareMode.Shared, true, 30);

                _buffBufferedWaveProvider           = new BufferedWaveProvider(new WaveFormat(AudioManager.INPUT_SAMPLE_RATE, 16, 1));
                _buffBufferedWaveProvider.ReadFully = true;

                RadioFilter filter = new RadioFilter(_buffBufferedWaveProvider.ToSampleProvider());

                //add final volume boost to all mixed audio
                _volumeSampleProvider        = new VolumeSampleProviderWithPeak(filter, (peak => SpeakerMax = peak));
                _volumeSampleProvider.Volume = SpeakerBoost;

                _waveOut.Init(_volumeSampleProvider);

                _waveOut.Play();
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error starting audio Output - Quitting! " + ex.Message);

                MessageBox.Show($"Problem Initialising Audio Output! Try a different Output device and please post your client log on the forums", "Audio Output Error", MessageBoxButton.OK,
                                MessageBoxImage.Error);

                Environment.Exit(1);
            }

            try
            {
                //opus
                _encoder = OpusEncoder.Create(AudioManager.INPUT_SAMPLE_RATE, 1, FragLabs.Audio.Codecs.Opus.Application.Voip);
                _encoder.ForwardErrorCorrection = false;
                _decoder = OpusDecoder.Create(AudioManager.INPUT_SAMPLE_RATE, 1);
                _decoder.ForwardErrorCorrection = false;

                _waveIn = new WaveIn(WaveCallbackInfo.FunctionCallback())
                {
                    BufferMilliseconds = AudioManager.INPUT_AUDIO_LENGTH_MS,
                    DeviceNumber       = mic
                };

                _waveIn.NumberOfBuffers = 2;
                _waveIn.DataAvailable  += _waveIn_DataAvailable;
                _waveIn.WaveFormat      = new WaveFormat(AudioManager.INPUT_SAMPLE_RATE, 16, 1);

                _waveIn.StartRecording();
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error starting audio Input - Quitting! " + ex.Message);

                MessageBox.Show($"Problem Initialising Audio Input! Try a different Input device and please post your client log on the forums", "Audio Input Error", MessageBoxButton.OK,
                                MessageBoxImage.Error);

                Environment.Exit(1);
            }
        }
Пример #6
0
 public CSCoreRecorder(MMDevice captureDevice, bool triggerSingleBlockRead, int bitrate, Application application)
 {
     _captureDevice          = captureDevice;
     _triggerSingleBlockRead = triggerSingleBlockRead;
     _opusEncoder            = OpusEncoder.Create(48000, 1, application);
     _opusEncoder.Bitrate    = bitrate;
     _bytesPerSegment        = _opusEncoder.FrameByteCount(SegmentFrames);
 }
Пример #7
0
        public Voice()
        {
            OnStatusChanged += EventOnStatusChanged;

            _decoder         = OpusDecoder.Create((int)SampleRate, 1);
            _encoder         = OpusEncoder.Create((int)SampleRate, 1, Application.Voip);
            _encoder.Bitrate = 8192 * 2;
            _bytesPerSegment = _encoder.FrameByteCount(StreamSize);
        }
 public OpusEncoderManager(AudioOutputWriter aout, int sampleRate)
 {
     this.aout        = aout;
     _bytesSent       = 0;
     _segmentFrames   = GlobalConfiguration.samplesPerPacket; //1024; //960;
     mEncoder         = OpusEncoder.Create(sampleRate, 1, FragLabs.Audio.Codecs.Opus.Application.Voip);
     mEncoder.Bitrate = GlobalConfiguration.encoderBps;       // 1KB/sec が最低値のようだ
     _bytesPerSegment = mEncoder.FrameByteCount(_segmentFrames);
 }
Пример #9
0
        public ConcentusCodec()
        {
            _encoder = OpusEncoder.Create(48000, 1, OpusApplication.OPUS_APPLICATION_AUDIO);

            SetBitrate(_bitrate);
            SetComplexity(_complexity);
            SetVBRMode(_vbr, _cvbr);
            _encoder.EnableAnalysis = true;
            _decoder = OpusDecoder.Create(48000, 1);
        }
Пример #10
0
        private Stream CompileMMX2(Stream basestream)
        {
            var mem = new MemoryStream();

            using (var writer = new BinaryWriter(mem, Encoding.ASCII, true))
                using (var wav = new WaveFileReader(basestream))
                    using (var res = new MediaFoundationResampler(wav, new WaveFormat(
                                                                      wav.WaveFormat.SampleRate < 24000 ? 24000 : 48000
                                                                      , 1)))
                    {
                        var opus = OpusEncoder.Create(res.WaveFormat.SampleRate, 1, FragLabs.Audio.Codecs.Opus.Application.Voip);
                        opus.Bitrate = PPeXCore.Settings.XggBitrate;
                        int packetsize = (int)(res.WaveFormat.SampleRate * 2 * PPeXCore.Settings.XggFrameSize);

                        writer.Write(Encoding.ASCII.GetBytes("XGG"));
                        writer.Write(Version);
                        writer.Write(packetsize);
                        writer.Write(opus.Bitrate);

                        long   oldpos = mem.Position;
                        ushort count  = 0;
                        writer.Write(count);

                        byte[] uncompressed = new byte[10000 + (res.WaveFormat.AverageBytesPerSecond / wav.WaveFormat.AverageBytesPerSecond) * wav.Length];

                        byte[] buffer = new byte[packetsize];
                        int    result = res.Read(buffer, 0, packetsize);
                        int    offset = 0;
                        while (result > 0)
                        {
                            /*
                             * count++;
                             * int outlen = 0;
                             * byte[] output = opus.Encode(buffer, result / 2, out outlen);
                             * writer.Write((uint)outlen);
                             * writer.Write(output, 0, outlen);*/

                            Array.Copy(buffer, 0, uncompressed, offset, result);
                            offset += result;

                            result = res.Read(buffer, 0, packetsize);
                        }


                        mem.Position = oldpos;
                        writer.Write(count);
                    }

            mem.Position = 0;
            _size        = (uint)mem.Length;
            return(mem);
        }
Пример #11
0
        public byte[] OpusEncoding(short[] inputAudioSamples)
        {
            OpusEncoder encoder = OpusEncoder.Create(48000, 1, OpusApplication.OPUS_APPLICATION_AUDIO);

            encoder.Bitrate = 12000;

            // Encoding loop
            byte[] outputBuffer   = new byte[1000];
            int    frameSize      = 960;
            int    thisPacketSize = encoder.Encode(inputAudioSamples, 0, frameSize, outputBuffer, 0, outputBuffer.Length); // this throws OpusException on a failure, rather than returning a negative number

            return(outputBuffer);
        }
        // Allows OverlordBot to listen for a specific word to start listening. Currently not used although the setup has all been done.
        // This is due to wierd state transition errors that I cannot be bothered to debug. Possible benefit is less calls to Speech endpoint but
        // not sure if that is good enough or not to keep investigating.
        //private readonly KeywordRecognitionModel _wakeWord;

        public SpeechRecognitionListener(BufferedWaveProvider bufferedWaveProvider, ConcurrentQueue <byte[]> responseQueue, RadioInformation radioInfo)
        {
            radioInfo.TransmissionQueue = responseQueue;
            _botType   = radioInfo.botType;
            _frequency = radioInfo.freq;
            _callsign  = radioInfo.callsign;

            _logClientId = radioInfo.name;

            switch (radioInfo.botType)
            {
            case "ATC":
                Controller = new AtcController
                {
                    Callsign = radioInfo.callsign,
                    Voice    = radioInfo.voice,
                    Radio    = radioInfo
                };
                break;

            case "AWACS":
                Controller = new AwacsController
                {
                    Callsign = radioInfo.callsign,
                    Voice    = radioInfo.voice,
                    Radio    = radioInfo
                };
                break;

            default:
                Controller = new MuteController
                {
                    Callsign = radioInfo.callsign,
                    Voice    = null,
                    Radio    = null
                };
                break;
            }

            var encoder = OpusEncoder.Create(AudioManager.InputSampleRate, 1, Application.Voip);

            encoder.ForwardErrorCorrection = false;
            encoder.FrameByteCount(AudioManager.SegmentFrames);

            var streamReader = new BufferedWaveProviderStreamReader(bufferedWaveProvider);

            _audioConfig = AudioConfig.FromStreamInput(streamReader, AudioStreamFormat.GetWaveFormatPCM(16000, 16, 1));

            //_wakeWord = KeywordRecognitionModel.FromFile($"Overlord/WakeWords/{callsign}.table");
        }
Пример #13
0
        public void Record_OPUS(int device_num, string ip)
        {
            network.Initializecon(ip);
            startTime       = DateTime.Now;
            bytesSent       = 0;
            segmentFrames   = 960;
            encoder         = OpusEncoder.Create(48000, 1, FragLabs.Audio.Codecs.Opus.Application.Voip);
            encoder.Bitrate = 8192;
            bytesPerSegment = encoder.FrameByteCount(segmentFrames);

            waveIn = new WaveIn(WaveCallbackInfo.FunctionCallback());
            waveIn.BufferMilliseconds = 50;
            waveIn.DeviceNumber       = device_num;
            waveIn.DataAvailable     += waveIn_DataAvailableEvent;
            waveIn.WaveFormat         = new WaveFormat(48000, 16, 1);
            waveIn.StartRecording();
        }
Пример #14
0
 // Token: 0x06004C38 RID: 19512 RVA: 0x00197928 File Offset: 0x00195D28
 private void CreateEncoders()
 {
     try
     {
         this._encoder         = OpusEncoder.Create(this._frequency, 1, this._app);
         this._encoder.Bitrate = this._bitrate;
         this._bytesPerSegment = this._encoder.FrameByteCount(this._segmentFrames);
         this._decoder         = OpusDecoder.Create(this._frequency, 1, this._bytesPerSegment);
         this._encoder.ForwardErrorCorrection = false;
         this._decoder.ForwardErrorCorrection = false;
         this._encoder.ExpectedPacketLossPct  = 0;
         this.isInitialized = true;
     }
     catch (DllNotFoundException ex)
     {
         throw ex;
     }
 }
Пример #15
0
        public void StartEncoding()
        {
            BotAudioProvider.StartListening();
            StartResponseCheckLoop();

            try
            {
                //opus
                _encoder = OpusEncoder.Create(InputSampleRate, 1, Application.Voip);
                _encoder.ForwardErrorCorrection = false;
                _decoder = OpusDecoder.Create(InputSampleRate, 1);
                _decoder.ForwardErrorCorrection = false;
            }
            catch (Exception ex)
            {
                Logger.Error(ex, $"{LogClientId}| Error starting audio Output - Quitting! " + ex.Message);

                Environment.Exit(1);
            }
        }
Пример #16
0
        public static void Main(string[] args)
        {
            string opusfile = @"C:\Users\Logan Stromberg\Desktop\Prisencolinensinainciusol.opus";
            string rawFile  = @"C:\Users\Logan Stromberg\Desktop\Prisencolinensinainciusol.raw";
            string rawFile2 = @"C:\Users\Logan Stromberg\Desktop\Prisencolinensinainciusol_out.raw";

            using (FileStream fileOut = new FileStream(opusfile, FileMode.Create))
            {
                OpusEncoder encoder = OpusEncoder.Create(48000, 2, OpusApplication.OPUS_APPLICATION_AUDIO);
                encoder.Bitrate = 96000;

                OpusTags tags = new OpusTags();
                tags.Fields[OpusTagName.Title]  = "Prisencolinensinainciusol";
                tags.Fields[OpusTagName.Artist] = "Adriano Celetano";
                OpusOggWriteStream oggOut = new OpusOggWriteStream(encoder, fileOut, tags);

                byte[]  allInput = File.ReadAllBytes(rawFile);
                short[] samples  = BytesToShorts(allInput);

                oggOut.WriteSamples(samples, 0, samples.Length);
                oggOut.Finish();
            }

            using (FileStream fileIn = new FileStream(opusfile, FileMode.Open))
            {
                using (FileStream fileOut = new FileStream(rawFile2, FileMode.Create))
                {
                    OpusDecoder       decoder = OpusDecoder.Create(48000, 2);
                    OpusOggReadStream oggIn   = new OpusOggReadStream(decoder, fileIn);
                    while (oggIn.HasNextPacket)
                    {
                        short[] packet = oggIn.DecodeNextPacket();
                        if (packet != null)
                        {
                            byte[] binary = ShortsToBytes(packet);
                            fileOut.Write(binary, 0, binary.Length);
                        }
                    }
                }
            }
        }
Пример #17
0
        public AudioEncoder(Codec codec)
        {
            Util.Init(ref opusQueue);
            Codec = codec;

            switch (codec)
            {
            case Codec.SpeexNarrowband:
                throw new NotSupportedException();

            case Codec.SpeexWideband:
                throw new NotSupportedException();

            case Codec.SpeexUltraWideband:
                throw new NotSupportedException();

            case Codec.CeltMono:
                throw new NotSupportedException();

            case Codec.OpusVoice:
                SampleRate  = 48000;
                Channels    = 1;
                opusEncoder = OpusEncoder.Create(SampleRate, Channels, Application.Voip);
                Bitrate     = 8192 * 2;
                break;

            case Codec.OpusMusic:
                SampleRate  = 48000;
                Channels    = 2;
                opusEncoder = OpusEncoder.Create(SampleRate, Channels, Application.Audio);
                Bitrate     = 8192 * 4;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(codec));
            }

            BitsPerSample     = 16;
            OptimalPacketSize = opusEncoder.FrameByteCount(SegmentFrames);
            segment           = new byte[OptimalPacketSize];
        }
Пример #18
0
        protected Stream SerialAudioEncode()
        {
            var mem = new MemoryStream();

            try
            {
                using (var writer = new BinaryWriter(mem, Encoding.Unicode, true))
                    using (var wav = new WaveFileReader(basesource.GetStream()))
                        using (var res = new MediaFoundationResampler(wav, new WaveFormat(
                                                                          wav.WaveFormat.SampleRate < 32000 ? 24000 : 48000
                                                                          , 1)))
                        {
                            var opus = OpusEncoder.Create(res.WaveFormat.SampleRate, 1, FragLabs.Audio.Codecs.Opus.Application.Voip);
                            opus.Bitrate = 96000;
                            int    packetsize = (int)(res.WaveFormat.SampleRate * 0.04);
                            byte[] buffer     = new byte[packetsize];
                            int    result     = res.Read(buffer, 0, packetsize);
                            while (result > 0)
                            {
                                int    outlen = 0;
                                byte[] output = opus.Encode(buffer, result / 2, out outlen);
                                writer.Write((uint)outlen);
                                writer.Write(output, 0, outlen);

                                result = res.Read(buffer, 0, packetsize);
                            }
                        }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                mem.Position = 0;
                _size        = (uint)mem.Length;
            }
            return(mem);
        }
Пример #19
0
        public MusicVersion ConvertTrack(MusicTrack musicTrack)
        {
            var path    = Path.Combine(folderPath, musicTrack.Id + ".ogg");
            var wavPath = musicTrack.MusicVersions.SingleOrDefault(p => p.Extension == "wav");

            byte[] bytes = ReadAllBytes(wavPath.TrackPath);

            using (var fileOut = new FileStream(path, FileMode.Create))
            {
                var encoder = OpusEncoder.Create(48000, 2, OpusApplication.OPUS_APPLICATION_AUDIO);
                var oggOut  = new OpusOggWriteStream(encoder, fileOut);

                //byte[] allInput = File.ReadAllBytes(wavPath.TrackPath);
                short[] samples = BytesToShorts(bytes);

                oggOut.WriteSamples(samples, 0, samples.Length);
                oggOut.Finish();
            }

            return(new MusicVersion()
            {
                MusicTrack = musicTrack, TrackPath = path, Extension = "ogg"
            });
        }
Пример #20
0
        public void StartEncoding()
        {
            BotAudioProvider = new BotAudioProvider(Client.DcsPlayerRadioInfo.radios[0], ResponseQueue)
            {
                SpeechRecognitionListener = { VoiceHandler = Client.SrsAudioClient, SrsClient = Client }
            };

            StartResponseCheckLoop();

            try
            {
                //opus
                _encoder = OpusEncoder.Create(InputSampleRate, 1, Application.Voip);
                _encoder.ForwardErrorCorrection = false;
                _decoder = OpusDecoder.Create(InputSampleRate, 1);
                _decoder.ForwardErrorCorrection = false;
            }
            catch (Exception ex)
            {
                Logger.Error(ex, $"{LogClientId}| Error starting audio Output - Quitting! " + ex.Message);

                Environment.Exit(1);
            }
        }
Пример #21
0
        private void ConvertToOgg(string uri, string fileLocation, string FileName)
        {
            MediaFoundationReader      reader    = new MediaFoundationReader(uri);
            WaveFormat                 newFormat = new WaveFormat(48000, reader.WaveFormat.Channels);
            WaveFormatConversionStream newStream = new WaveFormatConversionStream(newFormat, reader);
            WaveStream                 conv      = WaveFormatConversionStream.CreatePcmStream(newStream);

            byte[] bytes = new byte[conv.Length];
            conv.Position = 0;
            conv.Read(bytes, 0, (int)conv.Length);
            OpusEncoder encoder = OpusEncoder.Create(48000, 2, OpusApplication.OPUS_APPLICATION_AUDIO);
            var         memo    = new MemoryStream();
            var         oggOut  = new OpusOggWriteStream(encoder, memo);
            var         shorts  = ByteToShort(bytes);

            oggOut.WriteSamples(shorts, 0, shorts.Count());
            oggOut.Finish();
            var result = memo.ToArray();

            using (var stream = new FileStream(fileLocation + FileName, FileMode.Create))
            {
                stream.Write(result, 0, result.Length);
            }
        }
Пример #22
0
        public List <byte[]> GetOpusBytes()
        {
            List <byte[]> opusBytes = new List <byte[]>();

            byte[] resampledBytes;

            if (path.ToLower().EndsWith(".mp3"))
            {
                Logger.Info($"Reading MP3 it looks like a file");
                resampledBytes = GetMP3Bytes();
            }
            else
            {
                Logger.Info($"Doing Text To Speech as its not an MP3 path");
                resampledBytes = TextToSpeech();
            }

            Logger.Info($"Encode as Opus");
            var _encoder = OpusEncoder.Create(INPUT_SAMPLE_RATE, 1, FragLabs.Audio.Codecs.Opus.Application.Voip);

            int pos = 0;

            while (pos + (SEGMENT_FRAMES * 2) < resampledBytes.Length)
            {
                byte[] buf = new byte[SEGMENT_FRAMES * 2];
                Buffer.BlockCopy(resampledBytes, pos, buf, 0, SEGMENT_FRAMES * 2);

                var outLength = 0;
                var frame     = _encoder.Encode(buf, buf.Length, out outLength);

                if (outLength > 0)
                {
                    //create copy with small buffer
                    var encoded = new byte[outLength];

                    Buffer.BlockCopy(frame, 0, encoded, 0, outLength);

                    opusBytes.Add(encoded);
                }

                pos += (SEGMENT_FRAMES * 2);
            }

            if (pos + 1 < resampledBytes.Length)
            {
                //last bit - less than 40 ms
                byte[] buf = new byte[SEGMENT_FRAMES * 2];
                Buffer.BlockCopy(resampledBytes, pos, buf, 0, resampledBytes.Length - pos);

                var outLength = 0;
                var frame     = _encoder.Encode(buf, buf.Length, out outLength);

                if (outLength > 0)
                {
                    //create copy with small buffer
                    var encoded = new byte[outLength];

                    Buffer.BlockCopy(frame, 0, encoded, 0, outLength);

                    opusBytes.Add(encoded);
                }
            }

            _encoder.Dispose();
            Logger.Info($"Finished encoding as Opus");

            return(opusBytes);
        }
        public void StartPreview(int mic, MMDevice speakers)
        {
            try
            {
                _settings = SettingsStore.Instance;
                _waveOut  = new WasapiOut(speakers, AudioClientShareMode.Shared, true, 40);

                _buffBufferedWaveProvider =
                    new BufferedWaveProvider(new WaveFormat(AudioManager.INPUT_SAMPLE_RATE, 16, 1));
                _buffBufferedWaveProvider.ReadFully = true;
                _buffBufferedWaveProvider.DiscardOnBufferOverflow = true;

                RadioFilter filter = new RadioFilter(_buffBufferedWaveProvider.ToSampleProvider());

                //add final volume boost to all mixed audio
                _volumeSampleProvider = new VolumeSampleProviderWithPeak(filter,
                                                                         (peak => SpeakerMax = (float)VolumeConversionHelper.ConvertFloatToDB(peak)));
                _volumeSampleProvider.Volume = SpeakerBoost;

                if (speakers.AudioClient.MixFormat.Channels == 1)
                {
                    if (_volumeSampleProvider.WaveFormat.Channels == 2)
                    {
                        _waveOut.Init(_volumeSampleProvider.ToMono());
                    }
                    else
                    {
                        //already mono
                        _waveOut.Init(_volumeSampleProvider);
                    }
                }
                else
                {
                    if (_volumeSampleProvider.WaveFormat.Channels == 1)
                    {
                        _waveOut.Init(_volumeSampleProvider.ToStereo());
                    }
                    else
                    {
                        //already stereo
                        _waveOut.Init(_volumeSampleProvider);
                    }
                }

                _waveOut.Play();
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error starting audio Output - Quitting! " + ex.Message);

                ShowOutputError("Problem Initialising Audio Output!");

                Environment.Exit(1);
            }

            try
            {
                _speex = new Preprocessor(AudioManager.SEGMENT_FRAMES, AudioManager.INPUT_SAMPLE_RATE);
                //opus
                _encoder = OpusEncoder.Create(AudioManager.INPUT_SAMPLE_RATE, 1,
                                              FragLabs.Audio.Codecs.Opus.Application.Voip);
                _encoder.ForwardErrorCorrection = false;
                _decoder = OpusDecoder.Create(AudioManager.INPUT_SAMPLE_RATE, 1);
                _decoder.ForwardErrorCorrection = false;

                _waveIn = new WaveIn(WaveCallbackInfo.FunctionCallback())
                {
                    BufferMilliseconds = AudioManager.INPUT_AUDIO_LENGTH_MS,
                    DeviceNumber       = mic
                };

                _waveIn.NumberOfBuffers = 2;
                _waveIn.DataAvailable  += _waveIn_DataAvailable;
                _waveIn.WaveFormat      = new WaveFormat(AudioManager.INPUT_SAMPLE_RATE, 16, 1);

                //debug wave file
                //_waveFile = new WaveFileWriter(@"C:\Temp\Test-Preview.wav", _waveIn.WaveFormat);

                _waveIn.StartRecording();
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error starting audio Input - Quitting! " + ex.Message);
                ShowInputError();

                Environment.Exit(1);
            }
        }
        public void StartPreview(bool windowsN)
        {
            this.windowsN = windowsN;
            try
            {
                MMDevice speakers = null;
                if (_audioOutputSingleton.SelectedAudioOutput.Value == null)
                {
                    speakers = WasapiOut.GetDefaultAudioEndpoint();
                }
                else
                {
                    speakers = (MMDevice)_audioOutputSingleton.SelectedAudioOutput.Value;
                }

                _waveOut = new WasapiOut(speakers, AudioClientShareMode.Shared, true, 80, windowsN);

                _buffBufferedWaveProvider =
                    new BufferedWaveProvider(new WaveFormat(AudioManager.INPUT_SAMPLE_RATE, 16, 1));
                _buffBufferedWaveProvider.ReadFully = true;
                _buffBufferedWaveProvider.DiscardOnBufferOverflow = true;

                RadioFilter filter = new RadioFilter(_buffBufferedWaveProvider.ToSampleProvider());

                CachedLoopingAudioProvider natoEffect =
                    new CachedLoopingAudioProvider(filter.ToWaveProvider16(), new WaveFormat(AudioManager.INPUT_SAMPLE_RATE, 16, 1), CachedAudioEffect.AudioEffectTypes.NATO_TONE);

                //add final volume boost to all mixed audio
                _volumeSampleProvider = new VolumeSampleProviderWithPeak(natoEffect.ToSampleProvider(),
                                                                         (peak => SpeakerMax = (float)VolumeConversionHelper.ConvertFloatToDB(peak)));
                _volumeSampleProvider.Volume = SpeakerBoost;


                if (speakers.AudioClient.MixFormat.Channels == 1)
                {
                    if (_volumeSampleProvider.WaveFormat.Channels == 2)
                    {
                        _waveOut.Init(_volumeSampleProvider.ToMono());
                    }
                    else
                    {
                        //already mono
                        _waveOut.Init(_volumeSampleProvider);
                    }
                }
                else
                {
                    if (_volumeSampleProvider.WaveFormat.Channels == 1)
                    {
                        _waveOut.Init(_volumeSampleProvider.ToStereo());
                    }
                    else
                    {
                        //already stereo
                        _waveOut.Init(_volumeSampleProvider);
                    }
                }

                _waveOut.Play();
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error starting audio Output - Quitting! " + ex.Message);

                ShowOutputError("Problem Initialising Audio Output!");

                Environment.Exit(1);
            }

            try
            {
                _speex = new Preprocessor(AudioManager.SEGMENT_FRAMES, AudioManager.INPUT_SAMPLE_RATE);
                //opus
                _encoder = OpusEncoder.Create(AudioManager.INPUT_SAMPLE_RATE, 1,
                                              FragLabs.Audio.Codecs.Opus.Application.Voip);
                _encoder.ForwardErrorCorrection = false;
                _decoder = OpusDecoder.Create(AudioManager.INPUT_SAMPLE_RATE, 1);
                _decoder.ForwardErrorCorrection = false;

                var device = (MMDevice)_audioInputSingleton.SelectedAudioInput.Value;

                if (device == null)
                {
                    device = WasapiCapture.GetDefaultCaptureDevice();
                }

                device.AudioEndpointVolume.Mute = false;

                _wasapiCapture                   = new WasapiCapture(device, true);
                _wasapiCapture.ShareMode         = AudioClientShareMode.Shared;
                _wasapiCapture.DataAvailable    += WasapiCaptureOnDataAvailable;
                _wasapiCapture.RecordingStopped += WasapiCaptureOnRecordingStopped;

                //debug wave file
                //      _waveFile = new WaveFileWriter(@"C:\Temp\Test-Preview.wav", new WaveFormat(AudioManager.INPUT_SAMPLE_RATE, 16, 1));

                _wasapiCapture.StartRecording();
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error starting audio Input - Quitting! " + ex.Message);
                ShowInputError();

                Environment.Exit(1);
            }
        }
Пример #25
0
 public BotClient(string apiServer) : base(apiServer)                //Connection.ReceiveAudio = false in the base constructor
 {
     encoder                 = OpusEncoder.Create(sampleRate, 1, OpusApplication.OPUS_APPLICATION_VOIP);
     encoder.Bitrate         = 16 * 1024;
     Connection.ReceiveAudio = false;
 }
Пример #26
0
        //experimental multithreading attempts

        private Stream CompileMMX(Stream basestream)
        {
            var mem = new MemoryStream();

            using (var writer = new BinaryWriter(mem, Encoding.ASCII, true))
                using (var wav = new WaveFileReader(basestream))
                    using (var res = new MediaFoundationResampler(wav, new WaveFormat(
                                                                      wav.WaveFormat.SampleRate < 24000 ? 24000 : 48000
                                                                      , 1)))
                        using (MemoryStream decomp = new MemoryStream())
                        {
                            var opus = OpusEncoder.Create(res.WaveFormat.SampleRate, 1, FragLabs.Audio.Codecs.Opus.Application.Voip);
                            opus.Bitrate = PPeXCore.Settings.XggBitrate;
                            int packetsize = (int)(res.WaveFormat.SampleRate * 2 * PPeXCore.Settings.XggFrameSize);

                            writer.Write(Encoding.ASCII.GetBytes("XGG"));
                            writer.Write(Version);
                            writer.Write(packetsize);
                            writer.Write(opus.Bitrate);

                            long   oldpos = mem.Position;
                            ushort count  = 0;
                            writer.Write(count);

                            List <int> lengths = new List <int>();

                            byte[] buffer = new byte[packetsize];
                            int    result = res.Read(buffer, 0, packetsize);
                            while (result > 0)
                            {
                                decomp.Write(buffer, 0, packetsize);

                                result = res.Read(buffer, 0, packetsize);
                            }

                            byte[] dec = decomp.ToArray();
                            decomp.Close();

                            Parallel.For(0, (int)Math.Floor((double)dec.Length / packetsize), (i) =>
                            {
                                ArraySegment <byte> segment = new ArraySegment <byte>(dec,
                                                                                      i * packetsize,
                                                                                      ((i + 1) * packetsize < dec.Length) ? packetsize : dec.Length % packetsize);

                                if (segment.Count < 2)
                                {
                                    return;
                                }

                                int outlen = 0;
                                byte[] enc = opus.Encode(segment.ToArray(), segment.Count / 2, out outlen);

                                Array.Copy(BitConverter.GetBytes((uint)outlen), 0, dec, segment.Offset, 4);

                                for (int ii = 0; ii < segment.Count - 4; i++)
                                {
                                    if (ii < outlen)
                                    {
                                        dec[4 + segment.Offset + ii] = enc[ii];
                                    }
                                    else
                                    {
                                        dec[4 + segment.Offset + ii] = 0;
                                    }
                                }

                                lengths[i] = outlen;
                            });

                            int    total  = lengths.Sum();
                            byte[] output = new byte[total];

                            for (int i = 0; i < lengths.Count; i++)
                            {
                                mem.Write(dec, i * packetsize, lengths[i]);
                            }

                            mem.Position = oldpos;
                            writer.Write(count);
                        }

            mem.Position = 0;
            _size        = (uint)mem.Length;
            return(mem);
        }
Пример #27
0
 private void CreateEncoder()
 {
     _Encoder         = OpusEncoder.Create(outputSampleRate, channels, opusMode);
     _Encoder.Bitrate = bitRate * channels;
     _bytesPerSegment = _Encoder.FrameByteCount(_segmentFrames);
 }
Пример #28
0
        internal static int RunTest1(bool no_fuzz)
        {
            byte[]        mapping /*[256]*/ = { 0, 1, 255 };
            byte[]        db62 = new byte[36];
            int           i;
            int           rc, j;
            BoxedValueInt err = new BoxedValueInt();
            OpusEncoder   enc;
            OpusDecoder   dec;

            OpusDecoder[]   dec_err = new OpusDecoder[10];
            Pointer <short> inbuf;
            Pointer <short> outbuf;
            Pointer <short> out2buf;
            //int bitrate_bps;
            Pointer <byte> packet = Pointer.Malloc <byte>(MAX_PACKET + 257);
            uint           enc_final_range;
            uint           dec_final_range;
            //int fswitch;
            //int fsize;
            int count;

            /*FIXME: encoder api tests, fs!=48k, mono, VBR*/

            Console.WriteLine("  Encode+Decode tests.");

            enc = OpusEncoder.Create(48000, 2, OpusApplication.VOIP);
            if (err.Val != OpusError.OPUS_OK || enc == null)
            {
                TestFailed();
            }

            dec = OpusDecoder.Create(48000, 2);
            if (err.Val != OpusError.OPUS_OK || dec == null)
            {
                TestFailed();
            }

            // fixme: this tests assign() performed on a decoder struct, which doesn't exist
            //dec_err[0] = (OpusDecoder*)malloc(OpusDecoder_get_size(2));
            //memcpy(dec_err[0], dec, OpusDecoder_get_size(2));
            dec_err[0] = OpusDecoder.Create(48000, 2);
            dec_err[1] = OpusDecoder.Create(48000, 1);
            dec_err[2] = OpusDecoder.Create(24000, 2);
            dec_err[3] = OpusDecoder.Create(24000, 1);
            dec_err[4] = OpusDecoder.Create(16000, 2);
            dec_err[5] = OpusDecoder.Create(16000, 1);
            dec_err[6] = OpusDecoder.Create(12000, 2);
            dec_err[7] = OpusDecoder.Create(12000, 1);
            dec_err[8] = OpusDecoder.Create(8000, 2);
            dec_err[9] = OpusDecoder.Create(8000, 1);
            for (i = 1; i < 10; i++)
            {
                if (dec_err[i] == null)
                {
                    TestFailed();
                }
            }

            //{
            //    OpusEncoder* enccpy;
            //    /*The opus state structures contain no pointers and can be freely copied*/
            //    enccpy = (OpusEncoder*)malloc(opus_encoder_get_size(2));
            //    memcpy(enccpy, enc, opus_encoder_get_size(2));
            //    memset(enc, 255, opus_encoder_get_size(2));
            //    opus_encoder_destroy(enc);
            //    enc = enccpy;
            //}

            inbuf   = Pointer.Malloc <short>(SAMPLES * 2);
            outbuf  = Pointer.Malloc <short>(SAMPLES * 2);
            out2buf = Pointer.Malloc <short>(MAX_FRAME_SAMP * 3);
            if (inbuf == null || outbuf == null || out2buf == null)
            {
                TestFailed();
            }

            GenerateMusic(inbuf, SAMPLES);

            ///*   FILE *foo;
            //foo = fopen("foo.sw", "wb+");
            //fwrite(inbuf, 1, SAMPLES*2*2, foo);
            //fclose(foo);*/

            enc.Bandwidth = (OpusBandwidth.OPUS_BANDWIDTH_AUTO);

            for (rc = 0; rc < 3; rc++)
            {
                enc.UseVBR            = (rc < 2);
                enc.UseConstrainedVBR = (rc == 1);
                enc.UseInbandFEC      = (rc == 0);

                int[] modes = { 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2 };
                int[] rates = { 6000, 12000, 48000, 16000, 32000, 48000, 64000, 512000, 13000, 24000, 48000, 64000, 96000 };
                int[] frame = { 960 * 2, 960, 480, 960, 960, 960, 480, 960 * 3, 960 * 3, 960, 480, 240, 120 };

                for (j = 0; j < modes.Length; j++)
                {
                    int rate;
                    rate  = rates[j] + (int)FastRand() % rates[j];
                    count = i = 0;
                    do
                    {
                        OpusBandwidth bw;
                        int           len, out_samples, frame_size;
                        frame_size = frame[j];
                        if ((FastRand() & 255) == 0)
                        {
                            enc.ResetState();
                            dec.ResetState();

                            if ((FastRand() & 1) != 0)
                            {
                                dec_err[FastRand() & 1].ResetState();
                            }
                        }

                        if ((FastRand() & 127) == 0)
                        {
                            dec_err[FastRand() & 1].ResetState();
                        }

                        if (FastRand() % 10 == 0)
                        {
                            int complex = (int)(FastRand() % 11);
                            enc.Complexity = (complex);
                        }

                        if (FastRand() % 50 == 0)
                        {
                            dec.ResetState();
                        }

                        enc.UseInbandFEC      = (rc == 0);
                        enc.ForceMode         = (OpusMode.MODE_SILK_ONLY + modes[j]);
                        enc.UseDTX            = ((FastRand() & 1) != 0);
                        enc.Bitrate           = (rate);
                        enc.ForceChannels     = (rates[j] >= 64000 ? 2 : 1);
                        enc.Complexity        = ((count >> 2) % 11);
                        enc.PacketLossPercent = ((int)((FastRand() & 15) & (FastRand() % 15)));

                        bw = modes[j] == 0 ? OpusBandwidth.OPUS_BANDWIDTH_NARROWBAND + (int)(FastRand() % 3) :
                             modes[j] == 1 ? OpusBandwidth.OPUS_BANDWIDTH_SUPERWIDEBAND + (int)(FastRand() & 1) :
                             OpusBandwidth.OPUS_BANDWIDTH_NARROWBAND + (int)(FastRand() % 5);

                        if (modes[j] == 2 && bw == OpusBandwidth.OPUS_BANDWIDTH_MEDIUMBAND)
                        {
                            bw += 3;
                        }
                        enc.Bandwidth = (bw);
                        len           = enc.Encode(inbuf.Data, i << 1, frame_size, packet.Data, 0, MAX_PACKET);
                        if (len < 0 || len > MAX_PACKET)
                        {
                            TestFailed();
                        }
                        enc_final_range = enc.FinalRange;
                        if ((FastRand() & 3) == 0)
                        {
                            if (OpusRepacketizer.PadPacket(packet.Data, packet.Offset, len, len + 1) != OpusError.OPUS_OK)
                            {
                                TestFailed();
                            }
                            len++;
                        }
                        if ((FastRand() & 7) == 0)
                        {
                            if (OpusRepacketizer.PadPacket(packet.Data, packet.Offset, len, len + 256) != OpusError.OPUS_OK)
                            {
                                TestFailed();
                            }
                            len += 256;
                        }
                        if ((FastRand() & 3) == 0)
                        {
                            len = OpusRepacketizer.UnpadPacket(packet.Data, packet.Offset, len);
                            if (len < 1)
                            {
                                TestFailed();
                            }
                        }
                        out_samples = dec.Decode(packet.Data, 0, len, outbuf.Data, i << 1, MAX_FRAME_SAMP, false);
                        if (out_samples != frame_size)
                        {
                            TestFailed();
                        }
                        dec_final_range = dec.FinalRange;
                        if (enc_final_range != dec_final_range)
                        {
                            TestFailed();
                        }
                        /*LBRR decode*/
                        out_samples = dec_err[0].Decode(packet.Data, 0, len, out2buf.Data, 0, frame_size, ((int)FastRand() & 3) != 0);
                        if (out_samples != frame_size)
                        {
                            TestFailed();
                        }
                        out_samples = dec_err[1].Decode(packet.Data, 0, (FastRand() & 3) == 0 ? 0 : len, out2buf.Data, 0, /*MAX_FRAME_SAMP*/ frame_size, ((int)FastRand() & 7) != 0);
                        if (out_samples < 120)
                        {
                            TestFailed();
                        }
                        i += frame_size;
                        count++;
                    } while (i < (SSAMPLES - MAX_FRAME_SAMP));
                    Console.WriteLine("    Mode {0} FB encode {1}, {2} bps OK.", mstrings[modes[j]], rc == 0 ? " VBR" : rc == 1 ? "CVBR" : " CBR", rate);
                }
            }

            //if (opus_encoder_ctl(enc, OPUS_RESET_STATE) != OpusError.OPUS_OK) test_failed();
            //opus_encoder_destroy(enc);
            //if (opus_multistream_encoder_ctl(MSenc, OPUS_RESET_STATE) != OpusError.OPUS_OK) test_failed();
            //opus_multistream_encoder_destroy(MSenc);
            //if (OpusDecoder_ctl(dec, OPUS_RESET_STATE) != OpusError.OPUS_OK) test_failed();
            //OpusDecoder_destroy(dec);
            //if (opus_multistream_decoder_ctl(MSdec, OPUS_RESET_STATE) != OpusError.OPUS_OK) test_failed();

            return(0);
        }
Пример #29
0
        public void StartEncoding(int mic, MMDevice speakers, string guid, InputDeviceManager inputManager,
                                  IPAddress ipAddress, int port, MMDevice micOutput, VOIPConnectCallback voipConnectCallback)
        {
            _stop = false;


            try
            {
                _micInputQueue.Clear();

                InitMixers();

                InitAudioBuffers();

                //Audio manager should start / stop and cleanup based on connection successfull and disconnect
                //Should use listeners to synchronise all the state

                _waveOut = new WasapiOut(speakers, AudioClientShareMode.Shared, true, 40);

                //add final volume boost to all mixed audio
                _volumeSampleProvider = new VolumeSampleProviderWithPeak(_clientAudioMixer,
                                                                         (peak => SpeakerMax = (float)VolumeConversionHelper.ConvertFloatToDB(peak)));
                _volumeSampleProvider.Volume = SpeakerBoost;

                if (speakers.AudioClient.MixFormat.Channels == 1)
                {
                    if (_volumeSampleProvider.WaveFormat.Channels == 2)
                    {
                        _waveOut.Init(_volumeSampleProvider.ToMono());
                    }
                    else
                    {
                        //already mono
                        _waveOut.Init(_volumeSampleProvider);
                    }
                }
                else
                {
                    if (_volumeSampleProvider.WaveFormat.Channels == 1)
                    {
                        _waveOut.Init(_volumeSampleProvider.ToStereo());
                    }
                    else
                    {
                        //already stereo
                        _waveOut.Init(_volumeSampleProvider);
                    }
                }
                _waveOut.Play();

                //opus
                _encoder = OpusEncoder.Create(INPUT_SAMPLE_RATE, 1, Application.Voip);
                _encoder.ForwardErrorCorrection = false;
                _decoder = OpusDecoder.Create(INPUT_SAMPLE_RATE, 1);
                _decoder.ForwardErrorCorrection = false;

                //speex
                _speex = new Preprocessor(AudioManager.SEGMENT_FRAMES, AudioManager.INPUT_SAMPLE_RATE);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error starting audio Output - Quitting! " + ex.Message);


                ShowOutputError("Problem Initialising Audio Output!");


                Environment.Exit(1);
            }

            if (micOutput != null) // && micOutput !=speakers
            {
                //TODO handle case when they're the same?

                try
                {
                    _micWaveOut = new WasapiOut(micOutput, AudioClientShareMode.Shared, true, 40);

                    _micWaveOutBuffer           = new BufferedWaveProvider(new WaveFormat(AudioManager.INPUT_SAMPLE_RATE, 16, 1));
                    _micWaveOutBuffer.ReadFully = true;
                    _micWaveOutBuffer.DiscardOnBufferOverflow = true;

                    var sampleProvider = _micWaveOutBuffer.ToSampleProvider();

                    if (micOutput.AudioClient.MixFormat.Channels == 1)
                    {
                        if (sampleProvider.WaveFormat.Channels == 2)
                        {
                            _micWaveOut.Init(sampleProvider.ToMono());
                        }
                        else
                        {
                            //already mono
                            _micWaveOut.Init(sampleProvider);
                        }
                    }
                    else
                    {
                        if (sampleProvider.WaveFormat.Channels == 1)
                        {
                            _micWaveOut.Init(sampleProvider.ToStereo());
                        }
                        else
                        {
                            //already stereo
                            _micWaveOut.Init(sampleProvider);
                        }
                    }

                    _micWaveOut.Play();
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "Error starting mic audio Output - Quitting! " + ex.Message);

                    ShowOutputError("Problem Initialising Mic Audio Output!");


                    Environment.Exit(1);
                }
            }


            if (mic != -1)
            {
                try
                {
                    _waveIn = new WaveIn(WaveCallbackInfo.FunctionCallback())
                    {
                        BufferMilliseconds = INPUT_AUDIO_LENGTH_MS,
                        DeviceNumber       = mic,
                    };

                    _waveIn.NumberOfBuffers = 2;
                    _waveIn.DataAvailable  += _waveIn_DataAvailable;
                    _waveIn.WaveFormat      = new WaveFormat(INPUT_SAMPLE_RATE, 16, 1);

                    _tcpVoiceHandler =
                        new TCPVoiceHandler(_clientsList, guid, ipAddress, port, _decoder, this, inputManager, voipConnectCallback);
                    var voiceSenderThread = new Thread(_tcpVoiceHandler.Listen);

                    voiceSenderThread.Start();

                    _waveIn.StartRecording();


                    MessageHub.Instance.Subscribe <SRClient>(RemoveClientBuffer);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "Error starting audio Input - Quitting! " + ex.Message);

                    ShowInputError("Problem initialising Audio Input!");

                    Environment.Exit(1);
                }
            }
        }
Пример #30
0
        private void button1_Click(object sender, EventArgs e)
        {
            playersForm.StopAll();
            Remote remote = settings.Remotes[comboBox1.SelectedIndex];

            try
            {
                farEnd = remote.GetIPEndPoint();
            }
            catch
            {
                MessageBox.Show("This is not a valid IP Address");
                return;
            }
            button2.Enabled = true;
            button1.Enabled = false;
            label5.Visible  = true;
            segmentFrames   = 960;
            encoder         = OpusEncoder.Create(48000, 1, FragLabs.Audio.Codecs.Opus.Application.Audio);
            Decoder         = new DGDecoder(remote.CodecType, 48000, 1);
            encoder.Bitrate = 64000;
            bytesPerSegment = encoder.FrameByteCount(segmentFrames);

            waveIn = new WaveIn(WaveCallbackInfo.FunctionCallback());

            waveIn.DeviceNumber   = inputList.SelectedIndex;
            waveIn.DataAvailable += waveIn_DataAvailable;
            waveIn.WaveFormat     = new WaveFormat(48000, 16, 1);

            liveInput = new BufferedWaveProvider(waveIn.WaveFormat);
            List <ISampleProvider> sampleProviders = new List <ISampleProvider>();

            sampleProviders.Add(liveInput.ToSampleProvider());
            sampleProviders.Add(playersForm.Mixer);
            MixingSampleProvider = new MixingSampleProvider(sampleProviders);


            playBuffer = new BufferedWaveProvider(new WaveFormat(44100, 16, 2));
            playBuffer.DiscardOnBufferOverflow = true;
            waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());
            waveOut.DeviceNumber          = outputList.SelectedIndex;
            playersForm.PlaybackDeviceNum = outputList.SelectedIndex;



            session = new ComrexSession();
            session.SetDestination(SDPMediaTypesEnum.audio, farEnd, farEnd);
            session.OnRtpPacketReceived += Session_OnRtpPacketReceived;
            session.Start();
            killsession = false;

            waveOut.Init(playBuffer);
            waveIn.StartRecording();


            if (timer == null)
            {
                timer          = new Timer();
                timer.Interval = 100;
                timer.Tick    += timer_Tick;
            }
            timer.Start();

            comboBox1.Enabled  = false;
            outputList.Enabled = false;
            inputList.Enabled  = false;
        }