using NAudio.Wave; namespace Debug.ModelingMusic; public class MusicDebug { /// /// 默认模式 /// public static void Default() { do { var keyInfo = Console.ReadKey(true); // // 输出按下的键的信息,这里只是为了演示 if (keyInfo.Key == ConsoleKey.Escape) { Console.Clear(); //关闭程序 Environment.Exit(0); break; } char keyChar = keyInfo.KeyChar; if ((keyChar >= 'A' && keyChar <= 'Z') || (keyChar >= 'a' && keyChar <= 'z')) { new Thread(() => Music.Down(keyChar)).Start(); } } while (Program.IsWhile); } /// /// 随机下落测试模式 /// public static void DebugDefault() { Console.Clear(); var random = new Random(); while (Program.IsWhile) { //随机生成 a-z和A-Z var ch = (char)(random.Next(26) + 65); new Thread(() => Music.Down(ch)).Start(); //随机暂停 Thread.Sleep(random.Next(1000)); } } /// /// 指定下落字符串 /// public static void DesignateDown(char[] ch) { Console.Clear(); var random = new Random(); while (Program.IsWhile) { for (int v = 0; v < ch.Length; v++) { new Thread(() => Music.Down(ch[v])).Start(); //随机暂停 Thread.Sleep(random.Next(1000)); } } } /// /// 随机指定下落字符串 /// public static void DesignateRandmoDown(char[] ch) { Console.Clear(); var random = new Random(); while (Program.IsWhile) { int v = random.Next(0, ch.Length); new Thread(() => Music.Down(ch[v])).Start(); //随机暂停 Thread.Sleep(random.Next(600)); } } public static void MusicRhythm() { var cap = new WasapiLoopbackCapture(); cap.DataAvailable += (sender, e) => { // Console.Clear(); // Console.WriteLine(e.BytesRecorded + " " + e.Buffer.Length); float[] allSamples = Enumerable .Range(0, e.BytesRecorded / 4) .Select(i => BitConverter.ToSingle(e.Buffer, i * 4)) .ToArray(); int channelCount = cap.WaveFormat.Channels; float[][] channelSamples = Enumerable .Range(0, channelCount) .Select(channel => Enumerable .Range(0, allSamples.Length / channelCount) .Select(i => allSamples[channel + i * channelCount]) .ToArray()) .ToArray(); float[] averageSamples = Enumerable .Range(0, allSamples.Length / channelCount) .Select(index => Enumerable .Range(0, channelCount) .Select(channel => channelSamples[channel][index]) .Average()) .ToArray(); float rmsVolume = CalculateRMS(averageSamples); // Console.WriteLine("RMS Volume: " + rmsVolume); DrawVolumeBar(rmsVolume); Thread.Sleep(100); }; cap.StartRecording(); } static float CalculateRMS(float[] samples) { if (samples.Length == 0) return 0; float sumOfSquares = samples.Sum(sample => sample * sample); return (float)Math.Sqrt(sumOfSquares / samples.Length); } static void DrawVolumeBar(float rmsVolume) { int barLength = Program.chars.GetLength(1); // 控制条长度 int filledLength = Math.Min((int)(rmsVolume * 1000), barLength); for (int x = 0; x < Program.chars.GetLength(1); x++) { Program.chars[9, x].Color = ConsoleColor.DarkCyan; if (x < filledLength) { Program.chars[9, x].Char = '#'; // 绘制音量条 } else { Program.chars[9, x].Char = Program.ShowChar; } } } public static void MusicRhythm(FileInfo file) { // 创建一个 WasapiLoopbackCapture 对象来捕获系统音频输出 using (var capture = new WasapiLoopbackCapture()) { // 创建一个 WaveFileWriter 用于保存捕获的音频到文件 using (var writer = new WaveFileWriter(file.FullName, capture.WaveFormat)) { // 订阅 DataAvailable 事件,处理捕获到的音频数据 capture.DataAvailable += (s, e) => { // 将捕获的音频数据写入文件 writer.Write(e.Buffer, 0, e.BytesRecorded); }; // 启动音频捕获 capture.StartRecording(); Console.WriteLine("正在捕获音频... 按任意键停止。"); Console.ReadKey(); // 停止音频捕获 capture.StopRecording(); } } } }