Anklang 0.3.0-460-gc4ef46ba
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
transport.hh
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#ifndef __ASE_TRANSPORT_HH__
3#define __ASE_TRANSPORT_HH__
4
5#include <ase/defs.hh>
6
7namespace Ase {
8
12 NONE = 0,
13 FRONT_LEFT = 0x1,
14 FRONT_RIGHT = 0x2,
15 FRONT_CENTER = 0x4,
16 LOW_FREQUENCY = 0x8,
17 BACK_LEFT = 0x10,
18 BACK_RIGHT = 0x20,
19 // WAV reserved = 0xyyy00000,
20 AUX = uint64_t (1) << 63,
22 STEREO = FRONT_LEFT | FRONT_RIGHT,
23 STEREO_21 = STEREO | LOW_FREQUENCY,
24 STEREO_30 = STEREO | FRONT_CENTER,
25 STEREO_31 = STEREO_30 | LOW_FREQUENCY,
26 SURROUND_50 = STEREO_30 | BACK_LEFT | BACK_RIGHT,
27 SURROUND_51 = SURROUND_50 | LOW_FREQUENCY,
28#if 0 // TODO: dynamic multichannel support
29 FRONT_LEFT_OF_CENTER = 0x40,
30 FRONT_RIGHT_OF_CENTER = 0x80,
31 BACK_CENTER = 0x100,
32 SIDE_LEFT = 0x200,
33 SIDE_RIGHT = 0x400,
34 TOP_CENTER = 0x800,
35 TOP_FRONT_LEFT = 0x1000,
36 TOP_FRONT_CENTER = 0x2000,
37 TOP_FRONT_RIGHT = 0x4000,
38 TOP_BACK_LEFT = 0x8000,
39 TOP_BACK_CENTER = 0x10000,
40 TOP_BACK_RIGHT = 0x20000,
41 SIDE_SURROUND_50 = STEREO_30 | SIDE_LEFT | SIDE_RIGHT,
42 SIDE_SURROUND_51 = SIDE_SURROUND_50 | LOW_FREQUENCY,
43#endif
44};
45constexpr SpeakerArrangement speaker_arrangement_channels_mask { ~size_t (SpeakerArrangement::AUX) };
46uint8 speaker_arrangement_count_channels (SpeakerArrangement spa);
47SpeakerArrangement speaker_arrangement_channels (SpeakerArrangement spa);
48bool speaker_arrangement_is_aux (SpeakerArrangement spa);
49const char* speaker_arrangement_bit_name (SpeakerArrangement spa);
50std::string speaker_arrangement_desc (SpeakerArrangement spa);
51
53constexpr const int64 TRANSPORT_PPQN = 4838400; // Ticks per quarter note
54constexpr const int64 SEMIQUAVER_TICKS = TRANSPORT_PPQN / 4; // 1209600 = PPQN * 4 / 16;
55constexpr const int64 MIN_BPM = 10;
56constexpr const int64 MAX_BPM = 1776;
57constexpr const int64 MIN_SAMPLERATE = 8000;
58constexpr const int64 MAX_SAMPLERATE = 192000;
59
62protected:
63 static constexpr const int64 offset_ = 0; // currently unused
66 int32 beat_ticks_ = 0;
67 int32 bar_ticks_ = 0;
69 double bpm_ = 0;
70 int64 ticks_per_minute_ = 0;
71 double ticks_per_second_ = 0;
72 double inv_ticks_per_second_ = 0;
73 double ticks_per_sample_ = 0;
74 double sample_per_ticks_ = 0;
76public:
77 struct Beat {
78 int32 bar = 0;
79 int8 beat = 0;
80 double semiquaver = 0;
81 };
82 struct Time {
84 double seconds = 0;
85 };
86 double samplerate () const { return samplerate_; }
87 double inv_samplerate () const { return inv_samplerate_; }
88 double ticks_per_sample () const { return ticks_per_sample_; }
89 int64 sample_to_tick (int64 sample) const;
90 int64 sample_from_tick (int64 tick) const;
91 double bpm () const { return bpm_; }
92 int32 bar_ticks () const { return bar_ticks_; }
93 int32 beat_ticks () const { return beat_ticks_; }
94 int64 start_offset () const { return offset_; }
95 void set_samplerate (uint samplerate);
96 void set_bpm (double bpm);
97 Time time_from_tick (int64 tick) const;
98 int64 time_to_tick (const Time &time) const;
99 bool set_signature (uint8 beats_per_bar, uint8 beat_unit);
100 const uint8& beats_per_bar () const { return beats_per_bar_; }
101 const uint8& beat_unit () const { return beat_unit_; }
102 int32 bar_from_tick (int64 tick) const;
103 int64 bar_to_tick (int32 bar) const;
104 Beat beat_from_tick (int64 tick) const;
105 int64 beat_to_tick (const Beat &beat) const;
106 /*dflt*/ TickSignature ();
107 /*ctor*/ TickSignature (double bpm, uint8 beats_per_bar, uint8 beat_unit);
108 /*copy*/ TickSignature (const TickSignature &other);
109 TickSignature& operator= (const TickSignature &src);
110};
111
114 static constexpr int64 ppqn = TRANSPORT_PPQN;
116 const uint nyquist;
117 const double isamplerate;
118 const double inyquist;
120 // uint32 gap
121 TickSignature tick_sig;
123 int64 current_tick = 0;
124 __attribute__ ((aligned (64))) // align memory for project telemetry fields
125 double current_tick_d = 0;
129 float current_bpm = 0;
131 double current_seconds = 0;
132 int64 current_bar_tick = 0;
133 int64 next_bar_tick = 0;
134 bool running () const { return current_bpm != 0; }
135 void running (bool r);
136 void tempo (double newbpm, uint8 newnumerator, uint8 newdenominator);
137 void tempo (const TickSignature &ticksignature);
138 void set_tick (int64 newtick);
139 void set_beat (TickSignature::Beat b);
140 void advance (uint nsamples);
141 void update_current ();
142 explicit AudioTransport (SpeakerArrangement speakerarrangement, uint samplerate);
143 int64 sample_to_tick (int64 sample) const { return tick_sig.sample_to_tick (sample); }
144 int64 sample_from_tick (int64 tick) const { return tick_sig.sample_from_tick (tick); }
145};
146
147// == Implementations ==
148inline int64
149TickSignature::sample_to_tick (int64 sample) const
150{
151 return ticks_per_sample_ * sample;
152}
153
154inline int64
155TickSignature::sample_from_tick (int64 tick) const
156{
157 return sample_per_ticks_ * tick;
158}
159
160} // Ase
161
162#endif // __ASE_TRANSPORT_HH__
The Anklang C++ API namespace.
Definition api.hh:9
int32_t int32
A 32-bit signed integer.
Definition cxxaux.hh:28
SpeakerArrangement
Definition transport.hh:11
@ FRONT_LEFT
Stereo Left (FL)
@ FRONT_RIGHT
Stereo Right (FR)
@ AUX
Flag for side chain uses.
@ LOW_FREQUENCY
Low Frequency Effects (LFE)
@ MONO
Single Channel (M)
uint8_t uint8
An 8-bit unsigned integer.
Definition cxxaux.hh:22
int8_t int8
An 8-bit signed integer.
Definition cxxaux.hh:26
int64_t int64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:29
constexpr const int64 TRANSPORT_PPQN
Maximum number of sample frames to calculate in Processor::render().
Definition transport.hh:53
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:18
T sample(T... args)
typedef uint64_t
Transport information for AudioSignal processing.
Definition transport.hh:113
int32 current_bar
Bar of current_tick position.
Definition transport.hh:126
double current_semiquaver
The sixteenth with fraction within beat.
Definition transport.hh:128
const uint nyquist
Half the samplerate.
Definition transport.hh:116
const uint samplerate
Sample rate (mixing frequency) in Hz used for rendering.
Definition transport.hh:115
const double isamplerate
Precalculated 1.0 / samplerate.
Definition transport.hh:117
double current_seconds
Seconds of current_tick position.
Definition transport.hh:131
int64 current_frame
Number of sample frames processed since playback start.
Definition transport.hh:122
int8 current_beat
Beat within bar of current_tick position.
Definition transport.hh:127
float current_bpm
Running tempo in beats per minute.
Definition transport.hh:129
int32 current_minutes
Minute of current_tick position.
Definition transport.hh:130
const double inyquist
Precalculated 1.0 / nyquist.
Definition transport.hh:118
const SpeakerArrangement speaker_arrangement
Audio output configuration.
Definition transport.hh:119
Musical time signature and tick conversions.
Definition transport.hh:61
double seconds
Seconds with fraction after the minute.
Definition transport.hh:84
Beat beat_from_tick(int64 tick) const
Calculate beat from tick, requires set_signature().
Definition transport.cc:112
int64 bar_to_tick(int32 bar) const
Calculate tick from bar, requires set_signature().
Definition transport.cc:143
double bpm_
Current tempo in beats per minute.
Definition transport.hh:69
void set_samplerate(uint samplerate)
Assign sample rate.
Definition transport.cc:47
uint8 beats_per_bar_
Upper numeral (numerator), how many beats constitute a bar.
Definition transport.hh:64
uint8 beat_unit_
Lower numeral (denominator in [1 2 4 8 16]), note value that represents one beat.
Definition transport.hh:65
double inv_samplerate_
Precalculated 1.0 / samplerate.
Definition transport.hh:75
int32 minutes
Tick position in minutes.
Definition transport.hh:83
int32 samplerate_
Sample rate (mixing frequency) in Hz.
Definition transport.hh:68
int64 time_to_tick(const Time &time) const
Calculate tick from time, requires set_bpm().
Definition transport.cc:85
int32 bar
Bar of tick position.
Definition transport.hh:78
int64 beat_to_tick(const Beat &beat) const
Calculate tick from beat, requires set_signature().
Definition transport.cc:125
double semiquaver
The sixteenth with fraction within beat.
Definition transport.hh:80
Time time_from_tick(int64 tick) const
Calculate time from tick, requires set_bpm().
Definition transport.cc:74
bool set_signature(uint8 beats_per_bar, uint8 beat_unit)
Assign time signature and offset for the signature to take effect.
Definition transport.cc:95
int32 bar_from_tick(int64 tick) const
Calculate bar from tick, requires set_signature().
Definition transport.cc:136
void set_bpm(double bpm)
Assign tempo in beats per minute.
Definition transport.cc:59
int8 beat
Beat within bar of tick position.
Definition transport.hh:79