c# - 如何找出字节的格式?南音频
问题描述
我正在尝试做的事情:从麦克风接收数据(IWaveIn)=> 降低声音的幅度(降低音量)(问题)=> 将其播放到扬声器(IWaveProvider)
问题是:每当我尝试将样本乘以 x!=1.0f 时,都会给我非常嘈杂的反馈。我认为它可能是一种字节格式,但我不知道如何检查它。任何帮助/建议将不胜感激。
计数 = 17640;偏移量=0;
public int Read(byte[] buffer, int offset, int count)
{
int read = bufferedWaveProvider.Read(buffer, offset, count);
/*
waveIn.WaveFormat.Channels; //2
waveIn.WaveFormat.BlockAlign;//4
waveIn.WaveFormat.BitsPerSample;//16
waveIn.WaveFormat.SampleRate;//44100
*/
for (int i = 0; i < read / 4; i++)
{
int firstByte = i * 4;
float sample = BitConverter.ToSingle(buffer, firstByte);
sample = sample * 1.0f;
byte[] bytes = BitConverter.GetBytes(sample);
buffer[firstByte + 0] = bytes[0];
buffer[firstByte + 1] = bytes[1];
buffer[firstByte + 2] = bytes[2];
buffer[firstByte + 3] = bytes[3];
}
return read;
}
private void OnDataAvailable(object sender, WaveInEventArgs e)
{
bufferedWaveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
}
解决方案
您正在接收中指定格式的音频WaveIn.WaveFormat
。您的评论显示每个样本 16 位,这意味着您接收的音频是带符号的 16 位样本。所以你可以使用BitConverter.ToInt16
但是有更简单的方法可以做到这一点。如果您调用ToSampleProvider()
您的BufferedWaveProvider
then 您可以将其传递给 aVolumeSampleProvider
这将让您直接调整音量,而无需自己解压缩样本。
推荐阅读
- java - 用 Mono 压缩 Flux 的正确方法是什么?
- google-cloud-platform - 缺少对“billingAccounts/XXXXXXXXXXXXXXXXXXXXXXXX”的权限:billing.resourceAssociations.create Terraform (GCP)
- python - 如何在 django 中读取所有带有特定前缀的图像?
- javascript - 如何在 JavaScript 中仅获取我的 json 对象值的直接父级?
- reactjs - Ag Grid - React- Datepicker 作为单元格编辑器不起作用
- python - 我无法使用 re 模块,我的解释器显示错误
- python-3.x - 树莓派和python实时
- docker - 如何进入 docker 容器以查看已安装的卷?
- ios - iOS Swift:在不到 20 秒的时间内一次(并发)启动 100 个 URL 的下载任务
- python - 无法连接到cs go中的套接字