feat: refactor transport into separate module with master/slave clock support

Co-authored-by: aider (deepseek/deepseek-coder) <aider@aider.chat>
This commit is contained in:
Loic Coenen
2026-05-01 21:08:38 +00:00
parent c2ad0e874c
commit a47598df8c
6 changed files with 490 additions and 229 deletions

View File

@@ -6,13 +6,12 @@
#include <stdint.h>
#include <stdbool.h>
#include <stdatomic.h>
#include "transport.h"
#define MAX_SCENES 8
#define MAX_CHANNELS 8
#define MAX_CLIPS (MAX_SCENES * MAX_CHANNELS) // 64
#define MAX_BUFFER_SIZE 441000 // 10 seconds at 44.1kHz
#define MIDI_CLOCKS_PER_BEAT 24
#define BEATS_PER_BAR 4
// Convert scene/channel to flat clip index
#define CLIP_INDEX(scene, channel) ((scene) * MAX_CHANNELS + (channel))
@@ -30,13 +29,6 @@ typedef enum {
QUANTIZE_BAR
} QuantizeMode;
typedef struct {
bool rolling;
uint32_t clock_count;
uint32_t beat_position; // 0-3
uint32_t bar_position;
uint32_t sample_position; // derived from clock at current sample rate
} TransportState;
typedef struct {
ClipState state;
@@ -58,7 +50,13 @@ typedef enum {
CMD_SET_QUANTIZE_THRESHOLD,
CMD_RESET_TRANSPORT,
CMD_UNDO,
CMD_REDO
CMD_REDO,
CMD_TRANSPORT_PLAY,
CMD_TRANSPORT_PAUSE,
CMD_TRANSPORT_STOP,
CMD_TRANSPORT_TOGGLE_PLAY,
CMD_SET_CLOCK_SOURCE,
CMD_SET_BPM
} CommandType;
// Undo/Redo action types
@@ -134,8 +132,7 @@ typedef struct {
jack_nframes_t sample_rate;
// Transport and clock
TransportState transport;
bool clock_sync_enabled;
Transport *transport;
// Quantization
QuantizeMode quantize_mode;
@@ -146,11 +143,6 @@ typedef struct {
CommandQueue command_queue;
// Atomic flags for simple state that frontend reads
atomic_int transport_rolling; // bool
atomic_uint transport_clock_count;
atomic_uint transport_beat_position;
atomic_uint transport_bar_position;
atomic_uint transport_sample_position;
atomic_int quantize_mode_atomic; // QuantizeMode
atomic_uint quantize_threshold_atomic;
@@ -174,7 +166,12 @@ void engine_reset_clip(Engine *engine, int clip_index);
// Transport
void engine_set_quantize_mode(Engine *engine, QuantizeMode mode);
void engine_set_quantize_threshold(Engine *engine, jack_nframes_t samples);
void engine_reset_transport(Engine *engine);
void engine_transport_play(Engine *engine);
void engine_transport_pause(Engine *engine);
void engine_transport_stop(Engine *engine);
void engine_transport_toggle_play(Engine *engine);
void engine_set_clock_source(Engine *engine, ClockSource source);
void engine_set_bpm(Engine *engine, double bpm);
// Queue management (exposed for testing)
void queue_trigger(Engine *engine, int clip_index, bool is_scene, jack_nframes_t time);