Author Topic: Mix two stereo files and save  (Read 671 times)

arteme

  • Posts: 14
Mix two stereo files and save
« on: 22 Aug '14 - 14:00 »
Hi.
I have to mix two stereo files and save them. But the problem is, that files has different length and align must be with end of files

A [][][][][][][][][][][][][][][][][][]
   
B [][][][][][][][][][][][][][]

MIX(A,B) must return
[][][][][][][][][][][][][][][][][][]
||||||||[][][][][][][][][][][][][][]

How can I do that?

Ian @ un4seen

  • Administrator
  • Posts: 20401
Re: Mix two stereo files and save
« Reply #1 on: 22 Aug '14 - 17:24 »
You could delay the shorter file by the length difference via BASS_Mixer_StreamAddChannelEx, something like this...

Code: [Select]
QWORD len1=BASS_ChannelGetLength(source1, BASS_POS_BYTE); // get length of 1st source
QWORD len2=BASS_ChannelGetLength(source2, BASS_POS_BYTE); // get length of 2nd source
if (len1>len2) { // 1st source is longer
BASS_Mixer_StreamAddChannel(mixer, source1, 0); // add the 1st source to the mix
BASS_Mixer_StreamAddChannelEx(mixer, source2, 0, len1-len2, 0); // add the 2nd source to the mix with a delay
} else { // 2nd source is longer (or equal)
BASS_Mixer_StreamAddChannel(mixer, source2, 0); // add the 2nd source to the mix
BASS_Mixer_StreamAddChannelEx(mixer, source1, 0, len2-len1, 0); // add the 1st source to the mix with a delay
}

Please see the documentation for details on the aforementioned functions.

Note that this assumes that the mixer and sources all have the same sample format. If they don't you could convert the source lengths to seconds via BASS_ChannelBytes2Seconds before comparing them, and then convert the difference back to bytes for the delay...

Code: [Select]
double len1=BASS_ChannelBytes2Seconds(source1, BASS_ChannelGetLength(source1, BASS_POS_BYTE)); // get length of 1st source in seconds
double len2=BASS_ChannelBytes2Seconds(source2, BASS_ChannelGetLength(source2, BASS_POS_BYTE)); // get length of 2nd source in seconds
if (len1>len2) { // 1st source is longer
BASS_Mixer_StreamAddChannel(mixer, source1, 0); // add the 1st source to the mix
BASS_Mixer_StreamAddChannelEx(mixer, source2, 0, BASS_ChannelSeconds2Bytes(mixer, len1-len2), 0); // add the 2nd source to the mix with a delay
} else { // 2nd source is longer (or equal)
BASS_Mixer_StreamAddChannel(mixer, source2, 0); // add the 2nd source to the mix
BASS_Mixer_StreamAddChannelEx(mixer, source1, 0, BASS_ChannelSeconds2Bytes(mixer, len2-len1), 0); // add the 1st source to the mix with a delay
}

Please see the documentation for details on the aforementioned functions.

arteme

  • Posts: 14
Re: Mix two stereo files and save
« Reply #2 on: 17 Jan '15 - 15:10 »
Forget to answer you :)
Thanks for responce!