Skip to content

Instantly share code, notes, and snippets.

@dagon666
Last active December 25, 2015 19:39
Show Gist options
  • Save dagon666/7029630 to your computer and use it in GitHub Desktop.
Save dagon666/7029630 to your computer and use it in GitHub Desktop.
mid_converter processing loop
while (1) {
my @alsa_event = MIDI::ALSA::input();
# last event -> exit
last if ($alsa_event[0] == SND_SEQ_EVENT_PORT_UNSUBSCRIBED());
# midi event to midi score event
my @score_event = MIDI::ALSA::alsa2scoreevent( @alsa_event );
# filter out empty events
next unless (@score_event && defined $score_event[0] && defined $score_event[4]);
# accept only note events
next unless ('note' eq $score_event[0]);
print Dumper \@score_event;
# accept only events on specified channel
next unless (scalar @ARGV == 0 || grep { $_ == $score_event[3] } @ARGV );
# calculate the pitch and duration
# some offset since Arduino can't play notes lower than 31 Hz
my $note = int($notes[($score_event[4] % 96) + 32]);
# my $dur = int( $score_event[2]/1000 - 0.5);
my $dur = 160;
my $send = $port->write(slip_send($note, $dur));
while (!$port->write_drain) {}
my ($count_in, $string_in) = $port->read(5);
print "Sent: [$note/$dur] Recv [$count_in]: " . unpack "H*", $string_in;
print "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment