[Csnd] Saving arrays in a array and reading it out

Hello Everybody!

I’m working on a instrument where i can save multiple arrays in one array. Then i want to read these arrays out, one after another.
For example:

iArr1[] = 1, 2, 3, 4, 5
iArr2[] = 11, 22, 33, 44, 55
iArr3[] = 111, 222, 333, 444, 555

iAllArr[] = iArr1, iArr2, iArr3

After this i want to create automatically a new array with all the values like this:

iAllValues[] = 1, 2, 3, 4, 5, 11, 22, 33, 44, 55, 111, 222, 333, 444, 555

I already done this and this working quit well:

kPtr1[] fillarray 1, 2, 3, 4
kPtr2[] fillarray 11, 12, 13

iSngLng = lenarray(kPtr1) + lenarray(kPtr2)

gkPtrCmpl[] init iSngLng

kSngNdx init 0
kRdNdx init 0
kActPtr[] = kPtr1
while kSngNdx < iSngLng do
  kNewValue = kActPtr[kRdNdx]
  gkPtrCmpl[kSngNdx] = kNewValue
  kRdNdx += 1
  kSngNdx += 1
  if kRdNdx == lenarray(kActPtr) then
    kRdNdx = 0
    kActPtr = kPtr2
  endif
od

But this is only a solution for two arrays as input for a new array.
I want to use a abritary number of kPtrN - Arrays as Input for another Array (iPtrChain[] = iPtr1, iPtr1, iPtr2).
Then i want to read all the values into a new array.

How can i come there?

Greetings,
Philipp
Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

Hi Philipp!
Feb 18 2023, Philipp Neumann has written:
...

But this is only a solution for two arrays as input for a new array.
I want to use a abritary number of kPtrN - Arrays as Input for another Array iPtrChain\[\] = iPtr1, iPtr1, iPtr2.
Then i want to read all the values into a new array.

...
Here are a few things that might help. First, you can change the size of
an array using trim
https://csound.com/manual/trim.html
This can help to change the size of your song array.
You can copy values like you did in a while loop.
You can write a UDO to help you, extend the song by one pattern at a
time. Perhaps something like this:
opcode AddPattern, 0, k[]k[]
   kSong[], kPattern[] xin ; get the two arrays inside the UDO

   ; get the length of the pattern
   kPatternLen = lenarraykPattern ; or use a fixed value

   ; Get the length of the song so far
   kSongLen = lenarraykSong

   ; Extend the size of your song to accomodate the new pattern
   trimkSong, \(kSongLen \+ kPatternLen

   ; Now copy
   kReadIndex init 0
   kWriteIndex = kSongLen - 1 ; indexing starts at 0 so -1
   while kReadIndex &lt; kPatternLen do
     kSong[kWriteIndex] = kPattern[kReadIndex]
    kReadIndex += 1
    kWriteIndex += 1
   od
endop

I haven't tested the code, but this is the idea. Inside the orchestra
part or directly inside your instrument you can create the patterns and
then use the UDO like this:
instr Player
   ... ; do stuff
   kPart1[] = ...
   kPart2 = ...
   kSong[] = kPart1 ; yes the first pattern should probably be added like
     ; this, I'm not sure whether you can create an array with 0 elements
   AddPattern kSong, kPart2
   AddPattern kSong, kPart1
   ...
endin

HTH.

Best wishes,

Jeanette