libsdl-org / SDL_mixer

An audio mixer that supports various file formats for Simple Directmedia Layer.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

retrieve the level (peak amplitude)

thrive4 opened this issue · comments

Maybe there is an alternative trick with the sdl_mixer
that I am overlooking, if not, could it be worthwhile
implementing something similar to BASS_ChannelGetLevel?

https://www.un4seen.com/doc/#bass/BASS_ChannelGetLevel.html
'Retrieves the level (peak amplitude) of a sample, stream,
MOD music, or recording channel'

for clarity:
'This function measures the level of the channel's sample data
not its level in the final output mix, so the channel's volume
([BASS_ATTRIB_VOL](https://www.un4seen.com/doc/bass
/BASS_ATTRIB_VOL.html) attribute) and panning ([BASS_ATTRIB_PAN]
does not affect it'

Note un4seen bass has a quite sophisticated variant:
https://www.un4seen.com/doc/#bass/BASS_ChannelGetLevelEx.html

What's the use case for this? This would be a fairly expensive operation, requiring decoding the entire music clip and looking at each sample.

Well in my specific case 'audio normalization'
or a slightly more generic approach to 'replay gain'
in mp3 thus can be applied to other audio formats, wav, ogg, etc.

Other possible cases:

This would be a fairly expensive operation, requiring decoding the
entire music clip and looking at each sample.

True to some extent a recent extra add-on for Bass
illustrates this BASSloud (loudness measurement):
https://www.un4seen.com/forum/?topic=20129.0

The second post by Ian ...

BASS_LOUDNESS_CURRENT option, which allows a custom
window/duration (not only 400ms or 3s) to be used
in the loudness measurement ...

might give some insight on how to keep cpu usage
manageable.

For now I am using a work around with:

    ' ghetto attempt of dynamic range compression audio
     if drc = "true" and BASS_ChannelIsActive(fx1Handle) = 1 then
         musiclevel = BASS_ChannelGetLevel(fx1Handle)
         maxlevel = min(loWORD(musiclevel), HIWORD(musiclevel)) / 32768.0f
         drcvolume = ((1.0f + (4.75f - maxlevel)) - maxlevel) * sourcevolume
         BASS_ChannelSetAttribute(fx1Handle, BASS_ATTRIB_VOL, drcvolume)
     else
         BASS_ChannelSetAttribute(fx1Handle, BASS_ATTRIB_VOL, sourcevolume)  
     end if

windows` 7 / windows 10(1903)
ram usage ~2.2MB / ~2.2MB
handles ~120 / 200
threads 7 / 8
cpu ~1% (low) / ~2%
tested on intel i5-6600T

https://github.com/thrive4/app.fb.audioplayer-bass/blob/main/audioplayer.bas

sdl_mixer version (drc not working)
https://github.com/thrive4/app.fb.audioplayer-sdlmixer

Hope this clarifies a little.