Skip to content

Commit 1d5326c

Browse files
committed
WIP
1 parent 7c15a33 commit 1d5326c

2 files changed

Lines changed: 56 additions & 19 deletions

File tree

include/halp/audio.hpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@ struct tick
201201
int frames{};
202202
};
203203

204+
/**
205+
* Contains the info about the current audio buffer and the bar and quarter notes it's
206+
* associated with, so that the musical audio rendering utilities can do their job of
207+
* playing back the musical notes, etc.
208+
* They need to know when we are in the score.
209+
*
210+
* The quarter notes have their index starting at 0.
211+
* The position of the bars are in quarters. (in 4/4, the first bar has position 0.0 and the
212+
* second one has a position of 4.0)
213+
*
214+
* This struct is usually created and destroyed thousands of times per second.
215+
*/
204216
struct tick_musical
205217
{
206218
/**
@@ -218,18 +230,41 @@ struct tick_musical
218230
int denom;
219231
} signature;
220232

233+
/**
234+
* The total number of samples since the beginning of the playback of the score.
235+
*/
221236
int64_t position_in_frames{};
222237

238+
/**
239+
* Playback time since the beginning of the score.
240+
*/
223241
double position_in_nanoseconds{};
224242

225-
// Quarter note of the first sample in the buffer
243+
/**
244+
* Quarter note of the first sample in this buffer
245+
* This is a double.
246+
*
247+
* The first quarter note in a score has index 0.
248+
*
249+
* For example:
250+
* - 3.95 would be slightly before the 2nd bar.
251+
* - 4.05 would be slightly after the beginning of the 2nd bar.
252+
*/
226253
quarter_note start_position_in_quarters{};
227254

228-
// Quarter note of the first sample in the next buffer
229-
// (or one past the last sample of this buffer, e.g. a [closed; open) interval like C++ begin / end)
255+
/**
256+
* Quarter note of the first sample in the next buffer
257+
* (or one past the last sample of this buffer, e.g. a [closed; open) interval like C++ begin / end)
258+
*/
230259
quarter_note end_position_in_quarters{};
231260

232-
// Position of the last signature change in quarter notes (at the start of the tick)
261+
/**
262+
* Position of the last signature change in quarter notes (at the start of the tick)
263+
*
264+
* For example: 0.0 if the signature never changes in the score.
265+
* If we change the time signature at some, we'll give it the index (in quarter) of the
266+
* bar when it last changed.
267+
*/
233268
quarter_note last_signature_change{};
234269

235270
// Position of the last bar relative to start in quarter notes

tests/objects/patternal.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ TEST_CASE("The input pattern is stored correctly", "[advanced][patternal]")
3939

4040
// Create the input pattern:
4141
patternalProcessor.inputs.patterns.value = {
42-
(Pattern) {42, {127, 127, 127, 127}}, // hi-hat
43-
(Pattern) {38, {0, 127, 0, 127}}, // snare
44-
(Pattern) {35, {127, 0, 127, 0}}, // bass drum
42+
{42, {127, 127, 127, 127}}, // hi-hat
43+
{38, {0, 127, 0, 127}}, // snare
44+
{35, {127, 0, 127, 0}}, // bass drum
4545
};
4646

4747
// Check that the input pattern is stored correctly:
@@ -71,13 +71,13 @@ TEST_CASE("MIDI messages are output properly", "[advanced][patternal]")
7171
static constexpr double tickDuration = samplingRate / bufferSize; // seconds
7272
static constexpr double ticksPerSecond = 1.0 / tickDuration;
7373
static constexpr double testDuration = 3.0; // seconds
74-
static constexpr double NS_PER_S = 1000000;
74+
static constexpr double NS_PER_S = 1000000000;
7575

7676
Processor patternalProcessor;
7777
patternalProcessor.inputs.patterns.value = {
78-
(Pattern) {noteHiHat, {127, 127, 127, 127}}, // hi-hat
79-
(Pattern) {noteSnare, {0, 127, 0, 127}}, // snare
80-
(Pattern) {noteBassDrum, {127, 0, 127, 0}}, // bass drum
78+
{noteHiHat, {127, 127, 127, 127}}, // hi-hat
79+
{noteSnare, {0, 127, 0, 127}}, // snare
80+
{noteBassDrum, {127, 0, 127, 0}}, // bass drum
8181
};
8282

8383
// Check the output on each tick:
@@ -90,16 +90,22 @@ TEST_CASE("MIDI messages are output properly", "[advanced][patternal]")
9090
// INFO("Test tick " << tickIndex);
9191
pos_in_frames += bufferSize;
9292
pos_in_ns += tickDuration / NS_PER_S;
93+
double timeNow = tickIndex * tickDuration;
94+
double timeAtEndOfTick = (tickIndex + 1) * tickDuration;
95+
9396
tick_musical tk;
94-
// TODO tk.start_position_quarter =
95-
// TODO tk.end_position_quarter =
9697
tk.tempo = 120; // 120 BPM. One beat lasts 500 ms.
9798
tk.signature.num = 4;
9899
tk.signature.denom = 4;
99100
tk.frames = bufferSize;
100-
tk.position_in_nanoseconds = pos_in_ns;
101+
tk.position_in_nanoseconds = pos_in_ns; // We don't really need to set this pos in ns.
101102
tk.position_in_frames = pos_in_frames;
102-
double timeNow = tickIndex * tickDuration;
103+
tk.last_signature_change = 0; // The signature never changes.
104+
tk.start_position_in_quarters = TODO;
105+
tk.end_position_in_quarters = TODO;
106+
tk.bar_at_start = TODO;
107+
tk.bar_at_end = TODO;
108+
103109
patternalProcessor(tk);
104110

105111
// Test the 1st tick:
@@ -140,9 +146,5 @@ TEST_CASE("MIDI messages are output properly", "[advanced][patternal]")
140146
}
141147
// TODO: check the other ticks
142148
}
143-
144-
// TODO check somethig with:
145-
// - start_position_quarter
146-
// - last_signature_change
147149
}
148150

0 commit comments

Comments
 (0)