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

mythtv at phipps-hutton.freeserve.co.uk mythtv at phipps-hutton.freeserve.co.uk
Sat Aug 10 09:28:13 UTC 2013


Quoting Jean-Yves Avenard <jyavenard at gmail.com>:

> 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.

Agreed, 32 bits is not always enough to guarantee no overflow.  
Assuming 16 bits then abs(samples[]) can be 0x8000, for 8 channels  
that's 0x4_0000, sample rate of 64k = 0x1_0000. That's heading for  
0x40_0000_0000 for one second of audio which is way over the limit of  
0x7fff_ffff for a signed int. OK, it should be divided by the frame  
rate but that could be quite small.

A 64 bit unsigned will do it. I'd prefer that to going to float just  
to save some power for the planet and it may be running on a backend  
with no FPU one day.

So Mark, could you try it with uint_64 or 'unsigned long long' and let  
us know and the we can update the wiki page.

Cheers,
Tim.

P.S. What kind of program is this? Heavy Metal MTV or action movie?  
Maybe it's just shouty adverts.




More information about the mythtv-users mailing list