primus has asked for the wisdom of the Perl Monks concerning the following question:

hail monks,

well, i must say, you monks were so much help on my last post, i must tap some more of this bottomless knowledge...

OK, i am playing around with the Win32::MIDI module ( on a win32 machine ) and i wanted to know if it were possible to play a chord with this module? basically a snippet of the example is as follows:
my $midi_obj = Win32::MIDI->new(); # print number of available (writing) devices print $midi_obj->numDevices() . "\n"; # open a device $midi_obj->openDevice(0); # set default channel $midi_obj->cur_channel(1); # play a note by absolute value (numeric) # middle C - 60 # note,dur,velocity $midi_obj->play_note(60,2.5,127) || print $midi_obj->error() . "\n" an +d $midi_obj->reset_error();


there are some neat things you can do with this module, but i couldnt seem to find anything on playing chords... i was thinking maybe if you had two channels open each note would play on the each channel simultaneously, but that did not work... i dont know if this module is just not built to play chords, or what? but if you monks can suggest another module that can be used on win32 for chords, i would be much obliged.

thanks alot for the help!

Replies are listed 'Best First'.
Re: playing chords with MIDI modules...
by Mr. Muskrat (Canon) on Jan 20, 2003 at 19:30 UTC

    I got it to play a chord! It's not pretty, nor is it optimized. It is fairly easy to understand though.

    use Win32::MIDI; my $midi_obj = Win32::MIDI->new(); $midi_obj->openDevice(0); indiv(1, 3); # 1 second duration, start at middle C chord(3, 3); # 3 second duration, same chord sleep(1); $midi_obj->closeDevice(); # play_note(NOTE, DURATION, VELOCITY, CHANNEL, ON/OFF, OCTAVE) sub indiv { my $duration = shift; my $octave = shift; # play each note individually $midi_obj->play_note('C', $duration, 127, 1, 1, $octave) || print $midi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('E', $duration, 127, 1, 1, $octave) || print $midi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('G', $duration, 127, 1, 1, $octave) || print $midi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('C', $duration, 127, 1, 1, $octave+1) || print $midi_obj->error() . "\n" and $midi_obj->reset_error(); } sub chord { # get the duration to play the C chord my $duration = shift; # get the octave to play the C chord at my $octave = shift; # play the chord $midi_obj->play_note('C', $duration, 127, 1, 0, $octave) || print $midi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('E', $duration, 127, 1, 0, $octave) || print $midi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('G', $duration, 127, 1, 0, $octave) || print $midi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('C', $duration, 127, 1, 1, $octave+1) || print $midi_obj->error() . "\n" and $midi_obj->reset_error(); }

    Updated: Moved the individual notes into a different subroutine. Changed ON/OFF flag for final note of chord to 1. Added duration parameter to both subroutines.

      Addon to Mr. Muskrat's code:
      mary(3); sub mary { my $octave = shift; $midi_obj->play_note('E', ".5", 127, 1, 1, $octave) || print $midi_o +bj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('D', ".25", 127, 1, 1, $octave) || print $midi_ +obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('C', ".5", 127, 1, 1, $octave) || print $midi_o +bj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('D', ".5", 127, 1, 1, $octave) || print $midi_o +bj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('E', ".5", 127, 1, 1, $octave) || print $midi_o +bj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('E', ".5", 127, 1, 1, $octave) || print $midi_o +bj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('E', ".75", 127, 1, 1, $octave) || print $midi_ +obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('D', ".5", 127, 1, 1, $octave) || print $midi_o +bj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('D', ".5", 127, 1, 1, $octave) || print $midi_o +bj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('D', ".5", 127, 1, 1, $octave) || print $midi_o +bj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('E', ".5", 127, 1, 1, $octave) || print $midi_o +bj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('G', ".5", 127, 1, 1, $octave) || print $midi_o +bj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('G', ".5", 127, 1, 1, $octave) || print $midi_o +bj->error() . "\n" and $midi_obj->reset_error(); }
      Guess this tune...

      -----------------------
      Billy S.
      Slinar Hardtail - Hand of Dane
      Datal Ephialtes - Guildless
      RallosZek.Net Admin/WebMaster

      perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'
        Name this tune:
        song(3); sub song { my $octave = shift; my $num = 0; while ($num < 10) { $midi_obj->play_note('E', "1.25", 127, 1, 1, $octave) || print $ +midi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('G', ".5", 127, 1, 1, $octave) || print $mi +di_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('G#', ".25", 127, 1, 1, $octave) || print $ +midi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('B', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('E', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('D', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('D', "1", 127, 1, 1, $octave) || print $mid +i_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('B', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('F#', ".25", 127, 1, 1, $octave) || print $ +midi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('F', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('B', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('D', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('E', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $num++; } }
        Send me a message to see if your right...

        -----------------------
        Billy S.
        Slinar Hardtail - Hand of Dane
        Datal Ephialtes - Guildless
        RallosZek.Net Admin/WebMaster

        perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'
      Guess this song...
      song2(3); sub song2 { my $octave = shift; my $num = 0; while ($num < 10) { sleep(.5); $midi_obj->play_note('D', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('F', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('D', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('G', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('D', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('F', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); $midi_obj->play_note('D', ".25", 127, 1, 1, $octave) || print $m +idi_obj->error() . "\n" and $midi_obj->reset_error(); sleep(2); $num++; } }
      Send message to get answer...

      -----------------------
      Billy S.
      Slinar Hardtail - Hand of Dane
      Datal Ephialtes - Guildless
      RallosZek.Net Admin/WebMaster

      perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'
Re: playing chords with MIDI modules...
by Mr. Muskrat (Canon) on Jan 07, 2003 at 15:55 UTC

    I attempted to do it using the low level method and sending notes to three different channels without success but I'm no MIDI expert.

    use Win32::MIDI; my $midi_obj = Win32::MIDI->new(); $midi_obj->openDevice(0); ## Low-Level Method -- Writing Directly To The Device, ## Creating Your Own MIDI Messages my @on = ("\x00\127\127\x90","\x00\127\160\x91","\x00\127\090\x92"); + my @off = ("\x00\127\127\x80","\x00\127\160\x81","\x00\127\090\x82"); for (@on) { $midi_obj->writeMIDI(unpack("N",$_)); } sleep(3); for (@ff) { $midi_obj->writeMIDI(unpack("N",$_)); }

    Perhaps you should contact the author and see if he can help.

      thanks for the attempt, i think i will just email the creator...
Re: playing chords with MIDI modules...
by Mr. Muskrat (Canon) on Jan 07, 2003 at 23:55 UTC

    Win32::MIDI::API looks very interesting too.

    I think I'll wait until the author releases it for AS Perl though... (Which is mentioned in the Todo section of the POD.)

Re: playing chords with MIDI modules...
by primus (Scribe) on Jan 07, 2003 at 04:15 UTC
    well... i guess this cant be done, should i email the guy who wrote Win32::MIDI?