Article View: borland.public.cppbuilder.multimedia
Article #752Re: Play length of WAV in seconds?
From: Fredrik Kvamme
Date: Fri, 08 Nov 2002 02:01
Date: Fri, 08 Nov 2002 02:01
95 lines
4148 bytes
4148 bytes
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:
<zVEy9.2049$P35.41638@news2.ulv.nextra.no>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-06!sn-xit-04!supernews.com!newsfeed.gamma.ru!Gamma.RU!news.tele.dk!news.tele.dk!small.news.tele.dk!luth.se!news-peer-europe.sprintlink.net!news.stupi.se!newsfeed1.ulv.nextra.no!nextra.com!news2.ulv.nextra.no.POSTED!53ab2750!not-for-mailReferences:
<1b4esukg764ssd6jggno5ns52f3cv4168t@4ax.com>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-07 18:06:01 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-06!sn-xit-04!supernews.com!newsfeed.gamma.ru!Gamma.RU!news.tele.dk!news.tele.dk!small.news.tele.dk!luth.se!news-peer-europe.sprintlink.net!news.stupi.se!newsfeed1.ulv.nextra.no!nextra.com!news2.ulv.nextra.no.POSTED!53ab2750!not-for-mail From: "Fredrik Kvamme" <fkvamme@online.no> Newsgroups: borland.public.cppbuilder.multimedia References: <1b4esukg764ssd6jggno5ns52f3cv4168t@4ax.com> Subject: Re: Play length of WAV in seconds? Lines: 101 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2720.3000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Message-ID: <zVEy9.2049$P35.41638@news2.ulv.nextra.no> NNTP-Posting-Host: 80.213.32.53 X-Complaints-To: news-abuse@nextra.no NNTP-Posting-Date: Fri, 08 Nov 2002 03:01:03 MET X-Trace: news2.ulv.nextra.no 1036720863 80.213.32.53 Date: Fri, 08 Nov 2002 02:01:03 GMT