From: Mathias L. <lun...@us...> - 2005-02-18 21:32:39
|
Update of /cvsroot/lmuse/muse/muse In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7744/muse Modified Files: Tag: REL07 sync.cpp Log Message: Midi clock slave improvements Index: sync.cpp =================================================================== RCS file: /cvsroot/lmuse/muse/muse/sync.cpp,v retrieving revision 1.6.2.2 retrieving revision 1.6.2.3 diff -C2 -d -r1.6.2.2 -r1.6.2.3 *** sync.cpp 17 Jan 2005 21:41:13 -0000 1.6.2.2 --- sync.cpp 18 Feb 2005 21:32:28 -0000 1.6.2.3 *************** *** 288,301 **** double tdiff0 = mclock0 - mclock1; double tdiff1 = mclock1 - mclock2; processMidiClock(); // Compare w audio if playing: if (audio->isPlaying() /*state == PLAY*/) { recTick += config.division / 24; // The one we're syncing to int tempo = tempomap.tempo(0); int songtick = audio->pos().tick(); int tickdiff = songtick - recTick; ! if (debugSync) { // --- 288,400 ---- double tdiff0 = mclock0 - mclock1; double tdiff1 = mclock1 - mclock2; + double averagetimediff = 0.0; + + if (mclock1 != 0.0) { + if (storedtimediffs < 24) + { + timediff[storedtimediffs] = mclock0 - mclock1; + storedtimediffs++; + } + else { + for (int i=0; i<23; i++) { + timediff[i] = timediff[i+1]; + } + timediff[23] = mclock0 - mclock1; + } + // Calculate average timediff: + for (int i=0; i < storedtimediffs; i++) { + averagetimediff += timediff[i]/storedtimediffs; + } + /*if (debugSync) { + printf("stored diffs: %d tmptimediff: %f\n", storedtimediffs, averagetimediff); + }*/ + } processMidiClock(); // Compare w audio if playing: if (audio->isPlaying() /*state == PLAY*/) { + //BEGIN standard setup: recTick += config.division / 24; // The one we're syncing to int tempo = tempomap.tempo(0); + //double songtick = audio->pos().tick(); + //unsigned curFrame = audio->pos().frame(); + unsigned curFrame = audio->pos().frame(); + double songtick = (double(curFrame)/double(sampleRate)) * config.division * 1000000.0 / double(tempo); + //double tickdiff = songtick - (double) recTick; + + double scale = tdiff0/averagetimediff; + //double scale = averagetimediff/tdiff0; + double tickdiff = songtick - ((double) recTick - 24 + scale*24.0); + + //END standard setup + if (debugSync) { + // + // Create debug values for printing out which beat we're at, etc etc... yaddayadda... + // + int m, b, t; + audio->pos().mbt(&m, &b, &t); + int song_beat = b + m*4; // if the time-signature is different than 4/4, this will be wrong. + int sync_beat = recTick/config.division; + printf("pT=%.3f rT=%d diff=%.3f songB=%d syncB=%d scale=%.3f", songtick, recTick, tickdiff, song_beat, sync_beat, scale); + } + //if ((mclock2 !=0.0) && (tdiff1 > 0.0) && tickdiff != 0.0 && lastTempo != 0) { + if ((mclock2 !=0.0) && (tdiff1 > 0.0) && fabs(tickdiff) > 0.5 && lastTempo != 0) { + // Interpolate: + double tickdiff1 = songtick1 - recTick1; + double tickdiff2 = songtick2 - recTick2; + //double newtickdiff = tickdiff/3.0 + tickdiff1/5.0 + tickdiff2/7.0; //2 min 15 sec, 120BPM, -p 512 jackd + //double newtickdiff = tickdiff/4.0 + tickdiff1/4.0 + tickdiff2/4.0; // Not long... :-P + //double newtickdiff = tickdiff/5.0 + tickdiff1/8.0 + tickdiff2/12.0; //3,5 mins on 120BPM, -p 512 jackd + //double newtickdiff = tickdiff/7.0 + tickdiff1/8.0 + tickdiff2/9.0; //2 min 15 sec, 120BPM, -p 512 jackd + //double newtickdiff = tickdiff/5.0 + tickdiff1/8.0 + tickdiff2/16.0; //3,5 mins on 120BPM, -p 512 jackd + double newtickdiff = tickdiff/5.0 + tickdiff1/16.0 + tickdiff2/24.0; //5 mins 30 secs on 116BPM, -p 512 jackd + //double newtickdiff = tickdiff/5.0 + tickdiff1/23.0 + tickdiff2/31.0; //5 mins on 116BPM, -p 512 jackd + //double newtickdiff = tickdiff + tickdiff1/8.0 + tickdiff2/16.0; // Not long... + + /* + //BEGIN turbulence-test: + // Try to handle "turbulence" - if diffs have been huge lately, make that noticeable on the new change + double w = abs(tickdiff1 + tickdiff2); + double weight = (1.0 + w*w/50.0); + if (weight > 10.0) + weight = 10.0; + if (debugSync) + printf(" w=%.2f wt=%.2f", w, weight); + newtickdiff /= tickdiff/weight; + //END turbulence-test: + */ + + if (newtickdiff != 0.0) { + int newTempo = tempomap.tempo(0); + //newTempo += int(24.0 * newtickdiff * scale); + newTempo += int(24.0 * newtickdiff); + if (debugSync) + printf(" tdiff=%f ntd=%f lt=%d tmpo=%.3f", tdiff0, newtickdiff, lastTempo, (float)((1000000.0 * 60.0)/newTempo)); + //syncTempo = newTempo; + tempomap.setTempo(0,newTempo); + } + if (debugSync) + printf("\n"); + } + else if (debugSync) + printf("\n"); + + //BEGIN post calc + lastTempo = tempo; + recTick2 = recTick1; + recTick1 = recTick; + mclock2 = mclock1; + mclock1 = mclock0; + songtick2 = songtick1; + songtick1 = songtick; + //END post calc + #if 0 + //BEGIN standard setup: + recTick += config.division / 24; // The one we're syncing to + int tempo = tempomap.tempo(0); int songtick = audio->pos().tick(); int tickdiff = songtick - recTick; ! //END standard setup if (debugSync) { // *************** *** 324,332 **** printf("tpt0=%f ntd=%f td1=%d td2=%d lt=%d tempo=%f", time_per_tick0, newtickdiff, tickdiff1, tickdiff2, lastTempo, (float)((1000000.0 * 60.0)/newTempo)); ! syncTempo = newTempo; } if (debugSync) printf("\n"); } lastTempo = tempo; recTick2 = recTick1; --- 423,433 ---- printf("tpt0=%f ntd=%f td1=%d td2=%d lt=%d tempo=%f", time_per_tick0, newtickdiff, tickdiff1, tickdiff2, lastTempo, (float)((1000000.0 * 60.0)/newTempo)); ! //syncTempo = newTempo; ! tempomap.setTempo(0,newTempo); } if (debugSync) printf("\n"); } + //BEGIN post calc lastTempo = tempo; recTick2 = recTick1; *************** *** 336,341 **** songtick2 = songtick1; songtick1 = songtick; #if 0 - //BEGIN: Test w audio pos (just kept for safety, this is the one used ;-) (mathias) recTick += config.division / 24; // The one we're syncing to int tempo = tempomap.tempo(0); --- 437,475 ---- songtick2 = songtick1; songtick1 = songtick; + //END post calc + #endif + #if 0 + if ((mclock2 != 0.0) && (tdiff0 > 0.0)) { + int tempo0 = int(24000000.0 * tdiff0); + int tempo1 = int(24000000.0 * tdiff1); + int tempo = tempomap.tempo(0); + + int diff0 = tempo0 - tempo; + int diff1 = tempo1 - tempo0; + if (diff0 || tickdiff) { + int newTempo = tempo + diff0/8 + diff1/16; + if (debugSync) + printf(" tempo pre-jacksync %d", newTempo); + //Jack-sync issues: + if (tickdiff) { + double tmpscale = (double)config.division/(double)(config.division - tickdiff); + // Just take a small piece of that: + double scale = 1.0 - (1.0 - tmpscale) / 48.0; + newTempo = (int) ((double)newTempo * scale); + if (debugSync) + printf(" post jacksync %d scale: %f\n", newTempo, scale); + } + tempomap.setTempo(0, newTempo); + } + } + lastTempo = tempo; + recTick2 = recTick1; + recTick1 = recTick; + mclock2 = mclock1; + mclock1 = mclock0; + songtick2 = songtick1; + songtick1 = songtick; + #endif #if 0 recTick += config.division / 24; // The one we're syncing to int tempo = tempomap.tempo(0); *************** *** 344,351 **** int tickdiff = songtick - recTick; - // - // Create debug values for printing out which beat we're at: - // if (debugSync) { int m, b, t; audio->pos().mbt(&m, &b, &t); --- 478,485 ---- int tickdiff = songtick - recTick; if (debugSync) { + // + // Create debug values for printing out which beat we're at, etc etc... yaddayadda... + // int m, b, t; audio->pos().mbt(&m, &b, &t); *************** *** 358,391 **** double time_per_tick0 = (tdiff0 * 1000000.0 * 24.0)/(double)tempo; double time_per_tick1 = (tdiff1 * 1000000.0 * 24.0)/(double)lastTempo; - if (debugSync) { - printf(" tpt0=%f ", time_per_tick0); - } // Interpolate: - //int tickdiff0 = songtick - recTick; int tickdiff1 = songtick1 - recTick1; int tickdiff2 = songtick2 - recTick2; ! int newtickdiff = (float)tickdiff/2.0 + ((float)tickdiff1/4.0 + (float)tickdiff2/8.0); ! ! ! // New tempos which take tick-position difference into consideration: ! int tempo0 = int(24000000.0 * tdiff0); //+ .5 + 24.0 * ((double) tickdiff1 * time_per_tick0)); ! int tempo1 = int(24000000.0 * tdiff1); //+ .5 + 24.0 * ((double) tickdiff2 * time_per_tick1));//); ! int tempo = tempomap.tempo(0); //+ 24.0 * ((double) tickdiff * time_per_tick0); ! // Werner's stuff again, to nicen things up: ! int diff0 = tempo0 - tempo; ! int diff1 = tempo1 - tempo0; ! if (diff0) { ! int newTempo = tempo; //+ diff0/8 + diff1/16; ! // TEST: ! newTempo += int(24.0 * (double) (newtickdiff * (time_per_tick0/2.0 + time_per_tick1/2.0))); if (debugSync) ! printf("ntd=%d td1=%d td2=%d lt=%d tempo=%f", newtickdiff, tickdiff1, tickdiff2, lastTempo, (float)((1000000.0 * 60.0)/newTempo)); - - //tempomap.setTempo(0, newTempo); syncTempo = newTempo; } } lastTempo = tempo; --- 492,511 ---- double time_per_tick0 = (tdiff0 * 1000000.0 * 24.0)/(double)tempo; double time_per_tick1 = (tdiff1 * 1000000.0 * 24.0)/(double)lastTempo; // Interpolate: int tickdiff1 = songtick1 - recTick1; int tickdiff2 = songtick2 - recTick2; ! float newtickdiff = (float)tickdiff/3.0 + ((float)tickdiff1/5.0 + (float)tickdiff2/7.0); ! if (newtickdiff != 0.0) { ! int newTempo = tempomap.tempo(0); ! newTempo += int(24.0 * (newtickdiff * (time_per_tick0/2.0 + time_per_tick1/2.0))); if (debugSync) ! printf("tpt0=%f ntd=%f td1=%d td2=%d lt=%d tempo=%f", time_per_tick0, newtickdiff, tickdiff1, tickdiff2, lastTempo, (float)((1000000.0 * 60.0)/newTempo)); syncTempo = newTempo; } + if (debugSync) + printf("\n"); } lastTempo = tempo; *************** *** 396,412 **** songtick2 = songtick1; songtick1 = songtick; - if (debugSync) - printf("\n"); - //END: Test w audio-pos instead #endif - //Robert's stuff: - /* - unsigned curFrame = audio->curFrame(); - unsigned curTick = tempomap.frame2tick(curFrame); - recTick += config.division / 24; - int diff = recTick - curTick; - int tempo = tempomap.tempo(0); - tempo -= diff*24; - */ break; } // END state play --- 516,520 ---- *************** *** 459,462 **** --- 567,573 ---- //startPlay(); + storedtimediffs = 0; + for (int i=0; i<24; i++) + timediff[i] = 0.0; audio->msgPlay(true); } *************** *** 473,477 **** break; case 0xfc: // stop - syncTempo = 0; //TEST if (debugSync) printf(" stop\n"); --- 584,587 ---- |