public void Initialize()
        {
            _wasapiOut   = new WasapiOut();
            _opusDecoder = OpusDecoder.Create(48000, 1);

            //var waveForm = new WaveFormatExtensible(48000, 16, 1, Guid.Parse("00000003-0000-0010-8000-00aa00389b71"));
            var waveForm = new WaveFormat(48000, 16, 1);

            _writeableBufferingSource = new WriteableBufferingSource(waveForm)
            {
                FillWithZeros = true
            };

            IWaveSource waveSource;

            if (_triggerSingleBlockRead)
            {
                var singleBlockNotificationStream =
                    new SingleBlockNotificationStream(_writeableBufferingSource.ToSampleSource());
                singleBlockNotificationStream.SingleBlockRead += SingleBlockNotificationStreamOnSingleBlockRead;
                waveSource = singleBlockNotificationStream.ToWaveSource();
            }
            else
            {
                waveSource = _writeableBufferingSource;
            }

            _wasapiOut.Initialize(waveSource);
            _wasapiOut.Play();
        }
        static void Main(string[] args)

        {
            MMDevice dev = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);

            capture        = new WasapiLoopbackCapture();
            capture.Device = dev;
            capture.Initialize();

            SoundInSource soundInSource = new SoundInSource(capture);

            nStream = new SingleBlockNotificationStream(soundInSource.ToSampleSource());
            final   = nStream.ToWaveSource();
            nStream.SingleBlockRead     += NStream_SingleBlockRead;
            soundInSource.DataAvailable += encode;
            trashBuf = new byte[final.WaveFormat.BytesPerSecond / 2];

            Console.WriteLine($"sample rate:{capture.WaveFormat.SampleRate}");
            Console.WriteLine($"bits per sample:{capture.WaveFormat.BitsPerSample }");
            Console.WriteLine($"channels:{capture.WaveFormat.Channels }");
            Console.WriteLine($"bytes per sample:{capture.WaveFormat.BytesPerSample }");
            Console.WriteLine($"bytes per second:{capture.WaveFormat.BytesPerSecond }");
            Console.WriteLine($"AudioEncoding:{capture.WaveFormat.WaveFormatTag  }");


            EncodingContext context = FrameEncoder.GetDefaultsContext();

            context.Channels        = 6;
            context.SampleRate      = capture.WaveFormat.SampleRate;
            context.AudioCodingMode = AudioCodingMode.Front3Rear2;
            context.HasLfe          = true;
            context.SampleFormat    = A52SampleFormat.Float;
            enc = new FrameEncoderFloat(ref context);

            //_writer = new WaveWriter("test.ac3", final.WaveFormat);


            capture.Start();

            wBuffSrc = new WriteableBufferingSource(new WaveFormat(capture.WaveFormat.SampleRate, capture.WaveFormat.BitsPerSample, capture.WaveFormat.Channels, AudioEncoding.WAVE_FORMAT_DOLBY_AC3_SPDIF), (int)capture.WaveFormat.MillisecondsToBytes(20));

            w = new WasapiOut2(false, AudioClientShareMode.Shared, 20);

            w.Device = MMDeviceEnumerator.EnumerateDevices(DataFlow.Render, DeviceState.Active).Where(x => x.FriendlyName.Contains("Digital")).Single();
            AudioClient a = AudioClient.FromMMDevice(w.Device);

            w.Initialize(wBuffSrc);
            w.Play();


            Task.Run(async() => await encoderThread());
            //encodeSinus();

            Console.ReadLine();

            System.Environment.Exit(0);
        }
示例#3
0
        private void RunClient()
        {
            // client mode
            using (var soundOut = new WasapiOut {
                Latency = (int)(_configuration.MaxAudioLatency * 1000)
            })
            {
                var wavFormat = new WaveFormat(48000, 32, 2, AudioEncoding.IeeeFloat);
                using (var wb = new WriteableBufferingSource(wavFormat, (int)(wavFormat.BytesPerSecond *
                                                                              _configuration.MaxAudioLatency))
                {
                    FillWithZeros = true
                })
                {
                    _udpSocket = new UDPSocket();
                    _udpSocket.Client(_configuration.IpAddress, _configuration.Port, (point, bytes) =>
                    {
                        wb.Write(bytes, 0, bytes.Length);
                    });

                    _udpSocket.Send(new byte[] { 0 }, 1);
                    Console.WriteLine("Sent start request to server!");

                    soundOut.Initialize(wb);
                    soundOut.Play();

                    var paused = false;
                    while (true)
                    {
                        var key = Console.ReadKey();
                        switch (key.Key)
                        {
                        case ConsoleKey.Q:
                            soundOut.Stop();
                            _udpSocket.Send(new byte[] { 1 }, 1);
                            _stopClient.Close();
                            return;

                        case ConsoleKey.C:
                            _udpSocket.Send(new byte[] { 0 }, 1);
                            Console.WriteLine("Sending connection request to the server...");
                            break;

                        case ConsoleKey.P:
                            _udpSocket.Send(!paused ? new byte[] { 1 } : new byte[] { 0 }, 1);
                            paused = !paused;
                            break;

                        case ConsoleKey.R:
                            _udpSocket.Send(new byte[] { 2 }, 1);
                            _udpSocket.Send(new byte[] { 1 }, 1);
                            Console.WriteLine("Resetting server and client...");
                            return;

                        case ConsoleKey.X:
                            _udpSocket.Send(new byte[] { 3 }, 1);
                            Console.WriteLine("Stopping server and client...");
                            soundOut.Stop();
                            _stopClient.Close();
                            return;
                        }
                    }
                }
            }
        }