将单声道PCM转换为uLaw代码再转换为立体声

时间:2018-09-05 01:58:24

标签: c# pcm bitconverter mu-law

我有一些具有两个功能的C#代码。一种将线性PCM数据转换为MuLaw格式。另一个函数将其转换回去。

公共静态Byte [] LinearToMulaw(Byte []字节,int bitsPerSample,int通道)

公共静态Byte [] MuLawToLinear(Byte []字节,int bitsPerSample,int通道)

在以下说明中,我始终使用16位,两个通道。

发布此代码时,作者决定使函数将声音转换为单声道。我需要立体声。 如果我仅用返回的字节数替换每个函数,则确实具有所需的立体声。左右声道。

现在我知道需要做些什么。这是将线性PCM转换为MuLaw的代码:

    public static Byte[] LinearToMulaw(Byte[] bytes, int bitsPerSample, int channels)
    {
      //  return bytes;

        int blockAlign =  channels * bitsPerSample / 8;

        Byte[] result = new Byte[bytes.Length  / blockAlign]; 
        int resultIndex = 0;
        for (int i = 0; i < result.Length ; i++)
        {
            switch (bitsPerSample)
            {
                case 8:
                    switch (channels)
                    {
                        //8 Bit 1 Channel
                        case 1:
                            result[i] = linear2ulaw(bytes[resultIndex]);
                            resultIndex += 1;
                            break;

                        //8 Bit 2 Channel
                        case 2:
                            result[i] = linear2ulaw(bytes[resultIndex]);
                            resultIndex += 2;
                            break;
                    }
                    break;

                case 16:
                    switch (channels)
                    {
                        //16 Bit 1 Channel
                        case 1:
                            result[i] = linear2ulaw(BitConverter.ToInt16(bytes, resultIndex));
                            resultIndex += 2;
                            break;

                        //16 Bit 2 Channels
                        case 2:
                            result[i] = linear2ulaw(BitConverter.ToInt16(bytes, resultIndex));
                            resultIndex += 4; // this is 4 to skip the right channel
                            break;
                    }
                    break;
            }
        }


        return result;
    }

这是要转换回的代码:

    public static Byte[] MuLawToLinear(Byte[] bytes, int bitsPerSample, int channels)
    {
      // return bytes;

        int blockAlign = channels * bitsPerSample / 8;


        Byte[] result = new Byte[bytes.Length * blockAlign];
        for (int i = 0, counter = 0; i < bytes.Length; i++, counter += blockAlign)
        {

            int value = MulawToLinear(bytes[i]);
            Byte[] values = BitConverter.GetBytes(value);

            switch (bitsPerSample)
            {
                case 8:
                    switch (channels)
                    {
                        //8 Bit 1 Channel
                        case 1:
                            result[counter] = values[0];
                            break;

                        //8 Bit 2 Channel
                        case 2:
                            result[counter] = values[0];
                            result[counter + 1] = values[0];
                            break;
                    }
                    break;

                case 16:
                    switch (channels)
                    {
                        //16 Bit 1 Channel
                        case 1:
                            result[counter] = values[0];
                            result[counter + 1] = values[1];
                            break;

                        //16 Bit 2 Channels
                        case 2:
                            result[counter] = values[0];
                            result[counter + 1] = values[1];
                            result[counter + 2] = values[0]; //tried 3 and 4 here
                            result[counter + 3] = values[1];
                            break;
                    }
                    break;
            }
        }


        return result;
    }

这两个函数将采用线性立体声PCM字节数组并将其输出为单声道。

输入字节采用以下格式:字节0,字节1是左通道,字节2和3是右通道。现在,作者将结果索引加4,从而跳过右侧通道,从而将输入字节数组转换为mono。 在将MuLaw转换为Linear的过程中,他将右边的通道值放在左边。

我试图通过仅将结果索引增加2并读回结果字节来修改此设置,但是我得到的只是失真的声音,听起来好像它以半速运行。 谁能建议我要去哪里错了?

0 个答案:

没有答案
相关问题