🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.multimedia
Article #753

Re: Play length of WAV in seconds?

#753
From: Dave
Date: Fri, 08 Nov 2002 21:06
109 lines
4402 bytes
Thank you Fredrik I'll give it a try...


David


On Fri, 08 Nov 2002 02:01:03 GMT, "Fredrik Kvamme" <fkvamme@online.no>
wrote:

>This code snippet should extract the length of a Wav file.
>It has been ripped out of some other code I had here, so I hope it compiles
>without any problems.
>
>Fredrik Kvamme
>
>
>
>#include <stdio.h>
>
>class RIFFChunk {
>public:
>   int ID;              // 4 char ID. "RIFF", "WAVE", "fmt ", "DATA"
>   int ChunkSize;
>};
>
>
>class RIFFHeader : public RIFFChunk {
>public:
>   int Format;         // 'WAVE'
>};
>
>class RIFFWAVEFormat : public RIFFChunk {
>public:
>   short int AudioFmt;
>   short int NumChannels;
>   int SampleRate;
>   int ByteRate;
>   short int BlockAlign;
>   short int BitsPerSample;
>};
>
>
>
>float GetWAVLength(AnsiString FileName)
>{
>  FILE *infile;
>  RIFFHeader riffHeader; // All RIFF files start with this header
>  RIFFWAVEFormat riffFormat; // Then follows the Wave format
>
>  RIFFChunk riffData; // The last part is the wave samples. We only need the
>                                  // size of this part so we read only the
>RIFFChunk.
>
>  int BitsPerSample;
>  int NumChannels;
>  int SampleRate;
>  int NumSamples;
>
>  infile=fopen(FileName.c_str(),"rb");
>  fread(&riffHeader,sizeof(riffHeader),1,infile);
>  // Check that the file header makes sense.
>  if (riffHeader.ID!=*(int*)"RIFF" ) {
>    throw Exception((AnsiString)"Expected RIFF header.");
>  }
>  // Check that the format is Wave
>  if (riffHeader.Format!=*(int*)"WAVE" ) {
>    throw Exception((AnsiString)"Expected RIFF format to be WAVE.");
>  }
>  // Read the format chunk
>  fread(&riffFormat,sizeof(riffFormat),1,infile);
>  // Check that we have got the format header.
>  if (riffFormat.ID!=*(int*)"fmt ") {
>    throw Exception((AnsiString)"Expected the format chunk to be the first
>chunk.");
>  }
>  // Check that we have got a PCM file. No idea how to calculate size for a
>compressed file...
>  if (riffFormat.AudioFmt!=1) {
>    throw Exception((AnsiString)"Unexpected Wave format. Expected PCM. Got:
>" + riffFormat.AudioFmt);
>  }
>  // Copy the format data to the internal structures.
>  BitsPerSample=riffFormat.BitsPerSample;
>  NumChannels=riffFormat.NumChannels;
>  SampleRate=riffFormat.SampleRate;
>  // Move the filepointer if the chunksize is different from the riffFormat
>structure we are using.
>  // This might be the case if the format chunk is extended.
>  fseek(infile,8+riffFormat.ChunkSize-sizeof(riffFormat),SEEK_CUR);
>
>  // Next struct is the Wave data chunk. We don't need the samples, only the
>size so we will only read the RIFFChunk part.
>  // Read the datachunk in one operation:-) We love multimegabyte reads!
>  fread(&riffData,sizeof(riffData),1,infile);
>  fclose(infile); //  We are done with the file now.
>  // Lots of different encodings but it is only the 8 bit format which is
>coded as 1 byte per sample.
>  int BytesPerSample=((BitsPerSample==8)?1:2);
>  // Now we can get the number of samples from the chunksizefield in the
>datachunk.
>  NumSamples=riffData.ChunkSize/(NumChannels*BytesPerSample);
>  // We return the number of seconds.
>  return NumSamples/SampleRate;
>}
>
>
>
>
>
Technical Details
Message-ID: <hp9osuohk3vec1m24eocommul1b684c5t4@4ax.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-06!sn-xit-04!supernews.com!64.152.100.70.MISMATCH!sjc70.webusenet.com!news.webusenet.com!wn11feed!wn14feed!wn13feed!worldnet.att.net!207.217.77.102!newsfeed2.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!harp.news.atl.earthlink.net!not-for-mail
References: <1b4esukg764ssd6jggno5ns52f3cv4168t@4ax.com> <zVEy9.2049$P35.41638@news2.ulv.nextra.no>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10d0fc,3032b24542591630
X-Google-Attributes: gid10d0fc,public
X-Google-ArrivalTime: 2002-11-08 13:08:05 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-06!sn-xit-04!supernews.com!64.152.100.70.MISMATCH!sjc70.webusenet.com!news.webusenet.com!wn11feed!wn14feed!wn13feed!worldnet.att.net!207.217.77.102!newsfeed2.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!harp.news.atl.earthlink.net!not-for-mail
From: Dave <bob@bobnet.net>
Newsgroups: borland.public.cppbuilder.multimedia
Subject: Re: Play length of WAV in seconds?
Date: Fri, 08 Nov 2002 16:06:20 -0500
Organization: MindSpring Enterprises
Lines: 110
Message-ID: <hp9osuohk3vec1m24eocommul1b684c5t4@4ax.com>
References: <1b4esukg764ssd6jggno5ns52f3cv4168t@4ax.com> <zVEy9.2049$P35.41638@news2.ulv.nextra.no>
NNTP-Posting-Host: 42.20.54.71
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Server-Date: 8 Nov 2002 21:09:00 GMT
X-Newsreader: Forte Agent 1.91/32.564