将2 Alaw流转换为1个立体声wav文件

时间:2016-04-18 11:39:54

标签: c# wav

我有2个alaw流,我试图转换为立体声wav文件。 结果听起来很糟糕,我可以听到内容,但原始文件中不存在令人讨厌的引人注目的声音。

我正在做的代码:

对于wav标题:

   public static byte[] GetHeader(int fileLength, int bitsPerSample, int numberOfChannels, int sampleRate) // bitsPerSample = 8, numberOfChannels= 1 or 2 (mono or stereo) , sampleRate = 8000
        {
            MemoryStream memStream = new MemoryStream(WAVE_HEADER_SIZE);
            BinaryWriter writer = new BinaryWriter(memStream);
            writer.Write(Encoding.ASCII.GetBytes("RIFF"), 0, 4); //4 bytes of RIFF description
            writer.Write((fileLength  - 8) / 2);
            writer.Write(Encoding.ASCII.GetBytes("WAVE"), 0, 4);
            writer.Write(Encoding.ASCII.GetBytes("fmt "), 0, 4);
            writer.Write(16); //Chunk size 
            writer.Write((short)6); //wFormatTag (1 == uncompressed PCM, 6 = A law compression)
            writer.Write((short)numberOfChannels); //nChannels
            writer.Write(sampleRate); //nSamplesPerSec
            int blockAllign = bitsPerSample / 8 * numberOfChannels;
            writer.Write(sampleRate * blockAllign); //nAvgBytesPerSec
            writer.Write((short)blockAllign); //nBlockAlign
            writer.Write((short)bitsPerSample); //wBitsPerSample
            writer.Write(Encoding.ASCII.GetBytes("data"), 0, 4);
            writer.Write(fileLength / 2 - WAVE_HEADER_SIZE); //data length 
            writer.Flush();

            byte[] buffer = new byte[memStream.Length];
            memStream.Position = 0;
            memStream.Read(buffer, 0, buffer.Length);

            writer.Close();
            return buffer;
        }

数据(原始流从alaw文件中读取):

                do
                {
                    if (channels == 2)
                    {
                        if (dlRead == 0)
                        {
                            dlRead = dlStream.Read (dlBuffer, 0, dlBuffer.Length);

                        }
                        if (ulRead == 0)
                        {
                            ulRead =ulStream.Read (ulBuffer, 0, ulBuffer.Length);
                        }

                        if ((dlRead != 0) && (ulRead != 0))
                        {
                            //Create the stero wave buffer
                            Array.Clear(waveBuffer, 0, waveBuffer.Length);
                            for (int i = 0; i < dlRead / 2; ++i)
                            {
                                waveBuffer[i * 4 + 0] = dlBuffer[i * 2];
                                waveBuffer[i * 4 + 1] = dlBuffer[i * 2 + 1];
                            }
                            for (int i = 0; i < ulRead / 2; ++i)
                            {
                                waveBuffer[i * 4 + 2] = ulBuffer[i * 2];
                                waveBuffer[i * 4 + 3] = ulBuffer[i * 2 + 1];
                            }
                            bytesToWrite = Math.Max(ulRead * 2, dlRead * 2);
                            dlRead = 0;
                            ulRead = 0;
                        }
                        else
                            bytesToWrite = 0;
                    }
                    else
                    {
                        //Create the mono wave buffer
                        if (metadata.ULExists)
                        {
                            bytesToWrite = ulStream.Read(ulBuffer, 0, ulBuffer.Length);
                            Buffer.BlockCopy(ulBuffer, 0, waveBuffer, 0, ulBuffer.Length);

                        }
                        else if (metadata.DLExists)
                        {
                            bytesToWrite = dlStream.Read(dlBuffer, 0, dlBuffer.Length);
                            Buffer.BlockCopy(dlBuffer, 0, waveBuffer, 0, dlBuffer.Length);

                        }
                        else
                        {
                            bytesToWrite = 0;
                        }
                    }

                    if (bytesToWrite > 0)
                        fileStream.Write(waveBuffer, 0, bytesToWrite);
                } while (bytesToWrite > 0);

我做错了什么?有什么想法吗?

0 个答案:

没有答案