Mario Henrique wrote:
> This is a bad news to me :{
>
> Do you know if there is a way to get the size of a file? (It can be in
> bytes or kbytes) I´ll try to calculate the sleep time according to the
> size of the file. I´ll have to do this, because the audio files that I
> use have different lengths, and then I can´t establish a default sleep
> time
>
> Regards
>
Hello,
I have had the same problem and wrote a quick hack to measure the time
of a WAV file. It is very crude and only works on some WAV files
(specifically not on WAV files produced by Windows Sound Recorder). Here
is the code - no warranties at all.
/**
@brief Struct to contain a RIFF/WAVE file Header Chunk
*/
typedef struct
{
char RIFF [4];
long filesize;
char RIFFType [4];
}
HeadChunk;
#define FormatID 'fmt ' // chunkID for Format Chunk. NOTE: There is a
space at the end of this ID
/**
*/
typedef struct
{
char chunkID[4];
long chunkSize;
short wFormatTag;
unsigned short wChannels;
unsigned long dwSamplesPerSec;
unsigned long dwAvgBytesPerSec;
unsigned short wBlockAlign;
unsigned short wBitsPerSample;
// Note: there may be additional fields here, depending upon wFormatTag.
}
FormatChunk;
#define DataID 'data' // chunk ID for data Chunk
/**
@brief Struct to contain a WAV file data chunk
*/
typedef struct
{
char chunkID [4];
long chunkSize;
unsigned char waveformData[];
}
DataChunk;
//! Calculates duration of WAV file playout in milliseconds
int GetWavPlayTime( const char *fname )
{
FILE *fp;
int time_in_milliseconds= 0;
if ((fp = fopen(fname,"rb")))
{
HeadChunk sHead;
FormatChunk sFmt;
DataChunk sData;
if (fread(& sHead, sizeof(sHead), 1,fp))
{
if ((strncmp(sHead.RIFF , "RIFF" , 4) == 0) &&
(strncmp(sHead.RIFFType , "WAVE" , 4) == 0) )
{
if (fread(&sFmt, sizeof(sFmt), 1, fp))
{
if (strncmp(sFmt.chunkID, "fmt " , 4) == 0)
{
if (sFmt.chunkSize > 16)
{
char dummy;
for (int i = 16; i < sFmt.chunkSize; i++) fread(&dummy, 1,
1, fp);
}
if (fread(&sData, sizeof(sData), 1, fp))
{
if (strncmp(sData.chunkID, "data", 4 ) == 0)
{
int frames = sData.chunkSize / (sFmt.wChannels *
sFmt.wBitsPerSample / 8);
time_in_milliseconds = frames * 1000 / sFmt.dwSamplesPerSec;
printf("Load_Wave_File: file %s will play for %d ms\n",
fname, time_in_milliseconds);
}
}
}
}
}
}
}
if (fp) fclose(fp);
if (time_in_milliseconds == 0)
printf("Load_Wave_File: file %s was not a WAV file format\n", fname);
return time_in_milliseconds;
}
_______________________________________________
sipxtapi-dev mailing list
[email protected]
List Archive: http://list.sipfoundry.org/archive/sipxtapi-dev/