Given following parameters:
Sample size: 16
Channel count: 2
Codec: audio/pcm
Byte order: little endian
Sample rate: 11025
Sample type: signed int
How can I determine number of samples for N miliseconds
of recorded audio? I'm new in audio processing. The codec is PCM so I guess it's uncompressed audio.
I'm using Qt 4.8 on Windows 7 Ultimate x64.
Say you have X beats (quarter notes) per minute (BPM) = 60 seconds. Then one beat takes up 60/X seconds. If you record with a 44.1k sample rate, you have 44100 samples per second. And so one quarter = 60/X seconds = 60/X * 44100 samples.
The sampling frequency or sampling rate, fs, is the average number of samples obtained in one second, thus fs = 1/T. Its units are samples per second or hertz e.g. 48 kHz is 48,000 samples per second.
A typical digital audio recording has as many as 44,100 samples every second. However, it is not unusual to see 96,000 samples a second with some digital audio formats.
/**
* Converts milliseconds to samples of buffer.
* @param ms the time in milliseconds
* @return the size of the buffer in samples
*/
int msToSamples( int ms, int sampleRate, int channels ) {
return (int)(((long) ms) * sampleRate * channels / 1000);
}
/* get size of a buffer to hold nSamples */
int samplesToBytes(int nSamples, int sampleSizeBits) {
return nSamples * (sampleSizeBits / 8);
}
Reference
I think it is important here for you to understand what each of these terms means so that you can then write the code that gives you what you want.
Sample rate is the number of samples per second of audio, in your case 11025 (this is sometimes expressed in KHz) this is quite low when compared to something like CD audio which is 44.1KHz so 44100 sample rate and there are higher standards such as 48KHz, 96KHz.
Next you have the number of bits used for each sample, this can typically be 8/16/24/32 bits.
Next you can have an arbitrary number of channels for each sample.
So the code sample already posted shows how to apply each of these numbers together to get your milliseconds to samples which is simply multiplying the number of channels by the sample bits by the sample rate which gives you the data size for a single second of audio, then divide this number by 1000 to give you milliseconds.
This can get quite tricky when you start applying this to video which deals in frames which are either nice numbers like 25/30/50/60 frames a second to the NTSC based ones which are 23.98/29.97/59.94 frames a second in which case you have to do horrible calculations to make sure they align correctly.
Hope this helps.
Here a solution in pseudocode:
Given the
duration = 20... in milliseconds & sr = 11025 ... samplingrate in Hz
then the number of samples N
N = sr * dur/1000 = 220.5
You will need to round that to the closest integer number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With