コード例 #1
0
ファイル: AsioRecordingPanel.cs プロジェクト: jnm2/NAudio
        private void Start()
        {
            // allow change device
            if (asioOut != null &&
                (asioOut.DriverName != comboBoxAsioDevice.Text ||
                 asioOut.ChannelOffset != GetUserSpecifiedChannelOffset()))
            {
                asioOut.AudioAvailable -= OnAsioOutAudioAvailable;
                asioOut.Dispose();
                asioOut = null;
            }

            int recordChannelCount = GetUserSpecifiedChannelCount();

            // create device if necessary
            if (asioOut == null)
            {
                asioOut = new AsioOut(comboBoxAsioDevice.Text);
                asioOut.InputChannelOffset = GetUserSpecifiedChannelOffset();
                asioOut.InitRecordAndPlayback(null, recordChannelCount, 44100);
                asioOut.AudioAvailable += OnAsioOutAudioAvailable;
            }

            fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".wav");
            writer   = new WaveFileWriter(fileName, new WaveFormat(44100, recordChannelCount));
            asioOut.Play();
            timer1.Enabled = true;
            SetButtonStates();
        }
コード例 #2
0
    public AsioSampleProvider(string driverName, int sampleRate = 44100, int numChannels = 2)
    {
        this.sampleRate  = sampleRate;
        this.numChannels = numChannels;

        //For now assuming ASIO in is PCM 16 (H6 input is PCM 16bit and not planning to use another interface)
        //In future may need some sort of data conversion
        this.WaveFormat = new WaveFormat(sampleRate, numChannels);
        waveProvider    = new BufferedWaveProvider(this.WaveFormat);

        sampleProvider = waveProvider.ToSampleProvider();

        try
        {
            asio = new AsioOut(driverName);
            asio.InitRecordAndPlayback(null, numChannels, sampleRate);
            asio.AudioAvailable += OnAudioAvailable;
            asio.Play();
            Debug.Log(string.Format("Asio Playing: {0}", asio.PlaybackState));
        }
        catch (System.ArgumentException e)
        {
            Debug.Log(string.Format("Invalid ASIO Driver Name: {0}", e));
        }
        catch (System.Exception e)
        {
            Debug.Log(string.Format("Unknown ASIO Error: {0}", e));
        }
    }
コード例 #3
0
 public MainWindow()
 {
     String[] drivernames = AsioOut.GetDriverNames();
     ASIODriver = new AsioOut(drivernames[0]);
     buffer     = new BufferedWaveProvider(new WaveFormat());
     ASIODriver.AudioAvailable += new EventHandler <AsioAudioAvailableEventArgs>(ASIODriver_AudioAvailable);
     ASIODriver.InitRecordAndPlayback(buffer, 2, 44100);
     ASIODriver.Play();
 }
コード例 #4
0
        public void Setup()
        {
            var drives = AsioOut.GetDriverNames();

            asioOut = new AsioOut("ASIO4ALL v2");

            //asioOut.InputChannelOffset = inputNumber;
            asioOut.InitRecordAndPlayback(null, 1, sampleRate);
            asioOut.AudioAvailable += OnAsioAudioAvailable;
        }
コード例 #5
0
        private static AsioOut PrepareAsioOut()
        {
            var names = AsioOut.GetDriverNames();

            var asioDriverName = names[0];
            var asioOut        = new AsioOut(asioDriverName);

            //var inputChannels = asioOut.DriverInputChannelCount;
            asioOut.InputChannelOffset = 0;
            asioOut.InitRecordAndPlayback(null, 1, SampleRate);
            return(asioOut);
        }
コード例 #6
0
        private void initialise()
        {
            inputMapper.Name  = driver.DriverName;
            outputMapper.Name = driver.DriverName;

            inputMapper.Initialise(format, driver);
            outputMapper.Initialise(format, driver);

            driver.InitRecordAndPlayback(
                outputMapper.OutputBuffer,
                inputMapper.Format.Channels,
                inputMapper.Format.SampleRate);
#if DEBUG
            driver.ShowControlPanel();
#endif
            initialised = true;
        }
コード例 #7
0
        private void OnButtonBeginClick(object sender, RoutedEventArgs e)
        {
            if (!running)
            {
                running = true;
                asioOut = new AsioOut((string)comboAsioDevices.SelectedItem);
                int inputChannels  = asioOut.DriverInputChannelCount;               // that's all my soundcard has :(
                int outputChannels = Math.Min(asioOut.DriverOutputChannelCount, 4); // support up to 4
                inputPatcher = new AsioInputPatcher(44100, inputChannels, outputChannels);
                int ignored = 0;                                                    // yuck, really need to improve my API

                asioOut.InitRecordAndPlayback(new SampleToWaveProvider(inputPatcher),
                                              inputChannels, ignored);
                asioOut.AudioAvailable += OnAsioOutAudioAvailable;
                SetSliders();
                asioOut.Play();
                buttonBegin.Content = "Stop";
            }
            else
            {
                Stop();
            }
        }
コード例 #8
0
ファイル: App.cs プロジェクト: galilov/MovementDetector
        public static void Main(string[] args)
        {
            var names = AsioOut.GetDriverNames();

            var asioDriverName = names[0];

            Console.WriteLine("Selected ASIO driver: {0}", asioDriverName);

            var asioOut = new AsioOut(asioDriverName);

            //var inputChannels = asioOut.DriverInputChannelCount;
            asioOut.InputChannelOffset = 0;
            asioOut.InitRecordAndPlayback(null, 1, SampleRate);
            asioOut.AudioAvailable += OnAsioOutAudioAvailable;
            var soundProcessTask = Task.Run(ProcessAudioData, CancellationTokenSource.Token)
                                   .ContinueWith(t =>
            {
                t.Exception?.Handle(e => true);
                Console.WriteLine("You have canceled the Sound Process task");
            }, TaskContinuationOptions.OnlyOnCanceled);
            var playerTask = Task.Run(PlayAlarm, CancellationTokenSource.Token)
                             .ContinueWith(t =>
            {
                t.Exception?.Handle(e => true);
                Console.WriteLine("You have canceled the PlayAlarm task");
            }, TaskContinuationOptions.OnlyOnCanceled);

            ;
            asioOut.Play(); // start recording
            Console.WriteLine("Press ENTER to exit");
            Console.ReadLine();

            asioOut.Stop();
            CancellationTokenSource.Cancel();

            Task.WaitAll(soundProcessTask, playerTask);
        }
コード例 #9
0
        public async IAsyncEnumerable <Sample[]> EnumerateSamplesAsync(InputOption option, [EnumeratorCancellation] CancellationToken token = default)
        {
            if (string.IsNullOrEmpty(option.OptionName))
            {
                throw new ConfigurationNeededException();
            }
            using var asio           = new AsioOut(option.OptionName);
            option.SamplingFrequency = 44100;
            var wavprov = new BufferedWaveProvider(new WaveFormat(option.SamplingFrequency, 1));

            values = new float[samplesBatchSize];
            asio.AudioAvailable += HandleBuffer;
            //asio.InitRecordAndPlayback(wavprov, 1, 44100);
            asio.InitRecordAndPlayback(null, 1, option.SamplingFrequency);
            asio.Play();

            while (await sampleQueue.OutputAvailableAsync(token))
            {
                var samples = await sampleQueue.TakeAsync(token);

                yield return(samples);
            }
            asio.AudioAvailable -= HandleBuffer;
        }
コード例 #10
0
ファイル: Program.cs プロジェクト: kamiyaowl/asio4split
        static void Main(string[] args)
        {
            var sampleRate = 48000;
            var bitDepth   = 24;
            var byteDepth  = bitDepth / 8;
            var devices    = AsioOut.GetDriverNames();

            // 使用するデバイスを決定する
            Console.WriteLine("# Input Device");
            var srcIndex = SelectDeviceIndex(devices);

            Console.WriteLine("# Output Device");
            var dstIndex = SelectDeviceIndex(devices);
            // デバイスのルーティング設定
            // TEST: srcのMASTER OUT L, RをdstのLRに当てることにする
            var routingChannels = new int[] { 12, 13 }; // srcのチャンネル番号を並べて書く


            // あとはよしなに
            using (var src = new AsioOut(devices[srcIndex]))
                using (var dst = new AsioOut(devices[dstIndex])) {
                    // ルーティングテーブルがイマイチの場合に補完する
                    var routingChannelDiff = dst.DriverOutputChannelCount - routingChannels.Length;
                    if (routingChannelDiff > 0)
                    {
                        Console.WriteLine($"Warning: RoutingChannel added {routingChannelDiff} empty channles");
                        routingChannels = routingChannels.Concat(Enumerable.Repeat(-1, routingChannelDiff)).ToArray();
                    }
                    else if (routingChannelDiff < 0)
                    {
                        Console.WriteLine($"Warning: RoutingChannel resize {routingChannelDiff}");
                        routingChannels = routingChannels.Take(dst.DriverOutputChannelCount).ToArray();
                    }
                    Console.WriteLine($"RoutingChannels: [{string.Join(",", routingChannels)}]");
                    // Output Setting
                    var bufferProvider = new BufferedWaveProvider(new WaveFormat(sampleRate, bitDepth, dst.DriverOutputChannelCount));
                    dst.Init(bufferProvider);
                    dst.Play();
                    // Input Setting
                    float[] srcBuffer = null;
                    byte[]  dstBuffer = null;
                    src.AudioAvailable += (sx, ex) => {
                        if (srcBuffer == null)
                        {
                            srcBuffer = new float[ex.SamplesPerBuffer * src.DriverInputChannelCount]; // 1ch[0:511], 2ch[512:... nch
                            dstBuffer = new byte[ex.SamplesPerBuffer * dst.DriverOutputChannelCount * byteDepth];

                            Console.WriteLine("# Source");
                            Console.WriteLine($"\tDriverName: {src.DriverName}");
                            Console.WriteLine($"\tDriverInputChannelCount: {src.DriverInputChannelCount}");
                            Console.WriteLine($"\tDriverOutputChannelCount: {src.DriverOutputChannelCount}");
                            Console.WriteLine($"\tSamplePerBuffer: {ex.SamplesPerBuffer}");
                            Console.WriteLine($"\tAsioSampleType: {ex.AsioSampleType}");
                            Console.WriteLine($"\tsrcBufferSize: {srcBuffer.Length}");

                            Console.WriteLine("# Destination");
                            Console.WriteLine($"\tDriverName: {dst.DriverName}");
                            Console.WriteLine($"\tDriverInputChannelCount: {dst.DriverInputChannelCount}");
                            Console.WriteLine($"\tDriverOutputChannelCount: {dst.DriverOutputChannelCount}");
                            Console.WriteLine($"\tdstBufferSize: {dstBuffer.Length}");
                        }
                        ex.GetAsInterleavedSamples(srcBuffer);

                        // データの書き込み src -> dst
                        for (int dataPtr = 0; dataPtr < ex.SamplesPerBuffer; ++dataPtr)
                        {
                            for (int ch = 0; ch < routingChannels.Length; ++ch)
                            {
                                // src 1sampleのデータをbyteDepthに分割して書き込む
                                UInt32 raw;
                                if (routingChannels[ch] == -1)
                                {
                                    raw = 0;
                                }
                                else
                                {
                                    var   srcPtr = dataPtr + (routingChannels[ch] * ex.SamplesPerBuffer);
                                    Int32 sample = (Int32)(srcBuffer[srcPtr] * Int32.MaxValue);
                                    raw = (UInt32)sample;
                                }

                                var dstPtr = ((dataPtr * routingChannels.Length) + ch) * byteDepth;
                                for (int byteOffset = 0; byteOffset < byteDepth; ++byteOffset)
                                {
                                    dstBuffer[dstPtr + byteOffset] = (byte)((raw >> (8 * (3 - byteOffset))) & 0xff);
                                }
                            }
                        }
                        bufferProvider.AddSamples(dstBuffer, 0, dstBuffer.Length);
                    };
                    src.InitRecordAndPlayback(null, src.DriverInputChannelCount, sampleRate);
                    src.Play();

                    Console.ReadKey();
                }
        }
コード例 #11
0
ファイル: Player.cs プロジェクト: lazar2222/OtherProjects
 public void Init()
 {
     ASIO = new AsioOut(selected_device);
     ASIO.InitRecordAndPlayback(new BufferedWaveProvider(format), format.Channels, format.SampleRate);
     ASIO.AudioAvailable += ASIO_AudioAvailable;
 }
コード例 #12
0
        protected override SinkSourceMode driverConnect()
        {
            if (selectedDriver <= 0)
            {
                return(SinkSourceMode.Offline);
            }

            owner.logText(String.Format("Opening ASIO Device {0}", driverNames[selectedDriver]));
            try
            {
                waveDev = new AsioOut(driverNames[selectedDriver]);
                waveDev.ChannelOffset      = 0;
                waveDev.InputChannelOffset = 0;

                if ((waveDev.DriverInputChannelCount == 0) && (waveDev.DriverOutputChannelCount == 0))
                {
                    throw new Exception(String.Format("ASIO Driver {0} proveides no Input or output channels", driverNames[selectedDriver]));
                }

                RTIO[] ins  = new RTIO[min(waveDev.DriverInputChannelCount, maxChannels)];
                RTIO[] outs = new RTIO[min(waveDev.DriverOutputChannelCount, maxChannels)];
                if ((waveDev.DriverInputChannelCount > 0) && (maxChannels > 0))
                {
                    ins[0] = ioI1;
                }
                if ((waveDev.DriverInputChannelCount > 1) && (maxChannels > 1))
                {
                    ins[1] = ioI2;
                }
                if ((waveDev.DriverInputChannelCount > 2) && (maxChannels > 2))
                {
                    ins[2] = ioI3;
                }
                if ((waveDev.DriverInputChannelCount > 3) && (maxChannels > 3))
                {
                    ins[3] = ioI4;
                }
                if ((waveDev.DriverInputChannelCount > 4) && (maxChannels > 4))
                {
                    ins[4] = ioI5;
                }
                if ((waveDev.DriverInputChannelCount > 5) && (maxChannels > 5))
                {
                    ins[5] = ioI6;
                }
                if ((waveDev.DriverInputChannelCount > 6) && (maxChannels > 6))
                {
                    ins[6] = ioI7;
                }
                if ((waveDev.DriverInputChannelCount > 7) && (maxChannels > 7))
                {
                    ins[7] = ioI8;
                }
                if ((waveDev.DriverOutputChannelCount > 0) && (maxChannels > 0))
                {
                    outs[0] = ioO1;
                }
                if ((waveDev.DriverOutputChannelCount > 1) && (maxChannels > 1))
                {
                    outs[1] = ioO2;
                }
                if ((waveDev.DriverOutputChannelCount > 2) && (maxChannels > 2))
                {
                    outs[2] = ioO3;
                }
                if ((waveDev.DriverOutputChannelCount > 3) && (maxChannels > 3))
                {
                    outs[3] = ioO4;
                }
                if ((waveDev.DriverOutputChannelCount > 4) && (maxChannels > 4))
                {
                    outs[4] = ioO5;
                }
                if ((waveDev.DriverOutputChannelCount > 5) && (maxChannels > 5))
                {
                    outs[5] = ioO6;
                }
                if ((waveDev.DriverOutputChannelCount > 6) && (maxChannels > 6))
                {
                    outs[6] = ioO7;
                }
                if ((waveDev.DriverOutputChannelCount > 7) && (maxChannels > 7))
                {
                    outs[7] = ioO8;
                }

                setChannels(outs, ins);

                if (waveProvider == null)
                {
                    waveProvider = new NAudioShortWaveProvider(this);
                }
                waveProvider.SetWaveFormat(owner.sampleRate, toDriverChannels);

                if (fromDriverChannels == 0)
                {
                    waveDev.InitRecordAndPlayback(waveProvider, 0, owner.sampleRate);
                }
                else if (toDriverChannels == 0)
                {
                    waveDev.InitRecordAndPlayback(null, fromDriverChannels, owner.sampleRate);
                }
                else
                {
                    waveDev.InitRecordAndPlayback(waveProvider, fromDriverChannels, owner.sampleRate);
                }

                if (fromDriverChannels > 0)
                {
                    waveDev.AudioAvailable += ASIO_AudioAvailable;
                }

                waveDev.Play();

                if (fromDriverChannels == 0)
                {
                    processingType = ProcessingType.SynchronousSource;
                }
                else
                {
                    processingType = ProcessingType.SynchronousSink;
                }
                owner.logText(String.Format("ASIO Device {0} open", driverNames[selectedDriver]));
                return(SinkSourceMode.Online);
            }
            catch (Exception e)
            {
                owner.showLogWin();
                owner.logText(e.Message);
                return(SinkSourceMode.Error);
            }
        }
コード例 #13
0
 public void StartRecording()
 {
     _asioOut?.InitRecordAndPlayback(null, _waveFormat.Channels, _waveFormat.SampleRate);
     _asioOut?.Play();
 }
コード例 #14
0
ファイル: MainWindow.xaml.cs プロジェクト: TomArrow/SinusLab
        const int EXTRARECORDINGTIME = 10;// Extra recording time in seconds. Make sure we dont miss anything.

        private void processAudioFile()
        {
            goBtn.IsEnabled      = false;
            asioDevice.IsEnabled = false;

            bool fail = true;


            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Title = "Select w64 file to process";

            if (ofd.ShowDialog() == true)
            {
                SaveFileDialog sfd = new SaveFileDialog();

                string suffix = "";


                sfd.FileName = ofd.FileName + ".ASIOLFLA" + suffix + ".w64";
                if (sfd.ShowDialog() == true)
                {
                    SuperWAV         inputAudio = new SuperWAV(ofd.FileName);
                    SuperWAV.WavInfo inInfo     = inputAudio.getWavInfo();

                    if (inInfo.channelCount != 2)
                    {
                        MessageBox.Show("Only files with a channel count of 2 supported.");
                    }
                    else
                    {
                        fail = false;

                        // Allocate an output file.
                        SuperWAV outputAudio = new SuperWAV(sfd.FileName, SuperWAV.WavFormat.WAVE64, inInfo.sampleRate, 2, inInfo.audioFormat, inInfo.bitsPerSample);

                        ConcurrentQueue <float[]> sampleBuffer = new ConcurrentQueue <float[]>();
                        float[] tmp;

                        bool processingActive = true;

                        UInt64 outputFileOffset = 0;

                        // Do actual processing.
                        AsioOut          asioOut        = new AsioOut(selectedAsioDevice);
                        SuperWAVProvider sampleProvider = new SuperWAVProvider(inputAudio);
                        IWaveProvider    waveProvider   = new SampleToWaveProvider(sampleProvider);
                        asioOut.InitRecordAndPlayback(waveProvider, 2, (int)inInfo.sampleRate);
                        asioOut.AudioAvailable += (object sender, AsioAudioAvailableEventArgs e) => {
                            sampleBuffer.Enqueue(e.GetAsInterleavedSamples());
                        };

                        _ = Task.Run(() => {
                            Task writerTask = Task.Run(() => {
                                float[] tmp2;
                                while (processingActive)
                                {
                                    while (sampleBuffer.Count > 0)
                                    {
                                        if (!processingActive)
                                        {
                                            break;
                                        }
                                        if (sampleBuffer.TryDequeue(out tmp2))
                                        {
                                            outputAudio.writeFloatArrayFast(tmp2, outputFileOffset);
                                            outputFileOffset += (ulong)tmp2.Length / 2;
                                        }
                                    }

                                    System.Threading.Thread.Sleep(10); // Just make sure we don't eat up all CPU with constant checks of the queue, pointless
                                }
                            });
                            // Progress updates
                            Task progressTask = Task.Run(() => {
                                while (processingActive)
                                {
                                    double progress = (double)sampleProvider.InputOffset / (inputAudio.DataLengthInTicks + inInfo.sampleRate * EXTRARECORDINGTIME);
                                    Dispatcher.Invoke(() => {
                                        progressBar.Value = progress * 100.0;
                                    });
                                    if (progress > 1.0)
                                    {
                                        processingActive = false;
                                        asioOut.Stop();
                                    }
                                    System.Threading.Thread.Sleep(100);
                                }
                            });
                            writerTask.Wait();
                            progressTask.Wait();
                            Dispatcher.Invoke(() => {
                                goBtn.IsEnabled      = true;
                                asioDevice.IsEnabled = true;
                            });
                        });


                        asioOut.Play(); // start recording
                    }
                }
            }



            if (fail)
            {
                goBtn.IsEnabled      = true;
                asioDevice.IsEnabled = true;
            }
        }