Beispiel #1
1
        public void Play()
        {
            asioOut = new AsioOut(settings.DeviceNumber);

            //FIX!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            asioOut.InitRecordAndPlayback(new SampleToWaveProvider(mixingProvider), 2, Convert.ToInt32(settings.SampleRate));

            
            asioOut.AudioAvailable += AsioOut_AudioAvailable;
            asioOut.Play();

            connection.NewClientConnected += delegate(object source, EventArgs args)
            {
                
            };
            connection.Disconnected += delegate(object source, EventArgs args)
            {
                
            };
        }
        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();
        }
Beispiel #3
0
        public AsioCard(
            WaveFormat format,
            AsioOut driver, 
            AsioInputMapper inputMapper, 
            AsioOutputMapper outputMapper)
        {
            if (format == null)
            {
                throw new ArgumentNullException("format", "Must specify an audio format");
            }

            if (driver == null)
            {
                throw new ArgumentNullException("driver", "Asio driver cannot be null");
            }

            if (inputMapper == null)
            {
                throw new ArgumentNullException("inputMapper", "Asio input mapper cannot be null");
            }

            if (outputMapper == null)
            {
                throw new ArgumentNullException("outputMapper", "Asio output mapper cannot be null");
            }

            this.format = format;
            this.driver = driver;
            this.inputMapper = inputMapper;
            this.outputMapper = outputMapper;
        }
 private void Cleanup()
 {
     if (asioOut != null)
     {
         asioOut.Dispose();
         asioOut = null;
     }
     if (reader != null)
     {
         reader.Dispose();
         reader = null;
     }
 }
 private void Cleanup()
 {
     if (asioOut != null)
     {
         asioOut.Dispose();
         asioOut = null;
     }
     if (writer != null)
     {
         writer.Dispose();
         writer = null;
     }
 }
Beispiel #6
0
 private void Cleanup()
 {
     if (this.asioOut != null)
     {
         this.asioOut.Dispose();
         this.asioOut = null;
     }
     if (this.reader != null)
     {
         this.reader.Dispose();
         this.reader = null;
     }
 }
 /// <summary>
 ///     Initialize ASIO and starts playing
 ///     If ASIO throws exception, use Wasapi
 /// </summary>
 private void InitializeAsio()
 {
     try
     {
         if (_asioOut != null) _asioOut.Dispose();
         _asioOut = new AsioOut(1);
         _asioOut.Init(_waveProvider);
         _asioOut.Play();
     }
     catch (Exception e)
     {
         InitializeWasapi();
     }
 }
 private void OnButtonControlPanelClick(object sender, EventArgs args)
 {
     try
     {
         using (var asio = new AsioOut(SelectedDeviceName))
         {
             asio.ShowControlPanel();
         }
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message);
     }
 }
Beispiel #9
0
 public AsioOut(int driverIndex)
 {
     this.syncContext = SynchronizationContext.Current;
     string[] driverNames = AsioOut.GetDriverNames();
     if (driverNames.Length == 0)
     {
         throw new ArgumentException("There is no ASIO Driver installed on your system");
     }
     if (driverIndex < 0 || driverIndex > driverNames.Length)
     {
         throw new ArgumentException(string.Format("Invalid device number. Must be in the range [0,{0}]", driverNames.Length));
     }
     this.driverName = driverNames[driverIndex];
     this.InitFromName(this.driverName);
 }
Beispiel #10
0
        private void StopAsio()
        {
            if (mAsio != null)
            {
                mAsio.Stop();
                // Wait at least 200 ms for the last buffer update event to have happened
                Thread.Sleep(200);
#if NAUDIO_ASIO
                mAsio.Dispose();
#else
                mAsio.Release();
#endif
                mAsio = null;
            }
            mIsAsioRunning = false;
            mAsioStopCompleteEvent.Set();
        }
Beispiel #11
0
        public void Initialise(WaveFormat format, AsioOut driver)
        {
            if (driver == null)
            {
                throw new ArgumentNullException("driver", "Must specify an asio interface");
            }

            if (format == null)
            {
                throw new ArgumentNullException("format", "Must specify an audio format");
            }

            Format = WaveFormat.CreateIeeeFloatWaveFormat(format.SampleRate, driver.DriverOutputChannelCount);

            OutputBuffer = new BufferedWaveProvider(Format);

            mapOutputs(driver);
        }
Beispiel #12
0
 public override void Init(int DeviceID = 0, int channels = 2, int samplerate = 44100, int bufferMax = (44100 * 3))
 {
     base.Init(DeviceID, channels, samplerate, bufferMax);
     DisposeAsio();
     buffer = new float[bufferMax];
     result = new float[bufferMax];
     bcount = 0;
     bwriteIndex = 0;
     input = new AsioOut(DeviceID);
     //provider = new BufferedWaveProvider(new WaveFormat(samplerate, channels));
     input.InitRecordAndPlayback(null, channels, samplerate);
     //input.ShowControlPanel();
     input.AudioAvailable += Input_AudioAvailable;
     System.Diagnostics.Debug.WriteLine(input.PlaybackLatency + "ms");
     input.Play();
     tick.AutoReset = true;
     tick.Elapsed += Tick_Elapsed;
     tick.Start();
 }
Beispiel #13
0
        public void Initialise(WaveFormat format, AsioOut driver)
        {
            if (driver == null)
            {
                throw new ArgumentNullException("driver", "Must specify an asio interface");
            }

            if (format == null)
            {
                throw new ArgumentNullException("format", "Must specify an audio format");
            }

            driver.AudioAvailable += asioDriver_AudioAvailable;

            Format = WaveFormat.CreateIeeeFloatWaveFormat(format.SampleRate, driver.DriverInputChannelCount);
            formatPerLine = WaveFormat.CreateIeeeFloatWaveFormat(format.SampleRate, 1);

            mapInputs(driver);
        }
Beispiel #14
0
        public void Play()
        {
            asioOut = new AsioOut(settings.DeviceNumber);

            inputAudioBufferedWaveProvider = new BufferedWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(Convert.ToInt32(settings.SampleRate), 1));
            incomingAudioBufferedWaveProvider = new BufferedWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(Convert.ToInt32(settings.SampleRate), 1));

            inputAudioPanningProvider = new PanningSampleProvider(inputAudioBufferedWaveProvider.ToSampleProvider());
            incomingAudioPanningProvider = new PanningSampleProvider(incomingAudioBufferedWaveProvider.ToSampleProvider());

            inputAudioPanningProvider.Pan = 0.0f;

            mixingProvider = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(Convert.ToInt32(settings.SampleRate), 2));

            mixingProvider.AddMixerInput(inputAudioPanningProvider);
            mixingProvider.AddMixerInput(incomingAudioPanningProvider);
            mixingProvider.ReadFully = true;

            asioOut.InitRecordAndPlayback(new SampleToWaveProvider(mixingProvider), 2, Convert.ToInt32(settings.SampleRate));
            incomingBytes = new byte[Settings.GetInstance().BufferSize * 4];
            asioOut.AudioAvailable += AsioOut_AudioAvailable;
            asioOut.Play();
            connection.Connected += delegate (object source, EventArgs args)
            {
                inputAudioPanningProvider.Pan = +1.0f;
                incomingAudioPanningProvider.Pan = -1.0f;
                isAudioReceiving = true;
                isNewAudioAvailable = false;
                receivingAudio.Start();
            };
            connection.Disconnected += delegate (object source, EventArgs args)
            {
                inputAudioPanningProvider.Pan = 0.0f;
                isAudioReceiving = false;
            };
            connection.StartMessageReceiving();
        }
Beispiel #15
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();
 }
        /// <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);
            }
        }
Beispiel #17
0
        public unsafe void asioThread()
        {
            if (mSettingsMgr.Settings.MidiInDeviceNumbers == null)
            {
                mSettingsMgr.Settings.MidiInDeviceNumbers = new List <int>();
            }
            for (int i = 0; i < mSettingsMgr.Settings.MidiInDeviceNumbers.Count(); i++)
            {
                mMidiDevice.OpenInPort(mSettingsMgr.Settings.MidiInDeviceNumbers[i]);
            }

            try
            {
#if NAUDIO_ASIO
                mAsio = new NAudio.Wave.AsioOut(mSettingsMgr.Settings.AsioDeviceNr);
#else
                mAsio = AsioDriver.SelectDriver(AsioDriver.InstalledDrivers[mSettingsMgr.Settings.AsioDeviceNr]);
#endif
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            if (mAsio != null)
            {
#if NAUDIO_ASIO
                if (mAsio != null)
                {
                    mWaveProvider = new NAudio.Wave.BufferedWaveProvider(new NAudio.Wave.WaveFormat(44100, 16, 2));
                    mAsio.InitRecordAndPlayback(mWaveProvider, 2, 44100);
                    mAsio.AudioAvailable += mAsio_AudioAvailable;
                    mAsioBuffSize         = mSettingsMgr.Settings.AsioBufferSize;
                }
#else
                int p   = mAsio.BufferSizex.PreferredSize;
                int max = mAsio.BufferSizex.MaxSize;
                int min = mAsio.BufferSizex.MinSize;

                if (mSettingsMgr.Settings.AsioBufferSize < min)
                {
                    mSettingsMgr.Settings.AsioBufferSize = min;
                    mSettingsMgr.SaveSettings();
                }

                if (mSettingsMgr.Settings.AsioBufferSize > max)
                {
                    mSettingsMgr.Settings.AsioBufferSize = max;
                    mSettingsMgr.SaveSettings();
                }
                mAsioBuffSize = mSettingsMgr.Settings.AsioBufferSize;

                // get our driver wrapper to create its buffers
                mAsio.CreateBuffers(mAsioBuffSize);
                // this is our buffer fill event we need to respond to
                mAsio.BufferUpdate += new EventHandler(asio_BufferUpdateHandler);
                mAsioOutputLeft     = mAsio.OutputChannels[0];  // Use only 1x stereo out
                mAsioOutputRight    = mAsio.OutputChannels[1];

                mAsioInputBuffers = new AudioBufferInfo(2, mAsioBuffSize);
                mAsioInputLeft    = mAsio.InputChannels[0];     // Use only 1x stereo in
                mAsioInputRight   = mAsio.InputChannels[1];
#endif
                // todo: test
                //mMixLeft = new float[mAsioBuffSize];
                //mMixRight = new float[mAsioBuffSize];

                // and off we go

                stopWatchTicksForOneAsioBuffer = (long)(Stopwatch.Frequency / (mAsio.SampleRate / mAsioBuffSize));
#if NAUDIO_ASIO
                mAsioLeftInt32LSBBuff  = new byte[mAsioBuffSize * 4];
                mAsioRightInt32LSBBuff = new byte[mAsioBuffSize * 4];
                mAsio.Play();
#else
                mAsio.Start();
#endif
                mAsioStartEvent.Set();

                // Keep running untill stop request!
                mAsioStopEvent.Wait();
                StopAsio();
            }
            else
            {
                mIsAsioRunning = false;
                mAsioStartEvent.Set();
            }
        }
Beispiel #18
0
 /// <summary>
 /// Closes this device by destroying the asioOut member of this device
 /// </summary>
 /// <returns>True if successful</returns>
 public bool Close()
 {
     try
     {
         if (asioOut != null)
         {
             asioOut.Dispose();
             asioOut = null;
         }
     }
     catch (Exception e)
     {
         return false;
     }
     return true;
 }
Beispiel #19
0
        /// <summary>
        /// Rescans all compatible audio devices and creates an asioOut object when it finds one.
        /// </summary>
        /// <remarks>
        /// If more than one device is found, the first one is used.
        /// </remarks>
        /// <returns>True if a device was successfully opened.</returns>
        public bool Rescan()
        {
            String sSummary = "NAudio version: " + Assembly.GetAssembly(typeof(AsioOut)).GetName().Version.ToString();
            Debug.WriteLine(sSummary);

            // Find a list of compatible devices.  If more than 1, choose the first.
            var asioDriverNames = AsioOut.GetDriverNames().ToList().FindAll(d => d.Contains(csMicroconeDeviceNameKey1)
                && d.Contains(csMicroconeDeviceNameKey2));
            if (asioDriverNames.Count == 0)
            {
                Debug.Print("No compatible Microcone drivers found");
                return false;
            }

            // Create device if necessary
            if (this.asioOut == null)
            {
                try
                {
                    this.asioOut = new AsioOut(asioDriverNames[0]) { InputChannelOffset = 0 };
                }
                catch (Exception e)
                {
                    Debug.Print("No Microcone devices found: {0}", e.Message);
                    return false;
                }
                int inputChannelCount = Math.Min(this.asioOut.DriverInputChannelCount, ciNumberOfMicroconeAudioChannels);
                this.asioOut.InitRecordAndPlayback(null, inputChannelCount, ciSampleRateDefault);
                this.asioOut.AudioAvailable += asioOut_AudioAvailable;
            }

            iNumberOfChannelsActual = asioOut.NumberOfInputChannels;
            return true;
        }
Beispiel #20
0
 public static bool isSupported()
 {
     return(AsioOut.GetDriverNames().Length > 0);
 }
Beispiel #21
0
		//init driver
        private void CreateAsio()
        {
        	//recreate device if necessary
        	if (this.FAsioOut != null)
        	{
        		Cleanup();
        	}
        	
        	this.FAsioOut = new AsioOut(FDriverIn[0].Name);
        	
        	this.FAsioOut.Init(FWave);
        }
Beispiel #22
0
 /// <summary>
 /// Tries creating an asio interface.
 /// </summary>
 private static bool _CreateAsio(out IWavePlayer Player)
 {
     try
     {
         Player = new AsioOut();
         return true;
     }
     catch
     {
         Player = null;
         return false;
     }
 }
 private void DeviceCB_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     tmp = new AsioOut(Convert.ToInt32(deviceCB.SelectedIndex));
     inputCB.Items.Clear();
     for (int i = 0; i < tmp.DriverInputChannelCount; i++)
     {
         inputCB.Items.Add(i + 1);
     }
     tmp.Dispose();
     tmp = null;
 }
		//init driver
        public void CreateAsio()
        {
        	//recreate device if necessary
        	if (this.AsioOut != null)
        	{
        		Cleanup();
        	}
        	
        	this.AsioOut = new AsioOut(DriverName);
        	
        	this.AsioOut.Init(MultiInputProvider);
        }
		//close ASIO
		private void Cleanup()
		{
			if (this.AsioOut != null)
			{
				this.AsioOut.Dispose();
				this.AsioOut = null;
			}
		}
Beispiel #26
0
        private void mapOutputs(AsioOut driver)
        {
            var channelCount = driver.DriverOutputChannelCount;

            receivedData = new float[channelCount][];

            var outputs = new List<ISignalSink>();
            for (int i = 0; i < channelCount; i++)
            {
                var output = new SignalSink(this, i);
                output.ReceivedData += output_ReceivedData;
                output.Name = driver.AsioOutputChannelName(i);

                receivedData[i] = null;

                outputs.Add(output);
            }
            Sinks = outputs;
        }
Beispiel #27
0
 private void DisposeAsio()
 {
     try
     {
         if (input != null)
         {
             if (input.PlaybackState != PlaybackState.Stopped)
             {
                 input.Stop();
             }
             input.Dispose();
             input = null;
         }
     }
     catch (NullReferenceException)
     { }
 }
Beispiel #28
0
        private void button2_Click(object sender, EventArgs e)
        {
            var deviceIndex = cmbDevice.SelectedIndex;

            asioDriver = new AsioOut(deviceIndex);
            asioDriver.ChannelOffset = 0;

            var monoFormat = WaveFormat.CreateIeeeFloatWaveFormat(44100, 1);
            var stereoFormat = WaveFormat.CreateIeeeFloatWaveFormat(44100, 2);

            //waveIn = new WaveIn();
            //waveOut = new WaveOut();

            //waveIn.BufferMilliseconds = 10;
            //waveOut.DesiredLatency = 10;

            new Thread(() =>
            {
                asioInput = new AsioInputMapper();
                asioOutput = new AsioOutputMapper();
                asioCard = new AsioCard(monoFormat, asioDriver, asioInput, asioOutput);
                asioCard.Start();

                //waveCard = new WaveCard(stereoFormat, waveIn, waveOut, new WaveInputMapper(), new WaveOutputMapper());
                //waveCard.Start();

                BuildAudioInChain(monoFormat);

                signalNetworkForm.SignalNetworkControl.Nodes = new ObservableCollection<ISignalNode>();

                signalNetworkForm.SignalNetworkControl.Nodes.Add(asioInput);
                signalNetworkForm.SignalNetworkControl.Nodes.Add(flangerNode);
                signalNetworkForm.SignalNetworkControl.Nodes.Add(filterNode);
                signalNetworkForm.SignalNetworkControl.Nodes.Add(fourierNode);
                signalNetworkForm.SignalNetworkControl.Nodes.Add(asioOutput);

                //BuildSineWaveChain(stereoFormat);

            }).Start();

            for (int i = 0; i < asioDriver.DriverInputChannelCount; i++)
            {
                cboChannelIn.Items.Add(asioDriver.AsioInputChannelName(i));
            }

            for (int i = 0; i < asioDriver.DriverOutputChannelCount; i++)
            {
                cboChannelOut.Items.Add(asioDriver.AsioOutputChannelName(i));
            }
        }
Beispiel #29
0
        private void mapInputs(AsioOut driver)
        {
            var inputCount = driver.DriverInputChannelCount;

            var inputs = new List<ISignalSource>();
            floatBuffer = new float[inputCount][];

            for (int i = 0; i < inputCount; i++)
            {
                var source = new SignalSource(this);
                source.Name = driver.AsioInputChannelName(i);
                inputs.Add(source);
            }
            this.Sources = inputs;
        }