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

**URL:** <https://in-thread.sonic-pi.net/t/my-first-composition-q-how-do-i-read-data-from-a-text-file/5906>\
**Category:** Creations & Ideas\
**Created:** [August 28, 2021, 3:53pm UTC](https://in-thread.sonic-pi.net/t/my-first-composition-q-how-do-i-read-data-from-a-text-file/5906 "2021-08-28T15:53:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gordon\_Charlton](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/gordon_charlton/32/2087_2.png) [@Gordon\_Charlton](https://in-thread.sonic-pi.net/u/Gordon_Charlton)\
**Post date:** [August 28, 2021, 3:53pm UTC](https://in-thread.sonic-pi.net/t/my-first-composition-q-how-do-i-read-data-from-a-text-file/5906/1 "2021-08-28T15:53:26Z")

</div>

Hi, newbie here. 🙂

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.

```ruby
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

---

<div class="post-metadata">

**Author:** ![Gordon\_Charlton](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/gordon_charlton/32/2087_2.png) [@Gordon\_Charlton](https://in-thread.sonic-pi.net/u/Gordon_Charlton)\
**Post date:** [August 28, 2021, 6:07pm UTC](https://in-thread.sonic-pi.net/t/my-first-composition-q-how-do-i-read-data-from-a-text-file/5906/2 "2021-08-28T18:07:39Z")

</div>

I figured it out.

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

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

---

<div class="post-metadata">

**Author:** ![xavierriley](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/xavierriley/32/9_2.png) [@xavierriley](https://in-thread.sonic-pi.net/u/xavierriley)\
**Post date:** [August 28, 2021, 7:50pm UTC](https://in-thread.sonic-pi.net/t/my-first-composition-q-how-do-i-read-data-from-a-text-file/5906/3 "2021-08-28T19:50:52Z")

</div>

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

For example

```ruby
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.

---

<div class="post-metadata">

**Author:** ![Gordon\_Charlton](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/gordon_charlton/32/2087_2.png) [@Gordon\_Charlton](https://in-thread.sonic-pi.net/u/Gordon_Charlton)\
**Post date:** [August 29, 2021, 7:38am UTC](https://in-thread.sonic-pi.net/t/my-first-composition-q-how-do-i-read-data-from-a-text-file/5906/4 "2021-08-29T07:38:03Z")

</div>

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](https://soundcloud.com/beat-frequency/tracks)

---

<div class="post-metadata">

**Author:** ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)\
**Post date:** [August 29, 2021, 8:15am UTC](https://in-thread.sonic-pi.net/t/my-first-composition-q-how-do-i-read-data-from-a-text-file/5906/5 "2021-08-29T08:15:27Z")

</div>

> [@Gordon\_Charlton](#):
>
> but is it “Ruby plus music oriented stuff”?

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).

> [@Gordon\_Charlton](#):
>
> would I be able to transfer that knowledge directly into Sonic Pi?

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 🙂

---

<div class="post-metadata">

**Author:** ![Gordon\_Charlton](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/gordon_charlton/32/2087_2.png) [@Gordon\_Charlton](https://in-thread.sonic-pi.net/u/Gordon_Charlton)\
**Post date:** [August 29, 2021, 10:44am UTC](https://in-thread.sonic-pi.net/t/my-first-composition-q-how-do-i-read-data-from-a-text-file/5906/6 "2021-08-29T10:44:00Z")

</div>

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