MIDI Keyboard with Sonic Pi No Audio

I was able to get it to work in version 3.1. Must be a bug in version 3.2. Thank you again for your help!

1 Like

good evening @ahuffman24,

Maybe add use_real_time at the start of your live_loop.

live_loop :fromMidiDevice do
  
  use_real_time
  n, vel = sync "/midi:apc_key_25_midi_1:4:2/note_on"
  synth :piano, note: n, amp: 1 
  
end

my midi keyboard sends note_off when a key is released so i can’t test as with your own keyboard.
Just to warn you that for me the latence is too big to play “live”.

1 Like

I will try this in version 3.2 and see if it works. And I agree, even when using “use_real_time” in version 3.1, the latency is too big. Thank you!

@ahuffman24: just to clarify, when you refer to 3.2, do you mean 3.2.2? (This is the latest release for Sonic Pi).

emlyn you need "/midi*/note_on" "/midi*note_on" will not work

I would use something like this.

use_synth :piano
live_loop :midi_piano do
  use_real_time
  n,v = sync "/midi*/note_on"
  play n,amp: v/127.0,sustain: 0.2 if v>0
end

This works fine on 3.2.2 on my Pi4

2 Likes

Yes, I meant version 3.2.2! My bad!

Great! I will give that a try

Hi @robin.newman

your code works perfectly for me on windows 10 with sonic pi 3.2.2 and the latency is weak so i can really play via the midi keyboard
Now i wonder why the following code is not working

live_loop :fromMidiDevice do
  
  use_real_time
  n, vel = sync "/midi*/note_on"
  synth :dsaw, note: n, amp: 1
  
end

Thanks

Hmm. It works for me on 3.2.2 on a Windows 10 machine. I would suggest however that you add if vel > 0 at the end of the play line. Otherwise you will get a double note as many keyboards use note_on with velocity 0 when the key is released. Using the test eliminates the cue when the note is released.

live_loop :fromMidiDevice do
  
  use_real_time
  n, vel = sync "/midi*/note_on"
  synth :dsaw, note: n, amp: 1 if vel > 0
  
end

ok my bad. it works. Have tested too fast :slight_smile:. sorry for the inconvenience…

There is currently a bug in v3.2 when using multiple : in a sync path. This has been fixed on the main branch and will be fixed in the upcoming betas and next release. Apologies.

Until then, you can use the * fix as detailed above to mask the error :slight_smile:

2 Likes

Thanks, Robin!
Works with my yamaha 88

1 Like
use_real_time

in_thread do
  while(1) do
      note, vel = sync "/midi:midi_through_port-0:0:8/note_on"
      play note, amp: vel/127.0
    end
  end

works for me on v3.2.2, Linux 5.4.0-47-generic #51~18.04.1-Ubuntu x86_64 but obviously there are several colons : in the sync path. Am I missing something?

looking at your code
sync "/midi:midi_through_port-0:0:8/note_on"
This will sync to a midi cue on port midi_through_port-0 which has an id 0 and is on channel 8
If you don’t care about the port, or the (related) id number or the channel using
sync "/midi*/note_on" to respond to a note_on command received on ANY connected port on ANY connected channel.
You could use
sync "/midi*:2/midi_on" to react to a midi_on command on ANY port but ONLY on channel 2
or sync "/midi:usb_oxygen_8_v2:*/note_on" to react to midi input on ANY channel but only from my oxygen 8 midi keyboard.
Thus in most simple setups the sync "/midi*/note_on" will suffice

Thats right, I just wondered about the fact that Sam said there was a bug with several colons but it works for me.

My external midi keyboard works fine with Sonic Pi Beta 4.0, but not 3.3.1

I see the keyboard listed in Preferences under MIDI ports, but there is no activity in the log.

Just curious if there’s also an issue in v 3.3.1?

Which platform are you using 3.3.1 on? If on a Raspberry Pi on the latest raspberrypi OS (30th October 2021) you need version 3.3.1_2 downloadable from the sonic-pi.net website.

1 Like

Windows 11.

I hadn’t noticed there was a v 3.3.2 and so I installed it, but MIDI in still doesn’t work. I can use beta 4 for now.

OK what I was talking about was a binary version ONLY for Raspberry Pi.

OK, thanks. For what it’s worth, osc has the same issue. Doesn’t work for me on Win 11 v3.2.2, but works fine on beta 4