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

Hi, I'm trying to get MIDI::Realtime to work. It's an old but simple module. I'm using Perl 5.8.3. and ALSA 1.0.4

Anyways the module seems to install OK, but when I try to run the module's sample code, I get an error:

"Couldn't write to /dev/sequencer, bad file descriptor at (....)Realtime.pm line 201.
So the code block generating the error is:
sub send_midi_bytes { my $self = shift; my $device = $self->midi_device; my $stuff = pack('C*', (map {&SEQ_MIDIPUTC(), $_, $device, 0 } @_ ) ); my $bytes = syswrite(SEQ_FH, $stuff); if (not $bytes) { die("Couldn't write to " . $self->dev . ": $!" ); } return $bytes; }
and the sample code I'm trying to run is
#!/usr/bin/perl use warnings; use strict; use MIDI::Realtime; my $midi = MIDI::Realtime->new(dev=>'/dev/sequencer', midi_device=> 0); # Play note 47 with maximum velocity on channel 1 $midi->note(47, 127, 1); # Now have some fun with randomness my @notes = (37 .. 50); # use all the channels (with extra drums) my @channels = (1 .. 16, 10, 10, 10); my @velocities = (70 .. 100); for (0 .. 127) { $midi->note($notes[rand(@notes)], $channels[rand(@channels)], $velocities[rand(@velocities)] ); # Wait for a tenth of a second select(undef,undef,undef, 0.10); }
So anyone know how to get the right file descriptor?

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re: /dev/sequencer bad file descriptor
by BrowserUk (Patriarch) on Jun 19, 2004 at 16:09 UTC

    Maybe if you add an  or die... to the sysopen in the new() method, you'll see why the filehandle SEQ_FH isn't valid?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re: /dev/sequencer bad file descriptor
by Roger (Parson) on Jun 19, 2004 at 16:54 UTC
    Two possiblilities I can think of immediately:

    1) Well, did you install driver module for /dev/sequencer on your machine?

    2) Ummm, how about the permission of /dev/sequencer? Perhaps it's accessible only to root.

Re: /dev/sequencer bad file descriptor
by zentara (Cardinal) on Jun 19, 2004 at 21:55 UTC
    As BrowserUK suggested, I put a die statement on the open, and found that /dev/sequencer didn't exist, although it's there, and the permissions are OK.

    I started to think that MIDI::Realtime was written for OSS and it isn't quite compatible with ALSA's OSS compatibility mode.

    I did is some WildAs_ experimenting and changed /dev/sequencer to /dev/amidi and it "seemed to run" without error, and I could hear an elevated level of static in my FM radio, so I think it's putting out, except there is no output to the soundcard.

    It would seem that it should be simple to write to the sequencer? The utilities which come with alsa work well, but they seem to rely on port numbers, which I can't seem to fit anywhere in MIDI::Realtime.


    I'm not really a human, but I play one on earth. flash japh
Re: /dev/sequencer bad file descriptor
by zentara (Cardinal) on Jun 20, 2004 at 17:18 UTC
    For those who are interested, I finally got it to work!! Yeah. Trail and error methodology....but who cares?

    The problem seemed to be the midi_device needs to be 1,2,3,or 4 for my SBlive. Which corresponds to ports 65:0 , 65:1 , 65:2 , 65:3 . The midi_device defaults to 0 which is probably 64:0, and is the kernel MPU. Anyways the following works for my SBlive. :-) (Now to get it to write to my USB midi interface)

    UPDATE: Upon further testing, I found that my problem was caused by interference from the usb-hotplug interface for my UM-1 Midikeyboard connector. MIDI::Realtime works as described below, if hotplug is NOT started. If hotplug is started, the "bad file descriptor" error reappears. It was a funny bug, which really threw me off in my troubleshooting.

    #!/usr/bin/perl use warnings; use strict; use MIDI::Realtime; use diagnostics; my $midi = MIDI::Realtime->new(dev=>'/dev/sequencer', midi_device=> 1); # 1,2,3 or4 # Play note 47 with maximum velocity on channel 1 $midi->note(47,1,127); # Now have some fun with randomness my @notes = (37 .. 50); # use all the channels (with extra drums) my @channels = (1 .. 16, 10, 10, 10); my @velocities = (70 .. 100); for (0 .. 127) { $midi->note($notes[rand(@notes)], $channels[rand(@channels)], $velocities[rand(@velocities)] ); # Wait for a tenth of a second select(undef,undef,undef, 0.10); }

    I'm not really a human, but I play one on earth. flash japh