21 May '13 - 23:32 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]
  Reply  |  Print  
Author Topic: Position Difference between two channels  (Read 364 times)
atza
Posts: 69


« on: 3 Nov '11 - 09:12 »
Reply with quoteQuote

I'm trying to synchronize two channels already playing (without mixer or stoping) and I have to do this by simply setting position:

play(chan1);
play(chan2);

pos:=get_position(chan1);
set_position(chan2,pos);

Now they are almost in sync but not exactly so there is light delay/flange sound heard.
If I guess delay (based on what I hear) and just add 10000 or so bytes to position I can get them in total sync (after trying couple of times of course)
Is there a way to calculate that difference between chan1 and chan2 ? Because it would be different on different computers etc...so my guess is only good for my computer




Logged
atza
Posts: 69


« Reply #1 on: 3 Nov '11 - 09:21 »
Reply with quoteQuote

Well,i set a timer to calculate that difference:
pos1:=BASS_ChannelGetPosition(chan1,BASS_POS_BYTE);
pos2:=BASS_ChannelGetPosition(chan2channel,BASS_POS_BYTE);
dif:=pos2-pos1;

And it gives quite stable results.
If I set timer to repeat itself every 10msec dif  is always around the same value(8000) +/- 20 or so...
That's pretty good,(out of sync can't be noticed).
But there has to be more reliable way of calculating difference.
Something involving buffer sizes or computer latency
« Last Edit: 3 Nov '11 - 09:24 by atza » Logged
Ian @ un4seen
Administrator
Posts: 15259


« Reply #2 on: 3 Nov '11 - 15:20 »
Reply with quoteQuote

To get 2 channels to start playing in sync (there may still be a slight gap on Windows), you can link them, something like this...

BASS_ChannelSetLink(chan1, chan2); // link chan2 to chan1
BASS_ChannelPlay(chan1, FALSE); // start them both

To get a channel to start in sync with another one that is already playing, you could try using the BASS_INFO "latency" value (after adding the BASS_DEVICE_LATENCY flag to your BASS_Init call). Something like this...

BASS_INFO info;
BASS_GetInfo(&info);
QWORD pos=BASS_ChannelGetPosition(chan1, BASS_POS_BYTE); // get position of chan1
pos+=BASS_ChannelSeconds2Bytes(chan1, info.latency/1000.0); // add the device latency to it
BASS_ChannelSetPosition(chan2, pos); // set chan2 to that position
BASS_ChannelPlay(chan2, FALSE); // start playing chan2

But note that the BASS_ChannelPlay call will need to decode some data before beginning playback (there needs to be something to play), so that will add some delay to playback starting.

If there is a gap between the position of 2 playing channels and you would like to bring them closer together, that could be done by slightly adjusting the playback rate (BASS_ATTRIB_FREQ) of one of them.
Logged
atza
Posts: 69


« Reply #3 on: 3 Nov '11 - 15:51 »
Reply with quoteQuote

Excellent!  I mean so far so good

Before adjusting positions I started chan2 with its volume turned down to zero and then let it sleep(200) ;

BASS_INFO info;
BASS_GetInfo(&info);
BASS_ChannelSetAttribute(chan2, BASS_ATTRIB_VOL, 0);
BASS_ChannelPlay(chan2, FALSE);
sleep(200);
QWORD pos=BASS_ChannelGetPosition(chan1, BASS_POS_BYTE); // get position of chan1
pos+=BASS_ChannelSeconds2Bytes(chan1, info.latency/1000.0); // add the device latency to it
BASS_ChannelSetPosition(chan2, pos); // set chan2 to that position
BASS_ChannelSetAttribute(chan2, BASS_ATTRIB_VOL, vol);// restore volume

Works very well.
It seems that short sleep(200) wasn't necessery at all


As bass_init with latency flag adds ~3sec on startup,is it wise to store latency in some ini file?
Or is it necessary to calculate latency every time application is loaded?



Logged
Ian @ un4seen
Administrator
Posts: 15259


« Reply #4 on: 3 Nov '11 - 17:33 »
Reply with quoteQuote

So long as the device doesn't change, it will usually be safe to just store and reuse the latency value.
Logged
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.18 | SMF © 2013, Simple Machines