| 1 | package MIDI::Realtime; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | |||||||||||||||||||||||
| 6 | $VERSION = '0.01'; | ||||||||||||||||||||||
| 7 | |||||||||||||||||||||||
| 8 | 1 | ▶ | use Fcntl; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | require 'sys/soundcard.ph'; | ||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | MIDI::Realtime - Writes MIDI data to /dev/sequencer in real time | ||||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | =head1 WARNING | ||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | This is heavily experimental software and may change at any moment. | ||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | Use this software at your own risk. It works fine for me, but could do | ||||||||||||||||||||||
| 20 | anything to you, your pets or your computer. | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | MIDI::Realtime aims to be a handy utility for handing the output, and | ||||||||||||||||||||||
| 25 | one day soon, the input of MIDI data. | ||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | This module only writes MIDI data at the moment. There are many areas | ||||||||||||||||||||||
| 28 | of expansion - for example, timing, and reading MIDI data is yet to be | ||||||||||||||||||||||
| 29 | implemented, and people could make subclasses for sending system | ||||||||||||||||||||||
| 30 | exclusive data to their particular synthesiser. | ||||||||||||||||||||||
| 31 | |||||||||||||||||||||||
| 32 | Please contact me if you have any ideas for this. In fact, I'd like to | ||||||||||||||||||||||
| 33 | hear from you if you get any use out of it at all. | ||||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | For an example of how I'm using this module, see | ||||||||||||||||||||||
| 36 | http://www.slab.org/void/music/ | ||||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 39 | |||||||||||||||||||||||
| 40 | use MIDI::Realtime; | ||||||||||||||||||||||
| 41 | |||||||||||||||||||||||
| 42 | my $midi = MIDI::Realtime->new(); | ||||||||||||||||||||||
| 43 | |||||||||||||||||||||||
| 44 | # Play note 47 with maximum velocity on channel 1 | ||||||||||||||||||||||
| 45 | $midi->note(47, 127, 1); | ||||||||||||||||||||||
| 46 | |||||||||||||||||||||||
| 47 | # Now have some fun with randomness | ||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | my @notes = (37 .. 50); | ||||||||||||||||||||||
| 50 | # use all the channels (with extra drums) | ||||||||||||||||||||||
| 51 | my @channels = (1 .. 16, 10, 10, 10); | ||||||||||||||||||||||
| 52 | my @velocities = (70 .. 100); | ||||||||||||||||||||||
| 53 | |||||||||||||||||||||||
| 54 | for (0 .. 127) { | ||||||||||||||||||||||
| 55 | $midi->note($notes[rand(@notes)], | ||||||||||||||||||||||
| 56 | $channels[rand(@channels)], | ||||||||||||||||||||||
| 57 | $velocities[rand(@velocities)] | ||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||
| 59 | |||||||||||||||||||||||
| 60 | # Wait for a tenth of a second | ||||||||||||||||||||||
| 61 | select(undef,undef,undef, 0.10); | ||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | =head1 FUNCTIONS | ||||||||||||||||||||||
| 65 | |||||||||||||||||||||||
| 66 | =over 4 | ||||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | =item B<new>() | ||||||||||||||||||||||
| 69 | |||||||||||||||||||||||
| 70 | my $midi = MIDI::Realtime->new(dev => '/dev/sequencer', | ||||||||||||||||||||||
| 71 | midi_device => 0 | ||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | This is the object constructor. It has two, optional parameters - 'dev' | ||||||||||||||||||||||
| 75 | the device file to be written to (default is '/dev/sequencer'), and | ||||||||||||||||||||||
| 76 | 'midi_device' is the MIDI device to use (default is 0). | ||||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | 0 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||
| 81 | 0 | my ($pkg, %p) = @_; | |||||||||||||||||||||
| 82 | |||||||||||||||||||||||
| 83 | my $self = {_dev => ($p{dev} || '/dev/sequencer'), | ||||||||||||||||||||||
| 84 | 0 | ▶ | _midi_device => ($p{midi_device} || 0 ) | ||||||||||||||||||||
|
Condition0%0/4
$p{'dev'} || '/dev/sequencer'
$p{'midi_device'} || 0
MC/DC0%0/1
$p{'dev'} || '/dev/sequencer'
$p{'dev'}
MC/DC0%0/1
$p{'midi_device'} || 0
$p{'midi_device'}
Truth table0%0/2
A
A= $p{'dev'}
Truth table0%0/2
A
A= $p{'midi_device'}
| |||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||
| 86 | |||||||||||||||||||||||
| 87 | 0 | bless $self, $pkg; | |||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | 0 | sysopen(SEQ_FH, $self->dev(), O_WRONLY); | |||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | 0 | return $self; | |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | =item B<devices>() | ||||||||||||||||||||||
| 95 | |||||||||||||||||||||||
| 96 | Not yet written. Might one day return information about available MIDI | ||||||||||||||||||||||
| 97 | devices somehow. | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | 0 | ▶ | sub devices { | ||||||||||||||||||||
|
Subroutine
devices: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 102 | # TODO | ||||||||||||||||||||||
| 103 | #ioctl(SEQ, SNDCTL_SEQ_NRMIDIS, blah); | ||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||
| 105 | |||||||||||||||||||||||
| 106 | =back | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | =head1 METHODS THAT SEND MIDI | ||||||||||||||||||||||
| 109 | |||||||||||||||||||||||
| 110 | All the following methods return the number of bytes sent - ie 0 if something | ||||||||||||||||||||||
| 111 | unexpectedly evil happened. | ||||||||||||||||||||||
| 112 | |||||||||||||||||||||||
| 113 | =over 4 | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | =item B<patch>() | ||||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | $midi->patch(2); | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | Sends a patch change request to the MIDI device. Just pass it the new | ||||||||||||||||||||||
| 120 | patch number. | ||||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||
| 123 | |||||||||||||||||||||||
| 124 | 0 | ▶ | sub patch { | ||||||||||||||||||||
|
Subroutine
patch: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 125 | 0 | my ($self, $patch_no) = @_; | |||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | 0 | ▶ | die "patch method is writeonly at the moment" if not defined $patch_no; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 128 | |||||||||||||||||||||||
| 129 | 0 | return $self->send_midi_bytes(0xc0, $patch_no); | |||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||
| 131 | |||||||||||||||||||||||
| 132 | =item B<song>() | ||||||||||||||||||||||
| 133 | |||||||||||||||||||||||
| 134 | $midi->song(6); | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | Sends a patch change request to the MIDI device. Just pass it the new | ||||||||||||||||||||||
| 137 | song number. This didn't seem to make my synth do what I wanted it to - | ||||||||||||||||||||||
| 138 | change 'performance'. My guess is I have to send it sysex data to do that, | ||||||||||||||||||||||
| 139 | and that this is just for sequencers. Let me know if you find that this | ||||||||||||||||||||||
| 140 | method works for you. | ||||||||||||||||||||||
| 141 | |||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||
| 143 | |||||||||||||||||||||||
| 144 | 0 | ▶ | sub song { | ||||||||||||||||||||
|
Subroutine
song: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 145 | 0 | my ($self, $song_no) = @_; | |||||||||||||||||||||
| 146 | |||||||||||||||||||||||
| 147 | 0 | ▶ | die "song method is writeonly at the moment" if not defined $song_no; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | 0 | return $self->send_midi_bytes(0xf3, $song_no); | |||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||
| 151 | |||||||||||||||||||||||
| 152 | =item B<note>() | ||||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | $midi->note(40, 10, 100) | ||||||||||||||||||||||
| 155 | |||||||||||||||||||||||
| 156 | Takes three parameters. The first is the note number, and must be | ||||||||||||||||||||||
| 157 | supplied. The second is the channel number, and defaults to 1. The third | ||||||||||||||||||||||
| 158 | is the velocity and defaults to 127. | ||||||||||||||||||||||
| 159 | |||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||
| 161 | |||||||||||||||||||||||
| 162 | 0 | ▶ | sub note { | ||||||||||||||||||||
|
Subroutine
note: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||
| 163 | 0 | my ($self, $note_no, $channel, $velocity) = @_; | |||||||||||||||||||||
| 164 | |||||||||||||||||||||||
| 165 | 0 | ▶ | die "Missing note parameter" if not defined $note_no; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 166 | |||||||||||||||||||||||
| 167 | 0 | ▶ | $channel = 1 if not defined $channel; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 168 | 0 | ▶ | $velocity = 127 if not defined $velocity; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 169 | |||||||||||||||||||||||
| 170 | 0 | return $self->send_midi_bytes(0x90 + $channel - 1, $note_no, $velocity); | |||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||
| 172 | |||||||||||||||||||||||
| 173 | =item B<send_midi_bytes>() | ||||||||||||||||||||||
| 174 | |||||||||||||||||||||||
| 175 | $midi->send_midi_bytes(0x90, 40, 100); | ||||||||||||||||||||||
| 176 | |||||||||||||||||||||||
| 177 | This method sends the given bytes to the MIDI device. It is intended for | ||||||||||||||||||||||
| 178 | only internal use and experiments. If you find some nice bytes to send, | ||||||||||||||||||||||
| 179 | please let me know, or put it in MIDI::Realtime.pm and send me a patch. | ||||||||||||||||||||||
| 180 | |||||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||||
| 182 | |||||||||||||||||||||||
| 183 | 0 | ▶ | sub send_midi_bytes { | ||||||||||||||||||||
|
Subroutine
send_midi_bytes: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 184 | 0 | my $self = shift; | |||||||||||||||||||||
| 185 | |||||||||||||||||||||||
| 186 | 0 | my $device = $self->midi_device; | |||||||||||||||||||||
| 187 | |||||||||||||||||||||||
| 188 | my $stuff = pack('C*', | ||||||||||||||||||||||
| 189 | 0 | (map {&SEQ_MIDIPUTC(), | |||||||||||||||||||||
| 190 | $_, | ||||||||||||||||||||||
| 191 | $device, | ||||||||||||||||||||||
| 192 | 0 | ||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||
| 194 | @_ | ||||||||||||||||||||||
| 195 | ) | ||||||||||||||||||||||
| 196 | ); | ||||||||||||||||||||||
| 197 | |||||||||||||||||||||||
| 198 | 0 | my $bytes = syswrite(SEQ_FH, $stuff); | |||||||||||||||||||||
| 199 | |||||||||||||||||||||||
| 200 | 0 | ▶ | if (not $bytes) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 201 | 0 | die("Couldn't write to " | |||||||||||||||||||||
| 202 | . $self->dev | ||||||||||||||||||||||
| 203 | . ": $!" | ||||||||||||||||||||||
| 204 | ); | ||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||
| 206 | |||||||||||||||||||||||
| 207 | 0 | return $bytes; | |||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||
| 209 | |||||||||||||||||||||||
| 210 | =back | ||||||||||||||||||||||
| 211 | |||||||||||||||||||||||
| 212 | =head1 ACCESSOR METHODS | ||||||||||||||||||||||
| 213 | |||||||||||||||||||||||
| 214 | =over 4 | ||||||||||||||||||||||
| 215 | |||||||||||||||||||||||
| 216 | =item B<dev>(), B<midi_device>() | ||||||||||||||||||||||
| 217 | |||||||||||||||||||||||
| 218 | These two methods return the values that were supplied (or defaulted) when | ||||||||||||||||||||||
| 219 | you first called new(). They are read only. | ||||||||||||||||||||||
| 220 | |||||||||||||||||||||||
| 221 | =cut | ||||||||||||||||||||||
| 222 | |||||||||||||||||||||||
| 223 | 0 | ▶ | sub dev { $_[0]->{_dev} } | ||||||||||||||||||||
|
Subroutine
dev: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 224 | 0 | ▶ | sub midi_device { $_[0]->{_midi_device} } | ||||||||||||||||||||
|
Subroutine
midi_device: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 225 | |||||||||||||||||||||||
| 226 | =back | ||||||||||||||||||||||
| 227 | |||||||||||||||||||||||
| 228 | =head1 AUTHOR | ||||||||||||||||||||||
| 229 | |||||||||||||||||||||||
| 230 | Alex McLean - alex@slab.org | ||||||||||||||||||||||
| 231 | |||||||||||||||||||||||
| 232 | =cut | ||||||||||||||||||||||
| 233 | |||||||||||||||||||||||
| 234 | ## | ||||||||||||||||||||||
| 235 | |||||||||||||||||||||||
| 236 | 1; | ||||||||||||||||||||||