/// <summary>
        /// Read the header from a wave file, and move the
        /// reader's position to the beginning of the data chunk
        /// </summary>
        /// <param name="reader">Reader for the wave file.</param>
        /// <returns>Format of the wave.</returns>
        private WaveFormat1 ReadHeader(BinaryReader reader)
        {
            if (ReadChunk(reader) != "RIFF")
                throw new Exception("Invalid file format");

            reader.ReadInt32(); // File length minus first 8 bytes of RIFF description, we don't use it

            if (ReadChunk(reader) != "WAVE")
                throw new Exception("Invalid file format");

            if (ReadChunk(reader) != "fmt ")
                throw new Exception("Invalid file format");

            int len = reader.ReadInt32();
            if (len < 16) // bad format chunk length
                throw new Exception("Invalid file format");

            WaveFormat1 carrierFormat = new WaveFormat1();
            carrierFormat.FormatTag = reader.ReadInt16();
            carrierFormat.Channels = reader.ReadInt16();
            carrierFormat.SamplesPerSec = reader.ReadInt32();
            carrierFormat.AvgBytesPerSec = reader.ReadInt32();
            carrierFormat.BlockAlign = reader.ReadInt16();
            carrierFormat.BitsPerSample = reader.ReadInt16();

            // advance in the stream to skip the wave format block
            len -= 16; // minimum format size
            while (len > 0)
            {
                reader.ReadByte();
                len--;
            }

            // assume the data chunk is aligned
            string chunk;
            do
            {
                chunk = ReadChunk(reader);
            } while (reader.BaseStream.Position < reader.BaseStream.Length && chunk != "data");

            return carrierFormat;
        }
 /// <summary>Constructor.</summary>
 /// <param name="format">Format header information.</param>
 /// <param name="samples">Wave samples.</param>
 public WaveSound(WaveFormat1 format, short[] samples)
 {
     this.format = format;
     this.samples = samples;
 }
 public static extern int waveOutOpen(out IntPtr hWaveOut, int uDeviceID, WaveFormat1 lpFormat, WaveDelegate dwCallback, int dwInstance, int dwFlags);
        /// <summary>Reads a wave file.</summary>
        /// <param name="fileName">Name and path of the file.</param>
        /// <returns>Format and samples.</returns>
        private void ReadFile(string fileName)
        {
            BinaryReader reader = new BinaryReader(new FileStream(fileName, FileMode.Open));

            this.format = ReadHeader(reader);

            if (format.BitsPerSample != 8 && format.BitsPerSample != 16)
            {
                System.Windows.Forms.MessageBox.Show("The sound has " + format.BitsPerSample + " bits per sample. Please choose a sound with 8 or 16 bits per sanmple.");
                return;
            }

            int dataLength = reader.ReadInt32();

            int maxSampleValue = 0;
            int bytesPerSample = format.BitsPerSample / 8;
            int countSamples = dataLength / bytesPerSample;

            sbyte[] channelSamples8 = new sbyte[countSamples];
            short[] channelSamples16 = new short[countSamples];
            sbyte channelSample8;

            int channelSamplesIndex = 0;
            int absValue;
            for (int sampleIndex = 0; sampleIndex < countSamples; sampleIndex++)
            {

                if (format.BitsPerSample == 8)
                {
                    channelSample8 = reader.ReadSByte();
                    channelSamples16[channelSamplesIndex] = (short)(channelSample8 + (channelSample8 << 8));
                    if (Math.Abs((int)channelSample8) > maxSampleValue)
                    {
                        maxSampleValue = Math.Abs((int)channelSample8);
                    }
                }
                else
                {
                    channelSamples16[channelSamplesIndex] = reader.ReadInt16();
                    absValue = Math.Abs((int)channelSamples16[channelSamplesIndex]);
                    if (absValue > maxSampleValue)
                    {
                        maxSampleValue = (absValue > short.MaxValue) ? absValue - 1 : absValue;
                    }
                }

                channelSamplesIndex++;
            }

            reader.Close();

            format.BitsPerSample = 16;
            //if (format.BitsPerSample == 8)
            //{
            //    for (int n = 0; n < channelSamples8.Length; n++)
            //    {
            //        channelSamples16[n] = (short)channelSamples8[n];
            //    }
            //}
            var rrr = channelSamples8;
            this.samples = channelSamples16;
        }
Esempio n. 5
0
        //public short[] ReadFileRaw(string fileName, out float freq)
        //public short[] ReadFileRaw(out float freq, out int chan)
        //public short[] ReadFileRaw()
        public short[] ReadFileWav()
        {
            reader = new BinaryReader(new FileStream(filename, FileMode.Open));
            format = ReadHeader(reader);

            WaveFormat1 d = format;

            short[] ddss;
            int dataLength = reader.ReadInt32();

            int maxSampleValue = 0;
            int bytesPerSample = format.BitsPerSample / 8;
            int countSamples = dataLength / bytesPerSample;
            //freq = format.SamplesPerSec;
            { }

            //sbyte[] channelSamples8 = new sbyte[countSamples];
            short[] channelSamples16 = new short[countSamples];
            sbyte channelSample8;

            int channelSamplesIndex = 0;
            int absValue;

            if (format.BitsPerSample == 8)
            // 8 bit
            {
                reader.Close();

                byte[] bt;
                FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

                // Raw file don't have any "Header"
                BinaryReader br = new BinaryReader(fs);
                bt = br.ReadBytes((int)br.BaseStream.Length);
                br.Close();

                //ddss = new short[bt.Length - 44 - 76];
                ddss = new short[bt.Length - 44 - 158];

                int k = 0;
                for (int i = 44; i < bt.Length - 158; i++)
                    ddss[k++] = Convert.ToInt16(bt[i]);
            }
            // 16 bit
            else
            {
                ddss = new short[countSamples];
                for (int sampleIndex = 0; sampleIndex < countSamples; sampleIndex++)
                {
                    channelSamples16[channelSamplesIndex] = reader.ReadInt16();
                    absValue = Math.Abs((int)channelSamples16[channelSamplesIndex]);
                    if (absValue > maxSampleValue)
                    {
                        maxSampleValue = (absValue > short.MaxValue) ? absValue - 1 : absValue;
                    }

                    channelSamplesIndex++;
                }

                ddss = channelSamples16;
                reader.Close();
            }

            //chan = format.Channels;

            //return channelSamples16;
            return ddss;
        }