Irregular steps in Array.series

Hi people. How are you?
I’m trying to create an Array series of different ranges, for example [ 0,1, 2, 4, 3, 1, 6, 5 ]:
The result I’m looking for is: FloatArray[0, 1, 3, 7,10,11,17,22]

I am trying to concatenate a lot of audio files at different lengths

b = SoundFile.collect("path/*", s);

z = b.collect({arg n; n.duration.postln}); 

I tried:

Array.series(14, 0, z);

But this gave me arrays of arrays. And it’s not what I was looking for.

Is it possible to get this series of irregular steps?

Thank you all for everything!

Hello!

Why did you put s as the second argument of SoundFile.collect?

According to the help document it has only one argument:
https://doc.sccode.org/Classes/SoundFile.html#*collect

You allocated an array to the interpreter variable z and used the array as the third argument of the Array.series:

However, the third argument should be one of Number’s subclasses:

Array.series(3, 0, Complex(2, 5))
Array.series(3, 0, 1@2)
Array.series(3, 0, 0.1)
Array.series(3, 0, 1)

Since you used an array in place of Number’s subclasses, sclang processes each element of the array and returns it as the form of array:

Array.series(3, 0, [1, 2])
// the code above is the same as the following code:
Array.series(3, [0, 0], [1, 2])

// return:
[ [ 0, 0 ], [ 1, 2 ], [ 2, 4 ] ]

You should remove the arrays inside of the array as follows:

Array.series(3, 0, [1, 2]).flat

In this case, flatten(1) is also applicable:

Array.series(3, 0, [1, 2]).flatten(1)
Array.series(3, 0, [1, 2]).flatten // The default value of the argument of .flatten is 1.

Then you get some values which are the same as follows:

[ 0, 0, 1, 2, 2, 4 ]

You can erase these duplications by converting the array into a set as follows:

Array.series(3, 0, [1, 2]).flat.asSet
Set[ 4, 0, 1, 2 ]

Then you should convert it again to an array:

Array.series(3, 0, [1, 2]).flat.asSet.asArray

then should sort it:

Array.series(3, 0, [1, 2]).flat.asSet.asArray.sort

Thus, your code should be as follows I believe:

b = SoundFile.collect(Platform.resourceDir +/+ "sounds" +/+ "*");
z = b.collect {arg n; n.duration }; 
x = Array.series(14, 0, z).flat.asSet.asArray.sort;
Post <<< x

Is the result the same as your expectation?

In addition, Array.series can be written as follows:

Array.series(14, 0, 1)
Array.series(14, 0, 0.5)

(0, 1 .. 14 - 1)
(0, 0.5 .. 14 - 1 / 2)

//or

(0, 1 .. 13)
(0, 0.5 .. 6.5)
1 Like

Sorry, I did not see the following part of your post:

To get [0, 1, 3, 7,10,11,17,22] from
[ 0,1, 2, 4, 3, 1, 6, 5 ]

You need .integrate:

[0,1, 2, 4, 3, 1, 6, 5].integrate

Then, from the following code, x might not be what you want. Is y what you want to have?

b = SoundFile.collect( Platform.resourceDir +/+ "sounds/*");
z = b.collect({ |n| n.duration }); 
x = Array.series(14, 0, z).integrate
y = b.collect({ |n| n.duration }).integrate
1 Like

Hi Ivan,
Rather than using Array directly, you can just concatenate two together…

(
var snds = SoundFile.collect(...);
var breaks = [0] ++ snds.collect({ |s| s.duration }).integrate;
breaks.postln;
)

… I think this gives what you want.

2 Likes

Also, to get the ranges of the original audio back out, do this…

(
var snds = SoundFile.collect(...);
var breaks = [0] ++ snds.collect({ |s| s.duration }).integrate ;
var audio_ranges = [breaks[..(breaks.size-2)], breaks[1..]].flop;
audio_ranges
)
2 Likes

@prko

Thank you very much for your comments and suggestions.
They were so didactic and enlightening that I understood all the steps quite well.
In fact, I didn’t want an Array of Arrays, as I had commented earlier and the idea of ‘integrate’ is very much in line with what I was wanting at the beginning.
So, in an answer you taught me and indicated ways for me to understand more the idea of ‘flat’ ‘asSet’ and ‘asArray’.

Thank you very much!

1 Like

Hi @jordan

Thank you!

You advanced my study a little further and anticipated a possible question that would be how to segment the audio with this list.
And taking the breaks between the numbers in the list is a very good way out of this question!

Thank you very much.

2 Likes