gpt4 book ai didi

.net-core - 在树莓派上用.Net Core C#录制的音频保存到Wav文件

转载 作者:行者123 更新时间:2023-12-04 01:13:59 24 4
gpt4 key购买 nike

我发现很难找到一种方法来将使用 OpenTk.NetStandard 捕获的音频存储到 NetCore C# 中的适当 .WAV 文件中。

我正在寻找的是一种在 Raspberry pi 上运行时可以工作的解决方案,因此 NAudio 或任何 Windows 特定方法都无法解决我的问题。

我找到了其他几个 SO 答案,它们展示了如何使用 opentk 捕获音频,但没有关于如何将其存储在 wav 文件中的内容。

这是我从另一个 SO 问题中获取的应该从麦克风读取数据的代码的摘录,我看到 AudioCapture 类是一个:

  const byte SampleToByte = 2;
short[] _buffer = new short[512];
int _sampling_rate = 16000;
double _buffer_length_ms = 5000;
var _recorders = AudioCapture.AvailableDevices;
int buffer_length_samples = (int)((double)_buffer_length_ms * _sampling_rate * 0.001 / BlittableValueType.StrideOf(_buffer));

using (var audioCapture = new AudioCapture(_recorders.First(), _sampling_rate, ALFormat.Mono16, buffer_length_samples))
{
audioCapture.Start();
int available_samples = audioCapture.AvailableSamples;

_buffer = new short[MathHelper.NextPowerOfTwo((int)(available_samples * SampleToByte / (double)BlittableValueType.StrideOf(_buffer) + 0.5))];

if (available_samples > 0)
{
audioCapture.ReadSamples(_buffer, available_samples);

int buf = AL.GenBuffer();
AL.BufferData(buf, ALFormat.Mono16, buffer, (int)(available_samples * BlittableValueType.StrideOf(_buffer)), audio_capture.SampleFrequency);
AL.SourceQueueBuffer(src, buf);

// TODO: I assume this is where the save to WAV file logic should be placed...
}

}

如有任何帮助,我们将不胜感激!

最佳答案

这是一个 .NET Core 控制台程序,它将使用 Mono 16 位数据写入 WAV 文件。您应该通读源代码中的链接,以了解正在写入的值发生了什么。

这将记录 10 秒的数据并将其保存为 WAV 格式的文件:

using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using System;
using System.IO;
using System.Threading;

class Program
{
static void Main(string[] args)
{
var recorders = AudioCapture.AvailableDevices;
for (int i = 0; i < recorders.Count; i++)
{
Console.WriteLine(recorders[i]);
}
Console.WriteLine("-----");

const int samplingRate = 44100; // Samples per second

const ALFormat alFormat = ALFormat.Mono16;
const ushort bitsPerSample = 16; // Mono16 has 16 bits per sample
const ushort numChannels = 1; // Mono16 has 1 channel

using (var f = File.OpenWrite(@"C:\users\andy\desktop\out.wav"))
using (var sw = new BinaryWriter(f))
{
// Read This: http://soundfile.sapp.org/doc/WaveFormat/

sw.Write(new char[] { 'R', 'I', 'F', 'F' });
sw.Write(0); // will fill in later
sw.Write(new char[] { 'W', 'A', 'V', 'E' });
// "fmt " chunk (Google: WAVEFORMATEX structure)
sw.Write(new char[] { 'f', 'm', 't', ' ' });
sw.Write(16); // chunkSize (in bytes)
sw.Write((ushort)1); // wFormatTag (PCM = 1)
sw.Write(numChannels); // wChannels
sw.Write(samplingRate); // dwSamplesPerSec
sw.Write(samplingRate * numChannels * (bitsPerSample / 8)); // dwAvgBytesPerSec
sw.Write((ushort)(numChannels * (bitsPerSample / 8))); // wBlockAlign
sw.Write(bitsPerSample); // wBitsPerSample
// "data" chunk
sw.Write(new char[] { 'd', 'a', 't', 'a' });
sw.Write(0); // will fill in later

// 10 seconds of data. overblown, but it gets the job done
const int bufferLength = samplingRate * 10;
int samplesWrote = 0;

Console.WriteLine($"Recording from: {recorders[0]}");

using (var audioCapture = new AudioCapture(
recorders[0], samplingRate, alFormat, bufferLength))
{
var buffer = new short[bufferLength];

audioCapture.Start();
for (int i = 0; i < 10; ++i)
{
Thread.Sleep(1000); // give it some time to collect samples

var samplesAvailable = audioCapture.AvailableSamples;
audioCapture.ReadSamples(buffer, samplesAvailable);
for (var x = 0; x < samplesAvailable; ++x)
{
sw.Write(buffer[x]);
}

samplesWrote += samplesAvailable;

Console.WriteLine($"Wrote {samplesAvailable}/{samplesWrote} samples...");
}
audioCapture.Stop();
}

sw.Seek(4, SeekOrigin.Begin); // seek to overall size
sw.Write(36 + samplesWrote * (bitsPerSample / 8) * numChannels);
sw.Seek(40, SeekOrigin.Begin); // seek to data size position
sw.Write(samplesWrote * (bitsPerSample / 8) * numChannels);
}
}
}

关于.net-core - 在树莓派上用.Net Core C#录制的音频保存到Wav文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63946042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com