tune 0.36.0

Explore musical tunings and create synthesizer tuning files for microtonal scales.
Documentation
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::hash::Hash;

use crate::note::Note;
use crate::pitch::Pitch;
use crate::pitch::Pitched;
use crate::pitch::Ratio;
use crate::tuner::Group;
use crate::tuner::GroupBy;
use crate::tuner::IsErr;
use crate::tuner::TunableSynth;
use crate::tuning::Approximation;

pub struct JitTuner<K, S> {
    model: JitTuningModel<K>,
    synth: S,
}

impl<K, S: TunableSynth> JitTuner<K, S> {
    /// Starts a new [`JitTuner`] with the given `synth` and `pooling_mode`.
    pub fn start(synth: S, pooling_mode: PoolingMode) -> Self {
        Self {
            model: JitTuningModel::new(synth.num_channels(), synth.group_by(), pooling_mode),
            synth,
        }
    }
}

impl<K: Copy + Eq + Hash, S: TunableSynth> JitTuner<K, S> {
    /// Starts a note with the given `pitch`.
    ///
    /// `key` is used as identifier for currently sounding notes.
    pub fn note_on(&mut self, key: K, pitch: Pitch, attr: S::NoteAttr) -> S::Result {
        match self.model.register_key(key, pitch) {
            RegisterKeyResult::Accepted {
                channel,
                stopped_note,
                started_note,
                detuning,
            } => {
                if let Some(stopped_note) = stopped_note {
                    let result = self.synth.note_off(channel, stopped_note, attr.clone());
                    if result.is_err() {
                        return result;
                    }
                }
                let result = self
                    .synth
                    .notes_detune(channel, &[(started_note, detuning)]);
                if result.is_err() {
                    return result;
                }
                self.synth.note_on(channel, started_note, attr)
            }
            RegisterKeyResult::Rejected => S::Result::ok(),
        }
    }

    /// Stops the note of the given `key`.
    pub fn note_off(&mut self, key: K, attr: S::NoteAttr) -> S::Result {
        match self.model.deregister_key(key) {
            AccessKeyResult::Found {
                channel,
                found_note,
            } => self.synth.note_off(channel, found_note, attr),
            AccessKeyResult::NotFound => S::Result::ok(),
        }
    }

    /// Updates the note of `key` with the given `pitch`.
    pub fn note_pitch(&mut self, key: K, pitch: Pitch) -> S::Result {
        match self.model.access_key(key) {
            AccessKeyResult::Found {
                channel,
                found_note,
            } => {
                let detuning = Ratio::between_pitches(found_note.pitch(), pitch);
                self.synth.notes_detune(channel, &[(found_note, detuning)])
            }
            AccessKeyResult::NotFound => S::Result::ok(),
        }
    }

    /// Sets a polyphonic attribute for the note with the given `key`.
    pub fn note_attr(&mut self, key: K, attr: S::NoteAttr) -> S::Result {
        match self.model.access_key(key) {
            AccessKeyResult::Found {
                channel,
                found_note,
            } => self.synth.note_attr(channel, found_note, attr),
            AccessKeyResult::NotFound => S::Result::ok(),
        }
    }

    /// Sets a channel-global attribute.
    pub fn global_attr(&mut self, attr: S::GlobalAttr) -> S::Result {
        self.synth.global_attr(attr)
    }

    /// Stops the current [`JitTuner`] yielding the consumed [`TunableSynth`] for future reuse.
    pub fn stop(mut self) -> S {
        let active_keys: Vec<_> = self.model.active_keys().collect();

        for key in active_keys {
            self.note_off(key, S::NoteAttr::default());
        }

        self.synth
    }
}

/// A more flexible but also more complex alternative to the [`AotTuningModel`](super::AotTuningModel).
///
/// It allocates channels and yields detunings just-in-time and is, therefore, not dependent on any fixed tuning.
pub struct JitTuningModel<K> {
    num_channels: usize,
    group_by: GroupBy,
    pooling_mode: PoolingMode,
    pools: HashMap<Group, JitPool<K, usize, Note>>,
    groups: HashMap<K, Group>,
}

impl<K> JitTuningModel<K> {
    pub fn new(num_channels: usize, group_by: GroupBy, pooling_mode: PoolingMode) -> Self {
        Self {
            num_channels,
            group_by,
            pooling_mode,
            pools: HashMap::new(),
            groups: HashMap::new(),
        }
    }
}

impl<K: Copy + Eq + Hash> JitTuningModel<K> {
    pub fn register_key(&mut self, key: K, pitch: Pitch) -> RegisterKeyResult {
        let Approximation {
            approx_value,
            deviation,
        } = pitch.find_in_tuning(());

        let group = self.group_by.group(approx_value);

        let pool = self
            .pools
            .entry(group)
            .or_insert_with(|| JitPool::new(self.pooling_mode, 0..self.num_channels));

        match pool.key_pressed(key, approx_value) {
            Some((channel, stopped)) => {
                self.groups.insert(key, group);
                if let Some(stopped) = stopped {
                    self.groups.remove(&stopped.0);
                }
                RegisterKeyResult::Accepted {
                    stopped_note: stopped.map(|(_, note)| note),
                    started_note: approx_value,
                    channel,
                    detuning: deviation,
                }
            }
            None => RegisterKeyResult::Rejected,
        }
    }

    pub fn deregister_key(&mut self, key: K) -> AccessKeyResult {
        let pools = &mut self.pools;
        match self
            .groups
            .get(&key)
            .and_then(|group| pools.get_mut(group))
            .and_then(|pool| pool.key_released(key))
        {
            Some((channel, found_note)) => {
                self.groups.remove(&key);
                AccessKeyResult::Found {
                    channel,
                    found_note,
                }
            }
            None => AccessKeyResult::NotFound,
        }
    }

    pub fn access_key(&self, key: K) -> AccessKeyResult {
        match self
            .groups
            .get(&key)
            .and_then(|group| self.pools.get(group))
            .and_then(|pool| pool.find_key(key))
        {
            Some((channel, found_note)) => AccessKeyResult::Found {
                found_note,
                channel,
            },
            None => AccessKeyResult::NotFound,
        }
    }

    pub fn active_keys(&self) -> impl Iterator<Item = K> + '_ {
        self.pools.values().flat_map(|pool| pool.active_keys())
    }
}

/// Reports the channel, [`Note`] and detuning of a newly registered key.
///
/// If the key cannot be registered [`RegisterKeyResult::Rejected`] is returned.
/// If the new key requires a registered note to be stopped `stopped_note` is [`Option::Some`].
pub enum RegisterKeyResult {
    Accepted {
        channel: usize,
        stopped_note: Option<Note>,
        started_note: Note,
        detuning: Ratio,
    },
    Rejected,
}

/// Reports the channel and [`Note`] of a registered key.
///
/// If the key is not registered [`AccessKeyResult::NotFound`] is returned.
pub enum AccessKeyResult {
    Found { channel: usize, found_note: Note },
    NotFound,
}

struct JitPool<K, C, N> {
    mode: PoolingMode,
    free: VecDeque<C>,
    tuned: BTreeMap<u64, K>, // Insertion order is conserved
    active: HashMap<K, (u64, C, N)>,
    curr_usage_id: u64,
}

/// Defines what to do when the channel pool is full and a new key cannot be registered.
#[derive(Clone, Copy, Debug)]
pub enum PoolingMode {
    Block,
    Stop,
    Ignore,
}

impl<K: Copy + Eq + Hash, C: Copy, N: Copy> JitPool<K, C, N> {
    fn new(mode: PoolingMode, channels: impl IntoIterator<Item = C>) -> Self {
        Self {
            mode,
            free: VecDeque::from_iter(channels),
            tuned: BTreeMap::new(),
            active: HashMap::new(),
            curr_usage_id: 0,
        }
    }

    fn key_pressed(&mut self, key: K, note: N) -> Option<(C, Option<(K, N)>)> {
        if let Some(channel) = self.try_insert(key, note) {
            return Some((channel, None));
        }

        match self.mode {
            PoolingMode::Block => None,
            PoolingMode::Stop => self.find_old_key().map(|(channel, old_key, old_location)| {
                self.key_released(old_key);
                self.try_insert(key, note).unwrap();
                (channel, Some((old_key, old_location)))
            }),
            PoolingMode::Ignore => self.find_old_key().map(|(channel, old_key, _)| {
                self.weaken_key(old_key);
                self.try_insert(key, note).unwrap();
                (channel, None)
            }),
        }
    }

    fn key_released(&mut self, key: K) -> Option<(C, N)> {
        self.active
            .remove(&key)
            .map(|(usage_id, freed_channel, location)| {
                self.free_key(usage_id, freed_channel);
                (freed_channel, location)
            })
    }

    fn find_key(&self, key: K) -> Option<(C, N)> {
        self.active
            .get(&key)
            .map(|&(_, channel, location)| (channel, location))
    }

    fn active_keys(&self) -> impl Iterator<Item = K> + '_ {
        self.active.keys().copied()
    }

    fn try_insert(&mut self, key: K, note: N) -> Option<C> {
        let free_channel = self.free.pop_front()?;
        self.tuned.insert(self.curr_usage_id, key);
        self.active
            .insert(key, (self.curr_usage_id, free_channel, note));
        self.curr_usage_id += 1;
        Some(free_channel)
    }

    fn find_old_key(&mut self) -> Option<(C, K, N)> {
        let key = *self.tuned.values().next()?;
        let &(_, channel, location) = self.active.get(&key)?;
        Some((channel, key, location))
    }

    fn weaken_key(&mut self, key: K) {
        if let Some(&(usage_id, freed_channel, _)) = self.active.get(&key) {
            self.free_key(usage_id, freed_channel);
        }
    }

    fn free_key(&mut self, usage_id: u64, freed_channel: C) {
        if self.tuned.remove(&usage_id).is_some() {
            self.free.push_back(freed_channel);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pooling_mode_block() {
        let mut pool = JitPool::new(PoolingMode::Block, 0..3);

        assert_eq!(pool.key_pressed("keyA", "locA"), Some((0, None)));
        assert_eq!(pool.key_pressed("keyB", "locB"), Some((1, None)));
        assert_eq!(pool.key_pressed("keyC", "locC"), Some((2, None)));
        assert_eq!(pool.key_pressed("keyD", "locD"), None);

        assert_eq!(pool.find_key("keyA"), Some((0, "locA")));
        assert_eq!(pool.find_key("keyB"), Some((1, "locB")));
        assert_eq!(pool.find_key("keyC"), Some((2, "locC")));
        assert_eq!(pool.find_key("keyD"), None);

        assert_eq!(pool.key_released("keyB"), Some((1, "locB")));
        assert_eq!(pool.key_pressed("keyD", "locD"), Some((1, None)));
        assert_eq!(pool.key_pressed("keyE", "locE"), None);

        assert_eq!(pool.find_key("keyA"), Some((0, "locA")));
        assert_eq!(pool.find_key("keyB"), None);
        assert_eq!(pool.find_key("keyC"), Some((2, "locC")));
        assert_eq!(pool.find_key("keyD"), Some((1, "locD")));
        assert_eq!(pool.find_key("keyE"), None);

        assert_eq!(pool.key_released("keyA"), Some((0, "locA")));
        assert_eq!(pool.key_released("keyB"), None);
        assert_eq!(pool.key_released("keyC"), Some((2, "locC")));
        assert_eq!(pool.key_released("keyD"), Some((1, "locD")));
        assert_eq!(pool.key_released("keyE"), None);

        assert_eq!(pool.find_key("keyA"), None);
        assert_eq!(pool.find_key("keyB"), None);
        assert_eq!(pool.find_key("keyC"), None);
        assert_eq!(pool.find_key("keyD"), None);
        assert_eq!(pool.find_key("keyE"), None);
    }

    #[test]
    fn pooling_mode_stop() {
        let mut pool = JitPool::new(PoolingMode::Stop, 0..3);

        assert_eq!(pool.key_pressed("keyA", "locA"), Some((0, None)));
        assert_eq!(pool.key_pressed("keyB", "locB"), Some((1, None)));
        assert_eq!(pool.key_pressed("keyC", "locC"), Some((2, None)));
        assert_eq!(
            pool.key_pressed("keyD", "locD"),
            Some((0, Some(("keyA", "locA"))))
        );

        assert_eq!(pool.find_key("keyA"), None);
        assert_eq!(pool.find_key("keyB"), Some((1, "locB")));
        assert_eq!(pool.find_key("keyC"), Some((2, "locC")));
        assert_eq!(pool.find_key("keyD"), Some((0, "locD")));

        assert_eq!(pool.key_released("keyB"), Some((1, "locB")));
        assert_eq!(pool.key_pressed("keyD", "locD"), Some((1, None)));
        assert_eq!(
            pool.key_pressed("keyE", "locE"),
            Some((2, Some(("keyC", "locC"))))
        );

        assert_eq!(pool.find_key("keyA"), None);
        assert_eq!(pool.find_key("keyB"), None);
        assert_eq!(pool.find_key("keyC"), None);
        assert_eq!(pool.find_key("keyD"), Some((1, "locD")));
        assert_eq!(pool.find_key("keyE"), Some((2, "locE")));

        assert_eq!(pool.key_released("keyA"), None);
        assert_eq!(pool.key_released("keyB"), None);
        assert_eq!(pool.key_released("keyC"), None);
        assert_eq!(pool.key_released("keyD"), Some((1, "locD")));
        assert_eq!(pool.key_released("keyE"), Some((2, "locE")));

        assert_eq!(pool.find_key("keyA"), None);
        assert_eq!(pool.find_key("keyB"), None);
        assert_eq!(pool.find_key("keyC"), None);
        assert_eq!(pool.find_key("keyD"), None);
        assert_eq!(pool.find_key("keyE"), None);
    }

    #[test]
    fn pooling_mode_ignore() {
        let mut pool = JitPool::new(PoolingMode::Ignore, 0..3);

        assert_eq!(pool.key_pressed("keyA", "locA"), Some((0, None)));
        assert_eq!(pool.key_pressed("keyB", "locB"), Some((1, None)));
        assert_eq!(pool.key_pressed("keyC", "locC"), Some((2, None)));
        assert_eq!(pool.key_pressed("keyD", "locD"), Some((0, None)));

        assert_eq!(pool.find_key("keyA"), Some((0, "locA")));
        assert_eq!(pool.find_key("keyB"), Some((1, "locB")));
        assert_eq!(pool.find_key("keyC"), Some((2, "locC")));
        assert_eq!(pool.find_key("keyD"), Some((0, "locD")));

        assert_eq!(pool.key_released("keyB"), Some((1, "locB")));
        assert_eq!(pool.key_pressed("keyD", "locD"), Some((1, None)));
        assert_eq!(pool.key_pressed("keyE", "locE"), Some((2, None)));

        assert_eq!(pool.find_key("keyA"), Some((0, "locA")));
        assert_eq!(pool.find_key("keyB"), None);
        assert_eq!(pool.find_key("keyC"), Some((2, "locC")));
        assert_eq!(pool.find_key("keyD"), Some((1, "locD")));
        assert_eq!(pool.find_key("keyE"), Some((2, "locE")));

        assert_eq!(pool.key_released("keyA"), Some((0, "locA")));
        assert_eq!(pool.key_released("keyB"), None);
        assert_eq!(pool.key_released("keyC"), Some((2, "locC")));
        assert_eq!(pool.key_released("keyD"), Some((1, "locD")));
        assert_eq!(pool.key_released("keyE"), Some((2, "locE")));

        assert_eq!(pool.find_key("keyA"), None);
        assert_eq!(pool.find_key("keyB"), None);
        assert_eq!(pool.find_key("keyC"), None);
        assert_eq!(pool.find_key("keyD"), None);
        assert_eq!(pool.find_key("keyE"), None);
    }
}