How to get numbers from a file into an array for pitches?

Dear all. I am pretty new to SC and have had the fortune of the best teachers <3 and fantastic tutorials out there. But right now I am struggling with something I thought should be the simplest of tasks.

I have a txt file with comma-delimited data (numbers) that I want to use as input for pitches. To simplify the problem I chose freq/Hz. I want SC to read the file and to output these numbers sequentially as tones in a melody.

To simplify the problem, the file contains the following string:

382,440,440,220,880,176,576,576,1024,1024,3111,440

I want this to work equivalent to

`Pbind(\freq, Pseq([382, 440, 440, 220, 880, 176, 576, 576, 1024, 1024, 3111, 440], 2), \dur, 0.5).play;`

When I evaluate the above, I get the desired result - a melody with those numbers in a sequence (played twice, where each note has a duration of 0.5s).

However, when I evaluate this:

Pbind(\freq, Pseq(CSVFileReader.readInterpret("numbers1csv.txt"), 2), \dur, 0.5).play;

for some reason, the tones are played in unison rather than as a sequence. And I don’t understand why. If I write

CSVFileReader.readInterpret("numbers1csv.txt").postcs;

the post window in SC gives me

-> [ [ 382, 440, 440, 220, 880, 176, 576, 576, 1024, 1024, 3111, 440 ] ]

so here it seems the file is correctly parsed (including adding pretty blanks after the commas), but not when I use the Pbind/PSec.

I have a hunch I am missing something ridiculously simple but I’ve been struggling with this for a whole day now so I thought it’s time to ask. Any idea what I should do to get this work as I intended?

It would be much appreciated.

Two options.

Place each number in the file on a new line… (can’t remember if you need commas)

382
440 
440
220
...

Or access only the first line from the CSVFileReader…

CSVFileReader.readInterpret("numbers1csv.txt"), 2)[0]

Currently it is trying to play each line in the file sequentially, and all the things on one line in parallel.

1 Like

Magic! Thank you so much. I didn’t get this to work exactly as you suggested (or the way I understood it), but it helped my find the trick.

I placed the numbers on a new line as you suggested, didn’t get it to work with the commas left so I removed those. It still didn’t work at first but then I realised that without the commas I had to use FileReader rather than CSVFileReader. So this

Pseq(FileReader.readInterpret("users/gullfot/music/numbers3csv.txt")

worked perfectly, just as I intended.

Thank you so so much!

I believe another solution might have been to use .unbubble to remove the erronous outer square brackets (just for future reference! bubble and unbubble are useful)

1 Like

Maybe it was not clear yet why you heard all the notes simultaneously? It is because they were contained in an extra list.

Compare:

[ 440, 660, 880 ]  will play three successive notes
[ [440, 660, 880] ] will play one chord containing three notes
1 Like

This looks interesting, thank you! I tried to find documentation on .unbubble but didn’t find anything in the help files. Would you know where I can find out more?

This is GOLD. And very useful! Thank you. I was wondering about the brackets and their effects, now I don’t have to ask :heart:

unbubble (and lots of other interesting stuff!) is discussed here:

J concepts in SC | SuperCollider 3.12.2 Help.