Ways to enter data

Is there any way that the program asks for some data that is usable for the code itself?
For ex:
Select between 1 or 2: and depending on the answer, a certain code is triggered

Hi Skyggen. If you mean, can you make Sonic Pi ask the user for input, and respond to this in some way: not really. Sonic Pi is really just a text editor, rather than an interactive command line interface.

Although, you can do something similar, because Sonic Pi can respond to input - just not directly ask for text input like on a command line. What you can do is use a separate program or device to send an event to Sonic Pi, which it then responds to. Sonic Pi can receive and send MIDI events, and OSC (Open Sound Control) events.
(See: Sonic Pi - Tutorial and Sonic Pi - Tutorial).

Does that help answer your question? If you have a particular goal in mind where you wanted to use Sonic Pi in this way, you could describe it to us in more detail. I’m sure there’d be someone that might have helpful advice :slight_smile:

1 Like

hi @skyggen

Yes this is the way you will have to explore to interact with the user. You can use open stage control to create a input text field and then use the osc answer into Sonic Pi. If i have time i will try to give you an example. See you

1 Like

I did something similar where I used a midi keyboard and then told the user which notes correspond to which actions. I might provide an example when I get access to my computer again

Edit: Nvm. I fail to come up with a good example.

Hi there,

So this is my little contribution for a way to get user data (here a word) from an input field designed into open-stage-control to use them into sonicpi

the sonic pi part

# nlb -- 18-10-2021
# open stage control : https://openstagecontrol.ammd.net/download/
# User type some special words into an input field from open stage control
# then sonic pi triggers actions
# o-s-c in this document is for the long word open-stage-control :-)

# o-s-c is installed on the same host, same pc and receive osc messages on port 7777
use_osc "" , 7777

# to display the available words
set :default_msg, "Type one of these words :
roll, crow, piano, jingle"

# Get the user attention
messages = (ring "Hey you !",
            "Sonic Pi is talking to you",

use_bpm 60

(messages.length).times do
  # we send to o-s-c
  osc "/msg", messages.tick
  sleep 2 # 2 seconds as  bpm set to 60
# reset the input field
osc "/msg/input", "  "

define :jingle do
  use_bpm 240
  use_synth :pretty_bell
  n = scale :c, :diminished2
  use_octave +1
  8.times do
    play n.pick, pan: [-1,1].tick
    sleep 0.25

live_loop :detectUserInput do
  n, v = sync "/osc:"
  puts "I received a msg !"
  puts n
  case n
  when "roll"
    sample :drum_roll
    sleep 1
  when "crow"
    sample :misc_crow
    sleep 1
  when "piano"
    use_synth :piano
    use_bpm 120
    s = scale :c, 'major'
    (s.length).times do
      play s.tick
      sleep 0.25
  when "jingle"
    2.times do
      sample :bass_drop_c, finish: 0.15, rate: -2
      sleep 0.25
    use_bpm 60
    osc "/msg", "hum i don't know what to do"
    sleep 2
    osc "/msg", get[:default_msg]
    sleep 1

You will need to install open-stage-control then load the json file provided into this repo :slight_smile:

a video to illustrate

Hope it can help !


that was precisely what I was looking for, it enlightened me a lot ty


you are awesome, buddy, thanks a lot

1 Like