Infoshare
01-29-2001, 05:48 AM
How can I extract the format like PCM, DSP group speech, and the duration in seconds from a WAV file. Pls help
Multi Media WAV fileInfoshare 01-29-2001, 05:48 AM How can I extract the format like PCM, DSP group speech, and the duration in seconds from a WAV file. Pls help JAJ 02-20-2001, 01:17 PM The typical header for a wav file is contained within the first 44 bytes of the file, and looks something like this (typical values shown on right): Type WaveStruct FileID As String * 4 'Normally "RIFF" in ascii' FileLen_4 As Long 'Length of file after this value RIFFId As String * 4 '"WAVE" for a wave file' FmtID As String * 4 '"fmt "' FmtLen As Long 'Length of format chunk' WAVEFMT As WAVEFORMATEX 'Contains format data' DATAId As String * 4 '"DATA"' DataLen As Long 'Length of data chunk' End Type The WAVEFORMATEX structure contains the information you want and is defined as follows: Type WAVEFORMATEX wFormatTag As Integer '=1 for a PCM file nChannels As Integer '1 or 2 (mono or stereo) nSamplesPerSec As Long '11025, 22050, 44100' nAvgBytesPerSec As Long 'depends on format = nSamplesPerSec * nBlockAlign nBlockAlign As Integer 'Number of bytes in one sample wBitsPerSample As Integer '8 or 16(-bit) 'cbSize As Integer 'Not needed End Type Note that the cbSize parameter is commented out - this value is not present in a wave file header - it's just there so you can add extended information about other formats. The length of the format chunk could actually vary, but as long as you know the file you're opening is a wave file, you don't need to check this (the header is almost always 44 bytes long). So to answer your question, the format code is easy to extract once you've read the header of the file into this structure - it's the value of wFormatTag listed above (=1 for PCM). To work out the duration, take DataLen and divide by nAvgBytesPerSec to get the number of seconds. Hope this helps, JAJ. Infoshare 02-22-2001, 08:09 AM Thanks JAJ, Pls. tell me more about extracting the format and length of a wav file. 1) Which API call is used for it. 2) How we get the handle of a wavefile, for passing it to the API call. JAJ 02-22-2001, 11:47 AM Hmm, where to start... The functions that you mentioned in your email to me (ie waveInPrepareHeader, etc..) are NOT to do with accessing wave files - they are actually involved in sampling an input using your soundcard, which I'm not gonna go into right now. The handle you were referring to is the handle to the device you've opened, not a wave file. Anyway, to the point: The best way to access the header is to simply read the header of the file into the data structure I described in the last post. The inbuilt functions in VB aren't really the best way to do this, so I use the api file IO functions as follows (I think that the file IO declarations have been superceeded, but they work anyway): '********************************** '** File I/O Function Declarations: Declare Function lread& Lib "kernel32" Alias "_lread" (ByVal hFile As Long, lpBuffer As Any, ByVal wBytes As Long) 'Declare Function lwrite& Lib "kernel32" Alias "_lwrite" (ByVal hFile As Long, lpBuffer As Any, ByVal wBytes As Long) Declare Function lopen& Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) Declare Function lclose& Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) Public Const OF_SHARE_EXCLUSIVE& = &H10 Public Const READ_WRITE& = 2 'Could use READ if you wanted... ***** ***** Now the function that reads the header ' Filename is the name of the wave file you want to open function ReadWaveFileHeader( Filename as string ) dim filehandle as long, IRET as long ' Open the file filehandle = lopen(Filename, OF_SHARE_EXCLUSIVE Or READ_WRITE) ' Read the first 44 bytes into the structure IRET = lread(filehandle, WaveHd, 44) ' close the file lclose filehandle end function ******* The variable WaveHd would be defined as a global WaveStruct type, as shown in the previous post. After calling this function with the argument of a wave filename (eg "C:\windows\media\chime.wav"), this structure should contain all the information you need. If you're interested in actually playing wave-audio using the low-level api, let me know. I should warn you that you're better off using an activeX control for that kind of thing unless you want to do something that requires manipulation of the wave in realtime. Regards, John Jones. <P ID="edit"><FONT SIZE=-1><EM>Edited by JAJ on 02/22/01 03:14 PM (server time).</EM></FONT></P> |
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum