Exemple #1
0
        /// <summary>
        /// The UnPackBytes method is used to unpack a byte stream into the Wav information.
        /// </summary>
        /// <param name="rg">Specifies the byte stream.</param>
        /// <param name="fmt">Returns the WAV file format.</param>
        /// <returns>Returns the WAV file samples.</returns>
        public static List <double[]> UnPackBytes(byte[] rg, out WaveFormat fmt)
        {
            using (MemoryStream ms = new MemoryStream(rg))
                using (BinaryReader br = new BinaryReader(ms))
                {
                    int    nLen  = br.ReadInt32();
                    byte[] rgFmt = br.ReadBytes(nLen);
                    fmt = WAVWriter.ByteArrayToStructure <WaveFormat>(rgFmt);
                    int nCh = br.ReadInt32();
                    int nS  = br.ReadInt32();

                    List <double[]> rgData = new List <double[]>();
                    for (int i = 0; i < nCh; i++)
                    {
                        rgData.Add(new double[nS]);
                    }

                    for (int i = 0; i < nS; i++)
                    {
                        for (int j = 0; j < nCh; j++)
                        {
                            float fVal = br.ReadSingle();
                            rgData[j][i] = fVal;
                        }
                    }

                    return(rgData);
                }
        }
Exemple #2
0
        /// <summary>
        /// The PackBytes method packs the wav file information into a byte stream.
        /// </summary>
        /// <param name="fmt">Specifies the wav file format.</param>
        /// <param name="rgSamples">Specifies the wav file samples.</param>
        /// <returns>The byte stream is returned.</returns>
        public static byte[] PackBytes(WaveFormat fmt, List <float[]> rgSamples)
        {
            using (MemoryStream ms = new MemoryStream())
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    byte[]         rgHeader = WAVWriter.StructureToByteArray <WaveFormat>(fmt);
                    List <float[]> rgData   = rgSamples;

                    bw.Write(rgHeader.Length);
                    bw.Write(rgHeader);
                    bw.Write(rgData.Count);
                    bw.Write(rgData[0].Length);

                    for (int i = 0; i < rgData[0].Length; i++)
                    {
                        for (int j = 0; j < rgData.Count; j++)
                        {
                            bw.Write(rgData[j][i]);
                        }
                    }

                    return(ms.ToArray());
                }
        }