2024-08-25 09:55:05 +00:00
|
|
|
|
|
|
|
using NAudio.Wave;
|
|
|
|
|
|
|
|
namespace Debug.ModelingMusic;
|
|
|
|
public class MusicDebug
|
|
|
|
{
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 默认模式
|
|
|
|
/// </summary>
|
|
|
|
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();
|
|
|
|
}
|
2024-08-25 23:58:04 +00:00
|
|
|
} while (Program.IsWhile);
|
2024-08-25 09:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 随机下落测试模式
|
|
|
|
/// </summary>
|
|
|
|
public static void DebugDefault()
|
|
|
|
{
|
|
|
|
Console.Clear();
|
|
|
|
var random = new Random();
|
|
|
|
|
2024-08-25 23:58:04 +00:00
|
|
|
while (Program.IsWhile)
|
2024-08-25 09:55:05 +00:00
|
|
|
{
|
|
|
|
//随机生成 a-z和A-Z
|
|
|
|
var ch = (char)(random.Next(26) + 65);
|
|
|
|
new Thread(() => Music.Down(ch)).Start();
|
|
|
|
|
|
|
|
//随机暂停
|
|
|
|
Thread.Sleep(random.Next(1000));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 指定下落字符串
|
|
|
|
/// </summary>
|
|
|
|
public static void DesignateDown(char[] ch)
|
|
|
|
{
|
|
|
|
Console.Clear();
|
|
|
|
var random = new Random();
|
|
|
|
|
2024-08-25 23:58:04 +00:00
|
|
|
while (Program.IsWhile)
|
2024-08-25 09:55:05 +00:00
|
|
|
{
|
|
|
|
for (int v = 0; v < ch.Length; v++)
|
|
|
|
{
|
|
|
|
new Thread(() => Music.Down(ch[v])).Start();
|
|
|
|
//随机暂停
|
|
|
|
Thread.Sleep(random.Next(1000));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// 随机指定下落字符串
|
|
|
|
/// </summary>
|
|
|
|
public static void DesignateRandmoDown(char[] ch)
|
|
|
|
{
|
|
|
|
Console.Clear();
|
|
|
|
var random = new Random();
|
|
|
|
|
2024-08-25 23:58:04 +00:00
|
|
|
while (Program.IsWhile)
|
2024-08-25 09:55:05 +00:00
|
|
|
{
|
|
|
|
int v = random.Next(0, ch.Length);
|
|
|
|
new Thread(() => Music.Down(ch[v])).Start();
|
|
|
|
//随机暂停
|
|
|
|
Thread.Sleep(random.Next(600));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void MusicRhythm()
|
|
|
|
{
|
|
|
|
|
2024-08-25 23:51:27 +00:00
|
|
|
var cap = new WasapiLoopbackCapture();
|
2024-08-25 09:55:05 +00:00
|
|
|
cap.DataAvailable += (sender, e) =>
|
|
|
|
{
|
2024-08-25 23:51:27 +00:00
|
|
|
// Console.Clear();
|
|
|
|
// Console.WriteLine(e.BytesRecorded + " " + e.Buffer.Length);
|
2024-08-25 09:55:05 +00:00
|
|
|
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);
|
2024-08-25 23:51:27 +00:00
|
|
|
// Console.WriteLine("RMS Volume: " + rmsVolume);
|
2024-08-25 09:55:05 +00:00
|
|
|
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)
|
|
|
|
{
|
2024-08-25 23:58:04 +00:00
|
|
|
int barLength = Program.chars.GetLength(1); // 控制条长度
|
2024-08-25 09:55:05 +00:00
|
|
|
int filledLength = Math.Min((int)(rmsVolume * 1000), barLength);
|
2024-08-25 23:58:04 +00:00
|
|
|
for (int x = 0; x < Program.chars.GetLength(1); x++)
|
2024-08-25 09:55:05 +00:00
|
|
|
{
|
2024-08-25 23:58:04 +00:00
|
|
|
Program.chars[9, x].Color = ConsoleColor.DarkCyan;
|
2024-08-25 09:55:05 +00:00
|
|
|
if (x < filledLength)
|
|
|
|
{
|
|
|
|
|
2024-08-25 23:58:04 +00:00
|
|
|
Program.chars[9, x].Char = '#'; // 绘制音量条
|
2024-08-25 09:55:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-08-30 06:48:22 +00:00
|
|
|
Program.chars[9, x].Char = Program.ShowChar;
|
2024-08-25 09:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|