Esempio n. 1
0
 private void Cleanup()
 {
     if (asioOut != null)
     {
         asioOut.Dispose();
         asioOut = null;
     }
     if (reader != null)
     {
         reader.Dispose();
         reader = null;
     }
 }
Esempio n. 2
0
 private void Cleanup()
 {
     if (asioOut != null)
     {
         asioOut.Dispose();
         asioOut = null;
     }
     if (writer != null)
     {
         writer.Dispose();
         writer = null;
     }
 }
 public void Close()
 {
     if (asio.PlaybackState != PlaybackState.Stopped)
     {
         asio.Stop();
     }
     asio.Dispose();
 }
Esempio n. 4
0
 internal static void Abort()
 {
     asioOut.Stop();
     asioOut.Dispose();
     music.Dispose();
     hit.Dispose();
     miss.Dispose();
 }
Esempio n. 5
0
 protected override void driverDisconnect()
 {
     if (waveDev != null)
     {
         waveDev.Dispose();
         waveDev = null;
         owner.logText(String.Format("ASIO Device {0} closed", driverNames[selectedDriver]));
     }
     processingType = ProcessingType.Source;
     selectedDriver = 0;
 }
 private void Stop()
 {
     if (running)
     {
         asioOut.Stop();
         asioOut.Dispose();
         asioOut             = null;
         running             = false;
         buttonBegin.Content = "Begin";
     }
 }
Esempio n. 7
0
        private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            audioOutput.Stop();
            audioOutput.Dispose();

            if (midiIn != default(MidiIn))
            {
                midiIn.Stop();
                midiIn.Dispose();
            }
        }
 public void StopRecord()
 {
     if (_asioOut == null)
     {
         return;
     }
     _asioOut.Stop();
     _cancellationTokenSource.Cancel();
     _recordTask.Wait();
     _asioOut.Dispose();
     _asioOut = null;
     _action  = null;
 }
Esempio n. 9
0
        private void button1_Click(object sender, EventArgs e)
        {
            //if (recorder != null)
            //{
            //    recorder.StopRecording();
            //    recorder.Dispose();
            //}

            if (asioDriver != null)
            {
                asioDriver.Stop();
                asioDriver.Dispose();
            }
        }
Esempio n. 10
0
        public void Dispose()
        {
            if (_device != null)
            {
                _device.Stop();
                _device.Dispose();
                _device = null;
            }
            if (_waveStream != null)
            {
                _waveStream.Close();
                _waveStream = null;
            }

            GC.Collect();
        }
Esempio n. 11
0
        public static void ShowControlPanel(string asioDriverName)
        {
            if (asioOut != null)
            {
                asioOut.Dispose();
            }

            int i = 0;

            foreach (string s in AsioOut.GetDriverNames())
            {
                if (asioDriverName == s)
                {
                    break;
                }
                i++;
            }
            int retry = 3;

            do
            {
                try
                {
                    asioOut = new AsioOut(i);
                }
                catch
                {
                    asioOut = null;
                    Thread.Sleep(1000);
                    retry--;
                }
            } while (asioOut == null && retry > 0);

            asioOut.ShowControlPanel();
            asioOut.Dispose();
        }
Esempio n. 12
0
        public static IWavePlayer CreateDevice(out IDeviceInfo actualDeviceInfo, IDeviceInfo deviceInfo = null, int latency = 1, bool isExclusive = true)
        {
            //if (_currentDevice != null)
            //{
            //    return _currentDevice;
            //}

            bool useDefault = false;

            if (deviceInfo is null)
            {
                deviceInfo = GetDefaultDeviceInfo();
                useDefault = true;
            }

            if (CacheList == null)
            {
                EnumerateAvailableDevices().ToList();
            }
            //if (CacheList == null)
            //{
            //    CacheList = EnumerateAvailableDevices().ToList();
            //}

            IWavePlayer device = null;

            if (!useDefault && !CacheList.Contains(deviceInfo))
            {
                if (deviceInfo is WasapiInfo wasapiInfo)
                {
                    var foundResult = CacheList
                                      .Where(k => k.OutputMethod == OutputMethod.Wasapi)
                                      .Cast <WasapiInfo>()
                                      .FirstOrDefault(k => k.DeviceId == wasapiInfo.DeviceId);
                    if (foundResult?.Device != null)
                    {
                        wasapiInfo.Device = foundResult.Device;
                    }
                    else
                    {
                        deviceInfo = GetDefaultDeviceInfo();
                    }
                }
                else
                {
                    deviceInfo = GetDefaultDeviceInfo();
                }
            }
            Execute.OnUiThread(() =>
            {
                try
                {
                    switch (deviceInfo.OutputMethod)
                    {
                    case OutputMethod.WaveOut:
                        var waveOut = (WaveOutInfo)deviceInfo;

                        device = new WaveOutEvent
                        {
                            DeviceNumber   = waveOut.DeviceNumber,
                            DesiredLatency = Math.Max(latency, 100)
                        };

                        break;

                    case OutputMethod.DirectSound:
                        var dsOut = (DirectSoundOutInfo)deviceInfo;
                        if (dsOut.Equals(DirectSoundOutInfo.Default))
                        {
                            device = new DirectSoundOut(40);
                        }
                        else
                        {
                            device = new DirectSoundOut(dsOut.DeviceGuid, Math.Max(latency, 40));
                        }
                        break;

                    case OutputMethod.Wasapi:
                        var wasapi = (WasapiInfo)deviceInfo;
                        if (wasapi.Equals(WasapiInfo.Default))
                        {
                            device = new WasapiOut(AudioClientShareMode.Shared, 1);
                        }
                        else
                        {
                            device = new WasapiOut(wasapi.Device,
                                                   isExclusive ? AudioClientShareMode.Exclusive : AudioClientShareMode.Shared, true,
                                                   latency);
                        }
                        break;

                    case OutputMethod.Asio:
                        var asio = (AsioOutInfo)deviceInfo;
                        device   = new AsioOut(asio.FriendlyName);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(deviceInfo.OutputMethod),
                                                              deviceInfo.OutputMethod, null);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "Error while creating device.");
                    device?.Dispose();
                    deviceInfo = DirectSoundOutInfo.Default;
                    device     = new DirectSoundOut(40);
                }
            });

            _currentDevice   = device;
            actualDeviceInfo = deviceInfo;
            return(device);
        }
Esempio n. 13
0
 public void Dispose()
 {
     waveOut.Dispose();
 }
Esempio n. 14
0
        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();
            }
        }
Esempio n. 15
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);
            }
        }
Esempio n. 16
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();
            }
        }
Esempio n. 17
0
 public void Dispose()
 {
     outputDevice.Dispose();
     saver.Dispose();
 }