# Something wrong using"\>="? nilclass?

**URL:** <https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975>\
**Category:** \[P\] Patreon Support\
**Created:** [April 15, 2018, 8:03am UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975 "2018-04-15T08:03:40Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![micki](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@micki](https://in-thread.sonic-pi.net/u/micki)\
**Post date:** [April 15, 2018, 8:03am UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/1 "2018-04-15T08:03:40Z")

</div>

![Screenshot_20180415-155725](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/1X/d3f32fd4cd3fe01cad479bcd5218091bb419e01e.jpg)  
p3 is an auto-refresh value that comes from a sonar attached to RPI.GPIO,anything wrong?

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [April 15, 2018, 9:26am UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/2 "2018-04-15T09:26:29Z")

</div>

Hi there,

it looks like the current value of `p3` is `nil`. Could you share the code where that is set?

---

<div class="post-metadata">

**Author:** ![micki](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@micki](https://in-thread.sonic-pi.net/u/micki)\
**Post date:** [April 15, 2018, 10:06am UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/3 "2018-04-15T10:06:34Z")

</div>

```
# Sonic Pi init file
# Code in here will be evaluated on launch.

live_loop:listen do
  n1=sync "/osc/play_this1"
  n2=sync "/osc/play_this2"
  n3=sync "/osc/play_this3"
  p1=n1[0]
  p2=n2[0]
  p3=n3[0]
  '-----------------------'
  use_synth :beep
  if p1<=11 then
    play :C4
    sleep(0.1)
  elsif p1<21 then
    play :D4
    sleep(0.1)
  elsif p1<31 then
    play :E4
    sleep(0.1)
  elsif p1<41 then
    play :F4
    sleep(0.1)
  elsif p1<51 then
    play :G4
    sleep(0.1)
  end
  
  '-----------------------'
  use_synth :chipbass
  if p2<=11 then
    play :C4
    sleep(0.1)
  elsif p2<21 then
    play :D4
    sleep(0.1)
  elsif p2<31 then
    play :E4
    sleep(0.1)
  elsif p2<41 then
    play :F4
    sleep(0.1)
  elsif p2<51 then
    play :G4
    sleep(0.1)
  end
  '------------------'
  use_synth :piano
  if p3<=11 then
    play :C4
    sleep(0.1)
  elsif p3<21 then
    play :D4
    sleep(0.1)
  elsif p3<31 then
    play :E4
    sleep(0.1)
  elsif p3<41 then
    play :F4
    sleep(0.1)
  elsif p3<51 then
    play :G4
    sleep(0.1)
  end
end
    `

> indent preformatted text by 4 spaces

`
```

---

<div class="post-metadata">

**Author:** ![micki](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@micki](https://in-thread.sonic-pi.net/u/micki)\
**Post date:** [April 15, 2018, 10:07am UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/4 "2018-04-15T10:07:13Z")

</div>

THANKS A LOT IF YOU COULD ALSO SEE OUR PYTHON CODE!!!

> from time import sleep  
> from pythonosc import osc\_message\_builder  
> from pythonosc import udp\_client  
> import RPi.GPIO as GPIO  
> import time
> 
> sender = udp\_client.SimpleUDPClient(‘127.0.0.1’,4559)
> 
> def checkdist(trigger,echo):  
> GPIO.setmode(GPIO.BCM)  
> GPIO.setup(trigger,GPIO.OUT,initial=GPIO.LOW)  
> GPIO.setup(echo,GPIO.IN)  
> GPIO.output(trigger,GPIO.HIGH)  
> time.sleep(0.00015)  
> GPIO.output(trigger,GPIO.LOW)  
> while not GPIO.input(echo):  
> pass  
> t1 = time.time()  
> while GPIO.input(echo):  
> pass  
> t2 = time.time()  
> return (t2-t1)\*340/2
> 
> while True:  
> print(‘Distance1:%0.2f m’%checkdist(3,2))  
> print(‘Distance2:%0.2f m’%checkdist(17,27))  
> print(‘Distance3:%0.2f m’%checkdist(24,23))
> 
> time.sleep(0.1)
> 
> pitch1=round(float(‘%0.2f’%checkdist(3,2))\*100)  
> sender.send\_message(‘/play\_this1’,pitch1)
> 
> pitch2=round(float(‘%0.2f’%checkdist(17,27))\*100)  
> sender.send\_message(‘/play\_this2’,pitch2)
> 
> pitch3=round(float(‘%0.2f’%checkdist(24,23))\*100)  
> sender.send\_message(‘/play\_this3’,pitch3)

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [April 15, 2018, 11:25am UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/5 "2018-04-15T11:25:12Z")

</div>

Interesting.

What happens if you add

`puts "p3: #{p3}, n3: #{n3}"`

prior to the line:

`if p3<=11 then`

What do you see in the log?

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [April 15, 2018, 12:01pm UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/6 "2018-04-15T12:01:24Z")

</div>

Have you very recently installed python-osc? There is a bug in the latest release version. You need to install with

```ruby
sudo pip3 install python-osc==1.6.4

```

Version 1.6.5 has the bug which prevents values of 1 or 0 being sent as data in an OSC message.  
I think it may affect sending some floats too.  
The author is aware and hopes to release 1.6.6 soon

If you have version 1.6.5 installed just uninstall using sudo pip3 uninstall python-osc and then reinstall 1.6.4

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [April 15, 2018, 12:15pm UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/7 "2018-04-15T12:15:37Z")

</div>

Also looking at your code I’m not sure if three syncs one after the other will work. It will wait for the n1 sync then for the n2 sync then for the n3 sync. Do you not want a separate live loop for each one, OR use a wild card and work out which message was received, as I have done using my parse\_sync\_address function as featured in code

> <https://gist.github.com/rbnpi/a86ced02b74817f3070887df30fb0701>
>
> There are more than three files. show original

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [April 15, 2018, 7:29pm UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/8 "2018-04-15T19:29:56Z")

</div>

Just received notification that python-osc 1.6.6 has been released, so  
`sudo pip3 install python-osc` should be OK to use again.

---

<div class="post-metadata">

**Author:** ![micki](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@micki](https://in-thread.sonic-pi.net/u/micki)\
**Post date:** [April 16, 2018, 2:32pm UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/9 "2018-04-16T14:32:18Z")

</div>

As you can see, we’re working on a DIY theremin project but we met some problem, we have an video to describe our problem…Do you have WeChat (or other IM apps) number so that we can send it to u? \<3

---

<div class="post-metadata">

**Author:** ![micki](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@micki](https://in-thread.sonic-pi.net/u/micki)\
**Post date:** [April 16, 2018, 2:35pm UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/10 "2018-04-16T14:35:24Z")

</div>

![2%20(1)](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/1X/4f2c8bab1e6176f3f8b6db06354be30d02d35394.jpg)

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [April 16, 2018, 9:59pm UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/11 "2018-04-16T21:59:10Z")

</div>

Hi Micki  
Not sure if you only want notes a tone apart, but a more sliding sound (like a theremin) is obtained with the following code. Also, treat each input separately in its own live loop.  
I simulated your inputs using three sldiers on TouchOSC, hence the slightly different OSC addresses, but try removing the /test from three addresses and see if this code works with your system

```ruby
# Sonic Pi init file
# Code in here will be evaluated on launch.

live_loop:listen1 do
  use_real_time
  b=sync "/osc/test/play_this1"
  p1=b[0]
  
  '-----------------------'
  use_synth :beep
  play 60+7*p1,attack: 0.05,release: 0.15,amp: 1.5
  sleep 0.1
end

live_loop:listen2 do
  use_real_time
  b=sync "/osc/test/play_this2"
  p2=b[0]
  
  '-----------------------'
  use_synth :chipbass
  play 60+7*p2,attack: 0.05,release: 0.2
  sleep 0.1
end

live_loop:listen3 do
  use_real_time
  b=sync "/osc/test/play_this3"
  p3=b[0]
  
  '-----------------------'
  use_synth :piano
  
  play 60+7*p3,attack: 0.05,hard: 0.3,vel: 0.12,amp: 1.5
  sleep 0.1
end

```

Also, do you need to put this in the init.rb file rather than run it in a normal buffer?

---

<div class="post-metadata">

**Author:** ![micki](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@micki](https://in-thread.sonic-pi.net/u/micki)\
**Post date:** [April 17, 2018, 5:39am UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/12 "2018-04-17T05:39:54Z")

</div>

we used to set three loops, but the sound overlaps,that’s annoying

---

<div class="post-metadata">

**Author:** ![micki](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@micki](https://in-thread.sonic-pi.net/u/micki)\
**Post date:** [April 17, 2018, 5:41am UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/13 "2018-04-17T05:41:09Z")

</div>

the contents printed on python is more easier to stop refreshing,too. we couldn’t find the reason

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [April 17, 2018, 12:51pm UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/14 "2018-04-17T12:51:17Z")

</div>

OK. Here is another version nearer to your original which works with TouchOSC input.  
Try it on your system, altering the OSC addresses by removing /test  
ie  
`n=sync "/osc/test/play_this*"`  
becomes  
`n=sync "/osc/play_this*"`  
and  
`res = parse_sync_address "/osc/test/play_this*"` becomes  
`res = parse_sync_address "/osc/play_this*"`

```ruby
# Sonic Pi init file
# Code in here will be evaluated on launch.

define:parse_sync_address do |address| #gets info on wild cards used
  v= get_event(address).to_s.split(",")[6]
  if v != nil
    return v[3..-2].split("/")
  else
    return ["error"]
  end
end

live_loop:listen do
  use_real_time
  n=sync "/osc/test/play_this*" #remove the /test for your system
  
  nv=n[0].to_i
  res = parse_sync_address "/osc/test/play_this*" #remove the /test for your system
  p=res[2][9..-1].to_i
  puts p,nv
  case p
  when 1
    use_synth :beep
  when 2
    use_synth :chipbass
  when 3
    use_synth :piano
  end
  
  if nv<=11 then
    play :C4
    
  elsif nv<21 then
    play :D4
    
  elsif nv<31 then
    play :E4
    
  elsif nv<41 then
    play :F4
    
  elsif nv<51 then
    play :G4
    
  end
  sleep(0.1)
end

```

I can’t really comment on the python code without access to suitable hardware. I have one ultrasound sensor, but unfortunately I think it is damaged. However I think you said that osc messages were being sent OK.

---

<div class="post-metadata">

**Author:** ![micki](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@micki](https://in-thread.sonic-pi.net/u/micki)\
**Post date:** [April 17, 2018, 9:29pm UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/15 "2018-04-17T21:29:46Z")

</div>

![-6b30e98cc24ef5a5](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/1X/10c8747dc8f99d846dda26da52f6d3c993f0fa1b.png)  
work on RPI3 sonic-pi-3.0.1…  
thanks for your patience anyway…❤

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [April 17, 2018, 10:19pm UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/16 "2018-04-17T22:19:37Z")

</div>

ACTUALLY I FORGOT TO SAY YOU WILL HAVE TO CHANGE THE LINE WITH THE ERROR TO  
`p=res[1][9..-1].to_i`  
when you alter the OSC address line and remove the /test as you want to get data from the 2nd element not the third, and they number from 0.

---

<div class="post-metadata">

**Author:** ![micki](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@micki](https://in-thread.sonic-pi.net/u/micki)\
**Post date:** [April 19, 2018, 4:55am UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/19 "2018-04-19T04:55:05Z")

</div>

i set some rhymes using 1.times do in each “in\_thread” replace the former pitches.  
so every 0.1s it will play the rhyme…it’s bad.

i am now wondering about how to play audio in particular range of distance,and stop the audio when the distance is out of a specific number…  
does it have functions such as ‘stop all the audio’？

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [April 19, 2018, 10:54am UTC](https://in-thread.sonic-pi.net/t/something-wrong-using-nilclass/975/20 "2018-04-19T10:54:27Z")

</div>

I use two techniques to switch live\_loop audio on and off. The first stops the live loop and restarts it when required. The second leaves the live\_loop running but controls its volume using an fx :level wrapper.

In the first example you could use osc messages to call doloop to start and to set :go,0 to stop.

In the second example you could use osc messages to adjust the value of :vol using set :vol,0 or set :vol,1

```ruby
use_bpm 120

#technique 1 use stop to stop a live loop
#the function call will restart it next time it is called
define :doLoop do
  use_synth :tb303
  set :go,1
  live_loop :doodle do
    n=scale(:c3,:minor_pentatonic,num_octaves: 2).choose
    play n, cutoff: rrand(50,110),release: 0.1 if spread(5,8).tick
    sleep 0.2
    stop if get(:go)==0
  end
end
#illustrate starting and stopping the loop 4 times
4.times do
  doLoop
  sleep 4
  set :go,0
  sleep rrand_i(1,4)
end

#technique 2
# have a continously running live loop
#and embed it within fx :level
#control the level to switch the loop audio off and on
with_fx :level,amp: 1 do |v| #could start with amp: 0 if you want the loop to start silently
  in_thread do
    loop do
      control v,amp: get(:vol),amp_slide: 0.1
      sleep 0.1
    end
  end
  use_synth :tb303
  live_loop :doodle2 do
    n=scale(:c4,:minor_pentatonic,num_octaves: 2).tick
    play n, cutoff: rrand(50,110),release: 0.1 if spread(5,8).look
    sleep 0.2
  end
end
#illustrate controlling the volumn on and off 4 times
4.times do
  set :vol,1
  sleep 4
  set :vol,0
  sleep 4
end

```
