My first composition. Q: How do I read data from a text file?

Hi, newbie here. :slightly_smiling_face:

I’ve done this. The long list of numbers is the fist thousand or so numbers in Bell’s Triangle, which I computed using a different language and then copy-pasted into the Sonic Pi code.

I’ll be doing a lot of that - precalculating numbers, as some of the sequences I hope to use could take a very long time to compute, so can’t realistically be done during execution of a Sonic Pi piece. The sequences will typically be a lot longer than the one here.

My question is this - rather than copy-pasting the list, as I have done here, how do I say

`my_variable = <a list of the numbers in the text file bell_numbers.txt>

Here’s the composition. It’s my first ever. Any observations/suggestions are welcome.

bellnums = [
  1, 1, 2, 5, 15, 16, 23, 13, 0, 15, 19, 6, 13, 1,
  34, 5, 23, 0, 31, 5, 28, 15, 15, 10, 33, 13, 10,
  1, 23, 20, 15, 13, 32, 31, 15, 30, 1, 33, 10, 1,
  19, 20, 23, 33, 16, 23, 31, 18, 33, 1, 6, 13, 19,
  16, 23, 5, 0, 31, 23, 10, 33, 33, 10, 33, 31, 28,
  19, 5, 20, 15, 31, 14, 13, 33, 30, 1, 15, 28, 19,
  1, 20, 23, 15, 34, 5, 13, 18, 33, 19, 24, 31, 1,
  16, 23, 23, 18, 13, 5, 10, 33, 15, 28, 15, 13, 28,
  19, 23, 2, 33, 13, 14, 13, 15, 12, 19, 33, 28, 19,
  19, 2, 5, 33, 34, 5, 31, 0, 15, 1, 24, 31, 19, 34,
  5, 5, 18, 13, 23, 28, 15, 33, 28, 15, 31, 10, 1,
  5, 2, 33, 31, 32, 31, 33, 12, 19, 15, 10, 1, 1, 2,
  5, 15, 16, 23, 13, 0, 15, 19, 6, 13, 1, 34, 5, 23,
  0, 31, 5, 28, 15, 15, 10, 33, 13, 10, 1, 23, 20,
  15, 13, 32, 31, 15, 30, 1, 33, 10, 1, 19, 20, 23,
  33, 16, 23, 31, 18, 33, 1, 6, 13, 19, 16, 23, 5,
  0, 31, 23, 10, 33, 33, 10, 33, 31, 28, 19, 5, 20,
  15, 31, 14, 13, 33, 30, 1, 15, 28, 19, 1, 20, 23,
  15, 34, 5, 13, 18, 33, 19, 24, 31, 1, 16, 23, 23,
  18, 13, 5, 10, 33, 15, 28, 15, 13, 28, 19, 23, 2,
  33, 13, 14, 13, 15, 12, 19, 33, 28, 19, 19, 2, 5,
  33, 34, 5, 31, 0, 15, 1, 24, 31, 19, 34, 5, 5, 18,
  13, 23, 28, 15, 33, 28, 15, 31, 10, 1, 5, 2, 33,
  31, 32, 31, 33, 12, 19, 15, 10, 1, 1, 2, 5, 15,
  16, 23, 13, 0, 15, 19, 6, 13, 1, 34, 5, 23, 0, 31,
  5, 28, 15, 15, 10, 33, 13, 10, 1, 23, 20, 15, 13,
  32, 31, 15, 30, 1, 33, 10, 1, 19, 20, 23, 33, 16,
  23, 31, 18, 33, 1, 6, 13, 19, 16, 23, 5, 0, 31,
  23, 10, 33, 33, 10, 33, 31, 28, 19, 5, 20, 15, 31,
  14, 13, 33, 30, 1, 15, 28, 19, 1, 20, 23, 15, 34,
  5, 13, 18, 33, 19, 24, 31, 1, 16, 23, 23, 18, 13,
  5, 10, 33, 15, 28, 15, 13, 28, 19, 23, 2, 33, 13,
  14, 13, 15, 12, 19, 33, 28, 19, 19, 2, 5, 33, 34,
  5, 31, 0, 15, 1, 24, 31, 19, 34, 5, 5, 18, 13, 23,
  28, 15, 33, 28, 15, 31, 10, 1, 5, 2, 33, 31, 32,
  31, 33, 12, 19, 15, 10, 1, 1, 2, 5, 15, 16, 23,
  13, 0, 15, 19, 6, 13, 1, 34, 5, 23, 0, 31, 5, 28,
  15, 15, 10, 33, 13, 10, 1, 23, 20, 15, 13, 32, 31,
  15, 30, 1, 33, 10, 1, 19, 20, 23, 33, 16, 23, 31,
  18, 33, 1, 6, 13, 19, 16, 23, 5, 0, 31, 23, 10,
  33, 33, 10, 33, 31, 28, 19, 5, 20, 15, 31, 14, 13,
  33, 30, 1, 15, 28, 19, 1, 20, 23, 15, 34, 5, 13,
  18, 33, 19, 24, 31, 1, 16, 23, 23, 18, 13, 5, 10,
  33, 15, 28, 15, 13, 28, 19, 23, 2, 33, 13, 14, 13,
  15, 12, 19, 33, 28, 19, 19, 2, 5, 33, 34, 5, 31,
  0, 15, 1, 24, 31, 19, 34, 5, 5, 18, 13, 23, 28,
  15, 33, 28, 15, 31, 10, 1, 5, 2, 33, 31, 32, 31,
  33, 12, 19, 15, 10, 1, 1, 2, 5, 15, 16, 23, 13, 0,
  15, 19, 6, 13, 1, 34, 5, 23, 0, 31, 5, 28, 15, 15,
  10, 33, 13, 10, 1, 23, 20, 15, 13, 32, 31, 15, 30,
  1, 33, 10, 1, 19, 20, 23, 33, 16, 23, 31, 18, 33,
  1, 6, 13, 19, 16, 23, 5, 0, 31, 23, 10, 33, 33,
  10, 33, 31, 28, 19, 5, 20, 15, 31, 14, 13, 33, 30,
  1, 15, 28, 19, 1, 20, 23, 15, 34, 5, 13, 18, 33,
  19, 24, 31, 1, 16, 23, 23, 18, 13, 5, 10, 33, 15,
  28, 15, 13, 28, 19, 23, 2, 33, 13, 14, 13, 15, 12,
  19, 33, 28, 19, 19, 2, 5, 33, 34, 5, 31, 0, 15, 1,
  24, 31, 19, 34, 5, 5, 18, 13, 23, 28, 15, 33, 28,
  15, 31, 10, 1, 5, 2, 33, 31, 32, 31, 33, 12, 19,
  15, 10, 1, 1, 2, 5, 15, 16, 23, 13, 0, 15, 19, 6,
  13, 1, 34, 5, 23, 0, 31, 5, 28, 15, 15, 10, 33,
  13, 10, 1, 23, 20, 15, 13, 32, 31, 15, 30, 1, 33,
  10, 1, 19, 20, 23, 33, 16, 23, 31, 18, 33, 1, 6,
  13, 19, 16, 23, 5, 0, 31, 23, 10, 33, 33, 10, 33,
  31, 28, 19, 5, 20, 15, 31, 14, 13, 33, 30, 1, 15,
  28, 19, 1, 20, 23, 15, 34, 5, 13, 18, 33, 19, 24,
  31, 1, 16, 23, 23, 18, 13, 5, 10, 33, 15, 28, 15,
  13, 28, 19, 23, 2, 33, 13, 14, 13, 15, 12, 19, 33,
  28, 19, 19, 2, 5, 33, 34, 5, 31, 0, 15, 1, 24, 31,
  19, 34, 5, 5, 18, 13, 23, 28, 15, 33, 28, 15, 31,
  10, 1, 5, 2, 33, 31, 32, 31, 33, 12, 19, 15, 10,
  1, 1, 2, 5, 15, 16, 23, 13, 0, 15, 19, 6, 13, 1,
  34, 5, 23, 0, 31, 5, 28, 15, 15, 10, 33, 13, 10,
1, 23, 20, 15, 13, 32, 31, 15, 30, 1, 33, 10, 1 ]

use_synth :pretty_bell
with_fx :bitcrusher do
  live_loop :blah do
    y = bellnums[tick]
    if y == nil then
      stop
    end
    play y+36
    sleep ((y % 2)/2+0.5)/4
  end
end

Thanks, Gordon

2 Likes

I figured it out.

bellnums = eval_file "/path/to/bells.txt"

where the file contains the text [ 1, 1, 2 ... 33, 10, 1 ].

2 Likes

If your file wasn’t valid Ruby syntax, there’s also the option of using Ruby to parse things.

For example

IO.read('/path/to/file').lines.map(&:chomp)

This would load a file, turn it into an array of lines and then run .chomp on each line to remove trailing and leading whitespace. String manipulation is one of the strengths of Ruby (the underlying language for the Sonic Pi interface) so it should be possible to do most things.

1 Like

That’s good to know, thanks.

I had read that the Sonic Pi language is “based on Ruby”, but is it “Ruby plus music oriented stuff”? If I watched videos on how to do string and list manipulation in Ruby, would I be able to transfer that knowledge directly into Sonic Pi?

My basic idea is that anywhere I could use SP’s random number generator I could use a precomputed list of numbers. There are innumerable integer sequences that are musically rich in that they have structure, symmetry, repetition and variation - all the good stuff - and they fascinate me.

(If you’re interested, my soundcloud is full of stuff based on integer sequences - many of the pieces are in the ten minute region, but listening to 30 second’s worth will give you the gist for the whole piece. Beat Frequency on Soundcloud

Sonic Pi is designed as a DSL, or ‘Domain Specific Language’, meaning that it is designed with a specialised focus, (music composition and live coding/performance) rather than being a general purpose programming language.

(Yes, the Sonic Pi language is constructed from Ruby syntax, and interpreted with the Ruby interpreter, so any Sonic Pi script is technically valid Ruby code. However, while a Sonic Pi script is interpreted in this way, it is not guaranteed that any plain Ruby commands that are included will work).

Many of the simpler plain Ruby functions, such as basic maths operators and functions for operating on Strings and Arrays etc, will most likely work, but other more complex things may well not. If any plain Ruby things do work, such things 1) are not officially supported and 2) may stop working without warning as Sonic Pi’s internals change over time. It’s still worth doing, if you keep those points in mind - many folks have done similar things already :slight_smile:

OK, thanks. I’ll bear that in mind.