Only show user-presets in favorite sidebar
[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 namespace ARDOUR {
22 class Session;
23 }
24
25 class BeatBox {
26   public:
27         BeatBox (int sample_rate);
28         ~BeatBox ();
29
30         int register_ports (jack_client_t*);
31         int process (int nframes);
32
33         bool running() const { return _running || _start_requested; }
34         void start ();
35         void stop ();
36         void clear ();
37
38         void set_measure_count (int measures);
39         void set_meter (int beats, int beat_type);
40         void set_tempo (float bpm);
41
42         void set_quantize (int divisor);
43
44         float tempo() const { return _tempo; }
45         int meter_beats() const { return _meter_beats; }
46         int meter_beat_type() const { return _meter_beat_type; }
47
48   private:
49         bool _start_requested;
50         bool _running;
51         int   _measures;
52         float _tempo;
53         float _tempo_request;
54         int   _meter_beats;
55         int   _meter_beat_type;
56         jack_port_t* _input;
57         jack_port_t* _output;
58         superclock_t  superclock_cnt;
59         superclock_t  last_start;
60
61         int _sample_rate;
62         superclock_t whole_note_superclocks;
63         superclock_t beat_superclocks;
64         superclock_t measure_superclocks;
65         int _quantize_divisor;
66         bool clear_pending;
67         ARDOUR::MidiStateTracker inbound_tracker;
68         ARDOUR::MidiStateTracker outbound_tracker;
69
70         struct Event {
71                 superclock_t time;
72                 superclock_t whole_note_superclocks;
73                 size_t       size;
74                 unsigned char  buf[24];
75
76                 Event () : time (0), size (0) {}
77                 Event (jack_nframes_t t, size_t sz, unsigned char* b) : time (t), size (sz) { memcpy (buf, b, std::min (sizeof (buf), sz)); }
78                 Event (Event const & other) : time (other.time), size (other.size) { memcpy (buf, other.buf, other.size); }
79         };
80
81         struct EventComparator {
82                 bool operator () (Event const * a, Event const * b) const;
83         };
84
85         typedef std::vector<Event*> IncompleteNotes;
86         IncompleteNotes _incomplete_notes;
87
88         typedef std::set<Event*,EventComparator> Events;
89         Events _current_events;
90
91         typedef std::vector<Event*> EventPool;
92         EventPool  event_pool;
93
94         void compute_tempo_clocks ();
95 };
96
97
98 #endif /* __bb_h__ */