Wiimote Midi & Sonic Pi

Hello everyone!

I’m using a wii remote as a midi controller with glovepie and i am trying to get samples to sync. the thing is, the wii remote has some latency, so i’m wondering if there’s a way to trigger a sample via midi but rather than it playing after it’s triggered, it plays in beat with the main loop. As of now, the sample plays usually between 1-2 seconds after i push the button so i’m looking to see if there’s a solution that could cue a sample in the bpm. Below is my code. any help would be much appreciated :slight_smile:

bpm = 135

# trigger sample once
define :play_sample_once do |sample_name|
  in_thread do
    sample sample_name
    sleep sample_duration(sample_name)
  end
end

# trigger infinite loop sample
define :play_looping_sample do |sample_name|
  in_thread do
    loop do
      sample sample_name
      sleep sample_duration(sample_name)
    end
  end
end

# control loop to trigger the start of sample loops
live_loop :midi_input do
  midi_cc_samples = {
    20 => "C:/Users/elimc/OneDrive/Desktop/housepop/hp_drums.wav", # Home
    21 => "C:/Users/elimc/OneDrive/Desktop/housepop/hp_lead.wav", # Plus
    22 => "C:/Users/elimc/OneDrive/Desktop/housepop/hp_lead2.wav", # Minus
    23 => "C:/Users/elimc/OneDrive/Desktop/housepop/hp_chords.wav", # A
    24 => "C:/Users/elimc/OneDrive/Desktop/housepop/hp_arp.wav", # B
    25 => "", # Up
    26 => "", # Down
    27 => "", # Left
    28 => "", # Right
    31 => "", # One
    32 => "", # Two

  }
  
  cc, value = sync "/midi:wiimotemidi_0:2/control_change"
  
  if midi_cc_samples.key?(cc) && value == 127
    if cc == 20
      play_looping_sample midi_cc_samples[cc]  # loop the first sample indefinitely
    else
      play_sample_once midi_cc_samples[cc]  # trigger other samples when button pushed
    end
    sleep 0.5
  end
end```
1 Like

try adding use_real_time at the beginning of the live_loop :midi_input do
I set this up on my Mac with a different midi cc source and it seemed to work pretty well.
Also you don’t need the sleep 0.5 in the midi_input loop. The loop pauses until it gets another midi button pus detected. The 0.5 can slow it down if you have short samples.