On Fri, Jan 01, 2021 at 03:20:09PM -0500, Vadim Belman wrote:
> As it seems that Audio::PortMIDI lacks non-blocking interface, I think a 
> solution would be to read events in a dedicated thread and re-submit them 
> into a Supplier. Something like:
> 
> my Supplier $midi-events;
> 
> start {
>     loop {
>         my $ev = $midi.read;
>         $midi-events.emit: $ev;
>     }
> }
> 
> 
> $midi-events can then be used in your react block. BTW, I think calling 
> method 'poll' must not be needed because `read` should block until actual 
> event is available. At least this is how I understand the normal order of 
> things.

Vadim,

Thank you for your reply.  I tried out not calling $midi.poll,
but found that $midi.read would return undef events (rather than
blocking until it received an event).  So I added it back in.
Thank you for the example of how to set up a supply.


On Fri, Jan 01, 2021 at 02:51:57PM -0600, Brad Gilbert wrote:
> I think the simplest way to turn that into a Supply is to use the `supply`
> keyword
> 
>     my $pm = Audio::PortMIDI.new;
> 
> 
>     my $input = supply {
>         my $stream = $pm.open-input($input-device-number, 32);
> 
>         DONE {
>             $stream.close;
>         }
> 
>         loop {
>                 emit $stream.read(1);
>         }
>     }
> 
> 
>     react {
>         whenever key-pressed(:!echo) {
>             given .fc {
>                 when 'q' { done }
>                 default { .raku.say }
>             }
>         }
> 
>         my $voice = $pm.open-output($output-device-number, 32);
>         whenever $input {
>             $voice.write(|$_);
>         }
>     }

Brad,

Thank you for your reply.  I was able to get my code working
thanks to the examples from you and Vadim.

Here is what I ended up with:

    my $pm = Audio::PortMIDI.new;

    # Set up supply to read MIDI events.
    my $input = supply {
        my $stream = $pm.open-input($input-device-number, 32);

        LAST {
            $stream.close;
        }

        loop {
            emit $stream.read(1) if $stream.poll;
        }
    }

    # Set up handle to send MIDI events to the synthesizer.
    my $voice = $pm.open-output($output-device-number, 32);

    react {
        # Read key presses from the computer keyboard.
        # TODO: change $voice as needed based on computer keyboard input.
        whenever key-pressed(:!echo) {
            given .fc {
                when 'q' { done }
                default { .raku.say }
            }
        }

        # Read MIDI events and send to the synthesizer.
        whenever $input {
            $voice.write(|$_);
        }
    }

-kolibrie

Attachment: signature.asc
Description: PGP signature

Reply via email to