Author Topic: Ask about replay gain  (Read 645 times)

one808

  • Posts: 10
Ask about replay gain
« on: 26 Nov '22 - 08:53 »
BASS SDK song replay gain, similar to foobar2000

Ian @ un4seen

  • Administrator
  • Posts: 26083
Re: Ask about replay gain
« Reply #1 on: 28 Nov '22 - 14:55 »
Do you want to apply a Replaygain value? If so, you can do that with the BASS_ATTRIB_VOLDSP option:

Code: [Select]
float vol = pow10(gain/20); // convert the replaygain dB gain to a linear value
BASS_ChannelSetAttribute(handle, BASS_ATTRIB_VOLDSP, vol); // apply it

radio42

  • Posts: 4839
Re: Ask about replay gain
« Reply #2 on: 28 Nov '22 - 15:11 »
I guess he wants to calculate the ReplayGain value.
Applying it is as simple as you suggested.

Here is a free lib which you can use: https://github.com/MTG/essentia/blob/master/src/algorithms/standard/replaygain.cpp

one808

  • Posts: 10
Re: Ask about replay gain
« Reply #3 on: 3 Dec '22 - 09:45 »
Do you want to apply a Replaygain value? If so, you can do that with the BASS_ATTRIB_VOLDSP option:

Code: [Select]
float vol = pow10(gain/20); // convert the replaygain dB gain to a linear value
BASS_ChannelSetAttribute(handle, BASS_ATTRIB_VOLDSP, vol); // apply it

I want to do auto playback gain.

1. Detect the highest waveform DB of audio before playing

For example, suppose the DB range is 0~100DB, and the current detected DB value is 95DB. I limit 90DB playback, so there are 5DB more

2. So I write (- 5DB) into the tag of the audio file.

3. Read the DB value in the tag during the next playback, and use this DB value to adjust the original audio volume output.

What should we do,

Ian @ un4seen

  • Administrator
  • Posts: 26083
Re: Ask about replay gain
« Reply #4 on: 5 Dec '22 - 13:41 »
1. Detect the highest waveform DB of audio before playing

That isn't quite the same as what Replaygain does, which is more about perceived loudness rather than the highest waveform level. If you just want to get the peak level then you can do that with BASS_ChannelGetLevelEx, something like this:

Code: [Select]
decoder = BASS_StreamCreateFile(false, filename, 0, 0, BASS_STREAM_DECODE | BASS_SAMPLE_FLOAT); // create decoder for file
float peak;
BASS_ChannelGetLevelEx(decoder, &peak, 1000000, BASS_LEVEL_MONO); // get mono peak level of entire file
float peakdb = peak > 0 ? 20 * log10(peak) : -1000: // convert to dB

If you want a Replaygain value instead, BASS doesn't include that calculation, so you would need to use another library for it, eg. replace the BASS_ChannelGetLevelEx call with BASS_ChannelGetData and then feed the data to the Replaygain calculator.

2. So I write (- 5DB) into the tag of the audio file.

BASS doesn't include tag writing, so you would again need to use another library for that part. Or perhaps store the value separately (eg. in a database) if it will only be used by your app.

3. Read the DB value in the tag during the next playback, and use this DB value to adjust the original audio volume output.

You can use the code I posted in the first reply to apply the wanted gain during playback.

one808

  • Posts: 10
Re: Ask about replay gain
« Reply #5 on: 6 Dec '22 - 15:33 »
Hi,Ian @ un4seen

handle = BASS_ StreamCreateFile (False, file, 0, 0, 0);
BASS_ ChannelPlay(handle);
float vol = pow10(-5/20); // convert the replaygain dB gain to a linear value
bRet = BASS_ ChannelSetAttribute(handle, BASS_ATTRIB_VOLDSP, vol); // apply it
The returned bRet value is false. What is the problem?

Ian @ un4seen

  • Administrator
  • Posts: 26083
Re: Ask about replay gain
« Reply #6 on: 6 Dec '22 - 17:04 »
When a BASS function call fails, you can use BASS_ErrorGetCode to find out why (look up the error code in the failed function's documentation). In this case, it may be that an old BASS version is being loaded because the BASS_ATTRIB_VOLDSP option was added in BASS 2.4.17. You can check the loaded BASS version with BASS_GetVersion.

The BASS_ATTRIB_VOLDSP value should be set before calling BASS_ChannelPlay so that it has effect right from the start. Also note that the -5/20 parameter in your pow10 call will be rounded to 0 because it isn't floating-point. You could change it like this:

Code: [Select]
handle = BASS_StreamCreateFile (False, file, 0, 0, 0);
float vol = pow10((float)-5/20); // convert the replaygain dB gain to a linear value
bRet = BASS_ChannelSetAttribute(handle, BASS_ATTRIB_VOLDSP, vol); // apply it
BASS_ChannelPlay(handle, false);

one808

  • Posts: 10
Re: Ask about replay gain
« Reply #7 on: 7 Dec '22 - 01:00 »
thank you,Ian @ un4seen

My version of BASS is old
It's OK to use BASS 2.4.17

one808

  • Posts: 10
Re: Ask about replay gain
« Reply #8 on: 8 Dec '22 - 14:14 »
The replay gain "track gain" and "track peak" on foobar2000 look a little different. The value of "track peak" can be greater than 1.0000. "track gain" and "track peak" seem to have little to do with each other. I don't understand

BASS_ChannelGetLevelEx (chan, peak, 0.1, BASS_LEVEL_MONO)

The maximum value of "peak" returned by music files is generally 1.000000

Can you help me with this

Chris

  • Posts: 2216
Re: Ask about replay gain
« Reply #9 on: 8 Dec '22 - 14:45 »
Code: [Select]
handle = BASS_StreamCreateFile (False, file, 0, 0, 0);
I suggest to create the stream with Bass_Sample_Float flag (then the Volume can be grater that 1.0)

Code: [Select]
handle = BASS_StreamCreateFile (False, file, 0, 0, BASS_SAMPLE_FLOAT);