[mythtv-users] Commercial detection with silence - silence.cpp

Jean-Yves Avenard jyavenard at gmail.com
Sat Aug 10 00:28:39 UTC 2013


On 10 August 2013 02:55, Roger Siddons <dizygotheca at ntlworld.com> wrote:

> Although using float is safest, I'm sure there must be a better solution. If
> any audio/C gurus are reading, then here's the code in question. What's the
> appropriate portable C++ type to use that will cope with all audio formats ?
>
> // Allocate data buffer to contain audio data from one video frame.
> const size_t frameSamples = metadata.channels * metadata.samplerate / 25.0;
>
> // determine average audio level in this frame
> long avgabs = 0;
> for (unsigned i = 0; i < frameSamples; i++)
>      avgabs += abs(samples[i]);
> avgabs = avgabs / frameSamples;
>

a long is 32 bits on 32 bit platforms...
The issue may be as simple as an overflow when calculating your average.
You are calculating an average on positive numbers, so making it an
unsigned long would be a start.

Using a float for the purpose of calculating an average is good enough.
You could always change it to a uint64_t (or unsigned long long if you
prefer C type)

in const size_t frameSamples = metadata.channels * metadata.samplerate
/ 25.0; in theory you could also face an overflow (though not the case
in real life) changing it to:
const size_t frameSamples = metadata.channels / 25.0f * metadata.samplerate;
would solve all issues (note 25.0 is a double, 25.0f is a float, hence faster)


More information about the mythtv-users mailing list