Example #1
0
        public BassAudioProvider(BassItem device)
        {
            if (device == null)
            {
                throw new ArgumentNullException();
            }

            ManagedBass.Bass.RecordInit(device.Id);

            var devInfo = ManagedBass.Bass.RecordGetDeviceInfo(device.Id);

            if (devInfo.IsLoopback)
            {
                var playbackDevice = FindPlaybackDevice(devInfo);

                if (playbackDevice != -1)
                {
                    ManagedBass.Bass.Init(playbackDevice);
                    ManagedBass.Bass.CurrentDevice = playbackDevice;

                    _silenceHandle = ManagedBass.Bass.CreateStream(44100, 2, BassFlags.Default, Extensions.SilenceStreamProcedure);

                    ManagedBass.Bass.ChannelSetAttribute(_silenceHandle, ChannelAttribute.Volume, 0);
                }
            }

            ManagedBass.Bass.CurrentRecordingDevice = device.Id;

            var info = ManagedBass.Bass.RecordingInfo;

            _recordingHandle = ManagedBass.Bass.RecordStart(info.Frequency, info.Channels, BassFlags.RecordPause, RecordProcedure);
        }
        private void InitDevice(BassItem device)
        {
            _devices.Add(device.Id, new RecordingItem
            {
                DeviceId = device.Id
            });

            device.PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName != nameof(device.Active))
                {
                    return;
                }

                if (device.Active)
                {
                    AddDevice(device);
                }
                else
                {
                    RemoveDevice(device);
                }
            };

            if (device.Active)
            {
                AddDevice(device);
            }
        }
        private void AddDevice(BassItem device)
        {
            lock (_syncLock)
            {
                if (_devices[device.Id].RecordingHandle != 0)
                {
                    return;
                }

                ManagedBass.Bass.RecordInit(device.Id);

                var devInfo = ManagedBass.Bass.RecordGetDeviceInfo(device.Id);

                if (devInfo.IsLoopback)
                {
                    var playbackDevice = FindPlaybackDevice(devInfo);

                    if (playbackDevice != -1)
                    {
                        ManagedBass.Bass.Init(playbackDevice);
                        ManagedBass.Bass.CurrentDevice = playbackDevice;

                        var silence = ManagedBass.Bass.CreateStream(44100, 2, BassFlags.Default, Extensions.SilenceStreamProcedure);

                        ManagedBass.Bass.ChannelSetAttribute(silence, ChannelAttribute.Volume, 0);

                        _devices[device.Id].SilenceHandle = silence;
                    }
                }

                ManagedBass.Bass.CurrentRecordingDevice = device.Id;

                var info = ManagedBass.Bass.RecordingInfo;

                var handle = ManagedBass.Bass.RecordStart(info.Frequency, info.Channels, BassFlags.RecordPause, null);

                _devices[device.Id].RecordingHandle = handle;

                BassMix.MixerAddChannel(_mixer, handle, BassFlags.MixerDownMix | BassFlags.MixerLimit);

                if (_running)
                {
                    ManagedBass.Bass.ChannelPlay(handle);
                }
            }
        }
        private void RemoveDevice(BassItem device)
        {
            lock (_syncLock)
            {
                if (_devices[device.Id].RecordingHandle == 0)
                {
                    return;
                }

                var handle = _devices[device.Id].RecordingHandle;

                BassMix.MixerRemoveChannel(handle);

                ManagedBass.Bass.StreamFree(handle);

                _devices[device.Id].RecordingHandle = 0;

                ManagedBass.Bass.StreamFree(_devices[device.Id].SilenceHandle);

                _devices[device.Id].SilenceHandle = 0;
            }
        }