-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ce0cb9
commit b9b295c
Showing
12 changed files
with
277 additions
and
102 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Edge_tts_sharp.Model | ||
{ | ||
/// <summary> | ||
/// 播放音频配置参数 | ||
/// </summary> | ||
public class PlayOption | ||
{ | ||
/// <summary> | ||
/// 播放内容 | ||
/// </summary> | ||
public string Text { get; set; } | ||
/// <summary> | ||
/// 语速,是一个-100 - 100的数值 | ||
/// </summary> | ||
public int Rate { get; set; } = 0; | ||
/// <summary> | ||
/// 音量,是一个0 - 1的浮点数值 | ||
/// </summary> | ||
public float Volume { get; set; } = 1.0f; | ||
/// <summary> | ||
/// 音频保存地址 | ||
/// </summary> | ||
public string SavePath { get; set; } = string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using NAudio.Wave; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Edge_tts_sharp.Utils | ||
{ | ||
public class Mp3AudioStreamer | ||
{ | ||
private BufferedWaveProvider _bufferedWaveProvider; | ||
private WaveOutEvent _waveOut; | ||
|
||
public Mp3AudioStreamer() | ||
{ | ||
// 设定音频格式,确保与解码后的PCM数据格式一致 | ||
_bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat(44100, 16, 2)); | ||
_bufferedWaveProvider.BufferLength = 1024 * 1024; // 设置1MB缓冲区 | ||
_bufferedWaveProvider.DiscardOnBufferOverflow = true; // 避免缓冲区溢出 | ||
|
||
_waveOut = new WaveOutEvent | ||
{ | ||
DesiredLatency = 100 // 减少播放延迟 | ||
}; | ||
_waveOut.Init(_bufferedWaveProvider); | ||
_waveOut.Play(); | ||
} | ||
|
||
// 处理WebSocket的音频数据 | ||
public void OnAudioReceived(byte[] mp3Data) | ||
{ | ||
// 将 MP3 数据写入临时文件 | ||
string tempFilePath = Path.GetTempFileName() + ".mp3"; | ||
File.WriteAllBytes(tempFilePath, mp3Data); | ||
|
||
// 使用 MediaFoundationReader 解码临时文件 | ||
using (var reader = new MediaFoundationReader(tempFilePath)) | ||
{ | ||
var buffer = new byte[16384]; // 16KB 缓冲区 | ||
int bytesRead; | ||
|
||
while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0) | ||
{ | ||
_bufferedWaveProvider.AddSamples(buffer, 0, bytesRead); | ||
} | ||
} | ||
|
||
// 删除临时文件 | ||
File.Delete(tempFilePath); | ||
} | ||
|
||
|
||
|
||
public void Stop() | ||
{ | ||
_waveOut.Stop(); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.