4fcf477140bfc1af138766da4e2f0ed53ab53a34
[ardour.git] / tools / bb / bb.h
1 #ifndef __bb_h__
2 #define __bb_h__
3
4 #include <algorithm>
5 #include <vector>
6 #include <set>
7 #include <cstring>
8
9 #include <stdint.h>
10
11 #include <jack/jack.h>
12
13 #include "ardour/midi_state_tracker.h"
14
15 typedef uint64_t superclock_t;
16
17 static const superclock_t superclock_ticks_per_second = 508032000; // 2^10 * 3^4 * 5^3 * 7^2
18 inline superclock_t superclock_to_samples (superclock_t s, int sr) { return (s * sr) / superclock_ticks_per_second; }
19 inline superclock_t samples_to_superclock (int samples, int sr) { return (samples * superclock_ticks_per_second) / sr; }
20
21 class BeatBox {
22   public:
23         BeatBox (int sample_rate);
24         ~BeatBox ();
25
26         int register_ports (jack_client_t*);
27         int process (int nframes);
28
29         bool running() const { return _running || _start_requested; }
30         void start ();
31         void stop ();
32         void clear ();
33
34         void set_measure_count (int measures);
35         void set_meter (int beats, int beat_type);
36         void set_tempo (float bpm);
37
38         void set_quantize (int divisor);
39
40         float tempo() const { return _tempo; }
41         int meter_beats() const { return _meter_beats; }
42         int meter_beat_type() const { return _meter_beat_type; }
43
44   private:
45         bool _start_requested;
46         bool _running;
47         int   _measures;
48         float _tempo;
49         float _tempo_request;
50         int   _meter_beats;
51         int   _meter_beat_type;
52         jack_port_t* _input;
53         jack_port_t* _output;
54         superclock_t  superclock_cnt;
55         superclock_t  last_start;
56
57         int _sample_rate;
58         superclock_t whole_note_superclocks;
59         superclock_t beat_superclocks;
60         superclock_t measure_superclocks;
61         int _quantize_divisor;
62         bool clear_pending;
63         ARDOUR::MidiStateTracker inbound_tracker;
64         ARDOUR::MidiStateTracker outbound_tracker;
65
66         struct Event {
67                 superclock_t time;
68                 superclock_t whole_note_superclocks;
69                 size_t       size;
70                 unsigned char  buf[24];
71
72                 Event () : time (0), size (0) {}
73                 Event (jack_nframes_t t, size_t sz, unsigned char* b) : time (t), size (sz) { memcpy (buf, b, std::min (sizeof (buf), sz)); }
74                 Event (Event const & other) : time (other.time), size (other.size) { memcpy (buf, other.buf, other.size); }
75         };
76
77         struct EventComparator {
78                 bool operator () (Event const * a, Event const * b) const;
79         };
80
81         typedef std::set<Event*,EventComparator> Events;
82         Events _current_events;
83
84         typedef std::vector<Event*> EventPool;
85         EventPool  event_pool;
86
87         void compute_tempo_clocks ();
88 };
89
90
91 #endif /* __bb_h__ */