main/alles.c
uint8_t deserialize_event(char * message, uint16_t length) {
    // Don't process new messages if we're in MIDI mode
    if(global.status & MIDI_MODE) return 0;

    uint8_t mode = 0;
    int64_t sync = -1;
    int8_t sync_index = -1;
    uint8_t ipv4 = 0; 
    int16_t client = -1;
    uint16_t start = 0;
    uint16_t c = 0;
    struct event e = default_event();
    int64_t sysclock = esp_timer_get_time() / 1000;
    uint8_t sync_response = 0;
    // Put a null at the end for atoi
    message[length] = 0;

    // Cut the OSC cruft Max etc add, they put a 0 and then more things after the 0
    int new_length = length; 
    for(int d=0;d<length;d++) {
        if(message[d] == 0) { new_length = d; d = length + 1;  } 
    }
    length = new_length;

    //printf("received message ###%s### len %d\n", message, length);
    while(c < length+1) {
        uint8_t b = message[c];
        if(b == '_' && c==0) sync_response = 1;
        if( ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')) || b == 0) {  // new mode or end
            if(mode=='t') {
                e.time=atol(message + start);
                // if we haven't yet synced our times, do it now
                if(!computed_delta_set) {
                    computed_delta = e.time - sysclock;
                    computed_delta_set = 1;
                }
            }
            if(mode=='A') parse_adsr(&e, message+start);
            if(mode=='b') e.feedback=atof(message+start);
            if(mode=='c') client = atoi(message + start); 
            if(mode=='d') e.duty=atof(message + start);
            // reminder: don't use "E" or "e", lol 
            if(mode=='f') e.freq=atof(message + start); 
            if(mode=='F') e.filter_freq=atof(message + start);
            if(mode=='g') e.lfo_target = atoi(message + start); 
            if(mode=='i') sync_index = atoi(message + start);
            if(mode=='l') e.velocity=atof(message + start);
            if(mode=='L') e.lfo_source=atoi(message + start);
            if(mode=='n') e.midi_note=atoi(message + start);
            if(mode=='p') e.patch=atoi(message + start);
            if(mode=='P') e.phase=atof(message + start);
            if(mode=='r') ipv4=atoi(message + start);
            if(mode=='R') e.resonance=atof(message + start);
            if(mode=='s') sync = atol(message + start); 
            if(mode=='S') { 
                uint8_t voice = atoi(message + start); 
                if(voice > VOICES-1) { reset_voices(); } else { reset_voice(voice); }
            }
            if(mode=='T') e.adsr_target = atoi(message + start); 
            if(mode=='v') e.voice=(atoi(message + start) % VOICES); // allow voice wraparound
            if(mode=='V') { e.volume = atof(message + start); debug_voices(); }
            if(mode=='w') e.wave=atoi(message + start);
            mode=b;
            start=c+1;
        }
        c++;
    }
    if(sync_response) {
        // If this is a sync response, let's update our local map of who is booted
        update_map(client, ipv4, sync);
        length = 0; // don't need to do the rest
    }
    // Only do this if we got some data
    if(length >0) {
        // Now adjust time in some useful way:
        // if we have a delta & got a time in this message, use it schedule it properly
        if(computed_delta_set && e.time > 0) {
            // OK, so check for potentially negative numbers here (or really big numbers-sysclock) 
            int64_t potential_time = (e.time - computed_delta) + LATENCY_MS;
            if(potential_time < 0 || (potential_time > sysclock + LATENCY_MS + MAX_DRIFT_MS)) {
                printf("recomputing time base: message came in with %lld, mine is %lld, computed delta was %lld\n", e.time, sysclock, computed_delta);
                computed_delta = e.time - sysclock;
                printf("computed delta now %lld\n", computed_delta);
            }
            e.time = (e.time - computed_delta) + LATENCY_MS;

        } else { // else play it asap 
            e.time = sysclock + LATENCY_MS;
        }
        e.status = SCHEDULED;

        // Don't add sync messages to the event queue
        if(sync >= 0 && sync_index >= 0) {
            handle_sync(sync, sync_index);
        } else {
            // Assume it's for me
            uint8_t for_me = 1;
            // But wait, they specified, so don't assume
            if(client >= 0) {
                for_me = 0;
                if(client <= 255) {
                    // If they gave an individual client ID check that it exists
                    if(alive>0) { // alive may get to 0 in a bad situation
                        if(client >= alive) {
                            client = client % alive;
                        } 
                    }
                }
                // It's actually precisely for me
                if(client == client_id) for_me = 1;
                if(client > 255) {
                    // It's a group message, see if i'm in the group
                    if(client_id % (client-255) == 0) for_me = 1;
                }
            }
            if(for_me) add_event(e);
        }
    }
    return 0;
}
// parse a received event string and add event to queue
parse a received event string and add event to queue
parse a received event string and add event to queue
uint8_t deserialize_event(char * message, uint16_t length) {
    if(global.status & MIDI_MODE) return 0;
    uint8_t mode = 0;
    int64_t sync = -1;
    int8_t sync_index = -1;
    uint8_t ipv4 = 0; 
    int16_t client = -1;
    uint16_t start = 0;
    uint16_t c = 0;
    struct event e = default_event();
    int64_t sysclock = esp_timer_get_time() / 1000;
    uint8_t sync_response = 0;
    message[length] = 0;
    int new_length = length; 
    for(int d=0;d<length;d++) {
        if(message[d] == 0) { new_length = d; d = length + 1;  } 
    }
    length = new_length;
    while(c < length+1) {
        uint8_t b = message[c];
        if(b == '_' && c==0) sync_response = 1;
        if( ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')) || b == 0) {  
            if(mode=='t') {
                e.time=atol(message + start);
                if(!computed_delta_set) {
                    computed_delta = e.time - sysclock;
                    computed_delta_set = 1;
                }
            }
            if(mode=='A') parse_adsr(&e, message+start);
            if(mode=='b') e.feedback=atof(message+start);
            if(mode=='c') client = atoi(message + start); 
            if(mode=='d') e.duty=atof(message + start);
            if(mode=='f') e.freq=atof(message + start); 
            if(mode=='F') e.filter_freq=atof(message + start);
            if(mode=='g') e.lfo_target = atoi(message + start); 
            if(mode=='i') sync_index = atoi(message + start);
            if(mode=='l') e.velocity=atof(message + start);
            if(mode=='L') e.lfo_source=atoi(message + start);
            if(mode=='n') e.midi_note=atoi(message + start);
            if(mode=='p') e.patch=atoi(message + start);
            if(mode=='P') e.phase=atof(message + start);
            if(mode=='r') ipv4=atoi(message + start);
            if(mode=='R') e.resonance=atof(message + start);
            if(mode=='s') sync = atol(message + start); 
            if(mode=='S') { 
                uint8_t voice = atoi(message + start); 
                if(voice > VOICES-1) { reset_voices(); } else { reset_voice(voice); }
            }
            if(mode=='T') e.adsr_target = atoi(message + start); 
            if(mode=='v') e.voice=(atoi(message + start) % VOICES); 
            if(mode=='V') { e.volume = atof(message + start); debug_voices(); }
            if(mode=='w') e.wave=atoi(message + start);
            mode=b;
            start=c+1;
        }
        c++;
    }
    if(sync_response) {
        update_map(client, ipv4, sync);
        length = 0; 
    }
    if(length >0) {
        if(computed_delta_set && e.time > 0) {
            int64_t potential_time = (e.time - computed_delta) + LATENCY_MS;
            if(potential_time < 0 || (potential_time > sysclock + LATENCY_MS + MAX_DRIFT_MS)) {
                printf("recomputing time base: message came in with %lld, mine is %lld, computed delta was %lld\n", e.time, sysclock, computed_delta);
                computed_delta = e.time - sysclock;
                printf("computed delta now %lld\n", computed_delta);
            }
            e.time = (e.time - computed_delta) + LATENCY_MS;
        } else { 
            e.time = sysclock + LATENCY_MS;
        }
        e.status = SCHEDULED;
        if(sync >= 0 && sync_index >= 0) {
            handle_sync(sync, sync_index);
        } else {
            uint8_t for_me = 1;
            if(client >= 0) {
                for_me = 0;
                if(client <= 255) {
                    if(alive>0) { 
                        if(client >= alive) {
                            client = client % alive;
                        } 
                    }
                }
                if(client == client_id) for_me = 1;
                if(client > 255) {
                    if(client_id % (client-255) == 0) for_me = 1;
                }
            }
            if(for_me) add_event(e);
        }
    }
    return 0;
}
uint8_t deserialize_event ( char * message , uint16_t length ) { if ( global . status & MIDI_MODE ) return 0 ; uint8_t mode = 0 ; int64_t sync = -1 ; int8_t sync_index = -1 ; uint8_t ipv4 = 0 ; int16_t client = -1 ; uint16_t start = 0 ; uint16_t c = 0 ; struct event e = default_event ( ) ; int64_t sysclock = esp_timer_get_time ( ) / 1000 ; uint8_t sync_response = 0 ; message [ length ] = 0 ; int new_length = length ; for ( int d = 0 ; d < length ; d ++ ) { if ( message [ d ] == 0 ) { new_length = d ; d = length + 1 ; } } length = new_length ; while ( c < length + 1 ) { uint8_t b = message [ c ] ; if ( b == ' ' && c == 0 ) sync_response = 1 ; if ( ( ( b >= ' ' && b <= ' ' ) || ( b >= ' ' && b <= ' ' ) ) || b == 0 ) { if ( mode == ' ' ) { e . time = atol ( message + start ) ; if ( ! computed_delta_set ) { computed_delta = e . time - sysclock ; computed_delta_set = 1 ; } } if ( mode == ' ' ) parse_adsr ( & e , message + start ) ; if ( mode == ' ' ) e . feedback = atof ( message + start ) ; if ( mode == ' ' ) client = atoi ( message + start ) ; if ( mode == ' ' ) e . duty = atof ( message + start ) ; if ( mode == ' ' ) e . freq = atof ( message + start ) ; if ( mode == ' ' ) e . filter_freq = atof ( message + start ) ; if ( mode == ' ' ) e . lfo_target = atoi ( message + start ) ; if ( mode == ' ' ) sync_index = atoi ( message + start ) ; if ( mode == ' ' ) e . velocity = atof ( message + start ) ; if ( mode == ' ' ) e . lfo_source = atoi ( message + start ) ; if ( mode == ' ' ) e . midi_note = atoi ( message + start ) ; if ( mode == ' ' ) e . patch = atoi ( message + start ) ; if ( mode == ' ' ) e . phase = atof ( message + start ) ; if ( mode == ' ' ) ipv4 = atoi ( message + start ) ; if ( mode == ' ' ) e . resonance = atof ( message + start ) ; if ( mode == ' ' ) sync = atol ( message + start ) ; if ( mode == ' ' ) { uint8_t voice = atoi ( message + start ) ; if ( voice > VOICES - 1 ) { reset_voices ( ) ; } else { reset_voice ( voice ) ; } } if ( mode == ' ' ) e . adsr_target = atoi ( message + start ) ; if ( mode == ' ' ) e . voice = ( atoi ( message + start ) % VOICES ) ; if ( mode == ' ' ) { e . volume = atof ( message + start ) ; debug_voices ( ) ; } if ( mode == ' ' ) e . wave = atoi ( message + start ) ; mode = b ; start = c + 1 ; } c ++ ; } if ( sync_response ) { update_map ( client , ipv4 , sync ) ; length = 0 ; } if ( length > 0 ) { if ( computed_delta_set && e . time > 0 ) { int64_t potential_time = ( e . time - computed_delta ) + LATENCY_MS ; if ( potential_time < 0 || ( potential_time > sysclock + LATENCY_MS + MAX_DRIFT_MS ) ) { printf ( " \n " , e . time , sysclock , computed_delta ) ; computed_delta = e . time - sysclock ; printf ( " \n " , computed_delta ) ; } e . time = ( e . time - computed_delta ) + LATENCY_MS ; } else { e . time = sysclock + LATENCY_MS ; } e . status = SCHEDULED ; if ( sync >= 0 && sync_index >= 0 ) { handle_sync ( sync , sync_index ) ; } else { uint8_t for_me = 1 ; if ( client >= 0 ) { for_me = 0 ; if ( client <= 255 ) { if ( alive > 0 ) { if ( client >= alive ) { client = client % alive ; } } } if ( client == client_id ) for_me = 1 ; if ( client > 255 ) { if ( client_id % ( client - 255 ) == 0 ) for_me = 1 ; } } if ( for_me ) add_event ( e ) ; } } return 0 ; }
parse a received event string and add event to queue
parse a received event string and add event to queue
// Don't process new messages if we're in MIDI mode // Put a null at the end for atoi // Cut the OSC cruft Max etc add, they put a 0 and then more things after the 0 //printf("received message ###%s### len %d\n", message, length); // new mode or end // if we haven't yet synced our times, do it now // reminder: don't use "E" or "e", lol  // allow voice wraparound // If this is a sync response, let's update our local map of who is booted // don't need to do the rest // Only do this if we got some data // Now adjust time in some useful way: // if we have a delta & got a time in this message, use it schedule it properly // OK, so check for potentially negative numbers here (or really big numbers-sysclock)  // else play it asap  // Don't add sync messages to the event queue // Assume it's for me // But wait, they specified, so don't assume // If they gave an individual client ID check that it exists // alive may get to 0 in a bad situation // It's actually precisely for me // It's a group message, see if i'm in the group