public ViewModel(int rows, int cols)
        {
            Maze        = new Maze(rows, cols);
            _generators = new List <IMazeGenerator>()
            {
                new DepthFirstSearchGenerator(Maze),
                new BinaryTreeGenerator(Maze),
                new KruskalsRandomizedGenerator(Maze),
                new SidewinderGenerator(Maze),
                new HuntAndKillGenerator(Maze),
                new PrimsRandomizedGenerator(Maze),
                new EllersGenerator(Maze),
            };

            _asio = new AsioOut("Focusrite USB ASIO");
            _asio.Init(_sineWaveProvider);
            //waveOut = new WaveOut();
            //waveOut.Init(_sineWaveProvider);

            GenerateCommand = new RelayCommand(o =>
            {
                _sineWaveProvider.Frequency = 0;
                _asio.Play();
                //waveOut.Play();

                RunGenerator();
            });
        }
Example #2
0
        static Audio()
        {
            var asioDrivers = AsioOut.GetDriverNames();

            if (asioDrivers.Length == 0)
            {
                Console.WriteLine("please install http://www.asio4all.org/");
                Console.WriteLine("press any key to exit");
                Console.ReadKey();
                Environment.Exit(0);
            }
            asioOut = new AsioOut(asioDrivers[0]);
            mixer   = new MixingWaveProvider32();
            hit     = new WaveChannel32(new AudioFileReader("hitsound.wav"))
            {
                Volume = 0.2f,
            };
            miss = new WaveChannel32(new AudioFileReader("miss.wav"))
            {
                Volume = 0.2f,
            };
            var err = new SignalGenerator()
            {
                Type = SignalGeneratorType.SawTooth,
                Gain = 0.2,
            }.Take(TimeSpan.FromSeconds(0.1d)).ToWaveProvider();

            mixer.AddInputStream(hit);
            mixer.AddInputStream(miss);
            asioOut.Init(mixer);
            asioOut.Play();
        }
Example #3
0
    public static void Init(string drivername, int channeloffset)
    {
        mixer = new MixingWaveProvider32();

        waveOut = new AsioOut(drivername);
        waveOut.ChannelOffset = channeloffset;
        waveOut.Init(mixer);
        waveformat = new WaveFormat(44100, 2);

        DctWaveProvider = new Dictionary <string, IWaveProvider>();
    }
Example #4
0
    public static void Init(string drivername)
    {
        mixer = new MixingWaveProvider32();

        waveOut = new AsioOut(drivername);//NAudio.CoreAudioApi.AudioClientShareMode.Exclusive,100);
        //waveOut = new WasapiOut(NAudio.CoreAudioApi.AudioClientShareMode.Shared, 100);
        waveOut.Init(mixer);
        waveformat = new WaveFormat(44100, 2);

        DctWaveProvider = new Dictionary <string, IWaveProvider>();
    }
        private void InitAudio()
        {
            synthEngine = new SynthEngine(48000, 32)
            {
                Amplitude = 0.5f
            };

            audioOutput = new AsioOut();
            audioOutput.Init(synthEngine);
            audioOutput.Play();
        }
Example #6
0
        public AudioPlaybackEngine(int driverSelection, int sampleRate = 44100, int channelCount = 2)
        {
            this.sampleRate = sampleRate;
            midiTools       = new MIDITools();

            outputDevice    = new AsioOut(AsioOut.GetDriverNames()[driverSelection]);
            mixer           = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channelCount));
            mixer.ReadFully = true;
            saver           = new SavingSampleProvider(mixer, "test.wav");


            InitialiseActiveNotes(128);

            outputDevice.Init(saver);
            outputDevice.Play();
        }
Example #7
0
        public void MixWaveProviders(IList <WavePlayer> inputs)
        {
            try
            {
                _mixer = new MixingWaveProvider32(inputs.Select(c => c._channels));


                _waveStream = inputs[0]._reader;
                foreach (var obj in inputs)
                {
                    _waveStreamList.Add(obj._reader);
                }

                _device.Init(_mixer);
            }
            catch (Exception exception)
            {
                MessageBox.Show("All incoming channels must have the same format", "Errror", MessageBoxButton.OK, MessageBoxImage.Error);
                Log.Write(LogLevel.Error, "File Name : AsioAudioService.cs, Method Name : MixWaveProviders(), Line Number : 131");
                throw new ArgumentException(exception.Message, exception);
            }
        }
Example #8
0
        private Mixer()
        {
            try
            {
                mixer.ReadFully        = true;
                mixer.MixerInputEnded += OnInputEnded;
                VSP        = new VolumeSampleProvider(mixer);
                VSP.Volume = baseVolume;
                PSP        = new PanningSampleProvider(VSP);
                PSP.Pan    = basePan;
                // waveOut = new WaveOutEvent();
                // waveOut = new DirectSoundOut(40);

                waveOut = new NAudio.Wave.AsioOut("ASIO4ALL v2");
                //waveOut = new AsioOut();
                waveOut.Init(PSP);
                waveOut.Play();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #9
0
        private void Play()
        {
            // allow change device
            if (asioOut != null &&
                (asioOut.DriverName != comboBoxAsioDevice.Text ||
                 asioOut.ChannelOffset != GetUserSpecifiedChannelOffset()))
            {
                asioOut.Dispose();
                asioOut = null;
            }

            // create device if necessary
            if (asioOut == null)
            {
                asioOut = new AsioOut(comboBoxAsioDevice.Text);
                asioOut.ChannelOffset = GetUserSpecifiedChannelOffset();
                asioOut.Init(reader);
            }

            reader.Position = 0;
            asioOut.Play();
            timer1.Enabled = true;
            SetButtonStates();
        }
Example #10
0
        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();
                }
        }
Example #11
0
        public static void Start(Setting setting)
        {
            NAudioWrap.setting = setting;
            if (waveOut != null)
            {
                waveOut.Dispose();
            }
            waveOut = null;
            if (wasapiOut != null)
            {
                wasapiOut.Dispose();
            }
            wasapiOut = null;
            if (dsOut != null)
            {
                dsOut.Dispose();
            }
            dsOut = null;
            if (asioOut != null)
            {
                asioOut.Dispose();
            }
            asioOut = null;
            if (nullOut != null)
            {
                nullOut.Dispose();
            }
            nullOut = null;

            try
            {
                switch (setting.outputDevice.DeviceType)
                {
                case 0:
                    waveOut = new WaveOutEvent();
                    waveOut.DeviceNumber   = 0;
                    waveOut.DesiredLatency = setting.outputDevice.Latency;
                    for (int i = 0; i < WaveOut.DeviceCount; i++)
                    {
                        if (setting.outputDevice.WaveOutDeviceName == WaveOut.GetCapabilities(i).ProductName)
                        {
                            waveOut.DeviceNumber = i;
                            break;
                        }
                    }
                    waveOut.PlaybackStopped += DeviceOut_PlaybackStopped;
                    waveOut.Init(waveProvider);
                    waveOut.Play();
                    break;

                case 1:
                    System.Guid g = System.Guid.Empty;
                    foreach (DirectSoundDeviceInfo d in DirectSoundOut.Devices)
                    {
                        if (setting.outputDevice.DirectSoundDeviceName == d.Description)
                        {
                            g = d.Guid;
                            break;
                        }
                    }
                    if (g == System.Guid.Empty)
                    {
                        dsOut = new DirectSoundOut(setting.outputDevice.Latency);
                    }
                    else
                    {
                        dsOut = new DirectSoundOut(g, setting.outputDevice.Latency);
                    }
                    dsOut.PlaybackStopped += DeviceOut_PlaybackStopped;
                    dsOut.Init(waveProvider);
                    dsOut.Play();
                    break;

                case 2:
                    MMDevice dev        = null;
                    var      enumerator = new MMDeviceEnumerator();
                    var      endPoints  = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
                    foreach (var endPoint in endPoints)
                    {
                        if (setting.outputDevice.WasapiDeviceName == string.Format("{0} ({1})", endPoint.FriendlyName, endPoint.DeviceFriendlyName))
                        {
                            dev = endPoint;
                            break;
                        }
                    }
                    if (dev == null)
                    {
                        wasapiOut = new WasapiOut(setting.outputDevice.WasapiShareMode ? AudioClientShareMode.Shared : AudioClientShareMode.Exclusive, setting.outputDevice.Latency);
                    }
                    else
                    {
                        wasapiOut = new WasapiOut(dev, setting.outputDevice.WasapiShareMode ? AudioClientShareMode.Shared : AudioClientShareMode.Exclusive, false, setting.outputDevice.Latency);
                    }
                    wasapiOut.PlaybackStopped += DeviceOut_PlaybackStopped;
                    wasapiOut.Init(waveProvider);
                    wasapiOut.Play();
                    break;

                case 3:
                    if (AsioOut.isSupported())
                    {
                        int i = 0;
                        foreach (string s in AsioOut.GetDriverNames())
                        {
                            if (setting.outputDevice.AsioDeviceName == s)
                            {
                                break;
                            }
                            i++;
                        }
                        int retry = 1;
                        do
                        {
                            try
                            {
                                asioOut = new AsioOut(i);
                            }
                            catch
                            {
                                asioOut = null;
                                Thread.Sleep(1000);
                                retry--;
                            }
                        } while (asioOut == null && retry > 0);
                        asioOut.PlaybackStopped += DeviceOut_PlaybackStopped;
                        asioOut.Init(waveProvider);
                        asioOut.Play();
                    }
                    break;

                case 5:
                    nullOut = new NullOut(true);
                    nullOut.PlaybackStopped += DeviceOut_PlaybackStopped;
                    nullOut.Init(waveProvider);
                    nullOut.Play();
                    break;
                }
            }
            catch (Exception ex)
            {
                log.ForcedWrite(ex);
                waveOut = new WaveOutEvent();
                waveOut.PlaybackStopped += DeviceOut_PlaybackStopped;
                waveOut.Init(waveProvider);
                waveOut.DeviceNumber = 0;
                waveOut.Play();
            }
        }
        private void StartStopSineWave()
        {
            if (asioOut == null)
            {
                button1.Content = "Stop Sound";
                Console.WriteLine("User Selected Channels: " + selectedChannels);
                WaveOutCapabilities outdeviceInfo = WaveOut.GetCapabilities(0);
                waveOutChannels = outdeviceInfo.Channels;
                asioOut         = new AsioOut(0);

                int waveOutDevices = WaveOut.DeviceCount;
                for (int i = 0; i < waveOutDevices; i++)
                {
                    outdeviceInfo = WaveOut.GetCapabilities(i);
                    Console.WriteLine("Device {0}: {1}, {2} channels",
                                      i, outdeviceInfo.ProductName, outdeviceInfo.Channels);
                }

                List <IWaveProvider> inputs = new List <IWaveProvider>();
                frequencies = new List <int>();
                centerbins  = new List <int>();

                for (int c = 0; c < selectedChannels; c++)
                {
                    if (c != (selectedChannels - 1))
                    {
                        inputs.Add(new SineWaveProvider32(0, 0.25f, 44100, 1));
                        frequencies.Add(0);
                        centerbins.Add((int)Math.Round((0) / 10.768));
                    }
                    else
                    {
                        inputs.Add(new SineWaveProvider32(600, 0.25f, 44100, 1));
                        frequencies.Add(300);
                        centerbins.Add((int)Math.Round((600) / 10.768));
                    }
                }

                var splitter = new MultiplexingWaveProvider(inputs, selectedChannels);
                try
                {
                    asioOut.Init(splitter);
                    asioOut.Play();
                }
                catch (System.ArgumentException)
                {
                    Console.WriteLine("Invalid audio channel count. Please select a lower number of audio channels");
                }
                //waveOut = new WaveOut();
                //waveOut.Init(sineWaveProvider);
                //waveOut.Init(splitter);

                Console.WriteLine("Number of Channels: " + asioOut.NumberOfOutputChannels);
                Console.WriteLine("Playback Latency: " + asioOut.PlaybackLatency);
            }
            else
            {
                asioOut.Stop();
                asioOut.Dispose();
                asioOut         = null;
                button1.Content = "Start Sound";

                frequencies.Clear();
                centerbins.Clear();
                //Foutstream.Clear();
            }
        }
Example #13
0
        /// <summary>
        /// 再生する
        /// </summary>
        /// <param name="deviceID">再生デバイスID</param>
        /// <param name="waveFile">wavファイル</param>
        /// <param name="isDelete">再生後に削除する</param>
        /// <param name="volume">ボリューム</param>
        public static void Play(
            string deviceID,
            string waveFile,
            bool isDelete,
            int volume)
        {
            var sw = Stopwatch.StartNew();

            var volumeAsFloat = ((float)volume / 100f);

            try
            {
                IWavePlayer   player   = null;
                IWaveProvider provider = null;

                switch (TTSYukkuriConfig.Default.Player)
                {
                case WavePlayers.WaveOut:
                    player = new WaveOut()
                    {
                        DeviceNumber   = int.Parse(deviceID),
                        DesiredLatency = PlayerLatencyWaveOut,
                    };
                    break;

                case WavePlayers.DirectSound:
                    player = new DirectSoundOut(
                        Guid.Parse(deviceID),
                        PlayerLatencyDirectSoundOut);
                    break;

                case WavePlayers.WASAPI:
                    player = new WasapiOut(
                        deviceEnumrator.GetDevice(deviceID),
                        AudioClientShareMode.Shared,
                        false,
                        PlayerLatencyWasapiOut);
                    break;

                case WavePlayers.ASIO:
                    player = new AsioOut(deviceID);
                    break;
                }

                if (player == null)
                {
                    return;
                }

                provider = new AudioFileReader(waveFile)
                {
                    Volume = volumeAsFloat
                };

                player.Init(provider);
                player.PlaybackStopped += (s, e) =>
                {
                    player.Dispose();

                    var file = provider as IDisposable;
                    if (file != null)
                    {
                        file.Dispose();
                    }

                    if (isDelete)
                    {
                        File.Delete(waveFile);
                    }
                };

                // 再生する
                player.Play();
            }
            catch (Exception ex)
            {
                ActGlobals.oFormActMain.WriteExceptionLog(
                    ex,
                    "サウンドの再生で例外が発生しました。");
            }
            finally
            {
                sw.Stop();
                Debug.WriteLine(
                    "PlaySound ({0}) -> {1:N0} ticks",
                    TTSYukkuriConfig.Default.Player,
                    sw.ElapsedTicks);
            }
        }