MIDI Keyboard with Sonic Pi No Audio

Hi,

I have my midi keyboard (keystation 88) connected to my computer. I would like to use it with Sonic Pi, but I cannot hear anything when I play the keyboard. I have followed the instructions for MIDI In provided by Sam Aaron in the Sonic Pi app, and everything seems to be connected correctly. I’ve provided a screenshot of what I see on my screen.
Also, I know it is not an issue with my speakers because I can play something simple (such as “play :C4”), and I hear that just fine. If someone could help me figure out what I’m doing wrong, I would greatly appreciate it!

It looks to me from the cues window that all the notes are coming in with velocity 0, so it will play the synth with amp 0 too. Try ignoring the velocity and just setting the amp to 1 in the synth call.

Hi Emlyn, thanks for getting back to me. I tried setting the amp to 1 and deleting the velocity part, but I still don’t hear anything. I also am noticing in the cues now that when I press one of the keys down, it registers the note and the correct velocity, but when I release the key, another cue comes up with the correct note but with a velocity of 0. So it looks as though there are two cues per key press, one for when its pressed and one for when its released. I’m not sure if that matters, but I included a picture of the cues as I repeatedly press note 86 for you to see.

Ah yes, it’s true that some (most?) keyboards send a note_on with velocity 0 instead of a note_off.
In that case I think that probably the midi message is not triggering the sync command for some reason, so the synth command never gets called.
What about it you change the sync to: sync "/midi*note_on"?

There’s another thread here that looks similar and might give some clues: Midi in events not triggering

Changing the sync to midi*note_on did not work either. Looking at that other thread, it seems that some people were having issues with this in version 3.2 but not 3.1. I will download 3.1 now and see if I can get it to work. Thank you for your help!

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.