Get current buffer as a string

I was thinking about creating something really meta, such as sonifying the current buffer by running it. Is it possible to get the content of the buffer as a string?

Did not find any method from the Sonic Pi language but after few beers found a way:

buffer = project_path+"workspace_one.spi"
text = File.read(buffer)
notes = {}
text.chars.each do |char|
  if !notes[char]
    notes[char] = {note: rrand_i(60,120), sleep: [0.25,0.5].choose }
  end
  with_fx :ping_pong, feedback: 0.5, phase: 0.25  do
    synth :mod_beep, note: notes[char][:note]
  end
  sleep notes[char][:sleep]
end

Run this in buffer one … such meta :upside_down_face: :beers: :space_invader:

Hey @amiika,

Was looking over some old code I have lying around and stumbled across a line which might have helped you slightly with this…
If it’s still useful - and for anyone else that ends up reading: as mentioned above, there is no built-in way in the Sonic Pi language to get the path or contents of the current buffer, though by doing essentially what amiika shows above, you can read the file contents.
However - you can (currently) use a function deep in the Sonic Pi source code to get the name of the current buffer:

puts Thread.current.__system_thread_locals.get(:sonic_pi_spider_job_info)[:workspace]

…produces, for example:

"workspace_one"

(Of course, the success of using this internal function may or may not change in the future as Sonic Pi evolves).

1 Like