i am trying to read a wav file generated by ffmpeg with
ffmpeg -i av
FFmpeg generates a wav file with a header size of 18 but without any extension data.
This are my data structures:
struct wav_header {
uint32_t chunk_id;
uint32_t chunk_data_size;
uint32_t riff_type;
uint32_t fmt;
uint32_t fmt_chunk_size;
uint16_t format_tag;
uint16_t channels;
uint32_t samples_per_second;
uint32_t bytes_per_second;
uint16_t block_align; /* 1 => 8-bit mono, 2 => 8-bit stereo or 16-bit mono, 4 => 16-bit stereo */
uint16_t bits_per_sample;
};
struct fact_header {
uint32_t chunk_id;
uint32_t chunk_data_size;
uint32_t sample_length;
};
struct data_header {
uint32_t id;
uint32_t size;
};
If i read them out i get the following results of my wav file:
chunk_data_size: 40836134
ftm_chunk_size: 18
channels: 2
samples_per_second (samplerate): 48000
bytes_per_second: 192000
block_align: 4
bits_per_sample: 16
data_id: 61746164 -> 'data' OK
data_size: 40836096
I try now to calculate the length in seconds by using the formula
data_size / bytes_per_second
and get the following output:
length_in_seconds: 212.68800354
length_in_minutes: 3.54480004 (length_in_seconds / 60)
But when i open my file in itunes i get a length of 3:31. I also tried it with other sound files and i am always a little bit too far.
What i also tried was, to hexdump my wav file. The hexdump showed less output than if i do a for (i < data_size; i += 2) printf("%02x", data[i])
so i am somehow reading too far?
I searched the whole internet about formulas but im kinda stuck because I always come to the same results.
http://www-mmsp.ece.mcgill.ca/documents/audioformats/wave/wave.html
you can read the following statement:
"WAVE files often have information chunks that precede or follow the sound data (Data chunk). Some programs (naively) assume that for PCM data, the file header is exactly 44 bytes long and that the rest of the file contains sound data. This is not a safe assumption."
This is probably what i am doing wrong. But how can i get then the right sound_chuck_data_size?
EDIT
lile gcb pointed out below everything is alright. The solution was that the time was stored in decimal time and i had to convert it to regular time :-) this is what i came up with and it works fine:
track.duration_dec = (float)data.size / (header.bytes_per_second * 60);
track.duration_time = convert_time(track.duration_dec);
static double convert_time(double input) {
double integral;
double frac;
char buffer[48];
frac = modf(input, &integral);
sprintf(buffer, "%d.%1.f", (int)integral, frac*60);
return atof(buffer);
}