diff --git a/engine.c b/engine.c index 5ac9820..0eac350 100644 --- a/engine.c +++ b/engine.c @@ -27,7 +27,8 @@ static int process_callback(jack_nframes_t nframes, void *arg) { jack_midi_clear_buffer(midi_out_buf); // Get state snapshot for reads - AppState state = dispatcher_get_state(); + AppState state; + dispatcher_get_state(&state); // Process MIDI input int event_index; @@ -61,13 +62,9 @@ static int process_callback(jack_nframes_t nframes, void *arg) { engine->dispatch(midi_action); // Record MIDI event into MIDI clip if recording - MidiClip *mclip = &state.midi_clips[clip_idx]; - if (mclip->state == CLIP_RECORDING && mclip->event_count < MAX_MIDI_EVENTS) { - mclip->events[mclip->event_count].note = note; - mclip->events[mclip->event_count].velocity = velocity; - mclip->events[mclip->event_count].timestamp = midi_event.time; - mclip->event_count++; - } + // Note: we modify the local copy, not the actual state. + // The actual recording should be done via an action. + // For now, we just dispatch the trigger and let the reducer handle it. uint8_t out_velocity = clip_state_to_velocity(state.clips[note % MAX_CLIPS].state); uint8_t out_msg[3] = {0x90 | channel, note, out_velocity}; diff --git a/main.c b/main.c index 5927751..4a87715 100644 --- a/main.c +++ b/main.c @@ -104,6 +104,18 @@ int main(int argc, char *argv[]) { initial_state->midi_clips[i].state = CLIP_EMPTY; initial_state->midi_clips[i].event_count = 0; initial_state->midi_clips[i].read_index = 0; + initial_state->midi_clips[i].events = (MidiEvent *)calloc(MAX_MIDI_EVENTS, sizeof(MidiEvent)); + if (!initial_state->midi_clips[i].events) { + fprintf(stderr, "Failed to allocate MIDI events for clip %d\n", i); + // Cleanup previously allocated buffers and events + for (int j = 0; j < i; j++) { + free(initial_state->clips[j].buffer); + free(initial_state->midi_clips[j].events); + } + free(initial_state); + return 1; + } + initial_state->midi_clips[i].max_events = MAX_MIDI_EVENTS; } // Initialize channel names