Reading integer from text file to modulate Sonic Pi parameter


#1

Hello,

I work as exhibit specialist at a non-profit hands on science and technology museum, and I am thinking of using Sonic Pi in order to refresh the internals of one of our exhibits.

We own a mid-1930s horn speaker radio that has been internally converted to play historically accurate “stations” at the turn of a knob (one of the radio’s original Bakelight knobs). The knob turn-shaft has four electrical contacts, which trigger WAV playback on four respective standalone audio players (BrightSign brand). The existing system is outdated and barely functional - 2 of the 4 audio players have died, and the relays that trigger them are so touchy that it requires an unreasonable effort to “dial in” a station.

My plan is to reuse the existing knob and attach a linear potentiometer to it, which can be read with a Pi Cobbler from Adafruit. This will give me 1024 points of accuracy, as I can write the output from the Pi Cobbler to a text file.

My question concerns the external file I/O capabilities of Sonic Pi. The documentation doesn’t specify whether or not strings, integers, or variables can be read from an external source. Would it be possible to read an integer, between 0 and 1024, from a text file? I could then use it to modulate the amplitude of several live loops, each loading a WAV sample from disk and “listening” for the integral value for its current amplitude at any given time.

Let me know if I need to provide more info. Thank you!


#2

It is possible to read and write text files from Sonic Pi. I have done two projects which utilise this.

and more recently

The latter uses a json file format.
The links give full articles including links to the software code.

EDIT
Alternatively If you don’t need to store the integers permanently you can use a python osc client to send integer data to Sonic Pi, which again I have done in several projects. eg see this article


#3

You can read files, for example txt, line by line.
Examples of files

uno.txt
48,50,52,53,55,57,59
48,50,52,53,55,57,59

play voice[j].to_i

dos.txt
c3,d3,e3,f3,g3,a3,b3
c3,d3,e3,f3,g3,a3,b3

play voice[j]


i = 0
j = 0
data = Array.new
voice = Array.new
linea = ''
use_synth :tri
File.open('/home/raul/Documentos/uno.txt','r') do |f1|
  while linea = f1.gets
    
    while i <linea.length-1
      data = linea[i]+linea[i+1]
      voice = voice.push(data)
      play voice[j].to_i
      sleep 1
      j +=1
      i +=3
    end
    i = 0
  end
end