Ultrasonic theremin

Playing with Raspberry, Python and SonicPi.
Building a UltraSonic theramin Setup with Raspberry 3B+ running on and Audio Jack Output.

First steps at projects.raspberry.org. This will not run from scratch, here we go…

  • If you use Raspbian Buster and SonicPi 3.1 refused to start,
    try to update your SonicPi from the offical Site https://sonic-pi.net/ (see bug-report)
    The offical DEB will also need to install pulseaudio-module-jack erlang-base
  • The Python-Script from the Project-Site needs a little update to work
from gpiozero import DistanceSensor
from time import sleep

from pythonosc import osc_message_builder
from pythonosc import udp_client

sensor = DistanceSensor(echo=17, trigger=4)
sender = udp_client.SimpleUDPClient('127.0.0.1', 4560)

while True:
    pitch = round(sensor.distance * 100 + 30)
    sender.send_message('/play_this', pitch)
    sleep(0.1)
#Based on Sonic Pi theremin using time of flight sensor
#smooth continuous note pitch version
#Original by Robin Newman, August 2018 updated August 2020
#works on Mac or Pi3
with_fx :reverb,room: 0.8,mix: 0.7 do
  use_synth :fm
  #start long note at zero vol. It will be controlled by k later
  k = play 0,sustain: 10000,amp: 0
  set :k,k #store k pointer in time-state as :k
  live_loop :theremin do
    use_real_time
    b = sync "/osc*/play_this" #syntax updated
    v= (b[0].to_f) #scale and convert range data to a float
    puts v #put value on screen
    if v<=60
      #control the note retrieving :k pointer and adjusting pitch and amp
      control get(:k),note: v+47.9,amp: 0.7,amp_slide: 0.04,note_slide: 0.04
    else
      #if range too high set vol to 0
      control get(:k),amp: 0,amp_slide: 0.04
    end
  end

end #fx reverb

The premier show 🙂

Thank you for reading and

Have a look at rbnpi’s Homepage for more complex examples