how to find duration of sample in milli seconds?

charnjit

Well-known member
i apply formula on 44100 sample rate 16 bit .wav mono file
C++:
  SMP_DURATION = sizes[0]-44; // REMOVE 44byte HEADER
  SMP_DURATION = (SMP_DURATION /( 44100 * 1 * (16 / 8)))*1000;
   Serial.println("Getting Duration");
   Serial.print("1st Sample Duration:");  Serial.println(SMP_DURATION);  // SMP_DURATION uncorrect
i got 2000but my sample is 2264 i find it in audacity.
am i using wrong formula????
what is correct???
 
What is the data type of SMP_DURATION? You might need to do the calculation using float. Something like this
Code:
SMP_DURATION = (int)(((float)SMP_DURATION /( 44100.0f * 1 * (16 / 8)))*1000.0f);

Or you could try rearranging the order of the terms, something like this
Code:
SMP_DURATION = (SMP_DURATION * 1000 /( 44100 * 1 * (16 / 8)));
but watch out for overflow if the datatype of SMP_DURATION is too small
 
Thank you ... thebigg
it is
C++:
uint32_t SMP_DURATION;
yes , Your code SMP_DURATION = (SMP_DURATION * 1000 /( 44100 * 1 * (16 / 8))); gives a very similar result.

I got 2265 instead of 2264 using below code
C++:
  SMP_DURATION = sizes[0]-44; // REMOVED 44byte HEADER
 SMP_DURATION = (SMP_DURATION * 1000 /( 44100 * 1 * (16 / 8)));
   Serial.println("Getting Duration");
   Serial.print("1st Sample Duration:");  Serial.println(SMP_DURATION);
Do I have to make do with this or can I try something else to get the full result?

if we try SMP_DURATION = (int)(((float)SMP_DURATION /( 44100.0f * 1 * (16 / 8)))*1000.0f);
what data type is suited to take size from (upto 10mb .wav file)???
 
Back
Top