NAME

Win32::MIDI::API - Perl extension for MS Windows 32bit MIDI API

SYNOPSIS

  use Win32::MIDI::API qw( /^(MIM_)/ );
  $midi = new Win32::MIDI::API;

  # MIDI::Out::ShortMsg
  $mo = new Win32::MIDI::API::Out	   or die $midi->OutGetErrorText();
  $mo->ShortMsg(0x00403C90)			or die $mo->GetErrorText();
  sleep(1);
  $mo->ShortMsg(0x00003C90)			or die $mo->GetErrorText();
  $mo->Close					or die $mo->GetErrorText();

  # MIDI::Out::LongMsg
  $mo = new Win32::MIDI::API::Out	   or die $midi->OutGetErrorText();
  # sysEx: Set Master Volume
  $m = "\xf0\x7f\x7f\x04\x01\x7f\x7f\xf7";
  $midiHdr = pack ("PL4PL6",
  		   $m,	# lpData
  		   length $m, # dwBufferLength
  		   0, 0, 0, undef, 0, 0);
  $mo->PrepareHeader(unpack('L!', pack('P',$midiHdr)))
			 			or die $mo->GetErrorText();
  $mo->LongMsg($lpMidiOutHdr)			or die $mo->GetErrorText();
  $mo->UnprepareHeader($lpMidiOutHdr)		or die $mo->GetErrorText();
  $mo->Close					or die $mo->GetErrorText();

  # MIDI::Stream
  $ms = new Win32::MIDI::API::Stream()	   or die $midi->OutGetErrorText();
  $buf = pack('L*',
	      # System Exclusive (Set Master Volume Full)
	      0,  0, (&MEVT_LONGMSG << 24) | 8, 0x047F7FF0, 0xF77F7F01,
	      # Short Messages
	      0,  0, 0x007F3C90,
	      48, 0, 0x00003C90);
  $midihdr = pack("PLLLLPLL",
		  $buf,	# lpData
		  length $buf,	# dwBufferLength
		  length $buf,	# dwBytesRecorded
		  0,		# dwUser
		  0,		# dwFlags
		  undef,	# lpNext
		  0,		# reserved
		  0);		# dwOffset
  $lpMidiHdr = unpack('L!', pack('P', $midihdr));
  $ms->PrepareHeader($lpMidiHdr)		or die $ms->GetErrorText();
  $ms->Out($lpMidiHdr)				or die $ms->GetErrorText();
  $ms->Restart()				or die $ms->GetErrorText();
  sleep(1);
  $ms->UnprepareHeader($lpMidiHdr)		or die $ms->GetErrorText();
  $ms->Close()					or die $ms->GetErrorText();

  # MIDI::In
  $mi = new Win32::MIDI::API::In(0, \&midiincallback, 0x1234)
					    or die $midi->InGetErrorText();
  sub midiincallback {
    my ($self, $msg, $instance, $param1, $param2) = @_;
    if ($msg == MIM_OPEN) {
  	  print "MIM_OPEN\n";
  	  ...
    } elsif ($msg == MIM_LONGDATA) {
  	  print "MIM_LONGDATA\n";
  	  ...
    }
  }
  $buf = "\0" x 1024;
  $midihdr = pack ("PLLLLPLL",
		   $buf,	# lpData
		   length $buf, # dwBufferLength
		   0,		# dwBytesRecorded
		   0xBEEF,	# dwUser
		   0,		# dwFlags
		   undef,	# lpNext
		   0,		# reserved
		   0);		# dwOffset
  $lpMidiInHdr = unpack('L!', pack('P', $midihdr));
  $mi->PrepareHeader($lpMidiInHdr)		or die $mi->GetErrorText();
  $mi->AddBuffer($lpMidiInHdr)			or die $mi->GetErrorText();
  $mi->Start					or die $mi->GetErrorText();
  $mi->Reset					or die $mi->GetErrorText();
  $mi->UnprepareHeader($lpMidiInHdr)		or die $mi->GetErrorText();
  $mi->Close()					or die $ms->GetErrorText();

  sub midiincallback {
      my ($self, $msg, $instance, $param1, $param2) = @_;
      printf "<<<0x%x,0x%x,0x%x,0x%x>>>\n", $msg, $instance, $param1, $param2;
      if ($msg == MIM_OPEN) {
  	  print "MIM_OPEN\n";
      } elsif ($msg == MIM_CLOSE) {
  	  print "MIM_CLOSE\n";
      } elsif ($msg == MIM_ERROR) {
  	  print "MIM_ERROR\n";
      } elsif ($msg == MIM_DATA) {
  	  print "MIM_DATA\n";
      } elsif ($msg == MIM_LONGDATA) {
  	  print "MIM_LONGDATA\n";
  	  my $midiHdr = unpack('P32', pack('L!', $param1));
  	  my @d = unpack('LL4LL2', $midiHdr);
  	  printf "lpData:%x,Buflen:%x,bytesrecorded:%d,dwUser:%x,dwFlags:%d\n",
  	      @d[0..4];
  	  datadump(unpack("P$d[2]", $midiHdr));
      } elsif ($msg == MIM_LONGERROR) {
  	  print "MIM_LONGERROR\n";
      } else {
  	  print "unknown message type\n";
      }
  }

DESCRIPTION

Overview

Win32::MIDI::API is a wrapper for MS Windows 32bit MIDI API. It supports all MS Windoews 32bit MIDI API, MIDI output, input, and stream API.

This module is still under development. Interface may be changed to improve usability.

Querying MIDI Devices

$midi = new Win32::MIDI::API;
$midi-InGetNumDevs()>
$midi-OutGetNumDevs()>
$midi-InGetDevCapss(DeviceID)>
$midi-OutGetDevCapss([DeviceID = MIDI_MAPPER])>

Playing MIDI

Opening and Closing MIDI Output Device

$midiOut = new Win32::MIDI::API::Out([DeviceID = MIDI_MAPPER, [Callback = undef, [CallbackInstance = undef, [Flags = CALLBACK_NULL]]]]);
$midiOut-GetID()>
$midiOut-Close()>

Sending Individual MIDI Messages

$midiOut-ShortMsg(Msg)>
$midiOut-PrepareHeader($lpMidiOutHdr)>

Preparing a header that has already been prepared has no effect, and the function returns zero.

After the header has been prepared, do not modify the buffer. To free the buffer, use the midiInUnprepareHeader function.

Before using this function, you must set the lpData, dwBufferLength, and dwFlags members of the MIDIHDR structure.

The dwFlags member must be set to zero.

A stream buffer cannot be larger than 64K.

$midiOut-UnprepareHeader($lpMidiOutHdr)>
$midiOut-LongMsg($lpMidiOutHdr)>

Misc.

$midiOut-GetErrorText([mmsyserr])>
$midiOut-Reset()>
$midiOut-Connect($midiOut1)>
$midiOut-Disconnect($midiOut1)>

Recording MIDI Audio

Opening and Closing MIDI Output Device

$midiIn = new Win32::MIDI::API::In(DeviceID, [Callback = undef, [CallbackInstance = undef, [Flags = CALLBACK_NULL]]]);
$midiIn-GetID()>
$midiIn-Close()>

Managing MIDI Recording

$midiIn-PrepareHeader($lpMidiInHdr)>

Preparing a header that has already been prepared has no effect, and the function returns zero.

After the header has been prepared, do not modify the buffer. To free the buffer, use the midiInUnprepareHeader function.

Before using this function, you must set the lpData, dwBufferLength, and dwFlags members of the MIDIHDR structure.

The dwFlags member must be set to zero.

$midiIn-UnprepareHeader($lpMidiInHdr)>
$midiIn-AddBuffer($lpMidiInHdr)>
$midiIn-Reset()>
$midiIn-Start()>
$midiIn-Stop()>
$midiIn-GetErrorText([$mmsyserr])>
$midiIn-Connect($midiOut1)>
$midiIn-Disconnect($midiOut1)>

Sending MIDI Messages with Stream Buffers

$midiStream = new Win32::MIDI::API::Stream([DeviceID = MIDI_MAPPER, [Callback = undef, [CallbackInstance = undef, [Flags = CALLBACK_NULL]]]]);
$midiStream-Close()>
$midiStream-PrepareHeader($lpMidiInHdr)>

Preparing a header that has already been prepared has no effect, and the function returns zero.

After the header has been prepared, do not modify the buffer. To free the buffer, use the midiInUnprepareHeader function.

Before using this function, you must set the lpData, dwBufferLength, and dwFlags members of the MIDIHDR structure.

The dwFlags member must be set to zero.

A stream buffer cannot be larger than 64K.

$midiStream-UnprepareHeader($lpMidiInHdr)>
$midiStream-Out($midiOutHdr)>
$midiStream-Restart()>
$midiStream-Pause()>
$midiStream-Stop()>
$midiStream-GetErrorText([$mmsyserr])>
$midiStream-Position($lpmmtime)>
$midiStream-Property($lppropdata, $dwProperty)>

EXPORT

None by default. The following constant values can be exported.

CALLBACK_EVENT CALLBACK_FUNCTION CALLBACK_NULL CALLBACK_TASK
CALLBACK_THREAD CALLBACK_TYPEMASK CALLBACK_WINDOW

MEVT_COMMENT MEVT_EVENTPARM MEVT_EVENTTYPE MEVT_F_CALLBACK MEVT_F_LONG
MEVT_F_SHORT MEVT_LONGMSG MEVT_NOP MEVT_SHORTMSG MEVT_TEMPO MEVT_VERSION

MIDICAPS_CACHE MIDICAPS_LRVOLUME MIDICAPS_STREAM MIDICAPS_VOLUME

MIDIMAPPER MIDIPATCHSIZE MIDIPROP_GET MIDIPROP_SET MIDIPROP_TEMPO
MIDIPROP_TIMEDIV MIDISTRM_ERROR MIDI_CACHE_ALL MIDI_CACHE_BESTFIT
MIDI_CACHE_QUERY MIDI_IO_STATUS MIDI_MAPPER MIDI_UNCACHE

MIXERLINE_TARGETTYPE_MIDIIN MIXERLINE_TARGETTYPE_MIDIOUT
MIXER_OBJECTF_HMIDIIN MIXER_OBJECTF_HMIDIOUT
MIXER_OBJECTF_MIDIIN MIXER_OBJECTF_MIDIOUT

MIDIERR_BADOPENMODE MIDIERR_BASE MIDIERR_DONT_CONTINUE
MIDIERR_INVALIDSETUP MIDIERR_LASTERROR MIDIERR_NODEVICE MIDIERR_NOMAP
MIDIERR_NOTREADY MIDIERR_STILLPLAYING MIDIERR_UNPREPARED

MMSYSERR_ALLOCATED MMSYSERR_BADDB MMSYSERR_BADDEVICEID
MMSYSERR_BADERRNUM MMSYSERR_BASE MMSYSERR_DELETEERROR MMSYSERR_ERROR
MMSYSERR_HANDLEBUSY MMSYSERR_INVALFLAG MMSYSERR_INVALHANDLE
MMSYSERR_INVALIDALIAS MMSYSERR_INVALPARAM MMSYSERR_KEYNOTFOUND
MMSYSERR_LASTERROR MMSYSERR_NODRIVER MMSYSERR_NODRIVERCB
MMSYSERR_NOERROR MMSYSERR_NOMEM MMSYSERR_NOTENABLED
MMSYSERR_NOTSUPPORTED MMSYSERR_READERROR MMSYSERR_VALNOTFOUND
MMSYSERR_WRITEERROR

MM_MIM_CLOSE MM_MIM_DATA MM_MIM_ERROR MM_MIM_LONGDATA
MM_MIM_LONGERROR MM_MIM_MOREDATA MM_MIM_OPEN

MIM_CLOSE MIM_DATA MIM_ERROR MIM_LONGDATA
MIM_LONGERROR MIM_MOREDATA MIM_OPEN

MM_MOM_CLOSE MM_MOM_DONE MM_MOM_OPEN MM_MOM_POSITIONCB

MOM_CLOSE MOM_DONE MOM_OPEN MOM_POSITIONCB

TIME_CALLBACK_EVENT_PULSE TIME_CALLBACK_EVENT_SET
TIME_CALLBACK_FUNCTION TIME_MIDI

MOD_MIDIPORT MCIERR_SEQ_NOMIDIPRESENT MCI_SEQ_MIDI

AUTHOR

Hiroo Hayashi, <hiroo.hayashi@computer.org>

SEE ALSO

MICROSOFT Developer Network
http://msdn.microsoft.com/library/

TODO

port and test on Active Perl.

BUGS

If you find bugs, report to the author. Thank you.