Hi Chuck community,

Just a little message to tell you how I deal with my midi jitter problem.
I asked my friend to send me a midi message every beats. The first msg of four is different.
Then I code a program to do an average on time difference of midi event.
Then it resync all my sequences thanks to another class I coded ( MASTER_SEQ3.update_ref_times).
To fine tune the overall latency I asked my friend to generate a regular clic sound and I do it on my own. Then we recorded, analyses and updates the latency.
I am satified of the result, it works to play music together. Of course I think there is still around 2ms jitter but it is not an issue for us.

I copy/paste the code below if someone is interested.
Other part of my code can be find here: https://github.com/supabassmasta/dev
And don't hesistate to ask me if you want more details.


class tick_adjust {

    // ------------ LATENCY --------------
        196::ms => dur latency;
    48::ms => dur jitter_mean;
        [5 , 8, 16, 24, 32] @=> int refresh_rate[];
    0=> int refresh_index;

        time ref, next;
        0 => int started;
      0::ms => dur delta_mean;
        0 => int cnt;


        fun void midi_ev(int in) {
        // in == 1 : Bar (4 beat) start
        // other : other beats

                if (!started) {
          if (in == 1) {
            now => ref;
            ref - latency - jitter_mean => ref => next;
            MASTER_SEQ3.update_ref_times(ref, data.tick * 4 );
            <<<"UPDATE 1st ref">>>;
            1=> started;
          }

                }
                else {
                    now => time midi_time;
                    next  - (midi_time - latency - jitter_mean) => dur delta;
//                    midi_time- next + latency  => dur delta;

                    // ------------ MEAN ---------------
//                    delta * 0.05 + delta_mean * 0.95 => delta_mean;

                    delta * (1. / (refresh_rate[refresh_index] $ float) )  + delta_mean *(1.- ( 1. / (refresh_rate[refresh_index] $ float ))) => delta_mean;

                }

        if (started) {
          cnt + 1 => cnt;

          if (cnt == refresh_rate[refresh_index]){
            next - delta_mean => ref => next;
            MASTER_SEQ3.update_ref_times(ref, data.tick * 4 );
            <<<"UPDATE delta_mean: ", delta_mean/1::ms ," refresh rate: ", refresh_rate[refresh_index] >>>;
            0::ms => delta_mean;
            0=> cnt;
            if (refresh_index < refresh_rate.size() - 1){
              refresh_index + 1 => refresh_index;
            }
          }

          next + data.tick => next;
        }
        }


}



"Scarlett 2i4 USB MIDI 1" => string device;
MidiIn min;
MidiMsg msg;
// open the device
for(0 =>  int i; i < 8; i++ )
{
        // open the device
        if( min.open( i ) )
        {
                if ( min.name() == device ) {
                <<< "device", i, "->", min.name(), "->", "open as input: SUCCESS" >>>;


                break;
                }
                else {
//                    min.close();
                }

     }
        else {
                <<<"Cannot open", device>>>;    
            break;
        }
}

<<< "MIDI device:", min.num(), " -> ", min.name() >>>;

tick_adjust ta;

while( true )
{
    min => now;

    while( min.recv(msg) )
    {
        <<< msg.data1, msg.data2, msg.data3 >>>;
        if (msg.data1 == 144 && msg.data2 == 36 && msg.data3 == 100 ){
      // first beat of for
      ta.midi_ev(1);
    }
    else if (msg.data1 == 144 && msg.data2 == 24 && msg.data3 == 100 ) {
      // other beat
      ta.midi_ev(0);
    }

    }
}


 




2018-03-20 0:07 GMT+01:00 Mario Buoninfante <mario.buoninfante@gmail.com>:
Hi Julien,

as far as I know the best thing to do should be to set the period buffer to 3, at this point you could easily decrease the buffer size both on Jack and ChucK (you can find more info on Linux audio website or just googling). in my setup at the moment I have the buffer size on Jack = 256 and on ChucK = 512 and it works fine. I've got an average Asus machine, i5 (5th or 6th generation) with 8gb of RAM. but unfortunately MIDI on ALSA driver is not as accurate as CoreAudio is. let's say it's well known to introduce jitter. so I suspect the problem lies there.

cheers,
Mario

2018-03-19 16:03 GMT+00:00 Julien Saint-Martin <julien.saintmartin@googlemail.com>:
Hi Mario,

In qjackctl my config is (my parameters names are in French so maybe my translation is not the exactly same) :
Samples/Period: 4096
Sample rate: 44100
Period/Buffer: 2

Midi Driver: None

I call ChucK (Jack version) with parameter --bufsize8192

What is the specific setup your are talking about ?

Thanks!
Julien



2018-03-19 15:16 GMT+01:00 Mario Buoninfante <mario.buoninfante@gmail.com>:
Hi Julien,

I work on Linux as well, using Jack as audio core. there's a specific setup I've got that uses ChucK as slave (an external hardware sends MIDI clock to it and Pure Data). I have to say I've never experienced  anything like this, but honestly this specific ChucK script I'm using is in sync with everything, but doesn't need to be super accurate. So I've never measured if performance in this situation. could you provide some info more about your settings? how are you setting latency? buffer size on Jack, ChucK?

cheers,
Mario

2018-03-19 11:11 GMT+00:00 Julien Saint-Martin <julien.saintmartin@googlemail.com>:
Hi there,

I am trying to synchronize my ChucK Loops and Synth with a friend.
We use midi messages and I am Slave.
I use Linux with Jack server.

To ensure ChucK processing without glitches I play with latency (usually 186 ms).
But I noticed that perdiodic midi messages are randomly delayed (kind of jitter of about 80ms) when received in ChucK.
I also noticed that the jitter decrease when I reduce Latency.

Latency is not a problem for me, and it is needed for Chuck to compute without glitches (XRun in Jack server).

Do you know where the Jitter comes from (Linux, Jack, ChucK) ?
Is  there a solution to reduce it keeping some latency ?

Happy Chucking,

Julien

_______________________________________________
chuck-users mailing list
chuck-users@lists.cs.princeton.edu
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users



_______________________________________________
chuck-users mailing list
chuck-users@lists.cs.princeton.edu
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users



_______________________________________________
chuck-users mailing list
chuck-users@lists.cs.princeton.edu
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users



_______________________________________________
chuck-users mailing list
chuck-users@lists.cs.princeton.edu
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users