none
Wave file attributes???

    Question

  • I am trying to figure out the numSamples in this:

    The canonical WAVE format starts with the RIFF header:

    0         4   ChunkID          Contains the letters "RIFF" in ASCII form
                                   (0x52494646 big-endian form).
    4         4   ChunkSize        36 + SubChunk2Size, or more precisely:
                                   4 + (8 + SubChunk1Size) + (8 + SubChunk2Size)
                                   This is the size of the rest of the chunk
                                   following this number.  This is the size of the
                                   entire file in bytes minus 8 bytes for the
                                   two fields not included in this count:
                                   ChunkID and ChunkSize.
    8         4   Format           Contains the letters "WAVE"
                                   (0x57415645 big-endian form).

    The "WAVE" format consists of two subchunks: "fmt " and "data":
    The "fmt " subchunk describes the sound data's format:

    12        4   Subchunk1ID      Contains the letters "fmt "
                                   (0x666d7420 big-endian form).
    16        4   Subchunk1Size    16 for PCM.  This is the size of the
                                   rest of the Subchunk which follows this number.
    20        2   AudioFormat      PCM = 1 (i.e. Linear quantization)
                                   Values other than 1 indicate some
                                   form of compression.
    22        2   NumChannels      Mono = 1, Stereo = 2, etc.
    24        4   SampleRate       8000, 44100, etc.
    28        4   ByteRate         == SampleRate * NumChannels * BitsPerSample/8
    32        2   BlockAlign       == NumChannels * BitsPerSample/8
                                   The number of bytes for one sample including
                                   all channels. I wonder what happens when
                                   this number isn't an integer?
    34        2   BitsPerSample    8 bits = 8, 16 bits = 16, etc.
              2   ExtraParamSize   if PCM, then doesn't exist
              X   ExtraParams      space for extra parameters

    The "data" subchunk contains the size of the data and the actual sound:

    36        4   Subchunk2ID      Contains the letters "data"
                                   (0x64617461 big-endian form).
    40        4   Subchunk2Size    == NumSamples * NumChannels * BitsPerSample/8
                                   This is the number of bytes in the data.
                                   You can also think of this as the size
                                   of the read of the subchunk following this
                                   number.
    44        *   Data             The actual sound data.

    The subchunk2size = (numSamples * numChannels) * (bitsperSample / 8). This looks fine here. My ? is
    HOW do I figure out the numSamples?, as this is not described in the canonicle wave format above. Everything else is though.

    Can the numSamples be read from the wav file itself, or do i have to figure it out? If it can be read from the wav file, where would I put it according to this code;

    <code lang="VB.Net">Public Function Read() As Boolean
            If myPath.Length = 0 Then Return False ' make sure we have a path
            If Not System.IO.File.Exists(myPath) Then Return False ' make sure that path is valid

            Dim wavFileInfo As New System.IO.FileInfo(myPath)
            Dim wavFileStream As System.IO.FileStream = wavFileInfo.OpenRead()

            ' make sure the file is big enough to at least have the appropriate header
            Dim fileSize As Long = wavFileStream.Length
            If fileSize < 44 Then Return False


            ' first, read the header
            Dim header(43) As Byte ' the header holds 44 bytes, so dim the array from 0 to 43
            Try
                wavFileStream.Read(header, 0, 44)
                myHeader = header
                myFormat = System.BitConverter.ToInt16(header, 20)
                myChannels = System.BitConverter.ToInt16(header, 22)
                mySampleRate = System.BitConverter.ToInt32(header, 24)
                myByteRate = System.BitConverter.ToInt32(header, 28)
                myBlockAlign = System.BitConverter.ToInt16(header, 32)
                myBitsPerSample = System.BitConverter.ToInt16(header, 34)
                myDataSize = System.BitConverter.ToInt32(header, 40)
            Catch ex As Exception
                Return False
            End Try

            ' make sure the data exists
            If myDataSize = 0 Then Return False

            ' then read the data
            Dim tmpData(myDataSize) As Byte
            Try
                wavFileStream.Read(tmpData, 0, myDataSize)
                myData = tmpData.Clone
                'System.Array.Copy(tmpData, myData, tmpData.Length)
            Catch ex As Exception
                Return False
            End Try

            ' if it makes it here, then gravy
            Return True
        End Function </code>

    This code is from;
    AUTHOR: Evan X. Merz
    DATE: January 2008
    CLASS: WaveFileForIO

    This works fine for getting the wave file info, It just doesn't display the numSamples which I need.

    Once again, THNX in advance for ( any help ) and your time and patience.

    rspercy


    VB 6.0, VBExprss, ASP,Net, & Gaming
    Monday, June 22, 2009 10:27 PM

All replies

  • Hi

    Might it be

    numSamples = subchunk2size/(numChannels * bitsperSample / 8)  ?

    Kenneth
    Monday, June 22, 2009 11:02 PM
  • Hi,

    If you have 44100 as the sample rate which is in Hz ( Hertz ) and 2 channels
    of 16 bits per sample ( as is the default with a COMPACT DISC ) then ;

    44100 samples per second per channel, each taking 2 bytes.>>

    Each minute takes this amount of data.>>

    60 ( seconds in a minute ) X 44100 X 2 ( channels ) X 2 ( bytes per sample if 16 bit ) =

    10584000 bytes or approx 10.09 MB per minute.

    Hence a 700 MB or ( 700 X 1024 X 1024 ) = 734003200 bytes
    giving 734003200 / 10584000 = 69.35 minute c.d.
    * So why is a 700MB c.d. labelled as 80 minutes then?

    650 MB or ( 650 X 1024 X 1024 ) = 681574400 bytes
    giving 681574400 / 10584000 = 64.4 minute c.d.
    * So why is a 650MB c.d. labelled as 74 minutes then?

    * I think c.d. labelling is for convenience to rounded figures for the consumer market.

    Regards,

    John
    Tuesday, June 23, 2009 12:57 AM
  • Hi Ken,
      
         I was just reading "General RIFF File Background" By: Robert Shuler and Rob Ryan, and when I got to page 5, I came across this;

    FACT CHUNK
         The <fact-ck> fact chunk stores important information about the contents of a wave file.This chunk is defined as follows:

              <fact-ck> -> fact(<dwFileSize:DWORD>)  // Number of Samples

    The `fact'' chunk is required if the wave form data is contained in a `wavl'' List chunk and for all compressed audio formats. The chunk is not required for PCM files using the `data'' chunk format.

    End FACT CHUNK

    This describes the numSamples as being =2 the wave file size. Using this as a base and the numSamples above, this is what I get:
    ex: filesize = 44 as byte, .....So here it is...44*2*(16/8)= 176 which basically = filesize*4, or it could be the  # of frames in the subChunk2Size. Could it be that numSamples is =2 the (# of frames)?

    ex: # of frames = 176, max#frames = 7689....= 176/ 7689 = .02 or 2%, so 768.9 = .10 or 10%, so 7689/ 7689 = 1.0 or 100% which I am looking for. 

    I think I just figured out the answer that Ive been searching for in all my other posts. I have been trying to get the correct percentage to display in a progressbar and also in a label with the ETA.  

    Ken, THANK YOU. You got me thinking with your post. If this works, I'll mark it as answer.

    Regards;
    rspercy
    VB 6.0, VBExprss, ASP,Net, & Gaming
    Tuesday, June 23, 2009 1:43 AM