Arduino interface to sonic pi

what a surprise ! :slight_smile:
you should adapt to your path

killablesample t,:kill1 #start sample with name of kill flag

lol my bad, thanks again for the assistance

yes as above you can play wavs easily. Also it only kills the playing sample nothing else eg

set :f1,false
set :f2,false
define :killablesample do |s,flagname|
  set flagname,false
  in_thread do
    k=sample s
    t =sample_duration s
    (t/0.1).to_i.times do
      if get(flagname)
        kill k
        stop
      end
      sleep 0.1 #loop polled every 0.1 beats
    end
  end
end
killablesample "/Users/rbn/Desktop/samples/myfile.wav",:f1
sleep 2
set :f1,true
killablesample :loop_compus,:f2
sleep 2
set :f2,true

Hi can someone advise what I can do to change effects to a note currently being played thats being read from the midi. I want o try get that efefct you had in old analogue synths moog with the modulation effect.
I cant seem to get this right.
the midi sends a note say value for ex:60, 61, 62 etc and I want to change effects to this as its being played
code:
live_loop :playnotes do
note, velocity = sync “/midi:arduino_leonardo_midi_1:1:1/note_on”
effect = sync “/midi:arduino_leonardo_midi_1:1:1/control_change”

effectc = effect [1]
effectc = effectc /10

nnote=(note/10)

with_fx :gverb do
with_fx :flanger do

  s = use_synth :piano 
  synth s, note: note, release: rrand(-0.8, 0.8), cutoff: nnote, sustain: effectc , decay: effectc 
 
  
end

end

sleep nnote
end

in addition to the above, notes arent always being read or not being picked up and then it seems that the buffer stops - is there a way to ensure the buffer is always active and listening for input?

And when playing the audio wav file as above is there a way to play notes in tandem with the wav?

Here is how I would control level (volume) and cutoff for midi notes input and played by sonic pi. Adjust the control numvbers for the control inputs you want to use. I used 32 and 33

#demo on controlling level and cutoff for midi input

use_synth :tb303
#vol is a reference to the fx :level
with_fx :level,amp: 0.5 do |vol|
  set :vol,vol #store vol in timestate
  
  #r is a reference to the fx :nlpf
  with_fx :nlpf,cutoff: 50,mix: 0.7 do |r|
    #store r in the time state as :r
    set :r,r
    live_loop :getmidi do
      use_real_time
      n,v = sync "/midi*/note_on"
      play n,release: 0.2,amp: v/127.0
    end
    
  end
end

#this live loop gets value from control 33 and uses it to adjust
#amp level for the fx referenced by get(:vol)
live_loop :vcontrol do
  use_real_time
  b,v = sync "/midi*/control_change"
  if b==33 #adjust number to suit control you are using
    v=v/127.0
    control get(:vol),amp: v
  end
end
#this live loop gets value from control 32 and uses it to
#adjust cutoff value for fx referenced by get(:r)
live_loop :control do
  use_real_time
  b,v = sync "/midi*/control_change"
  if b==32 #adjust number to suit control you are using
    v=v/127.0
    control get(:r), cutoff: 50+v*60
  end
end

thank you Robin, apologies for the belated reply - unusual times.
This seems to be in line with what I’m looking for.
How can I add some beat and rhythm to this using the similar techniques from the midi.
How would I be able to add choral angelic voices to this?

if you also get a chance can you please explain the logic of the code you posted ```
#demo on controlling level and cutoff for midi input

Hi,

from my own experience I can say it is a very productive thing to analyze code I don’t understand line by line and try to find out, what it does. This might take some time and be exhausting but it almost always leads to more knowledge and understanding in a deeper way. And if not that, I afterwards will surely be able to ask more specific questions than before.

My friends in this situation are the documentation and especially the command puts to find out, what a variable contains/is used for/works like at a certain point. You can e. g. use the following to log and study the incoming midi information:

live_loop :getmidi do
      use_real_time
      n,v = sync "/midi*/note_on"
      puts "Value of n: #{n} / value of v: #{v}"
      play n,release: 0.2,amp: v/127.0
      puts "Calculated value of v = #{v/127.0}"
    end

Of course this is just an example and if you want you might adapt this to your own ends. Some things you will probably have to try out and then also use Google. This might be the case e. g. to find out why the code says v/127.0 and not v/127.

Often things don’t make any sense at all at the first sight. But after some time, some testing and some meditation about it on my side they gradually do.

Thanks I do usually do that however the code has some concepts I didn’t quite follow.

I have another big question: for my application to work I need the code in cache 0 to run when sonic starts up, that is, there shouldn’t be any need for me to push the run command.
How do I get this to happen or configure sonic pi to auto run code in cache 0? right now using the above code it doesn’t autorun until I push run?

Hey @wro :slight_smile:

depending on what you are hoping for explanation over, it often helps to ask specific questions - the more focused they are, the more focused the answers can be :slight_smile:

1 Like

As far as I know there is no way to run code in a specific buffer automatically after program start.

But there is the ~/.sonic-pi/init.rb (this is the path on Linux), which might accomplish what you need. This file will be evaluated after program start, see one possible example here.

can someone tell me if thats true because then my whole project cant use sonic pi, i need it to autorun the code on startup.

the init.rb file allows you to run a piece of code at start up, without needing to have it in a specific buffer.

sounds promising - phew! please send me an example

@Martin mentioned one above :+1:

thanks - whats the equivalent in windows os

Usually the file is in C:\Users\[your name]\.sonic-pi by default.

:+1: much appreciated

No worries, you’re welcome :+1: