diff --git a/dispatcher.c b/dispatcher.c index 665a842..8e2e2e8 100644 --- a/dispatcher.c +++ b/dispatcher.c @@ -533,7 +533,11 @@ AppState reducer(AppState state, Action action) { mclip->event_count = 0; mclip->read_index = 0; mclip->max_events = MAX_MIDI_EVENTS; - // events pointer should already be valid from init + // Ensure events pointer is valid + if (mclip->events == NULL) { + mclip->events = (MidiEvent *)calloc(MAX_MIDI_EVENTS, sizeof(MidiEvent)); + mclip->max_events = MAX_MIDI_EVENTS; + } } // Reset Carla host diff --git a/engine.c b/engine.c index 89232af..f228194 100644 --- a/engine.c +++ b/engine.c @@ -102,7 +102,7 @@ static int process_callback(jack_nframes_t nframes, void *arg) { int clip_idx = g * GRID_ROWS * GRID_COLS + s * GRID_COLS + ch; MidiClip *mclip = &state.midi_clips[clip_idx]; - if (mclip->state == CLIP_LOOPING && mclip->event_count > 0) { + if (mclip->state == CLIP_LOOPING && mclip->event_count > 0 && mclip->events != NULL) { if (mclip->read_index < mclip->event_count) { int idx = mclip->read_index; uint8_t msg[3] = { @@ -113,7 +113,7 @@ static int process_callback(jack_nframes_t nframes, void *arg) { jack_midi_event_write(midi_out_buf, mclip->events[idx].timestamp, msg, 3); mclip->read_index++; - + if (mclip->read_index >= mclip->event_count) { mclip->read_index = 0; } @@ -145,12 +145,12 @@ static int process_callback(jack_nframes_t nframes, void *arg) { Clip *clip = &state.clips[clip_idx]; if (clip->state == CLIP_RECORDING) { - if (clip->write_position < MAX_BUFFER_SIZE) { + if (clip->write_position < MAX_BUFFER_SIZE && clip->buffer != NULL) { clip->buffer[clip->write_position++] = audio_in[ch][i]; } } - if (clip->state == CLIP_LOOPING && clip->buffer_size > 0) { + if (clip->state == CLIP_LOOPING && clip->buffer_size > 0 && clip->buffer != NULL) { rack_in[i] += clip->buffer[clip->read_position]; clip->read_position = (clip->read_position + 1) % clip->buffer_size; }