using System.Text.Json; using System.Text.Json.Serialization; namespace ShengShengBuXi.ConsoleApp.Models; /// /// 电话亭配置类,包含所有可配置项 /// public class PhoneBoothConfig { /// /// 音频文件配置 /// public AudioFilesConfig AudioFiles { get; set; } = new(); /// /// 拨号配置 /// public DialConfig Dial { get; set; } = new(); /// /// 录音配置 /// public RecordingConfig Recording { get; set; } = new(); /// /// 电话流程配置 /// public CallFlowConfig CallFlow { get; set; } = new(); /// /// 从文件加载配置 /// /// 配置文件路径 /// 配置对象 public static PhoneBoothConfig LoadFromFile(string filePath) { if (!File.Exists(filePath)) { return CreateDefaultConfig(); } try { string json = File.ReadAllText(filePath); var config = JsonSerializer.Deserialize(json) ?? CreateDefaultConfig(); return config; } catch (Exception ex) { Console.WriteLine($"加载配置文件失败: {ex.Message}"); return CreateDefaultConfig(); } } /// /// 保存配置到文件 /// /// 配置文件路径 public void SaveToFile(string filePath) { try { var options = new JsonSerializerOptions { WriteIndented = true }; string json = JsonSerializer.Serialize(this, options); File.WriteAllText(filePath, json); } catch (Exception ex) { Console.WriteLine($"保存配置文件失败: {ex.Message}"); } } /// /// 创建默认配置 /// /// 默认配置对象 public static PhoneBoothConfig CreateDefaultConfig() { return new PhoneBoothConfig(); } } /// /// 音频文件配置 /// public class AudioFilesConfig { /// /// 音频文件基础路径 /// public string AudioBasePath { get; set; } = "mp3"; /// /// 等待嘟音文件路径 /// public string WaitingToneFile { get; set; } = "等待嘟音.wav"; /// /// 等待接电话音频文件路径 /// public string WaitForPickupFile { get; set; } = "等待接电话.mp3"; /// /// 电话接起音频文件路径 /// public string PhonePickupFile { get; set; } = "电话接起.mp3"; /// /// 提示用户录音音频文件路径 /// public string PromptUserRecordFile { get; set; } = "提示用户录音.mp3"; /// /// 滴提示音文件路径 /// public string BeepPromptFile { get; set; } = "滴提示音.wav"; /// /// 数字按键音频文件模板(0-9) /// public string DigitToneFileTemplate { get; set; } = "{0}.mp3"; /// /// 按键音的最小播放时间(毫秒),即使按键释放也会至少播放这么长时间 /// public int MinKeyTonePlayTimeMs { get; set; } = 200; /// /// 按键音音量倍数 (正常音量为1.0) /// public float KeyToneVolume { get; set; } = 1.0f; /// /// 待机嘟声音量倍数 (正常音量为1.0) /// public float WaitingToneVolume { get; set; } = 1.0f; /// /// 拨出嘟声音量倍数 (正常音量为1.0) /// public float DialToneVolume { get; set; } = 0.9f; /// /// 提示留言音量倍数 (正常音量为1.0) /// public float PromptAfterBeepVolume { get; set; } = 1.0f; /// /// 滴提示音音量倍数 (正常音量为1.0) /// public float BeepPromptVolume { get; set; } = 1.0f; /// /// 风铃提示音文件名 /// public string WindChimeFile { get; set; } = "风铃.wav"; /// /// 获取完整的音频文件路径 /// /// 文件名 /// 完整路径 public string GetFullPath(string fileName) { return Path.Combine( Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AudioBasePath), fileName); } /// /// 获取数字按键音频文件路径 /// /// 数字(0-9) /// 文件路径 public string GetDigitToneFilePath(int digit) { return GetFullPath(string.Format(DigitToneFileTemplate, digit)); } } /// /// 拨号配置 /// public class DialConfig { /// /// 拨号所需的最小位数 /// public int MinDigitsToDialOut { get; set; } = 8; /// /// 无新按键后自动拨出的等待秒数 /// public int AutoDialOutAfterSeconds { get; set; } = 5; /// /// 位数不足时,无操作重置的等待秒数 /// public int ResetTimeoutSeconds { get; set; } = 10; } /// /// 录音配置 /// public class RecordingConfig { /// /// 录音保存文件夹 /// public string RecordingFolder { get; set; } = "recordings"; /// /// 录音设备编号 /// public int RecordingDeviceNumber { get; set; } = 0; /// /// 录音采样率 /// public int SampleRate { get; set; } = 16000; /// /// 录音通道数(1=单声道,2=立体声) /// public int Channels { get; set; } = 1; /// /// 录音缓冲区大小(毫秒) /// public int BufferMilliseconds { get; set; } = 100; /// /// 静音检测阈值 /// public float SilenceThreshold { get; set; } = 0.02f; /// /// 无声音自动挂断的时间(秒) /// public int SilenceTimeoutSeconds { get; set; } = 30; /// /// 是否允许用户手动挂断(按回车键) /// public bool AllowUserHangup { get; set; } = true; /// /// 是否上传录音文件到服务器 /// public bool UploadRecordingToServer { get; set; } = false; /// /// 是否在录音时播放背景音乐 /// public bool EnableBackgroundMusic { get; set; } = true; /// /// 录音背景音乐文件名 /// public string BackgroundMusicFile { get; set; } = "bj.mp3"; /// /// 背景音乐音量 (0.0-1.0) /// public float BackgroundMusicVolume { get; set; } = 0.1f; /// /// 无声音播放风铃提示的时间(秒) /// public float WindChimePromptSeconds { get; set; } = 7f; /// /// 风铃声淡出的时间(毫秒) /// public int WindChimeFadeOutMs { get; set; } = 2000; } /// /// 电话流程配置 /// public class CallFlowConfig { /// /// 等待接电话音频的最小持续时间(秒) /// public int WaitForPickupMinSeconds { get; set; } = 3; /// /// 等待接电话音频的最大持续时间(秒) /// public int WaitForPickupMaxSeconds { get; set; } = 6; /// /// 播放电话接起音频的概率(0-1之间的小数) /// public float PlayPickupProbability { get; set; } = 0.15f; /// /// 用户挂断后是否重置系统,false表示直接退出程序 /// public bool ResetSystemAfterHangup { get; set; } = true; }