Tabula Sonora 0.1.0
A native C++20 implementation of the Roland Sound Canvas VA synth voice
Loading...
Searching...
No Matches
sequence_player.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <cstdint>
8#include <filesystem>
9#include <optional>
10#include <span>
11#include <vector>
12
13namespace ts {
14
20public:
24 SequencePlayer(ToneGenerator& generator, std::vector<MidiEvent> events);
25
31
36 const std::filesystem::path& path);
37
44 [[nodiscard]] std::vector<int> addressed_parts() const;
45
47 [[nodiscard]] ToneGenerator& generator() noexcept { return *generator_; }
48
53 [[nodiscard]] std::int64_t last_event_position() const noexcept { return last_event_position_; }
54
56 [[nodiscard]] std::int64_t position() const noexcept { return position_; }
57
59 [[nodiscard]] const std::optional<smf::SongLoop>& loop() const noexcept { return loop_; }
60
69 void set_loop_count(int count);
70
71 [[nodiscard]] int loop_count() const noexcept { return loop_count_; }
72
74 [[nodiscard]] int loops_played() const noexcept { return loops_played_; }
75
77 void set_fade_seconds(double seconds);
78
81 [[nodiscard]] bool at_end() const noexcept
82 {
83 return finished_ || (cursor_ >= events_.size() && generator_->active_voices() == 0);
84 }
85
94 void render(std::span<float> left, std::span<float> right);
95
101 [[nodiscard]] RenderResult render_to_end(double tail_seconds = 2.2,
102 std::optional<double> end_seconds = std::nullopt);
103
111 void seek(std::int64_t sample);
112
113private:
115 void replay_to(std::int64_t sample);
116
118 void all_notes_off();
119
121 void dispatch();
122
125 bool handle_loop_point();
126
127 ToneGenerator* generator_;
128 std::vector<MidiEvent> events_;
129 std::int64_t last_event_position_ = 0;
130 std::int64_t position_ = 0;
131 std::size_t cursor_ = 0;
132
133 std::optional<smf::SongLoop> loop_;
134 int loop_count_ = 1;
135 int loops_played_ = 0;
136 double fade_seconds_ = 7.0;
139 std::int64_t rendered_ = 0;
141 std::int64_t fade_start_ = -1;
142 bool finished_ = false;
144 int notes_before_reset_ = 0;
145};
146
147} // namespace ts
bool at_end() const noexcept
Whether every event has been dispatched and nothing is still sounding, or the post-loop fade has run ...
Definition sequence_player.hpp:81
std::int64_t last_event_position() const noexcept
Position of the final event of any kind.
Definition sequence_player.hpp:53
void set_loop_count(int count)
How many times the looped section should play, counting the first pass.
static SequencePlayer from_file(ToneGenerator &generator, const std::filesystem::path &path)
Reads a music file and creates a player for it.
RenderResult render_to_end(double tail_seconds=2.2, std::optional< double > end_seconds=std::nullopt)
Streams the whole file into memory, from wherever the player currently is.
std::int64_t position() const noexcept
Current position in samples.
Definition sequence_player.hpp:56
void set_fade_seconds(double seconds)
The post-loop fade length. Zero cuts at the loop end.
SequencePlayer(ToneGenerator &generator, smf::Song song)
Creates a player over an engine and a parsed song, keeping its loop points.
const std::optional< smf::SongLoop > & loop() const noexcept
The file's loop points, when it declared any.
Definition sequence_player.hpp:59
SequencePlayer(ToneGenerator &generator, std::vector< MidiEvent > events)
Creates a player over an engine and an event list ordered by position.
std::vector< int > addressed_parts() const
Which parts the file actually addresses, as port * 16 + channel.
void seek(std::int64_t sample)
Jumps to a position, leaving the engine in the state the file would have put it in.
int loop_count() const noexcept
Definition sequence_player.hpp:71
int loops_played() const noexcept
Completed passes over the loop body, counting up from zero on the initial pass.
Definition sequence_player.hpp:74
ToneGenerator & generator() noexcept
The engine being driven.
Definition sequence_player.hpp:47
void render(std::span< float > left, std::span< float > right)
Renders audio, dispatching every event that falls inside it.
The real-time engine: MIDI in, audio out, rendered a block at a time.
Definition tone_generator.hpp:100
Definition control_decode.hpp:7
A finished render.
Definition render_options.hpp:107
A parsed file: the event list plus what the container knew beyond the events.
Definition smf_reader.hpp:98