tune-cli 0.29.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
use std::collections::BTreeSet;
use std::error::Error;
use std::io;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;

use async_std::task;
use clap::Parser;
use clap::ValueEnum;
use midir::ConnectError;
use midir::InitError;
use midir::MidiIO;
use midir::MidiInput;
use midir::MidiOutput;
use midir::MidiOutputConnection;
use serde::Deserialize;
use serde::Serialize;
use tune::key::PianoKey;
use tune::mts::ScaleOctaveTuningFormat;
use tune::tuner::MidiTarget;
use tune::tuner::TunableMidi;

use crate::CliResult;
use crate::portable;
use crate::portable::SendTask;

#[derive(Parser)]
pub struct MidiInArgs {
    /// First MIDI channel to listen to for MIDI events
    #[arg(long = "in-chan", default_value = "0")]
    pub in_channel: u8,

    /// Number of MIDI input channels to listen to.
    /// Wraps around at zero-based channel number 15.
    /// For example --in-chan=10 and --in-chans=15 uses all channels but the drum channel.
    #[arg(long = "in-chans", default_value = "16")]
    pub num_in_channels: u8,

    /// Offset in scale steps per channel number.
    /// Required for keyboards with more than 128 keys like the Lumatone.
    #[arg(long = "chan-offs", default_value = "0")]
    pub channel_offset: i16,
}

impl MidiInArgs {
    pub fn get_midi_source(&self) -> CliResult<MidiSource> {
        Ok(MidiSource {
            channels: get_channels("Input", self.in_channel, self.num_in_channels)?.collect(),
            lumatone_offset: self.channel_offset,
        })
    }
}

pub struct MidiSource {
    pub channels: BTreeSet<u8>,
    pub lumatone_offset: i16,
}

impl MidiSource {
    pub fn get_offset(&self, channel: u8) -> MultiChannelOffset {
        MultiChannelOffset {
            offset: i32::from(channel) * i32::from(self.lumatone_offset),
        }
    }
}

pub struct MultiChannelOffset {
    pub offset: i32,
}

impl MultiChannelOffset {
    pub fn get_piano_key(&self, midi_number: u8) -> PianoKey {
        PianoKey::from_midi_number(i32::from(midi_number) + self.offset)
    }
}

const DEFAULT_OUT_CHANNEL: u8 = 0;
const DEFAULT_NUM_OUT_CHANS: u8 = 9;

#[derive(Clone, Debug, Deserialize, Serialize, Parser)]
pub struct MidiOutArgs {
    /// First MIDI channel to send the modified MIDI events to
    #[arg(long = "out-chan", default_value_t = DEFAULT_OUT_CHANNEL)]
    pub out_channel: u8,

    /// Number of MIDI output channels that should be retuned.
    /// Wraps around at zero-based channel number 15.
    /// For example --out-chan=10 and --out-chans=15 uses all channels but the drum channel.
    #[arg(long = "out-chans", default_value_t = DEFAULT_NUM_OUT_CHANS)]
    pub num_out_channels: u8,

    #[serde(flatten)]
    #[command(flatten)]
    pub device_id: DeviceIdArg,

    /// First tuning program to be used to store the channel-specific tuning information.
    /// Wraps around at tuning program number 127.
    #[arg(long = "tun-pg", default_value = "0")]
    pub tuning_program: u8,
}

impl Default for MidiOutArgs {
    fn default() -> Self {
        Self {
            out_channel: DEFAULT_OUT_CHANNEL,
            num_out_channels: DEFAULT_NUM_OUT_CHANS,
            device_id: Default::default(),
            tuning_program: Default::default(),
        }
    }
}

impl MidiOutArgs {
    pub fn get_midi_target<H>(&self, handler: H) -> CliResult<MidiTarget<H>> {
        Ok(MidiTarget {
            handler,
            channels: get_channels("Output", self.out_channel, self.num_out_channels)?.collect(),
        })
    }

    pub fn create_synth<H>(&self, target: MidiTarget<H>, method: TuningMethod) -> TunableMidi<H> {
        match method {
            TuningMethod::FullKeyboard => TunableMidi::single_note_tuning_change(
                target,
                false,
                self.device_id.device_id,
                self.tuning_program,
            ),
            TuningMethod::FullKeyboardRt => TunableMidi::single_note_tuning_change(
                target,
                true,
                self.device_id.device_id,
                self.tuning_program,
            ),
            TuningMethod::Octave1 => TunableMidi::scale_octave_tuning(
                target,
                false,
                self.device_id.device_id,
                ScaleOctaveTuningFormat::OneByte,
            ),
            TuningMethod::Octave1Rt => TunableMidi::scale_octave_tuning(
                target,
                true,
                self.device_id.device_id,
                ScaleOctaveTuningFormat::OneByte,
            ),
            TuningMethod::Octave2 => TunableMidi::scale_octave_tuning(
                target,
                false,
                self.device_id.device_id,
                ScaleOctaveTuningFormat::TwoByte,
            ),
            TuningMethod::Octave2Rt => TunableMidi::scale_octave_tuning(
                target,
                true,
                self.device_id.device_id,
                ScaleOctaveTuningFormat::TwoByte,
            ),
            TuningMethod::ChannelFineTuning => TunableMidi::channel_fine_tuning(target),
            TuningMethod::PitchBend => TunableMidi::pitch_bend(target),
        }
    }
}

fn get_channels(
    description: &str,
    first_channel: u8,
    num_channels: u8,
) -> CliResult<impl Iterator<Item = u8>> {
    if first_channel >= 16 {
        return Err(format!("{description} channel is not in the range [0..16)").into());
    }
    if num_channels > 16 {
        return Err(format!(
            "Cannot use more than 16 {} channels",
            description.to_lowercase()
        )
        .into());
    }
    Ok((0..num_channels).map(move |channel| (first_channel + channel) % 16))
}

const DEFAULT_DEVICE_ID: u8 = 0x7f;

#[derive(Clone, Debug, Deserialize, Serialize, Parser)]
pub struct DeviceIdArg {
    /// ID of the device that should respond to MTS messages
    #[arg(long = "dev-id", default_value_t = DEFAULT_DEVICE_ID)]
    pub device_id: u8,
}

impl Default for DeviceIdArg {
    fn default() -> Self {
        Self {
            device_id: DEFAULT_DEVICE_ID,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize, ValueEnum)]
pub enum TuningMethod {
    #[value(name = "full")]
    #[serde(rename = "full")]
    FullKeyboard,
    #[value(name = "full-rt")]
    #[serde(rename = "full-rt")]
    FullKeyboardRt,
    #[value(name = "octave-1")]
    #[serde(rename = "octave-1")]
    Octave1,
    #[value(name = "octave-1-rt")]
    #[serde(rename = "octave-1-rt")]
    Octave1Rt,
    #[value(name = "octave-2")]
    #[serde(rename = "octave-2")]
    Octave2,
    #[value(name = "octave-2-rt")]
    #[serde(rename = "octave-2-rt")]
    Octave2Rt,
    #[value(name = "fine-tuning")]
    #[serde(rename = "fine-tuning")]
    ChannelFineTuning,
    #[value(name = "pitch-bend")]
    #[serde(rename = "pitch-bend")]
    PitchBend,
}

pub type MidiResult<T> = Result<T, MidiError>;

#[derive(Clone, Debug)]
pub enum MidiError {
    DeviceNotFound {
        wanted: String,
        available: Vec<String>,
    },
    AmbiguousDevice {
        wanted: String,
        matches: Vec<String>,
    },
    Other(String),
}

impl<T: Error> From<T> for MidiError {
    fn from(error: T) -> Self {
        MidiError::Other(error.to_string())
    }
}

pub fn print_midi_devices(mut dst: impl io::Write, client_name: &str) -> MidiResult<()> {
    let midi_input = MidiInput::new(client_name)?;
    writeln!(dst, "Readable MIDI devices:")?;
    for port in midi_input.ports() {
        writeln!(dst, "- {}", midi_input.port_name(&port)?)?;
    }

    let midi_output = MidiOutput::new(client_name)?;
    writeln!(dst, "Writable MIDI devices:")?;
    for port in midi_output.ports() {
        writeln!(dst, "- {}", midi_output.port_name(&port)?)?;
    }

    Ok(())
}

pub fn start_in_connect_loop(
    client_name: String,
    fuzzy_port_name: String,
    callback: impl FnMut(&[u8]) + Send + 'static,
    report_status: impl FnMut(String) + Send + 'static,
) {
    let callback = Arc::new(Mutex::new(callback));

    start_connect_loop(
        fuzzy_port_name,
        move || MidiInput::new(&client_name),
        move |driver, port| {
            driver.connect(
                port,
                "MIDI-in",
                {
                    let callback = callback.clone();
                    move |_, message, _| (callback.try_lock().unwrap())(message)
                },
                (),
            )
        },
        |conn| {
            conn.close();
        },
        report_status,
    );
}

pub fn connect_to_out_device(
    client_name: &str,
    fuzzy_port_name: &str,
) -> MidiResult<(String, MidiOutputConnection)> {
    let midi_output = MidiOutput::new(client_name)?;

    let (port_name, port) = find_port_by_name(&midi_output, fuzzy_port_name)?;

    Ok((port_name, midi_output.connect(&port, "MIDI-out")?))
}

fn start_connect_loop<D: MidiIO, C>(
    fuzzy_port_name: String,
    mut driver_factory: impl FnMut() -> Result<D, InitError> + SendTask + 'static,
    mut connect: impl FnMut(D, &D::Port) -> Result<C, ConnectError<D>> + SendTask + 'static,
    mut disconnect: impl FnMut(C) + SendTask + 'static,
    mut report_status: impl FnMut(String) + SendTask + 'static,
) where
    D::Port: SendTask + 'static,
    C: SendTask + 'static,
{
    const SCAN_INTERVAL: Duration = Duration::from_secs(1);
    const REPORT_INTERVAL: u8 = 10;

    let mut port_name_conn = None;
    let mut report_count = 0;

    portable::spawn_task(async move {
        loop {
            match driver_factory() {
                Ok(driver) => {
                    if let Some((port, name, conn)) = port_name_conn.take() {
                        match driver.port_name(&port) {
                            Ok(_) => port_name_conn = Some((port, name, conn)),
                            Err(err) => {
                                report_status(format!("Lost connection to {name}: {err}"));
                                disconnect(conn);
                            }
                        }
                    };

                    if port_name_conn.is_none() {
                        if report_count == 0 {
                            report_status(format!(
                                "Waiting for MIDI device `{fuzzy_port_name}` to come online..."
                            ));
                        }

                        if let Ok((name, port)) = find_port_by_name(&driver, &fuzzy_port_name) {
                            match connect(driver, &port) {
                                Ok(conn) => {
                                    report_status(format!("Connected to {name}"));
                                    port_name_conn = Some((port, name, conn));
                                }
                                Err(err) => {
                                    report_status(format!("Failed to connect to {name}: {err}"));
                                }
                            }
                        }
                    }
                }
                Err(err) => report_status(format!("Unable to initialize MIDI driver: {err}")),
            }

            report_count += 1;
            report_count %= REPORT_INTERVAL;

            task::sleep(SCAN_INTERVAL).await;
        }
    });
}

fn find_port_by_name<IO: MidiIO>(
    midi_io: &IO,
    fuzzy_port_name: &str,
) -> MidiResult<(String, IO::Port)> {
    let target_port_lowercase = fuzzy_port_name.to_lowercase();

    let mut matching_ports = midi_io
        .ports()
        .into_iter()
        .filter_map(|port| {
            midi_io
                .port_name(&port)
                .ok()
                .filter(|port_name| port_name.to_lowercase().contains(&target_port_lowercase))
                .map(|port_name| (port_name, port))
        })
        .collect::<Vec<_>>();

    match matching_ports.len() {
        0 => Err(MidiError::DeviceNotFound {
            wanted: target_port_lowercase,
            available: midi_io
                .ports()
                .iter()
                .filter_map(|port| midi_io.port_name(port).ok())
                .collect(),
        }),
        1 => Ok(matching_ports.pop().unwrap()),
        _ => Err(MidiError::AmbiguousDevice {
            wanted: target_port_lowercase,
            matches: matching_ports
                .into_iter()
                .map(|(port_name, _)| port_name)
                .collect(),
        }),
    }
}