Author Topic: Add voiceover while decoding/transcoding  (Read 176 times)

mkllc

  • Posts: 40
Add voiceover while decoding/transcoding
« on: 3 May '21 - 18:12 »
Hi I'm doing a trans-coding (decoding, getdata, send to encoder) with different audio formats in a do-while loop using BASS_ChannelGetData. It works flawless but now I want to add at some point (for example in the middle of the file) a voice over from another file (a classic "you're listening blablabla") so I will have to do some fade to reduce the volume (without stoping it) of the main file mix with the voiceover file and then restore the original volume.

I think I had some idea of how to do this in real-time using a mixer and some syncs to detect the position then trigger a slider play the second audio and at the end of the voiceover audio trigger a slider to restore the original position.

But I want to do it in a decoding time to be several times faster (will be impractically with 1h file for example)

Will be possible? Any tip about how to do it? Thanks

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Add voiceover while decoding/transcoding
« Reply #1 on: 4 May '21 - 17:12 »
You can still use syncs (BASS_SYNC_POS) to trigger the fade-in/out, but instead of sliding the BASS_ATTRIB_VOL setting, you would use either BASS_Mixer_ChannelSetEnvelope (with BASS_MIXER_ENV_VOL) or the BASS_FX_VOLUME effect (via BASS_ChannelSetFX) to do it. Please see the documentation for details on those 2 options.

mkllc

  • Posts: 40
Re: Add voiceover while decoding/transcoding
« Reply #2 on: 4 May '21 - 17:23 »
And what about playing the second file at the same time? I don't use a bass mixer in the reencoding loop, just StreamCreateFile with DECODE flag and then plug it on Encode_MP3_StartFile and do-while on ChannelGetData, it works perfect and very fast (several times less than real-time playing)

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Add voiceover while decoding/transcoding
« Reply #3 on: 4 May '21 - 17:53 »
You will need a mixer to mix the voice-over with the main file. You would plug both streams into the mixer (via BASS_Mixer_StreamAddChannel) and use the mixer's handle in the BASS_Encode_MP3_StartFile and BASS_ChannelGetData calls.

mkllc

  • Posts: 40
Re: Add voiceover while decoding/transcoding
« Reply #4 on: 5 May '21 - 10:13 »
In that case I should maintain the DECODE flags on StreamCreateFile? Or I should move it to the mixer or both? (thinking on encoding as faster as possible, not in realtime). I should do the ChannelGetData on mixer too isn't it? and last one, will ChannelPlay work? or will start mixing and encoding the voice over once voice over stream is inserted on the mixer? Thanks

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Add voiceover while decoding/transcoding
« Reply #5 on: 5 May '21 - 15:36 »
Yes, mixer sources need to be "decoding channels", so you would keep the BASS_STREAM_DECODE flag set on the file streams. You would also set BASS_STREAM_DECODE on the mixer, so that you can call BASS_ChannelGetData to process it (as fast as possible) instead of BASS_ChannelPlay.

mkllc

  • Posts: 40
Re: Add voiceover while decoding/transcoding
« Reply #6 on: 6 May '21 - 21:39 »
After toying with this I got it working with mixer and everything. But I'm having problems with the fade out/fade in while inserting the voice over.

The BASS_ChannelSetAttribute works flawless but its too abrupt. I tried without luck BASS_ChannelSlideAttribute. Also with FX (BASS_ChannelSetFX(chan, BASS_FX_VOLUME, 1)) but works in a weird way I use a BASS_ChannelSetSync(chan, BASS_SYNC_POS to mark the point were I want to insert the voice over and then a BASS_ChannelSetSync(voice, BASS_SYNC_END to detect the finish of the voice and up the volume again. But don't work properly with Slide, FX or with a couple of nodes using BASS_Mixer_ChannelSetEnvelope.

Seems the problem is how to detect the proper point (position) to trigger the fade out and then insert, as the fadings are slower than the insertion (around 1 sec delay)

Any tip or idea on how I can do this timing properly? thanks!

Ian @ un4seen

  • Administrator
  • Posts: 23640
Re: Add voiceover while decoding/transcoding
« Reply #7 on: 7 May '21 - 15:59 »
You shouldn't use BASS_ChannelSlideAttribute for this because that slides at realtime speed regardless of what the decoding/processing speed is, eg. a 1 second slide will take 1 second even if 10 seconds of data was decoded/processed in that time.

Are you having a problem with the fade-in too, or just the fade-out? When using BASS_Mixer_ChannelSetEnvelope, you can use a single call for both, ie. set an envelope that fades-in at the start, holds the level, and fades-out at the end. For example, a 2 second fade-in and fade-out could be done something like this:

Code: [Select]
BASS_Mixer_StreamAddChannel(mixer, channel, BASS_MIXER_CHAN_NORAMPIN); // add source to the mix
double length = BASS_ChannelBytes2Seconds(channel, BASS_ChannelGetLength(channel, BASS_POS_BYTE)); // get source length in seconds
BASS_MIXER_NODE nodes[4];
nodes[0].pos = 0;
nodes[0].val = 0; // start muted
nodes[1].pos = BASS_ChannelSeconds2Bytes(mixer, 2);
nodes[1].val = 1; // full volume after 2 seconds
nodes[2].pos = BASS_ChannelSeconds2Bytes(mixer, length - 2);
nodes[2].val = 1; // still full volume 2 seconds from end
nodes[3].pos = BASS_ChannelSeconds2Bytes(mixer, length);
nodes[3].val = 0; // muted at the end
BASS_Mixer_ChannelSetEnvelope(channel, BASS_MIXER_ENV_VOL, nodes, 4); // apply the envelope

Note that the envelope's "pos" values are based on the mixer's sample format (not the source's), hence the mixer handle in those BASS_ChannelSeconds2Bytes calls. You could also use the BASS_MIXER_ENV_REMOVE flag if you want the source removed at the end of the envelope.