#!/usr/bin/perl -w

### See the $usage variable for how to run this script.
###
### Read the MIDI::Simple perldoc page to understand how this works.

use strict;
# kff todo: which ones are required?  I think only one...
# use MIDI;
use MIDI::Simple;
# use MIDI::Opus;
# use MIDI::Track;
# use MIDI::Event;
# use MIDI::Score;

my $usage =
    "Usage: playpitch.pl PITCH DURATION INSTRUMENT\n"
    . "\n"
    . "PITCH is a string of the form NOTE[MODIFIER]NUMBER.  The\n"
    . "letter is the name of the note, the optional modifier is \"s\" for\n"
    . "sharp, \"f\" for flat, and the number is the octave.\n"
    . "\n"
    . "DURATION is a number of seconds; it may be fractional.\n"
    . "\n"
    . "INSTRUMENT is an instrument name.\n";

# Read the arguments
my $pitch      = shift || die ($usage);
my $duration   = shift || die ($usage);
my $instrument = shift || die ($usage);

# A temporary file to hold the midi data.
my $tmp_name = "/tmp/playpitch.pl-$$";

# Convert human-readable names into the format MIDI expects.
my $midi_pitch      = $MIDI::note2number{$pitch};  # todo: unused
my $midi_instrument = $MIDI::patch2number{$instrument};


## Following code taken from the MIDI::Simple perldoc page:

&new_score ();
&text_event ("A pitch: $pitch, $duration seconds, $instrument.\n");
&set_tempo (1000000);  # 1 qn => 1 seconds (1,000,000 microseconds)
&patch_change (1, $midi_instrument);  # For example, patch 8 = Celesta

&noop ("c1", "f", "o2");  # Setup.  (Why is this necessary?  -kff)

# n "d${duration}", $pitch;
&n ("qn", "$pitch");

&write_score ("${tmp_name}.mid");
