1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use sim_kernel::{Error, Result, Symbol};
use crate::{
LaneDescriptor, LaneId, LaneKind, LaneTarget, Music, MusicError, Pitch, PitchClass, PlayEvent,
Time,
};
/// Arrangement of playable material laid out across lanes on a timeline.
///
/// Holds the ordered set of [ArrangerPlacement]s plus the lane ids the
/// arrangement declares, and renders them into play events.
#[derive(Clone, Debug)]
pub struct Arranger {
/// Placements that make up the arrangement, in declaration order.
pub placements: Vec<ArrangerPlacement>,
/// Lane ids the arrangement declares.
pub lanes: Vec<LaneId>,
}
/// Single placement of playable material at a point on the arranger timeline.
///
/// Carries the source reference plus the per-placement transforms, stretch,
/// pitch remap, filter, and trace policy applied during rendering.
#[derive(Clone, Debug)]
pub struct ArrangerPlacement {
/// Stable identifier used to attribute diagnostics and traces.
pub id: Symbol,
/// Playable material this placement renders.
pub playable: PlayableRef,
/// Onset of the placement on the arranger timeline.
pub at: Time,
/// Optional explicit duration used for clipping and fit-to-duration stretch.
pub duration: Option<Time>,
/// Lane the rendered notes are assigned to.
pub lane: LaneId,
/// Targets the lane drives.
pub targets: Vec<LaneTarget>,
/// Time-scaling policy applied to the placement.
pub stretch: StretchPolicy,
/// Ordered list of pitch and time transforms applied to the placement.
pub transform: Vec<PlacementTransform>,
/// Pitch remapping applied after transforms.
pub remap_pitch: PitchRemap,
/// Optional lane filter applied after pitch remapping.
pub filter: Option<FilterRef>,
/// Optional seed for deterministic placement behavior.
pub seed: Option<u64>,
/// Tracing verbosity for the placement.
pub trace: TracePolicy,
}
/// Reference to the playable material a placement renders.
#[derive(Clone, Debug)]
pub enum PlayableRef {
/// Inline music object owned by the placement.
Inline(Box<Music>),
/// Named reference resolved by the host at render time.
Symbol(Symbol),
}
/// Pitch or time transform applied to a placement's notes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PlacementTransform {
/// Transposes every pitch by a number of semitones.
TransposeSemitones(i32),
/// Transposes every pitch by a number of octaves.
TransposeOctaves(i16),
/// Inverts every pitch around a fixed pitch axis.
InvertAroundPitch(Pitch),
/// Inverts every pitch class around a fixed pitch-class axis.
InvertAroundPitchClass(PitchClass),
/// Reverses the placement in time.
Retrograde,
}
/// Time-scaling policy applied to a placement.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum StretchPolicy {
/// Leaves timing unchanged.
None,
/// Scales timing by the reciprocal of the given tempo ratio.
TempoRatio(Time),
/// Scales timing directly by the given time ratio.
TimeRatio(Time),
/// Scales the placement to fill its declared duration.
FitToDuration,
}
/// Pitch remapping applied after a placement's transforms.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PitchRemap {
/// Leaves pitches unchanged.
None,
/// Shifts every pitch by a fixed number of semitones.
Chromatic(i32),
/// Replaces one pitch class with another.
PitchClass {
/// Pitch class to match.
from: PitchClass,
/// Pitch class to substitute.
to: PitchClass,
},
/// Maps source MIDI keys to target keys for drum lanes.
DrumKey(Vec<(u8, u8)>),
/// Scale-degree remap resolved by a host-provided resolver.
ScaleDegree(Symbol),
/// Chord-tone remap resolved by a host-provided resolver.
ChordTone(Symbol),
/// Tuning remap resolved by a host-provided resolver.
Tuning(Symbol),
/// Vector remap resolved by a host-provided resolver.
Vector(Symbol),
/// Matrix remap resolved by a host-provided resolver.
Matrix(Symbol),
/// Callable remap resolved by a host-provided resolver.
Callable(Symbol),
}
/// Lane filter that keeps a placement only when its lane is retained.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FilterRef {
/// Identifier reported in filter diagnostics.
pub id: Symbol,
/// Lanes the filter keeps; an empty list acts as identity.
pub keep_lanes: Vec<LaneId>,
}
/// Tracing verbosity for a placement during rendering.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TracePolicy {
/// Emits no trace output.
Off,
/// Emits diagnostic-level trace output.
Diagnostics,
/// Emits full trace events.
Full,
}
/// Result of rendering an [Arranger]: play events plus diagnostics.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArrangerRender {
/// Stable-ordered play events produced by the arrangement.
pub events: Vec<PlayEvent>,
/// Diagnostics collected while rendering.
pub diagnostics: Vec<ArrangerDiagnostic>,
}
/// Diagnostic raised while rendering a placement.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArrangerDiagnostic {
/// Identifier of the placement that produced the diagnostic.
pub placement_id: Symbol,
/// Time on the arranger timeline the diagnostic refers to.
pub at: Time,
/// Human-readable diagnostic message.
pub message: String,
}
impl Arranger {
/// Builds an arranger from placements and lane ids, validating each placement.
pub fn new(placements: Vec<ArrangerPlacement>, lanes: Vec<LaneId>) -> Result<Self> {
let arranger = Self { placements, lanes };
arranger.validate()?;
Ok(arranger)
}
fn validate(&self) -> Result<()> {
for placement in &self.placements {
placement.validate()?;
}
Ok(())
}
}
impl ArrangerPlacement {
/// Builds a placement from a string id, playable, and onset using defaults.
pub fn new(id: impl Into<String>, playable: PlayableRef, at: Time) -> Result<Self> {
Self::with_symbol_id(Symbol::new(id.into()), playable, at)
}
/// Builds a placement from a symbol id, playable, and onset using defaults.
pub fn with_symbol_id(id: Symbol, playable: PlayableRef, at: Time) -> Result<Self> {
let placement = Self {
id,
playable,
at,
duration: None,
lane: LaneId::new("notes"),
targets: vec![LaneTarget::Instrument(Symbol::qualified(
"music/target",
"default",
))],
stretch: StretchPolicy::None,
transform: Vec::new(),
remap_pitch: PitchRemap::None,
filter: None,
seed: None,
trace: TracePolicy::Off,
};
placement.validate()?;
Ok(placement)
}
/// Sets the placement's explicit duration, rejecting negative values.
pub fn with_duration(mut self, duration: Time) -> Result<Self> {
ensure_non_negative_kernel(duration, "arranger placement duration")?;
self.duration = Some(duration);
Ok(self)
}
/// Sets the lane the placement's notes are assigned to.
pub fn with_lane(mut self, lane: LaneId) -> Self {
self.lane = lane;
self
}
/// Replaces the placement's targets with a single target.
pub fn with_target(mut self, target: LaneTarget) -> Self {
self.targets = vec![target];
self
}
/// Replaces the placement's targets with the given list.
pub fn with_targets(mut self, targets: Vec<LaneTarget>) -> Self {
self.targets = targets;
self
}
/// Sets the placement's stretch policy.
pub fn with_stretch(mut self, stretch: StretchPolicy) -> Self {
self.stretch = stretch;
self
}
/// Replaces the placement's transform list.
pub fn with_transform(mut self, transform: Vec<PlacementTransform>) -> Self {
self.transform = transform;
self
}
/// Sets the placement's pitch remap.
pub fn with_pitch_remap(mut self, remap_pitch: PitchRemap) -> Self {
self.remap_pitch = remap_pitch;
self
}
/// Sets the placement's lane filter.
pub fn with_filter(mut self, filter: FilterRef) -> Self {
self.filter = Some(filter);
self
}
/// Sets the placement's deterministic seed.
pub fn with_seed(mut self, seed: u64) -> Self {
self.seed = Some(seed);
self
}
/// Sets the placement's trace policy.
pub fn with_trace(mut self, trace: TracePolicy) -> Self {
self.trace = trace;
self
}
pub(crate) fn validate(&self) -> Result<()> {
ensure_non_negative_kernel(self.at, "arranger placement onset")?;
if let Some(duration) = self.duration {
ensure_non_negative_kernel(duration, "arranger placement duration")?;
}
if self.targets.is_empty() {
return Err(Error::Eval(
"arranger placement must have at least one target".to_owned(),
));
}
for target in &self.targets {
LaneDescriptor::new(self.lane.clone(), LaneKind::Note, target.clone(), 0)
.map_err(music_err)?;
}
Ok(())
}
}
impl PlayableRef {
/// Builds an inline reference that owns the given music object.
pub fn inline(music: Music) -> Self {
Self::Inline(Box::new(music))
}
/// Builds a symbolic reference resolved by the host at render time.
pub fn symbol(symbol: Symbol) -> Self {
Self::Symbol(symbol)
}
}
impl FilterRef {
/// Builds a filter from an identifier and the lanes it keeps.
pub fn new(id: Symbol, keep_lanes: Vec<LaneId>) -> Self {
Self { id, keep_lanes }
}
}
pub(crate) fn ensure_non_negative_kernel(value: Time, context: &str) -> Result<()> {
if value < Time::from_integer(0) {
Err(Error::Eval(format!("{context} cannot be negative")))
} else {
Ok(())
}
}
pub(crate) fn music_err(err: MusicError) -> Error {
Error::Eval(err.to_string())
}