Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate number of samples in audio given some parameters?

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.

like image 349
Donotalo Avatar asked Mar 06 '12 14:03

Donotalo


People also ask

How do you calculate the number of samples in audio?

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.

How do you find the number of sample frequencies?

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.

How many samples per second is audio?

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.


3 Answers

   /**
     * 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

like image 80
perreal Avatar answered Sep 30 '22 01:09

perreal


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.

like image 45
EdChum Avatar answered Sep 30 '22 02:09

EdChum


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.

like image 39
amir.audio Avatar answered Sep 30 '22 03:09

amir.audio