pikopiko 0.1.0

MML parser, compiler and chiptune player for games, with diagnostics designed for tools and AI.
Documentation
//! 調と和音の推定。

use pikopiko::composer::analysis::{self, chord_of, ChordQuality, Key, Mode};
use pikopiko::{compile, compile_with, describe, CompileOptions};

/// 和音を書けるテスト用の音源(`basic` の同時発音数だけを変えた)。
const POLY: pikopiko::SynthSpec = pikopiko::SynthSpec {
    texts: &[],
    name: "poly",
    aliases: &[],
    polyphony: 8,
    ..pikopiko::synth::BASIC
};

fn song(src: &str) -> pikopiko::Song {
    let synths = [&pikopiko::synth::BASIC, &POLY];
    let opts = CompileOptions {
        synths: &synths,
        ..CompileOptions::default()
    };
    let (s, d) = compile_with(src, &opts);
    s.unwrap_or_else(|| panic!("{src}\n{d:?}"))
}

fn key_of(src: &str) -> (Key, i32) {
    let k = analysis::song_key(&song(src), None).unwrap();
    (k.key, k.confidence)
}

#[test]
fn major_scale_is_c_major() {
    let (k, conf) = key_of("A o4 l8 c d e f g a b > c4 < g4 e4 c2");
    assert_eq!(
        k,
        Key {
            tonic: 0,
            mode: Mode::Major
        }
    );
    assert_eq!(k.name(), "C major");
    assert!(conf > 700, "{conf}");
}

#[test]
fn harmonic_minor_is_a_minor() {
    let (k, _) = key_of("A o4 l8 a b > c d e f g+ a4 e4 c4 < a2");
    assert_eq!(
        k,
        Key {
            tonic: 9,
            mode: Mode::Minor
        }
    );
    assert_eq!(k.name(), "A minor");
}

#[test]
fn transposed_melody_moves_the_key() {
    // 同じ旋律を全音上げると D major
    let (k, _) = key_of("A k2 o4 l8 c d e f g a b > c4 < g4 e4 c2");
    assert_eq!(k.name(), "D major");
    let (k, _) = key_of("A o4 l8 f g a a+ > c d e f4 c4 < a4 f2");
    assert_eq!(k.name(), "F major");
}

#[test]
fn chords_count_every_note() {
    let (k, _) = key_of("#synth poly\nA o4 'g b > d'2 'c e g'2 'f a > c'2 'g b > d f'2 'c e g'1");
    assert_eq!(k.name(), "C major");
}

#[test]
fn no_pitched_notes_means_no_key() {
    let s = compile("A r1").unwrap();
    assert!(analysis::song_key(&s, None).is_none());
    // AY のノイズのみの音色は音源の説明を渡すと除く
    let opts = CompileOptions::default();
    let (s, _) = compile_with("#synth ay\nA @1 o4 c d e f g", &opts);
    let s = s.unwrap();
    assert!(analysis::song_key(&s, None).is_some());
    assert!(analysis::song_key(&s, Some(&pikopiko::synth::AY_3_8912)).is_none());
}

#[test]
fn result_is_deterministic_and_handles_long_songs() {
    let src = format!("A t300 [o4 l1 c e g > c <]{}", 200);
    let a = analysis::song_key(&compile(&src).unwrap(), None).unwrap();
    let b = analysis::song_key(&compile(&src).unwrap(), None).unwrap();
    assert_eq!(a, b);
    assert_eq!(a.key.name(), "C major");
}

#[test]
fn key_names_and_scales() {
    assert_eq!(Key::all().count(), 24);
    let k = Key::parse("F# minor").unwrap();
    assert_eq!(
        k,
        Key {
            tonic: 6,
            mode: Mode::Minor
        }
    );
    assert_eq!(Key::parse("bb major").unwrap().name(), "Bb major");
    assert_eq!(Key::parse("c+ major").unwrap().name(), "Db major");
    assert!(Key::parse("H major").is_none());
    assert!(Key::parse("C dorian").is_none());
    let c = Key {
        tonic: 0,
        mode: Mode::Major,
    };
    assert_eq!(c.scale(), [0, 2, 4, 5, 7, 9, 11]);
    assert!(c.contains(64) && !c.contains(61));
    let a = Key {
        tonic: 9,
        mode: Mode::Minor,
    };
    assert_eq!(a.scale(), [9, 11, 0, 2, 4, 5, 7]);
    for k in Key::all() {
        assert_eq!(Key::parse(&k.name()), Some(k));
    }
}

#[test]
fn chord_names() {
    let name = |keys: &[u8]| chord_of(keys).map(|c| c.name());
    assert_eq!(name(&[60, 64, 67]).as_deref(), Some("C"));
    assert_eq!(name(&[57, 60, 64]).as_deref(), Some("Am"));
    assert_eq!(name(&[55, 59, 62, 65]).as_deref(), Some("G7"));
    assert_eq!(name(&[60, 64, 67, 71]).as_deref(), Some("Cmaj7"));
    assert_eq!(name(&[57, 60, 64, 67]).as_deref(), Some("Am7"));
    assert_eq!(name(&[59, 62, 65]).as_deref(), Some("Bdim"));
    assert_eq!(name(&[60, 64, 68]).as_deref(), Some("Caug"));
    assert_eq!(name(&[60, 65, 67]).as_deref(), Some("Csus4"));
    assert_eq!(name(&[59, 62, 65, 69]).as_deref(), Some("Bm7-5"));
    assert_eq!(name(&[48, 55, 60]).as_deref(), Some("C5"));
    // 転回形と足りない音
    assert_eq!(name(&[52, 55, 60]).as_deref(), Some("C/E"));
    assert_eq!(name(&[60, 64]).as_deref(), Some("C"));
    assert_eq!(name(&[60, 72]), None);
    assert_eq!(name(&[60]), None);
    assert_eq!(name(&[]), None);
    assert_eq!(name(&[60, 61, 62, 63]), None);
    let c = chord_of(&[52, 55, 60]).unwrap();
    assert_eq!((c.root, c.quality, c.bass), (0, ChordQuality::Major, 4));
}

#[test]
fn chord_at_uses_all_tracks() {
    let s = compile("A o4 c1 d1\nB o4 e1 f1\nC o4 g1 a1").unwrap();
    let notes = analysis::notes(&s, None);
    let whole = 4 * pikopiko::PPQ as u64;
    assert_eq!(analysis::chord_at(&notes, 0, |_| true).unwrap().name(), "C");
    assert_eq!(
        analysis::chord_at(&notes, whole, |_| true).unwrap().name(),
        "Dm"
    );
    // ミュートしたトラックは除く
    assert_eq!(
        analysis::chord_at(&notes, 0, |t| t != 1).unwrap().name(),
        "C5"
    );
    assert!(analysis::chord_at(&notes, 2 * whole, |_| true).is_none());
}

#[test]
fn notes_split_chords_and_track_tones() {
    let s = song("#synth poly\nA o4 @2 'c e g'4 @1 d4");
    let n = analysis::notes(&s, None);
    assert_eq!(n.len(), 4);
    assert_eq!(n[0].key, 60);
    assert_eq!(n[0].tone, 2);
    assert_eq!(n[3].key, 62);
    assert_eq!(n[3].tone, 1);
    assert_eq!(n[0].end, n[3].start);
}

#[test]
fn describe_has_the_key() {
    let info = describe(&compile("A o4 l8 a b > c d e f g+ a4 e4 c4 < a2").unwrap());
    let k = info.key.unwrap();
    assert_eq!(k.name, "A minor");
    assert_eq!(k.tonic, "a");
    assert_eq!(k.mode, Mode::Minor);
    assert!(k.confidence > 0.5 && k.confidence <= 1.0);
    assert!(describe(&compile("A r4").unwrap()).key.is_none());
}