Author Topic: Calculating ReplayGain ?  (Read 143 times)

ken

  • Posts: 739
Calculating ReplayGain ?
« on: 7 Oct '17 - 06:23 »
Hi,

Need to calculate the gain and peak values from a bass decode stream.

Found ReplayGain C# code on github, but not sure how to get the samples data from BASS, I have tried looping true the stream and feed it with L/R from BASS_ChannelGetLevel but the values out are not correct.

https://github.com/karamanolev/NReplayGain


my code so fare ;)

Code: [Select]

private void TestGain(string fname)
        {
            int chan = Bass.BASS_StreamCreateFile(fname, 0, 0, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT);
            long dur = Bass.BASS_ChannelGetLength(chan);

            List<int> _sLeft = new List<int>();
            List<int> _sRight = new List<int>();

            Task.Run(() => {
                int bufLen = 1024;
                byte[] buf = new byte[bufLen];

                while (Bass.BASS_ChannelGetPosition(chan) < dur)
                {

                    int length = Bass.BASS_ChannelGetData(chan, buf, bufLen);

   
                    int level = Bass.BASS_ChannelGetLevel(chan);

                    int _l = Un4seen.Bass.Utils.LowWord32(level);
                    int _r = Un4seen.Bass.Utils.HighWord32(level);

                    _sLeft.Add(_l);
                    _sRight.Add(_r);

                }

                Bass.BASS_StreamFree(chan);

                TrackGain trackGain = new TrackGain(44100, 16);
                trackGain.AnalyzeSamples(_sLeft.ToArray(), _sRight.ToArray());

                double gain = trackGain.GetGain();
                double peak = trackGain.GetPeak();

                Console.WriteLine("Gain: " + gain + ", Peak: " + peak);

            });


        }



Any suggestions?

Thanks!
« Last Edit: 7 Oct '17 - 08:59 by ken »

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Calculating ReplayGain ?
« Reply #1 on: 9 Oct '17 - 16:58 »
All of the samples need to be analysed, so you will need to use BASS_ChannelGetData rather than BASS_ChannelGetLevel. Perhaps your loop could look something like this:

Code: [Select]
                int bufLen = 1024;
                short[] buf = new short[bufLen];
               
                while (true)
                {
                    int length = Bass.BASS_ChannelGetData(chan, buf, bufLen * sizeof(short));
                    if (length == -1) break;

                    for (int a = 0; a < length / sizeof(short); a += 2)
                    {
                        _sLeft.Add(buf[a]);
                        _sRight.Add(buf[a + 1]);
                    }
                }

It looks like the code requires the sample data to be 16-bit, so you should also remove the BASS_SAMPLE_FLOAT flag from the BASS_StreamCreateFile call.

ken

  • Posts: 739
Re: Calculating ReplayGain ?
« Reply #2 on: 11 Oct '17 - 16:49 »
Thanks Ian, that helped!