首页 > 解决方案 > 如何找出字节的格式?南音频

问题描述

我正在尝试做的事情:从麦克风接收数据(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);
    }

标签: c#audionaudio

解决方案


您正在接收中指定格式的音频WaveIn.WaveFormat。您的评论显示每个样本 16 位,这意味着您接收的音频是带符号的 16 位样本。所以你可以使用BitConverter.ToInt16

但是有更简单的方法可以做到这一点。如果您调用ToSampleProvider()您的BufferedWaveProviderthen 您可以将其传递给 aVolumeSampleProvider这将让您直接调整音量,而无需自己解压缩样本。


推荐阅读