// STAThread is ESSENTIAL to make this work
		[STAThread] public static void Main(string[] args)
		{
            // no messing, this is high priority stuff
			Thread.CurrentThread.Priority = ThreadPriority.Highest;
            Process myP = Process.GetCurrentProcess();
            myP.ProcessorAffinity = (IntPtr)1;
            
			// make sure we have at least one ASIO driver installed
			if (AsioDriver.InstalledDrivers.Length == 0)
			{
				Console.WriteLine("There appears to be no ASIO drivers installed on your system.");
				Console.WriteLine("If your soundcard supports ASIO natively, install the driver");
				Console.WriteLine("from the support disc. If your soundcard has no native ASIO support");
				Console.WriteLine("you can probably use the generic ASIO4ALL driver.");
				Console.WriteLine("You can download this from: http://www.asio4all.com/");
				Console.WriteLine("It's very good!");
				Console.WriteLine();
				Console.WriteLine("Hit Enter to exit...");
				Console.ReadLine();
				return;
			}

            // bingo, we've got at least one
			Console.WriteLine("Your system has the following ASIO drivers installed:");
			Console.WriteLine();

            // so iterate through them
			for (int index = 0; index < AsioDriver.InstalledDrivers.Length; index++)
			{
                // and display them
				Console.WriteLine(string.Format("  {0}. {1}", index + 1, AsioDriver.InstalledDrivers[index]));
			}

			Console.WriteLine();

			int driverNumber = 0;

            // get them to choose one
			while (driverNumber < 1 || driverNumber > AsioDriver.InstalledDrivers.Length)
			{
				// we'll keep telling them this until they make a valid selection
				Console.Write("Select which driver you wish to use (x for exit): ");
				ConsoleKeyInfo key = Console.ReadKey();
				Console.WriteLine();

				// deal with exit condition
				if (key.KeyChar == 'x') return;

				// convert from ASCII to int
				driverNumber = key.KeyChar - 48;
			}

			Console.WriteLine();
			Console.WriteLine("Using: " + AsioDriver.InstalledDrivers[driverNumber - 1]);
			Console.WriteLine();

			// load and activate the desited driver
			AsioDriver driver = AsioDriver.SelectDriver(AsioDriver.InstalledDrivers[driverNumber - 1]);

			// popup the driver's control panel for configuration
            driver.ShowControlPanel();

			// now dump some details
            Console.WriteLine("  Driver name = " + driver.DriverName);
            Console.WriteLine("  Driver version = " + driver.Version);
            Console.WriteLine("  Input channels = " + driver.NumberInputChannels);
            Console.WriteLine("  Output channels = " + driver.NumberOutputChannels);
            Console.WriteLine("  Min buffer size = " + driver.BufferSizex.MinSize);
            Console.WriteLine("  Max buffer size = " + driver.BufferSizex.MaxSize);
            Console.WriteLine("  Preferred buffer size = " + driver.BufferSizex.PreferredSize);
            Console.WriteLine("  Granularity = " + driver.BufferSizex.Granularity);
            Console.WriteLine("  Sample rate = " + driver.SampleRate);

			// get our driver wrapper to create its buffers
			driver.CreateBuffers(false);

			// write out the input channels
            Console.WriteLine("  Input channels found = " + driver.InputChannels.Length);
			Console.WriteLine("  ----");

            foreach (Channel channel in driver.InputChannels)
			{
				Console.WriteLine(channel.Name);
			}

			// and the output channels
            Console.WriteLine("  Output channels found = " + driver.OutputChannels.Length);
            Console.WriteLine("----");

            foreach (Channel channel in driver.OutputChannels)
			{
				Console.WriteLine(channel.Name);
			}

            Console.Write("Select which effect you wish to use (1 = delay, 2 = flanger, 3 = phaser, 4 = reverb): ");
            ConsoleKeyInfo useEffect = Console.ReadKey();
            if (useEffect.KeyChar == '1')
                effectType = effect.delay;
            else if (useEffect.KeyChar == '2')
                effectType = effect.flanger;
            else if (useEffect.KeyChar == '3')
                effectType = effect.phaser;
            else if (useEffect.KeyChar == '4')
                effectType = effect.reverb;
            else
                effectType = effect.none;



            // create standard sized buffers with a size of PreferredSize x MaxBuffers 
            _delayBuffer = new float[driver.BufferSizex.PreferredSize, MaxBuffers];

            // create a feedback buffer for the delay effect
            _delayFBbuffer = new float[driver.BufferSizex.PreferredSize, MaxBuffers];

            // create a input buffer for reverb effect
            _delayINbuffer = new float[driver.BufferSizex.PreferredSize, MaxBuffers];

            // create a output buffer for reverb effect
            _delayOUTbuffer = new float[driver.BufferSizex.PreferredSize, MaxBuffers];

            // this is our buffer fill event we need to respond to
            driver.BufferUpdate += new EventHandler(AsioDriver_BufferUpdate);

            // and off we go
            driver.Start();

            // create a wav file for recording
            wav.Create("test.wav", true, 44100, 16);

            // wait for enter key
            Console.WriteLine();
            Console.WriteLine("Press Enter to end");
			Console.ReadLine();

            // and all done
            driver.Stop();

            // close the wav file
            wav.Close();

        }
Esempio n. 2
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();
            }
        }
Esempio n. 3
0
        public static void StartDriver()
        {
            // This apartment state is required for the ASIOOutput.StartDriver method
            // If an execption is thrown here that's because you need [STAThread] attribute on your static void Main(string[] args) method
            // or you need to set this thread's ApartmentState to STA before starting the thread.
            Thread.CurrentThread.SetApartmentState(ApartmentState.STA);

            // make sure we have at least one ASIO driver installed
            if (AsioDriver.InstalledDrivers.Length == 0)
            {
                Console.WriteLine("There appears to be no ASIO drivers installed on your system.");
                Console.WriteLine("If your soundcard supports ASIO natively, install the driver");
                Console.WriteLine("from the support disc. If your soundcard has no native ASIO support");
                Console.WriteLine("you can probably use the generic ASIO4ALL driver.");
                Console.WriteLine("You can download this from: http://www.asio4all.com/");
                Console.WriteLine("It's very good!");
                Console.WriteLine();
                Console.WriteLine("Hit Enter to exit...");
                Console.ReadLine();
                return;
            }

            // bingo, we've go at least one
            Console.WriteLine("Your system has the following ASIO drivers installed:");
            Console.WriteLine();

            // so iterate through them
            for (int index = 0; index < AsioDriver.InstalledDrivers.Length; index++)
            {
                // and display them
                Console.WriteLine($"  {index + 1}. {AsioDriver.InstalledDrivers[index]}");
            }

            Console.WriteLine();

            // Currently hardcoded: todo: make it not.
            int driverNumber = 2;

            Console.WriteLine();
            Console.WriteLine("Using: " + AsioDriver.InstalledDrivers[driverNumber - 1]);
            Console.WriteLine();

            // load and activate the desited driver
            AsioDriver driver = AsioDriver.SelectDriver(AsioDriver.InstalledDrivers[driverNumber - 1]);

            // popup the driver's control panel for configuration
            //driver.ShowControlPanel();

            // now dump some details
            Console.WriteLine("  Driver name = " + driver.DriverName);
            Console.WriteLine("  Driver version = " + driver.Version);
            Console.WriteLine("  Input channels = " + driver.NumberInputChannels);
            Console.WriteLine("  Output channels = " + driver.NumberOutputChannels);
            Console.WriteLine("  Min buffer size = " + driver.BufferSizex.MinSize);
            Console.WriteLine("  Max buffer size = " + driver.BufferSizex.MaxSize);
            Console.WriteLine("  Preferred buffer size = " + driver.BufferSizex.PreferredSize);
            Console.WriteLine("  Granularity = " + driver.BufferSizex.Granularity);
            Console.WriteLine("  Sample rate = " + driver.SampleRate);

            if (driver.SampleRate != FrameOutput.SAMPLES_PER_SECOND)
            {
                throw new Exception("Driver sample rate is different than what the game expects. Please adjust driver settings to have a " + FrameOutput.SAMPLES_PER_SECOND + " sample rate.");
            }

            // get our driver wrapper to create its buffers
            driver.CreateBuffers(false);

            // write out the input channels
            Console.WriteLine("  Input channels found = " + driver.InputChannels.Length);
            Console.WriteLine("  ----");

            foreach (Channel channel in driver.InputChannels)
            {
                Console.WriteLine(channel.Name);
            }

            // and the output channels
            Console.WriteLine("  Output channels found = " + driver.OutputChannels.Length);
            Console.WriteLine("----");

            foreach (Channel channel in driver.OutputChannels)
            {
                Console.WriteLine(channel.Name);
            }

            // this is our buffer fill event we need to respond to
            driver.BufferUpdate += new EventHandler(AsioDriver_BufferUpdate);

            // and off we go
            driver.Start();

            //while(true)
            //{
            //	Thread.Sleep(100);
            //}

            //// and all donw
            //driver.Stop();
        }