Esempio n. 1
0
 public DotWaveInfo(DotWaveInfo DWI)
 {
     data          = DWI.Data;
     bitsPerSample = DWI.BitsPerSample;
     sampleRate    = DWI.SampleRate;
     RecalcAll();
 }
Esempio n. 2
0
        /// <summary>
        /// Соберёт массив байт аудио формата .wav из предоставленного DotWaveInfo
        /// </summary>
        /// <param name="DWI">Объект DotWaveInfo</param>
        /// <returns></returns>
        public static byte[] SetWaves(DotWaveInfo DWI)
        {
            byte[] bytes = new byte[44 + DWI.Data.GetLength(0) * DWI.Data.GetLength(1) * (DWI.BitsPerSample / 8)];

            BitConverter.GetBytes(DWI.ChunkID).CopyTo(bytes, 0);
            BitConverter.GetBytes(DWI.ChunkSize).CopyTo(bytes, 4);
            BitConverter.GetBytes(DWI.Format).CopyTo(bytes, 8);
            BitConverter.GetBytes(DWI.Subchunk1ID).CopyTo(bytes, 12);
            BitConverter.GetBytes(DWI.Subchunk1Size).CopyTo(bytes, 16);
            BitConverter.GetBytes(DWI.AudioFormat).CopyTo(bytes, 20);
            BitConverter.GetBytes(DWI.NumChannels).CopyTo(bytes, 22);
            BitConverter.GetBytes(DWI.SampleRate).CopyTo(bytes, 24);
            BitConverter.GetBytes(DWI.ByteRate).CopyTo(bytes, 28);
            BitConverter.GetBytes(DWI.BlockAlign).CopyTo(bytes, 32);
            BitConverter.GetBytes(DWI.BitsPerSample).CopyTo(bytes, 34);
            BitConverter.GetBytes(DWI.Subchunk2ID).CopyTo(bytes, 36);
            BitConverter.GetBytes(DWI.Subchunk2Size).CopyTo(bytes, 40);

            for (uint block = 1000; block < DWI.Data.GetLength(0); block++)
            {
                for (uint ch = 0; ch < DWI.Data.GetLength(1); ch++)
                {
                    byte[] val = BitConverter.GetBytes((UInt32)((double)DWI.Data[block, ch] / (double)UInt32.MaxValue * Math.Pow(256, DWI.BitsPerSample / 8)));
                    for (int i = 0; i < DWI.BitsPerSample / 8; i++)
                    {
                        if (DWI.BitsPerSample > 8)
                        {
                            bytes[44 + block * DWI.Data.GetLength(1) * (DWI.BitsPerSample / 8) + ch * (DWI.BitsPerSample / 8) + i] = val[i];
                        }
                        else
                        {
                            byte vl = val[0];
                            if (vl < 127)
                            {
                                vl += 127;
                            }
                            else
                            {
                                vl -= 127;
                            }
                            bytes[44 + block * DWI.Data.GetLength(1) * (DWI.BitsPerSample / 8) + ch * (DWI.BitsPerSample / 8) + i] = vl;
                        }
                    }
                }
            }

            return(bytes);
        }