11using System ;
22using System . Collections . Generic ;
3- using System . Linq ;
43using ManagedBass ;
54using ManagedBass . Mix ;
65using System . Runtime . InteropServices ;
@@ -21,11 +20,11 @@ class RecordingItem
2120 public int DeviceId { get ; set ; }
2221
2322 public int RecordingHandle { get ; set ; }
23+
24+ public int SilenceHandle { get ; set ; }
2425 }
2526
2627 readonly Dictionary < int , RecordingItem > _devices = new Dictionary < int , RecordingItem > ( ) ;
27- readonly int _filler ; // Fill when no audio source is selected
28- bool _fillerAdded ;
2928 readonly int _mixer ;
3029 readonly object _syncLock = new object ( ) ;
3130 bool _running ;
@@ -42,13 +41,14 @@ public MixedAudioProvider(IEnumerable<BassItem> Devices, int FrameRate, bool Mut
4241
4342 Bass . UpdatePeriod = updatePeriod . Clip ( 5 , 100 ) ;
4443
44+ Console . WriteLine ( $ "BASS Update Period: { Bass . UpdatePeriod } ") ;
45+
4546 for ( var i = 0 ; i < BufferCount ; ++ i )
4647 {
4748 _buffers . Add ( new byte [ 0 ] ) ;
4849 }
4950
50- _mixer = BassMix . CreateMixerStream ( 44100 , 2 , BassFlags . Default ) ;
51- _filler = Bass . CreateStream ( 44100 , 2 , BassFlags . Decode , Extensions . SilenceStreamProcedure ) ;
51+ _mixer = BassMix . CreateMixerStream ( 44100 , 2 , BassFlags . MixerNonStop ) ;
5252
5353 foreach ( var recordingDevice in Devices )
5454 {
@@ -99,7 +99,22 @@ void RemoveDevice(BassItem Device)
9999 Bass . StreamFree ( handle ) ;
100100
101101 _devices [ Device . Id ] . RecordingHandle = 0 ;
102+
103+ Bass . StreamFree ( _devices [ Device . Id ] . SilenceHandle ) ;
104+
105+ _devices [ Device . Id ] . SilenceHandle = 0 ;
106+ }
107+ }
108+
109+ int FindPlaybackDevice ( DeviceInfo LoopbackDeviceInfo )
110+ {
111+ for ( var i = 0 ; Bass . GetDeviceInfo ( i , out var info ) ; ++ i )
112+ {
113+ if ( info . Driver == LoopbackDeviceInfo . Driver )
114+ return i ;
102115 }
116+
117+ return - 1 ;
103118 }
104119
105120 void AddDevice ( BassItem Device )
@@ -110,6 +125,26 @@ void AddDevice(BassItem Device)
110125 return ;
111126
112127 Bass . RecordInit ( Device . Id ) ;
128+
129+ var devInfo = Bass . RecordGetDeviceInfo ( Device . Id ) ;
130+
131+ if ( devInfo . IsLoopback )
132+ {
133+ var playbackDevice = FindPlaybackDevice ( devInfo ) ;
134+
135+ if ( playbackDevice != - 1 )
136+ {
137+ Bass . Init ( playbackDevice ) ;
138+ Bass . CurrentDevice = playbackDevice ;
139+
140+ var silence = Bass . CreateStream ( 44100 , 2 , BassFlags . Default , Extensions . SilenceStreamProcedure ) ;
141+
142+ Bass . ChannelSetAttribute ( silence , ChannelAttribute . Volume , 0 ) ;
143+
144+ _devices [ Device . Id ] . SilenceHandle = silence ;
145+ }
146+ }
147+
113148 Bass . CurrentRecordingDevice = Device . Id ;
114149
115150 var info = Bass . RecordingInfo ;
@@ -175,12 +210,14 @@ public void Dispose()
175210 {
176211 Bass . StreamFree ( _mixer ) ;
177212
178- foreach ( var rec in _devices . Values . Where ( M => M . RecordingHandle != 0 ) )
213+ foreach ( var rec in _devices . Values )
179214 {
180- Bass . StreamFree ( rec . RecordingHandle ) ;
181- }
215+ if ( rec . RecordingHandle != 0 )
216+ Bass . StreamFree ( rec . RecordingHandle ) ;
182217
183- Bass . StreamFree ( _filler ) ;
218+ if ( rec . SilenceHandle != 0 )
219+ Bass . StreamFree ( rec . SilenceHandle ) ;
220+ }
184221
185222 _running = false ;
186223 }
@@ -193,21 +230,17 @@ public void Start()
193230 {
194231 lock ( _syncLock )
195232 {
196- foreach ( var rec in _devices . Values . Where ( M => M . RecordingHandle != 0 ) )
233+ foreach ( var rec in _devices . Values )
197234 {
198- Bass . ChannelPlay ( rec . RecordingHandle ) ;
235+ if ( rec . SilenceHandle != 0 )
236+ Bass . ChannelPlay ( rec . SilenceHandle ) ;
237+
238+ if ( rec . RecordingHandle != 0 )
239+ Bass . ChannelPlay ( rec . RecordingHandle ) ;
199240 }
200241
201242 Bass . ChannelPlay ( _mixer ) ;
202243
203- if ( ! _fillerAdded )
204- {
205- // Add Filler only after mixer has started
206- BassMix . MixerAddChannel ( _mixer , _filler , BassFlags . Default ) ;
207-
208- _fillerAdded = true ;
209- }
210-
211244 _running = true ;
212245 }
213246 }
@@ -221,9 +254,13 @@ public void Stop()
221254 {
222255 Bass . ChannelPause ( _mixer ) ;
223256
224- foreach ( var rec in _devices . Values . Where ( M => M . RecordingHandle != 0 ) )
257+ foreach ( var rec in _devices . Values )
225258 {
226- Bass . ChannelPause ( rec . RecordingHandle ) ;
259+ if ( rec . RecordingHandle != 0 )
260+ Bass . ChannelPause ( rec . RecordingHandle ) ;
261+
262+ if ( rec . SilenceHandle != 0 )
263+ Bass . ChannelPause ( rec . SilenceHandle ) ;
227264 }
228265
229266 _running = false ;
0 commit comments