简体   繁体   English

将float []转换为byte []以获取NAudio

[英]Convert float[] to byte[] for NAudio

I am receiving a string representing an array of audio samples from a browser captured via getUserMedia. 我从通过getUserMedia捕获的浏览器中接收到一个表示音频样本数组的字符串。 getUserMedia is recording at 48000, and I am interleaving 2 channels before I send the string values to the server. getUserMedia的录制时间是48000,在将字符串值发送到服务器之前,我要交织2个频道。 I turn this string into a float[] like so: 我将字符串转换为float [],如下所示:

string[] raw = buffer.ToString().Split(new char[]{ ',' });
float[] fArray = new float[raw.Length];
for (int x = 0; x < raw.Length; x++)
{
     float sampleVal = float.Parse(raw[x]);
     fArray[x] = sampleVal;
}

What I would like to do is convert the float[] array into a byte[] array so that I can pass it to a BufferedWaveProvider (48000, 16, 1) for playback. 我想做的就是将float []数组转换为byte []数组,以便可以将其传递给BufferedWaveProvider(48000,16,1)进行播放。 Here's how I am currently trying to do the conversion: 这是我目前尝试进行转换的方式:

byte[] bSamples = new byte[fArray.Length * 2];
for (int x = 0; x < fArray.Length; x += 2)
{
    short sSample = (short)Math.Floor(fArray[x] * 32767);

    byte[] tmp = BitConverter.GetBytes(sSample);
    bSamples[x] = tmp[0];
    bSamples[x + 1] = tmp[1];
}

Using the code above, only is garbage produced. 使用上面的代码,只会产生垃圾。 Can anyone point me in the right direction for doing such a conversion? 谁能指出我进行这种转换的正确方向?

I've seen this , but it didn't get me where I need to go. 我已经看到 ,但是并没有带我去哪里。

It looks like your indexing isn't quite right in the second loop. 看来您的索引在第二个循环中不太正确。 You're looping over the float samples, and using the same index in the short output: 您正在遍历float样本,并在short输出中使用相同的索引:

for (int x = 0; x < fArray.Length; x += 2)

Another thing (let's assume that the floating-point input is true IEEE 32-bit float samples in the range [-1.0,1.0], so we won't worry about converting). 另一件事(假设浮点输入是[-1.0,1.0]范围内的真实IEEE 32位浮点样本,因此我们不必担心转换)。 Is it stereo input? 是立体声输入吗? If so, then you'll need to combine the samples before converting to 'short'. 如果是这样,那么您将需要合并样本,然后再转换为“ short”。 That's easy to do. 这很容易做到。 Just average successive pairs of float values (left channel/right channel). 仅对连续的float值对进行平均(左通道/右通道)。

The size of the output array should be correct. 输出数组的大小应正确。 Technically, it'd be something like this: 从技术上讲,应该是这样的:

int inchannels = 2;  // doesn't have to be a variable, but shows 'stereo' input.
int numsamples = fArray.Length / inchannels;
byte [] bSamples = new byte [numsamples * sizeof(Int16)];

Then you should be able to do the conversion as follows. 然后,您应该能够进行如下转换。 Note that this assumes a stereo input, so it averages the float samples. 请注意,这假设是立体声输入,因此将浮点采样取平均值。

int outindex = 0;
for( int inindex = 0; inindex < fArray.Length; inindex += 2 )
{
    // 'insample' is the average of left and right channels.  This isn't
    // entirely accurate - if one channel is 0.0, then we should just use
    // the other channel verbatim.  But this is close enough.
    float insample = (fArray[inindex] + fArray[inindex+1]) / 2.0f;

    // The output sample.  It's probably better to use Int16.MaxValue
    // than the hard-coded value.
    Int16 outsample = (Int16)(insample * (float)Int16.MaxValue);

    // I'm old-school, so I'm a fan of 'unsafe'.  But you can use the
    // BitConverter that you were already using.  Actually, I would've
    // probably done this entire conversion in 'unsafe' mode.
    unsafe
    {
        fixed( byte * pbyte = &bSamples[outindex] )
        {
            Int16 * p = (Int16 *)pbyte;
            *p = outsample;
            outindex += sizeof(Int16);
        }
    }
}

为时已晚,但仍然有用-我已经将一个转换代码从float[] samples数组发布到那里的byte[]数组https://stackoverflow.com/a/42151979/4778700

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM