Sending Generated Notes as OSC Int

Hello,

I’m trying to send notes as integers via OSC but I don’t know how to access my notes data generated by my play method. Must be simple but I’m new. Thanking you in advance.

tisane

use_osc “127.0.0.1”, 8000

live_loop :shapes do
use_synth :sine
notes = play (scale :e3, :minor_pentatonic).tick, release: 0.95
print notes
osc “/p5/note”, notes.to_i
sleep 1
end

Hi Tisane,

a warm welcome to the Sonic Pi community - it’s lovely to have you here :slight_smile:

In your example, the notes variable you define happens to be an object which represents the actual running synthesiser in the SuperCollider synthesis engine. This is used to control the synth as it’s running. See http://sonic-pi.net/tutorial.html#section-7-1 for more information about this.

The easiest way to achieve what you want is to figure out the note first and use a variable for it:

live_loop :shapes do
  use_synth :sine
  note = (scale :e3, :minor_pentatonic).tick
  play note, release: 0.95
  print note
  osc “/p5/note”, note.to_i
  sleep 1
end

Hope this helps :slight_smile:

Dear Sam,

thank you for the warm welcome and the rapid reply. Makes perfect sense now. I’m coming from OOP and thinking we have dot operators to access object variables. Just a slight yet important correction in the above code. notes.to_i should be replaced by note.to_i :––) Thanks again.

1 Like

Hi Sam,
Thanks for the great software! My first post here is OSC related as well.
I was able to access note info in Processing from your code above, but was trying to figure out how to use chord function and still get to access its notes and information like attack or release. Any tips on how to write osc into the code below? Thanks :)

use_osc "localhost", 12000
live_loop :arp do
  play chord(:e, :minor, num_octaves: 2).tick, release: 0.5
  sleep 0.125
end

Hi,

Does this help ?

use_osc "localhost", 12000

live_loop :foo do
  use_synth :sine
  myChord = chord :e3, '+5'
  myChord.each do |n|
    puts n
    play n, release: 0.95
    print n
    osc "/p5/note", n.to_i
  end
    sleep 0.5
end

or i think maybe this but not sure it’s a good solution as the length of the values would change towards the chord you use so…

live_loop :shapes do
  use_synth :sine
  myChord = chord :e3, '11+'
  osc "/p5/note", myChord.join(" ") # each value separated by a space in a string 
  sleep 0.5
end

Cheers

Hi nlb
Thanks for the suggestion, it does help! I figured out if I add myChord.tick at the end of the osc line, I can spit out note numbers one by one in sync with the music as I wanted. However, 1. I still can’t figure out how to access float number for release: and attack: to send it through osc. 2. This might sound stupid, but on the tutorial, it says this part "/p5/note" is the path, but path to what?

thanks for your patient, time and kind answers for these beginner questions : )

use_osc "localhost", 12000
use_real_time
live_loop :arp do
  myChord = chord :c4, '+5'
  play myChord.tick, release: 0.1, attack: 0.2
  osc "/p5/note", myChord.tick
  sleep 0.25
end

Could you give us the link of this tutorial ?

I’m guessing juju means Sonic Pi - Tutorial

@juju I’d think of the ‘path’ as just meaning something like the specific ‘address’ that the receiver will listen to, in order to receive data there. Kind of like a labelled post office box maybe? That could be a totally inaccurate analogy for all I know :sweat_smile: maybe someone else has a better description :slightly_smiling_face:

As far as sending more parameters to your endpoint like envelope values etc, one way you could do it is just by sending extra OSC messages with a different path :slightly_smiling_face:

Hi @juju

ah ok i thought you want to use process https://p5js.org/.
So

This piece of code from @robin.newman shows you how it works. Copy paste into a buffer in sonic pi and run

use_osc "localhost",4560 # used only for local testing where SP creates the data.
use_debug false
use_osc_logging false

live_loop :var1 do #receive and store var1 data
  v1 = sync "/osc*/test/var1"
  set :v1,v1[0] # store the received data
end

live_loop :var2 do
  v2 = sync "/osc*/test/var2" #receive and store var2 datga
  set :v2,v2[0] #store the second variable value
end

#as many as you need like the above

live_loop :latestData do #independent live loop processes latest available data
  puts "latest values var1 #{get(:v1)} var2 #{get(:v2)}"
  synth :tri,note: get(:v1),release: 0.4 #first variable adjust pitch
  synth :saw,note: get(:v2),release: 0.4 #second variable adjusts pitch
  sleep 0.4 #latest values read every 0.4 beats
end

#local generation of test osc messages
live_loop :createData1 do
  osc "/test/var1",rrand_i(60,72)
  sleep 0.2
end
live_loop :createData2 do
  osc "/test/var2",rrand_i(72,84)
  sleep 0.6
end

@ethancrawford thanks for the explanation, I think I figured the path between Sonic Pi and Processing.

As far as sending more parameters to your endpoint like envelope values etc, one way you could do it is just by sending extra OSC messages with a different path

However, I still couldn’t figure out how to send release or attack out as float numbers, I know I must got the syntax wrong. I keep getting string :release, instead I need float number 0.1, tips?
Screen Shot 2021-06-03 at 5.28.54 PM

use_osc "localhost", 12000
use_real_time
live_loop :arp do
  myChord = chord :c4, '+5'
  play myChord.tick, release: 0.1, attack: 0.2
  osc "/Processing/note", myChord.tick, :release
  sleep 0.25
end

Hi @nlb

Yes I am sending out osc message from Sonic Pi to Processing (not P5JS). Thanks for the code, having hard time understanding it but I will try to poke around.

No worries :slight_smile: when I mentioned previously ‘sending extra OSC messages with a different path’, I was suggesting something like this:

osc "/Processing/attack", 0.2
osc "/Processing/release", 0.1

But you have almost the right idea already with osc "/Processing/note", myChord.tick, :release - instead of :release, just store the float value in a variable, and then use it later:

release = 0.1
attack = 0.2
play myChord.tick, release: release, attack: attack
osc "/Processing/note", myChord.tick, release, attack
1 Like

(The reason osc "/Processing/note", myChord.tick, :release doesn’t work is because things like attack: and release: are labels/names of optional synth parameters, and only mean things to a limited set of Sonic Pi commands - using them elsewhere they have no special meaning to Sonic Pi).

1 Like

variable! yes! Now I can change these parameters and send out osc on the fly! Thank you so much!

use_osc "localhost", 12000
use_real_time

live_loop :arp do
myChord = chord :c, :major, num_octaves: 4
  
  release = 0.1
  attack = 0.1
  sleep = 0.25

  play myChord.tick, release: release, attack: attack
  osc "/Processing/note", myChord.tick, release, attack, sleep
  sleep sleep
end
2 Likes

You’re welcome, have fun! :grinning_face_with_smiling_eyes:

1 Like