This commit is contained in:
zpc 2025-03-29 03:43:31 +08:00
parent 185c9b6739
commit b7ab90865c
4 changed files with 62 additions and 62 deletions

View File

@ -105,12 +105,12 @@ public class AudioFilesConfig
/// 电话接起音频文件路径
/// </summary>
public string PhonePickupFile { get; set; } = "电话接起.mp3";
/// <summary>
/// 提示用户录音音频文件路径
/// </summary>
public string PromptUserRecordFile { get; set; } = "提示用户录音.mp3";
/// <summary>
/// 滴提示音文件路径
/// </summary>
@ -120,7 +120,7 @@ public class AudioFilesConfig
/// 数字按键音频文件模板0-9
/// </summary>
public string DigitToneFileTemplate { get; set; } = "{0}.mp3";
/// <summary>
/// 按键音的最小播放时间(毫秒),即使按键释放也会至少播放这么长时间
/// </summary>
@ -146,6 +146,11 @@ public class AudioFilesConfig
/// </summary>
public float PromptAfterBeepVolume { get; set; } = 1.0f;
/// <summary>
/// 滴提示音音量倍数 (正常音量为1.0)
/// </summary>
public float BeepPromptVolume { get; set; } = 1.0f;
/// <summary>
/// 风铃提示音文件名
/// </summary>
@ -159,7 +164,7 @@ public class AudioFilesConfig
public string GetFullPath(string fileName)
{
return Path.Combine(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AudioBasePath),
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AudioBasePath),
fileName);
}
@ -183,12 +188,12 @@ public class DialConfig
/// 拨号所需的最小位数
/// </summary>
public int MinDigitsToDialOut { get; set; } = 8;
/// <summary>
/// 无新按键后自动拨出的等待秒数
/// </summary>
public int AutoDialOutAfterSeconds { get; set; } = 5;
/// <summary>
/// 位数不足时,无操作重置的等待秒数
/// </summary>
@ -204,32 +209,32 @@ public class RecordingConfig
/// 录音保存文件夹
/// </summary>
public string RecordingFolder { get; set; } = "recordings";
/// <summary>
/// 录音设备编号
/// </summary>
public int RecordingDeviceNumber { get; set; } = 0;
/// <summary>
/// 录音采样率
/// </summary>
public int SampleRate { get; set; } = 16000;
/// <summary>
/// 录音通道数1=单声道2=立体声)
/// </summary>
public int Channels { get; set; } = 1;
/// <summary>
/// 录音缓冲区大小(毫秒)
/// </summary>
public int BufferMilliseconds { get; set; } = 100;
/// <summary>
/// 静音检测阈值
/// </summary>
public float SilenceThreshold { get; set; } = 0.02f;
/// <summary>
/// 无声音自动挂断的时间(秒)
/// </summary>
@ -239,22 +244,22 @@ public class RecordingConfig
/// 是否允许用户手动挂断(按回车键)
/// </summary>
public bool AllowUserHangup { get; set; } = true;
/// <summary>
/// 是否上传录音文件到服务器
/// </summary>
public bool UploadRecordingToServer { get; set; } = false;
/// <summary>
/// 是否在录音时播放背景音乐
/// </summary>
public bool EnableBackgroundMusic { get; set; } = true;
/// <summary>
/// 录音背景音乐文件名
/// </summary>
public string BackgroundMusicFile { get; set; } = "bj.mp3";
/// <summary>
/// 背景音乐音量 (0.0-1.0)
/// </summary>
@ -263,7 +268,7 @@ public class RecordingConfig
/// <summary>
/// 无声音播放风铃提示的时间(秒)
/// </summary>
public float WindChimePromptSeconds { get; set; } = 7.5f;
public float WindChimePromptSeconds { get; set; } = 7f;
/// <summary>
/// 风铃声淡出的时间(毫秒)
@ -280,12 +285,12 @@ public class CallFlowConfig
/// 等待接电话音频的最小持续时间(秒)
/// </summary>
public int WaitForPickupMinSeconds { get; set; } = 3;
/// <summary>
/// 等待接电话音频的最大持续时间(秒)
/// </summary>
public int WaitForPickupMaxSeconds { get; set; } = 6;
/// <summary>
/// 播放电话接起音频的概率0-1之间的小数
/// </summary>
@ -295,4 +300,4 @@ public class CallFlowConfig
/// 用户挂断后是否重置系统false表示直接退出程序
/// </summary>
public bool ResetSystemAfterHangup { get; set; } = true;
}
}

View File

@ -33,6 +33,7 @@
"WaitingToneVolume": 1.0, // 待机嘟声音量倍数 (0.0-1.0)
"DialToneVolume": 0.9, // 拨出嘟声音量倍数 (0.0-1.0)
"PromptAfterBeepVolume": 1.0, // 提示留言音量倍数 (0.0-1.0)
"BeepPromptVolume": 1.0, // 滴提示音音量倍数 (0.0-1.0)
"WindChimeFile": "风铃.wav" // 风铃提示音文件名
},
"Dial": {

View File

@ -956,10 +956,18 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
{
_lastSpeechTime = DateTime.Now;
// 如果风铃声正在播放,平滑停止它
// 如果风铃声正在播放,先停止风铃声
if (_isWindChimePlaying)
{
Console.WriteLine("检测到用户说话,停止风铃声...");
_ = StopWindChime(true); // 平滑淡出
// 风铃声停止后,重新启动背景音乐
if (!_isBackgroundMusicPlaying && _config.Recording.EnableBackgroundMusic)
{
Console.WriteLine("重新启动背景音乐...");
_ = StartPlayingBackgroundMusic(_programCts.Token);
}
}
}
@ -1379,7 +1387,7 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
else if (audioPath.Contains(_config.AudioFiles.BeepPromptFile))
{
// 滴提示音的音量
device.Volume = Math.Clamp(_config.AudioFiles.PromptAfterBeepVolume > 0 ? _config.AudioFiles.PromptAfterBeepVolume : 1.0f, 0.0f, 1.0f);
device.Volume = Math.Clamp(_config.AudioFiles.BeepPromptVolume > 0 ? _config.AudioFiles.BeepPromptVolume : 1.0f, 0.0f, 1.0f);
}
else if (audioPath.Contains(_config.AudioFiles.WaitingToneFile))
{
@ -1539,12 +1547,10 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
{
_backgroundMusicReader.Position = 0;
}
// 设置音量到设备而非Reader对象确保独立控制默认为10%
_backgroundMusicDevice.Volume = Math.Clamp(_config.Recording.BackgroundMusicVolume, 0.0f, 1.0f);
// 如果没有在播放,则开始播放
if (_backgroundMusicDevice.PlaybackState != PlaybackState.Playing)
{
_backgroundMusicDevice.Play();
}
@ -1632,7 +1638,15 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
secondsSinceLastSpeech < silenceTimeoutSeconds &&
!_isWindChimePlaying)
{
Console.WriteLine($"检测到{windChimePromptSeconds}秒无声音,播放风铃提示音...");
Console.WriteLine($"检测到{windChimePromptSeconds}秒无声音,先停止背景音乐,再播放风铃提示音...");
// 先停止背景音乐
if (_isBackgroundMusicPlaying)
{
StopBackgroundMusic();
}
// 然后播放风铃提示音
_ = PlayWindChime();
}
}
@ -1688,32 +1702,23 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
// 开始播放
_windChimeDevice.Play();
// 单独开启一个任务检测是否有声音,如有则淡出停止
// 启动循环播放任务
_ = Task.Run(async () =>
{
try
{
while (_isWindChimePlaying && !token.IsCancellationRequested)
{
// 如果检测到有声音,淡出停止
if ((DateTime.Now - _lastSpeechTime).TotalSeconds < 1.0)
{
Console.WriteLine("检测到用户说话,风铃提示音淡出...");
await StopWindChime(true); // 平滑淡出
break;
}
// 如果到达音频文件末尾,循环播放
if (_windChimeReader != null && _windChimeReader.Position >= _windChimeReader.Length)
{
_windChimeReader.Position = 0;
}
// 确保背景音乐音量不受风铃音量影响
if (_isBackgroundMusicPlaying && _backgroundMusicDevice != null)
// 确保风铃声仍在播放
if (_windChimeDevice != null && _windChimeDevice.PlaybackState != PlaybackState.Playing)
{
// 重新应用背景音乐的原始音量设置
_backgroundMusicDevice.Volume = Math.Clamp(_config.Recording.BackgroundMusicVolume, 0.0f, 1.0f);
_windChimeDevice.Play();
}
await Task.Delay(100, token);
@ -1725,7 +1730,7 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
}
catch (Exception ex)
{
Console.WriteLine($"风铃提示音监控错误: {ex.Message}");
Console.WriteLine($"风铃提示音播放循环错误: {ex.Message}");
}
});
}
@ -1767,20 +1772,14 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
float ratio = 1.0f - ((float)i / steps);
_windChimeDevice.Volume = startVolume * ratio;
// 确保背景音乐音量不受影响
if (_isBackgroundMusicPlaying && _backgroundMusicDevice != null)
{
_backgroundMusicDevice.Volume = Math.Clamp(_config.Recording.BackgroundMusicVolume, 0.0f, 1.0f);
}
await Task.Delay(stepDelay);
}
}
// 正式停止
_windChimeCts?.Cancel();
if (_windChimeDevice != null)
{
_windChimeDevice.Stop();
@ -1796,12 +1795,6 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
_isWindChimePlaying = false;
Console.WriteLine("风铃提示音已停止");
// 再次确保背景音乐音量恢复正常
if (_isBackgroundMusicPlaying && _backgroundMusicDevice != null)
{
_backgroundMusicDevice.Volume = Math.Clamp(_config.Recording.BackgroundMusicVolume, 0.0f, 1.0f);
}
}
catch (Exception ex)
{

View File

@ -8,10 +8,11 @@
"BeepPromptFile": "\u6EF4\u63D0\u793A\u97F3.wav",
"DigitToneFileTemplate": "{0}.mp3",
"MinKeyTonePlayTimeMs": 200,
"KeyToneVolume": 1.0,
"WaitingToneVolume": 1.0,
"DialToneVolume": 0.9,
"PromptAfterBeepVolume": 1.0,
"KeyToneVolume": 0.7,
"WaitingToneVolume": 0.7,
"DialToneVolume": 0.7,
"PromptAfterBeepVolume": 0.7,
"BeepPromptVolume": 0.1,
"WindChimeFile": "\u98CE\u94C3.wav"
},
"Dial": {
@ -25,14 +26,14 @@
"SampleRate": 16000,
"Channels": 1,
"BufferMilliseconds": 50,
"SilenceThreshold": 0.1,
"SilenceTimeoutSeconds": 60,
"SilenceThreshold": 0.14,
"SilenceTimeoutSeconds": 20,
"AllowUserHangup": true,
"UploadRecordingToServer": false,
"EnableBackgroundMusic": true,
"BackgroundMusicFile": "bj.mp3",
"BackgroundMusicVolume": 0.15,
"WindChimePromptSeconds": 7.5,
"BackgroundMusicVolume": 0.3,
"WindChimePromptSeconds": 10,
"WindChimeFadeOutMs": 3000
},
"CallFlow": {