#!/usr/bin/env perl

# Example: perl eg/cadence 16 A minor 4 110 '2 7'

use strict;
use warnings;

use MIDI::Util;
use Music::Cadence;
use Music::Scales;

my $max    = shift || 16;
my $note   = shift || 'C';
my $type   = shift || 'major';
my $octave = shift || 4;
my $bpm    = shift || 100;
my $leads  = shift || '1 2 4 7';

my $quarter = 'qn';
my $half    = 'hn';

my @scale = get_scale_notes( $note, $type );
# Transform to MIDI accidentals
for ( @scale ) {
    s/#/s/;
    s/b/f/;
}

my @leaders = split /\s+/, $leads;

my $score = MIDI::Util::setup_score( bpm => $bpm );

my $mc = Music::Cadence->new(
    key    => $note,
    scale  => $type,
    octave => $octave,
    format => 'midi',
);

for my $i ( 1 .. $max ) {
    # Get a random selection of scale notes and add them to the score
    my @notes = map { $scale[ int rand @scale ] } 1 .. 2;
    $score->n( $quarter, $_ ) for @notes;

    # Add a half cadence after every 4th iteration
    if ( $i % 4 == 0 ) {
        my $chords = $mc->cadence(
            type    => 'half',
            leading => $leaders[ int rand @leaders ],
        );
        $score->n( $half, @$_ ) for @$chords;
    }
}

my $chords = $mc->cadence(
    type      => 'deceptive',
    variation => int( rand 2 ) + 1,
);
$score->n( $half, @$_ ) for @$chords;

$chords = $mc->cadence( type => 'plagal' );
$score->n( $half, @$_ ) for @$chords;

$score->write_score("$0.mid");
