Replace busy-wait with proper signal handling.
[quincer.git] / src / context.h
blob9aa0778aeb58e45f1ca261b0bebdd77a78997107
1 #ifndef __context_h__
2 #define __context_h__
4 #include <jack/jack.h>
6 typedef struct {
7 jack_port_t* jackport;
8 int clock_numerator;
9 int clock_denominator;
10 jack_nframes_t clock_next_tick;
11 int clock_tick_index;
12 } qn_port_t;
14 typedef struct {
15 char* name;
16 int clock_numerator;
17 int clock_denominator;
18 } qn_config_port_t;
20 typedef struct {
21 char* name;
22 char* portname;
23 int channel;
25 qn_port_t* port; // NULL at parse time, set at init time
26 } qn_voice_t;
28 #define NBYTES_MIDI 3
29 // Used to sort all the output events for a port
30 typedef struct {
31 jack_nframes_t at;
32 int szdata;
33 char data[NBYTES_MIDI];
34 } qn_outevent_t;
36 typedef struct {
38 int start_tick;
40 // TODO:
41 /* int offset_from_tick; */
42 /* foo_t offset_type; */
44 char data[NBYTES_MIDI];
46 } qn_event_t;
49 enum eventset {
50 // When handling the prefix events of the next pattern
51 PREFIX,
53 // When handling the main events of the current pattern
54 CURRENT,
56 // When handling the suffix events of the previous pattern
57 SUFFIX,
59 NEVENTSETS
62 typedef struct {
63 qn_voice_t* voice;
64 int nbeats;
66 int nevents;
67 qn_event_t* events;
69 int playhead[NEVENTSETS];
70 int playhead_tmp[NEVENTSETS];
71 } qn_patternvoice_t;
73 enum BpmRelation {
74 Absolute,
75 RelativeToBase
78 typedef struct {
79 int npatternvoices;
80 qn_patternvoice_t* patternvoices;
82 float swing_value;
83 int tbm_ticks_per_beat;
84 float tbm_beats_per_bar;
85 float tbm_beat_type;
86 int beats_per_minute;
87 enum BpmRelation bpm_relation;
88 qn_patternvoice_t* longest_pv;
89 } qn_pattern_t;
91 typedef qn_pattern_t* (*next_pattern_proto)(void* ctx);
92 qn_pattern_t* loop_one(void* ctx);
93 qn_pattern_t* once_through(void* ctx);
94 qn_pattern_t* loop_all(void* ctx);
96 typedef struct {
97 int noutports;
98 qn_config_port_t* outports;
100 int nvoices;
101 qn_voice_t* voices;
103 int npatterns;
104 qn_pattern_t* patterns;
106 // Optional, can override the default
107 next_pattern_proto next_pattern_func;
108 } qn_config_t;
110 typedef struct {
111 jack_client_t* client;
112 qn_port_t** ports;
113 int base_beats_per_minute; // current, user can change real-time
115 qn_pattern_t* prev;
116 qn_pattern_t* curr;
118 jack_nframes_t sample_rate;
119 jack_nframes_t elapsed;
120 jack_transport_state_t transtate;
122 // We are the jack timebase master
123 int tbm_bar;
124 int tbm_beat;
125 int tbm_tick;
126 double tbm_bar_start_tick;
127 jack_nframes_t frame_in_tick;
129 int awaiting_common_start_beat;
130 } qn_runstate_t;
132 typedef struct {
133 qn_runstate_t* run;
134 qn_config_t* config;
136 // A pluggable function determines the next pattern.
137 // This paves the way for a live mode vs song mode, or
138 // various test patterns.
139 next_pattern_proto next_pattern_func;
140 } qn_context_t;
142 qn_config_t* qn_get_fake_config();
143 void qn_init_config(qn_config_t* config);
144 qn_context_t* qn_init_context(qn_config_t* config, jack_client_t* client);
145 void qn_reset_playhead(qn_context_t* ctx);
146 void qn_free_config(qn_config_t* cfg);
147 void qn_free_context(qn_context_t* ctx);
148 int cmp_outevent(const void* a, const void* b);
151 #endif /* __context_h__ */