• Advertisement
Sign in to follow this  

Cant play correct a Wave File

This topic is 1538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello Guys,

 

i have the problem that when i try to read and load a Wave File that by the start of playing the File i get always a little crack, i used befor the WaveStream Class and there works everything perfect but i would to have my own Class, but the really strange thing is that the length of the Wave File is everytime different between the WaveStream Class and my own Class sad.png

 

WaveStream Class, everything works perfect:

[cs]

            using (WaveStream waveFile = new WaveStream(fileName))
            {
                this.description = new SoundBufferDescription();
                this.description.Format = waveFile.Format;
                this.description.SizeInBytes = (int)waveFile.Length;
                this.description.Flags = BufferFlags.ControlFrequency | BufferFlags.ControlPan | BufferFlags.ControlVolume;

                this.buffer = new SecondarySoundBuffer(device, this.description);

                this.data = new byte[this.description.SizeInBytes];
                waveFile.Read(this.data, 0, (int)waveFile.Length);
                this.buffer.Write(this.data, 0, LockFlags.None);
            }

[/cs]

 

My Class

[cs]

            byte[] waveBuffer = File.ReadAllBytes(fileName);
            int waveFileLength = waveBuffer.Length;

            uint chunkID = Mathhelper.GetInt(waveBuffer, 0);
            uint chunkSize = Mathhelper.GetInt(waveBuffer, 4);
            uint format = Mathhelper.GetInt(waveBuffer, 8);
            uint subchunk1ID = Mathhelper.GetInt(waveBuffer, 12);
            uint subchunk1Size = Mathhelper.GetInt(waveBuffer, 16);
            ushort audioFormat = Mathhelper.GetShort(waveBuffer, 20);
            ushort numChannels = Mathhelper.GetShort(waveBuffer, 22);
            uint sampleRate = Mathhelper.GetInt(waveBuffer, 24);
            uint byteRate = Mathhelper.GetInt(waveBuffer, 28);
            ushort blockAlign = Mathhelper.GetShort(waveBuffer, 32);
            ushort bitsPerSample = Mathhelper.GetShort(waveBuffer, 34);
            uint subchunk2ID = Mathhelper.GetInt(waveBuffer, 36);
            uint subchunk2Size = Mathhelper.GetInt(waveBuffer, 40);

            WaveFormat waveFormat = new WaveFormat();
            waveFormat.FormatTag = WaveFormatTag.Pcm;
            waveFormat.Channels = (short)numChannels;
            waveFormat.SamplesPerSecond = (int)sampleRate;
            waveFormat.BlockAlignment = (short)blockAlign;
            waveFormat.BitsPerSample = (short)bitsPerSample;
            waveFormat.AverageBytesPerSecond = waveFormat.SamplesPerSecond * waveFormat.BlockAlignment;

            this.description = new SoundBufferDescription();
            this.description.Format = waveFormat;
            this.description.SizeInBytes = waveFileLength;
            this.description.Flags = BufferFlags.ControlFrequency | BufferFlags.ControlPan | BufferFlags.ControlVolume;

            this.buffer = new SecondarySoundBuffer(device, this.description);
            this.data = waveBuffer;
            this.buffer.Write(this.data, 0, LockFlags.None);

[/cs]

 

What make i wrong ? sad.png

 

Greets

SharinganShuriken

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement