Skip to content

Commit ff46bfd

Browse files
committed
update_audio_adjust_volume_error
1 parent f6c471f commit ff46bfd

File tree

7 files changed

+19
-21
lines changed

7 files changed

+19
-21
lines changed
0 Bytes
Binary file not shown.
260 Bytes
Binary file not shown.

.vs/Edge_tts_sharp/v17/.suo

-27.5 KB
Binary file not shown.
-286 KB
Binary file not shown.
-4.29 MB
Binary file not shown.

Edge_tts_sharp/Edge_tts_sharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<!--以下是我们自己添加的-->
77
<!--版本号,重要性不必多说-->
8-
<Version>1.1.3</Version>
8+
<Version>1.1.4</Version>
99
<!--添加该配置,在编译时进行打包-->
1010
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1111
<!--添加该配置才会生成注释文件,默认无注释-->

Edge_tts_sharp/Utils/Audio.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)