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