How to calculate the number of samples in audio, taking into account some parameters?

Given the following parameters:

Sample size: 16 Channel count: 2 Codec: audio/pcm Byte order: little endian Sample rate: 11025 Sample type: signed int 

How to determine the number of samples for N miliseconds recorded audio? I am new to audio processing. The codec is PCM, so I think it is uncompressed sound.

I am using Qt 4.8 on Windows 7 Ultimate x64.

+4
source share
3 answers

I think itโ€™s important here to understand what each of these terms means so that you can write code that gives you what you want.

The sampling frequency is the number of samples per second of audio, in your case 11025 (this is sometimes expressed in kHz) it is quite low compared to something like an audio CD, which is 44.1 kHz, so the sampling frequency is 44100 and higher standards. such as 48 kHz, 96 kHz.

Then you have the number of bits used for each sample, usually 8/16/24/32 bits.

Then you can have an arbitrary number of channels for each sample.

Thus, the already presented code example shows how to apply each of these numbers together to get milliseconds for samples that simply multiply the number of channels by sample bits by the sampling frequency, which gives you the data size per second of audio, then divide that number by 1000 to give you milliseconds.

It can get quite complicated if you start applying it to videos that deal with frames that are good numbers, such as 25/30/50/60 frames per second for NTSC, which are 23.98 / 29.97 / 59.94 frames per second, in this case you have to do terrible calculations to make sure they are aligned correctly.

Hope this helps.

+9
source
  /** * 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); } 

Link

+9
source

Here's the solution in pseudo code:

Considering

duration = 20 ... in milliseconds & sr = 11025 ... sampling in Hz

then the number of samples N

N = sr * dur / 1000 = 220.5

You need to round this to the nearest integer.

0
source

Source: https://habr.com/ru/post/1400023/


All Articles