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.


In reply to Re: playing chords with MIDI modules... by Mr. Muskrat
in thread playing chords with MIDI modules... by primus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.