change superclock ticks per second (no need for 11,13,17)
[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   private:
39         bool _start_requested;
40         bool _running;
41         int   _measures;
42         float _tempo;
43         int   _meter_beats;
44         int   _meter_beat_type;
45         jack_port_t* _input;
46         jack_port_t* _output;
47         superclock_t  superclock_cnt;
48         superclock_t  last_start;
49
50         int _sample_rate;
51         superclock_t whole_note_superclocks;
52         superclock_t beat_superclocks;
53         superclock_t measure_superclocks;
54         int _quantize_divisor;
55         bool clear_pending;
56
57         struct Event {
58                 superclock_t time;
59                 size_t       size;
60                 unsigned char  buf[24];
61
62                 Event () : time (0), size (0) {}
63                 Event (jack_nframes_t t, size_t sz, unsigned char* b) : time (t), size (sz) { memcpy (buf, b, std::min (sizeof (buf), sz)); }
64                 Event (Event const & other) : time (other.time), size (other.size) { memcpy (buf, other.buf, other.size); }
65         };
66
67         struct EventComparator {
68                 bool operator () (Event const * a, Event const * b) const;
69         };
70
71         typedef std::set<Event*,EventComparator> Events;
72         Events _current_events;
73
74         typedef std::vector<Event*> EventPool;
75         EventPool  event_pool;
76 };
77
78
79 #endif /* __bb_h__ */