Esempio n. 1
0
        /*
         * public struct WaveInCaps
         * {
         *  public short wMid;
         *  public short wPid;
         *  public int vDriverVersion;
         *  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = WinMM.MAXPNAMELEN)] public string szPname;
         *  public int dwFormats;
         *  public short wChannels;
         * }*/

        #endregion

        public static byte[] WriteWAVFile(WaveFormat wf, byte[][] rawData)
        {
            var bw      = new MemoryStream();
            var rawSize = 0;

            for (var j = 0; j < rawData.Length; j++)
            {
                rawSize += rawData[j].Length;
            }
            //RIFF HEADER
            bw.Write(Encoding.ASCII.GetBytes("RIFF"), 0, 4);
            bw.Write(BitConverter.GetBytes(rawSize + 36), 0, 4);
            bw.Write(Encoding.ASCII.GetBytes("WAVE".ToCharArray()), 0, 4);

            // SUBCHUNK
            bw.Write(Encoding.ASCII.GetBytes("fmt ".ToCharArray()), 0, 4);
            bw.Write(BitConverter.GetBytes(16), 0, 4);
            var chunk = MarshalUtil.StructureToByteArray(wf);

            bw.Write(chunk, 0, chunk.Length);

            // DATA CHUNK
            bw.Write(Encoding.ASCII.GetBytes("data".ToCharArray()), 0, 4);
            bw.Write(BitConverter.GetBytes(rawSize), 0, 4);
            for (var i = 0; i < rawData[0].Length; i += wf.BitsPerSample / 8)
            {
                for (var j = 0; j < rawData.Length; j++)
                {
                    bw.WriteByte(rawData[j][i]);
                    if (wf.BitsPerSample == 16)
                    {
                        bw.WriteByte(rawData[j][i + 1]);
                    }
                }
            }

            return(bw.ToArray());
        }
Esempio n. 2
0
        public static void WriteWAVFile(string filePath, WaveFormat wf, byte[][] rawData)
        {
            var fileStream = File.OpenWrite(filePath);
            var rawSize    = 0;

            for (var j = 0; j < rawData.Length; j++)
            {
                rawSize += rawData[j].Length;
            }
            //RIFF HEADER
            fileStream.Write(Encoding.ASCII.GetBytes("RIFF"), 0, 4);
            fileStream.Write(BitConverter.GetBytes(rawSize + 36), 0, 4);
            fileStream.Write(Encoding.ASCII.GetBytes("WAVE".ToCharArray()), 0, 4);

            // SUBCHUNK
            fileStream.Write(Encoding.ASCII.GetBytes("fmt ".ToCharArray()), 0, 4);
            fileStream.Write(BitConverter.GetBytes(16), 0, 4);
            var chunk = MarshalUtil.StructureToByteArray(wf);

            fileStream.Write(chunk, 0, chunk.Length);

            // DATA CHUNK
            fileStream.Write(Encoding.ASCII.GetBytes("data".ToCharArray()), 0, 4);
            fileStream.Write(BitConverter.GetBytes(rawSize), 0, 4);
            for (var i = 0; i < rawData[0].Length; i += wf.BitsPerSample / 8)
            {
                for (var j = 0; j < rawData.Length; j++)
                {
                    fileStream.WriteByte(rawData[j][i]);
                    if (wf.BitsPerSample == 16)
                    {
                        fileStream.WriteByte(rawData[j][i + 1]);
                    }
                }
            }

            fileStream.Close();
        }