Exemplo n.º 1
0
        public void start(double delay)
        {
            latency = (float)delay / 10;
            var str   = (inlist.Items[inlist.SelectedIndex] as string);
            var array = str.Split(' ');

            sourceIndex = Convert.ToInt32(array[0]);
            str         = (outlist.Items[outlist.SelectedIndex] as string);
            array       = str.Split(' ');
            outIndex    = Convert.ToInt32(array[0]);
            Console.WriteLine(sourceIndex + " " + outIndex);
            Console.WriteLine(delay);

            stop();
            Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero); // "No Sound" device init

            // Garbage collector can't track the reference BassWasapi holds,
            // so we're adding a reference here to prevent garbage collection
            inProc  = sourceWasapiProc;
            outProc = outWasapiProc;
            BassWasapi.BASS_WASAPI_Init(sourceIndex, 44100, 0, 0, 1, 0, inProc, IntPtr.Zero);
            BassWasapi.BASS_WASAPI_Init(outIndex, 44100, 0, 0, 4 * latency, 1 * latency, outProc, IntPtr.Zero);

            BassWasapi.BASS_WASAPI_SetDevice(outIndex);
            pushstr = Bass.BASS_StreamCreatePush(44100, 2, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT, IntPtr.Zero);
            outstr  = BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT);
            BassMix.BASS_Mixer_StreamAddChannel(outstr, pushstr, 0);

            BassWasapi.BASS_WASAPI_SetDevice(sourceIndex);
            BassWasapi.BASS_WASAPI_Start();
            BassWasapi.BASS_WASAPI_SetDevice(outIndex);
            BassWasapi.BASS_WASAPI_Start();
        }
Exemplo n.º 2
0
 public static void Stop()
 {
     // Stop WASAPI
     BassWasapi.BASS_WASAPI_SetDevice(InputDevice);
     BassWasapi.BASS_WASAPI_Stop(false);
     BassWasapi.BASS_WASAPI_SetDevice(OutputDevice);
     BassWasapi.BASS_WASAPI_Stop(false);
 }
Exemplo n.º 3
0
 public static void Start()
 {
     // Start WASAPI
     BassWasapi.BASS_WASAPI_SetDevice(InputDevice);
     BassWasapi.BASS_WASAPI_Start();
     BassWasapi.BASS_WASAPI_SetDevice(OutputDevice);
     BassWasapi.BASS_WASAPI_Start();
 }
Exemplo n.º 4
0
 public void stop()
 {
     BassWasapi.BASS_WASAPI_SetDevice(outIndex);
     BassWasapi.BASS_WASAPI_Stop(true);
     BassWasapi.BASS_WASAPI_Free();
     Bass.BASS_Free();
     BassWasapi.BASS_WASAPI_SetDevice(sourceIndex);
     BassWasapi.BASS_WASAPI_Stop(true);
     BassWasapi.BASS_WASAPI_Free();
     Bass.BASS_Free();
 }
        public override void SetInputStream(BassStream stream, bool passThrough)
        {
            if (_deviceState != DeviceState.Stopped)
            {
                throw new BassPlayerException("Device state is not 'DeviceState.Stopped'");
            }

            _inputStream = stream;
            _flags       = BASSWASAPIInit.BASS_WASAPI_AUTOFORMAT | BASSWASAPIInit.BASS_WASAPI_BUFFER;

            // If Exclusive mode is used, check, if that would be supported, otherwise init in shared mode
            bool isExclusive = Controller.GetSettings().WASAPIExclusiveMode;

            if (isExclusive)
            {
                _flags |= BASSWASAPIInit.BASS_WASAPI_EXCLUSIVE;

                BASSWASAPIFormat wasapiFormat = BassWasapi.BASS_WASAPI_CheckFormat(_deviceNo,
                                                                                   _inputStream.SampleRate,
                                                                                   _inputStream.Channels,
                                                                                   BASSWASAPIInit.BASS_WASAPI_EXCLUSIVE);
                if (wasapiFormat == BASSWASAPIFormat.BASS_WASAPI_FORMAT_UNKNOWN)
                {
                    Log.Info("BASS: WASAPI exclusive mode not directly supported for samplerate of {0} and {1} channels", _inputStream.SampleRate, _inputStream.Channels);
                    isExclusive = false;
                }
            }

retry:
            if (!isExclusive)
            {
                Log.Debug("BASS: Init WASAPI shared mode with Event driven system enabled.");
                _flags &= ~BASSWASAPIInit.BASS_WASAPI_EXCLUSIVE;
                _flags |= BASSWASAPIInit.BASS_WASAPI_SHARED | BASSWASAPIInit.BASS_WASAPI_EVENT;
            }

            Log.Debug("BASS: Try to init WASAPI with a samplerate of {0} and {1} channels", _inputStream.SampleRate, _inputStream.Channels);

            bool result = BassWasapi.BASS_WASAPI_Init(_deviceNo, _inputStream.SampleRate, _inputStream.Channels, _flags, 0.5f, 0f, _streamWriteProcDelegate, IntPtr.Zero);

            BASSError?bassInitErrorCode = result ? null : new BASSError?(Bass.BASS_ErrorGetCode());

            if (bassInitErrorCode.HasValue)
            {
                if (bassInitErrorCode.Value == BASSError.BASS_ERROR_ALREADY)
                {
                    if (!BassWasapi.BASS_WASAPI_SetDevice(_deviceNo))
                    {
                        throw new BassLibraryException("BASS_WASAPI_SetDevice");
                    }
                }
                else if (isExclusive)
                {
                    // Allow one retry in shared mode
                    Log.Warn("BASS: Failed to initialize WASAPI exclusive mode for samplerate of {0} and {1} channels. Trying fallback to shared mode.", _inputStream.SampleRate, _inputStream.Channels);
                    isExclusive = false;
                    goto retry;
                }
                else
                {
                    throw new BassLibraryException("BASS_WASAPI_Init");
                }
            }

            // If the GetDeviceNo() method returned BassConstants.BassDefaultDevice, we must request the actual device number
            // of the choosen default device
            _deviceNo = BassWasapi.BASS_WASAPI_GetDevice();

            CollectDeviceInfo(_deviceNo);
            BASS_WASAPI_INFO wasapiInfo = BassWasapi.BASS_WASAPI_GetInfo();

            Log.Debug("BASS: ---------------------------------------------");
            Log.Debug("BASS: Buffer Length: {0}", wasapiInfo.buflen);
            Log.Debug("BASS: Channels: {0}", wasapiInfo.chans);
            Log.Debug("BASS: Frequency: {0}", wasapiInfo.freq);
            Log.Debug("BASS: Format: {0}", wasapiInfo.format.ToString());
            Log.Debug("BASS: InitFlags: {0}", wasapiInfo.initflags.ToString());
            Log.Debug("BASS: Exclusive: {0}", wasapiInfo.IsExclusive.ToString());
            Log.Debug("BASS: ---------------------------------------------");
            Log.Info("BASS: WASAPI Device successfully initialised");

            // For shared mode we require a mixer to change the sampling rates of input stream to device output stream.
            if (!wasapiInfo.IsExclusive)
            {
                // Recreate Mixer with new value
                Log.Debug("BASS: Creating new {0} channel mixer for frequency {1}", wasapiInfo.chans, wasapiInfo.freq);
                _mixerHandle = BassMix.BASS_Mixer_StreamCreate(wasapiInfo.freq, wasapiInfo.chans, MIXER_FLAGS);
                if (_mixerHandle == BassConstants.BassInvalidHandle)
                {
                    throw new BassLibraryException("BASS_Mixer_StreamCreate");
                }
                _mixer = BassStream.Create(_mixerHandle);
                AttachStream();
            }

            int ms = Convert.ToInt32(Controller.GetSettings().DirectSoundBufferSize.TotalMilliseconds);

            if (!Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_BUFFER, ms))
            {
                throw new BassLibraryException("BASS_SetConfig");
            }

            // Enable update thread while the output device is active
            if (!Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_UPDATEPERIOD, ms / 4))
            {
                throw new BassLibraryException("BASS_SetConfig");
            }

            if (passThrough)
            {
                _fader = new BassStreamFader(_inputStream, Controller.GetSettings().FadeDuration);
            }

            ResetState();
        }
Exemplo n.º 6
0
        public static void SetDevice(int inputDevice, int outputDevice)
        {
            thisControl = PlayerControl.MainFormControl;

            //Get some info about their selected input device
            var inputdeviceinfo = BassWasapi.BASS_WASAPI_GetDeviceInfo(inputDevice);

            InputDevice     = inputDevice;
            InputDeviceInfo = inputdeviceinfo;

            //Get some info about their selected ouput device
            var outputdeviceinfo = BassWasapi.BASS_WASAPI_GetDeviceInfo(outputDevice);

            OutputDevice     = outputDevice;
            OutputDeviceInfo = outputdeviceinfo;


            if (!m_isWasapiInit)
            {
                Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_UPDATEPERIOD, 0);
                Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_FLOATDSP, true);
                Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, PlayerControl.MainFormControl.Handle);

                Player.Mixer = MixerStreamCreate(outputdeviceinfo.mixfreq);

                if (Player.Mixer == 0)
                {
                    var error = Bass.BASS_ErrorGetCode();
                    MessageBox.Show(error.ToString(), "Could not create mixer!");
                    Bass.BASS_Free();
                    return;
                }

                _outstream = Player.Mixer;

                _instream = Bass.BASS_StreamCreatePush(inputdeviceinfo.mixfreq, inputdeviceinfo.mixchans, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE, IntPtr.Zero);
                // create a stream to receive the data
                // string fn = @"D:\tanie\karaokeNow Resources\LdVocal_01.wav";
                // _instream = Bass.BASS_StreamCreateFile(fn, 0L, 0L, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_LOOP);

                //_stream[0] = BassMix.BASS_Split_StreamCreate(_instream, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE, null);//_instream;
                //_stream[1] = BassMix.BASS_Split_StreamCreate(_instream, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE, null);// _instream;
                //_stream[2] = BassMix.BASS_Split_StreamCreate(_instream, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE, null);//_instream;
                //_stream[3] = BassMix.BASS_Split_StreamCreate(_instream, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE, null);//_instream;


                //  _outstream = BassMix.BASS_Mixer_StreamCreate(outputdeviceinfo.mixfreq, outputdeviceinfo.mixchans, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE);
                //  Player.Mixer = _outstream;

                //Group 1 VST Effects ============================================================
                Channel1Fx.SetEffects(_instream);


                //Group 2 VST Effects ============================================================
                // Channel2Fx.SetEffects(_stream[1]);


                //Group 3 VST Effects ============================================================
                //Channel3Fx.SetEffects(_stream[2]);


                //Group 4 VST Effects ============================================================
                //Channel4Fx.SetEffects(_stream[3]);

                BassMix.BASS_Mixer_StreamAddChannel(_outstream, _instream, 0);
                // BassMix.BASS_Mixer_StreamAddChannel(_outstream, _stream[1], 0);
                //BassMix.BASS_Mixer_StreamAddChannel(_outstream, _stream[2], 0);
                // BassMix.BASS_Mixer_StreamAddChannel(_outstream, _stream[3], 0);

                m_isWasapiInit = true;
            }

            _wasapiInProc = new WASAPIPROC(InputProc);
            GC.KeepAlive(_wasapiInProc);


            _wasapiOutProc = new WASAPIPROC(OutputProc);
            GC.KeepAlive(_wasapiOutProc);

            //Set the input device so subsequent calls are on it
            BassWasapi.BASS_WASAPI_SetDevice(InputDevice);
            // Initialize BASS WASAPI input
            BASS_WASAPI_INFO input = new BASS_WASAPI_INFO();

            BassWasapi.BASS_WASAPI_GetInfo(input);

            BassWasapi.BASS_WASAPI_Init(inputDevice, inputdeviceinfo.mixfreq, inputdeviceinfo.mixchans,
                                        BASSWASAPIInit.BASS_WASAPI_EVENT | BASSWASAPIInit.BASS_WASAPI_BUFFER | BASSWASAPIInit.BASS_WASAPI_SHARED, input.buflen, 0, _wasapiInProc, IntPtr.Zero);

            //Set the output device so subsequent calls are on it
            BassWasapi.BASS_WASAPI_SetDevice(OutputDevice);
            BASS_WASAPI_INFO output = new BASS_WASAPI_INFO();

            BassWasapi.BASS_WASAPI_GetInfo(output);

            // Initialize BASS WASAPI output
            BassWasapi.BASS_WASAPI_Init(outputDevice, outputdeviceinfo.mixfreq, outputdeviceinfo.mixchans,
                                        BASSWASAPIInit.BASS_WASAPI_EVENT | BASSWASAPIInit.BASS_WASAPI_SHARED, output.buflen, 0, _wasapiOutProc, IntPtr.Zero);
        }