@@ -14,30 +14,28 @@ namespace Edge_tts_sharp.Utils
1414{
1515 public static class Audio
1616 {
17- public static async Task PlayToStreamAsync ( Stream source , float volume = 1.0f , float speed = 0. 0f, CancellationToken cancellationToken = default )
17+ public static async Task PlayToStreamAsync ( Stream source , float volume = 1f , float speed = 0f , CancellationToken cancellationToken = default ( CancellationToken ) )
1818 {
19- using ( var sr = new StreamMediaFoundationReader ( source ) )
19+ StreamMediaFoundationReader sr = new StreamMediaFoundationReader ( source ) ;
20+ SmbPitchShiftingSampleProvider smbPitchShiftingSampleProvider = new SmbPitchShiftingSampleProvider ( sr . ToSampleProvider ( ) ) ;
21+ smbPitchShiftingSampleProvider . PitchFactor = ( float ) Math . Pow ( 2.0 , ( double ) speed / 100.0 ) ;
22+
23+ VolumeSampleProvider volumeProvider = new VolumeSampleProvider ( smbPitchShiftingSampleProvider ) ;
24+ volumeProvider . Volume = volume ;
25+
26+ DirectSoundOut directSoundOut = new DirectSoundOut ( ) ;
27+ directSoundOut . Init ( volumeProvider . ToWaveProvider ( ) ) ;
28+ directSoundOut . Play ( ) ;
29+
30+ while ( directSoundOut . PlaybackState == PlaybackState . Playing )
2031 {
21- var sampleProvider = sr . ToSampleProvider ( ) ;
22- var pitchShiftingProvider = new SmbPitchShiftingSampleProvider ( sampleProvider ) ;
23- using ( var directSoundOut = new DirectSoundOut ( ) )
32+ if ( cancellationToken . IsCancellationRequested )
2433 {
25- pitchShiftingProvider . PitchFactor = ( float ) Math . Pow ( 2.0 , speed / 100.0 ) ;
26- var waveProvider = pitchShiftingProvider . ToWaveProvider ( ) ;
27- directSoundOut . Init ( waveProvider ) ;
28- directSoundOut . Volume = volume ;
29- directSoundOut . Play ( ) ;
30-
31- while ( directSoundOut . PlaybackState == PlaybackState . Playing )
32- {
33- if ( cancellationToken . IsCancellationRequested )
34- {
35- directSoundOut . Stop ( ) ;
36- break ;
37- }
38- await Task . Delay ( 1000 , cancellationToken ) ;
39- }
34+ directSoundOut . Stop ( ) ;
35+ break ;
4036 }
37+
38+ await Task . Delay ( 1000 , cancellationToken ) ;
4139 }
4240 }
4341
0 commit comments