🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.multimedia
3 messages
3 total messages Started by Dave Tue, 05 Nov 2002 00:39
Play length of WAV in seconds?
#751
Author: Dave
Date: Tue, 05 Nov 2002 00:39
9 lines
1212 bytes
is there a simple way to get/calculate the run time of a wav file.

I know its embedded in the header:

something like  length = samples / samplerate ??

but which bytes hold sample rate and data starts where..

any help would be appreciated..
Re: Play length of WAV in seconds?
#752
Author: Fredrik Kvamme
Date: Fri, 08 Nov 2002 02:01
95 lines
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;
}
Re: Play length of WAV in seconds?
#753
Author: 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;
>}
>
>
>
>
>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads