encode to file with volume boost

Started by serkanp,

serkanp

i am trying to encode a mono file on m1 mac to another file.
what i want to do is to increase/boost volume
but this code does not make any difference..
what am i missing?
ps: it would be also great if we can convert to stereo while converting..


public bool EncoderExample(string inputPath, string outputPath, float volumeMultiplier = 2.0f)
        {
            var initialized = Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_NOSPEAKER, IntPtr.Zero);
            var en1= Bass.BASS_GetVersion();
            var en2 = BassEnc.BASS_Encode_GetVersion();
            var en3 = BassEnc_Mp3.BASS_Encode_MP3_GetVersion();
            int stream = Bass.BASS_StreamCreateFile(inputPath, 0, 0,
                BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT);

            // BFX_VOLUME kullan (daha doğru)
            int fx = Bass.BASS_ChannelSetFX(stream, BASSFXType.BASS_FX_VOLUME, -500);
            Console.WriteLine($"FX Handle: {fx}");

            var volume = new BASS_BFX_VOLUME();
            var oldVolume = Bass.BASS_FXGetParameters(stream, volume);
            volume.fVolume = volumeMultiplier;
            volume.lChannel = BASSFXChan.BASS_BFX_CHANALL;
            var res1 = Bass.BASS_FXSetParameters(fx, volume);
            var err = Bass.BASS_ErrorGetCode();
            Console.WriteLine($"FX Result: {res1}, Error: {err}");

            // get channel info
            BASS_CHANNELINFO info = Bass.BASS_ChannelGetInfo(stream);
            Console.WriteLine($"Channels: {info.chans}, Frequency: {info.freq}");
            //Bass.BASS_ChannelSetAttribute(stream, BASSAttribute.BASS_ATTRIB_VOL, volumeMultiplier);
            var encChan =
                BassEnc_Mp3.BASS_Encode_MP3_StartFile(stream, null, BASSEncode.BASS_ENCODE_DEFAULT, outputPath);
            int len = 0;
            while (len >= 0)
            {
                byte[] encBuffer = new byte[65536]; // 64KB buffer
                len = Bass.BASS_ChannelGetData(stream, encBuffer, encBuffer.Length);
            }

            var res3 = Bass.BASS_StreamFree(stream);
            var res4 = BassEnc.BASS_Encode_Stop(encChan);
            return true;
        }

Ian @ un4seen

You appear to be using the wrong parameter structure for the BASS_FX_VOLUME effect. The BASS_FX_VOLUME effect uses the BASS_FX_VOLUME_PARAM structure (the BASS_BFX_VOLUME structure is for the BASS_FX add-on's BASS_FX_BFX_VOLUME effect). But a simpler solution is to use the BASS_ATTRIB_VOLDSP attribute instead, like this:

Bass.BASS_ChannelSetAttribute(stream, BASSAttribute.BASS_ATTRIB_VOLDSP, volumeMultiplier);

For converting mono to stereo, you could use the BASSmix add-on's splitter feature, which allows channel rearranging and duplication. Like this:

int[] chanmap = { 0, 0, -1 }; // mono to stereo mapping
splitter = BassMix.BASS_Split_StreamCreate(stream, BASSFlag.BASS_STREAM_DECODE, chanmap); // create splitter

You would then use the splitter handle in the BASS_Encode_MP3_StartFile and BASS_ChannelGetData calls. Please see the BASS_Split_StreamCreate documentation for details on it.

serkanp

perfect.. refined code and works great..
thanks ian
 
public bool EncoderExample(string inputPath, string outputPath, float volumeMultiplier = 2.0f)
        {
            var initialized = Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_NOSPEAKER, IntPtr.Zero);
            var en1= Bass.BASS_GetVersion();
            var en2 = BassEnc.BASS_Encode_GetVersion();
            var en3 = BassEnc_Mp3.BASS_Encode_MP3_GetVersion();
            int stream = Bass.BASS_StreamCreateFile(inputPath, 0, 0,
                BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT);

           var res= Bass.BASS_ChannelSetAttribute(stream, BASSAttribute.BASS_ATTRIB_VOLDSP, volumeMultiplier);

            var err = Bass.BASS_ErrorGetCode();
             
            // get channel info
            BASS_CHANNELINFO info = Bass.BASS_ChannelGetInfo(stream);
            Console.WriteLine($"Channels: {info.chans}, Frequency: {info.freq}");
            int[] chanmap = { 0, 0, -1 }; // mono to stereo mapping
            var splitter = BassMix.BASS_Split_StreamCreate(stream, BASSFlag.BASS_STREAM_DECODE, chanmap);
            var encChan =
                BassEnc_Mp3.BASS_Encode_MP3_StartFile(splitter, null, BASSEncode.BASS_ENCODE_DEFAULT, outputPath);
            int len = 0;
            while (len >= 0)
            {
                byte[] encBuffer = new byte[65536]; // 64KB buffer
                len = Bass.BASS_ChannelGetData(splitter, encBuffer, encBuffer.Length);
            }

            var res2 = Bass.BASS_StreamFree(splitter);
            var res3 = Bass.BASS_StreamFree(stream);
            var res4 = BassEnc.BASS_Encode_Stop(encChan);
            return true;
        }