Skip to content

trim

Adjust size of a one-dimensional array.

📝 Note

Up to Csound 6, trimi was called trim_i.

Syntax

trimi(iarray, ilen)
trim(xarray, klen)
trim_i iarray, ilen
trim xarray, klen

Performance

iarray -- a one-dimensional irate array

xarray -- a one-dimensional array

klen -- desired length

Makes the array the desired length, either truncating or padding with zero/Null values.

Examples

Here is an example of the trim opcode. It uses the file trim-modern.csd.

Example of the trim opcode.
<CsoundSynthesizer>
<CsOptions>
-n
</CsOptions>
<CsInstruments>
instr 1
  A1:k[] = fillarray(0, 1, 2, 3, 4, 5, 6, 7) ; <-- 8 elements
  printf("lenarray(A1) before slicearray: %d\n", 1, lenarray(A1))
  A2:k[] = slicearray(A1, 1, 4) ; <-- 4 elements
  printf("lenarray(A2) AFTER  slicearray: %d\n", 1, lenarray(A2))
  trim(A1, 4)
  printks("A1 after trim: { ", 0)
  Cnt:k = 0
  while (Cnt < lenarray:k(A1)) do
    printf("%d ", Cnt + 1, A1[Cnt])
    Cnt += 1
  od
  printks("}\n", 0)
  turnoff()
endin
</CsInstruments>

<CsScore>
i1  0 0.1
e
</CsScore>

</CsoundSynthesizer>

Here is an example of the trim opcode. It uses the file trim.csd.

Example of the trim opcode.
<CsoundSynthesizer>
<CsOptions>
-n
</CsOptions>
<CsInstruments>
instr 1
    kA1[]   fillarray       0, 1, 2, 3, 4, 5, 6, 7 ; <-- 8 elements
            printf          "lenarray(kA1) before slicearray: %d\n", 1, lenarray(kA1)
            kA2[]     slicearray      kA1, 1, 4 ; <-- 4 elements
            printf          "lenarray(kA2) AFTER  slicearray: %d\n", 1, lenarray(kA2)
            trim            kA1, 4
            printks         "kA1 after trim: { ", 0
      kCnt    =               0

    while (kCnt < lenarray:k(kA1)) do
                    printf  "%d ", kCnt + 1, kA1[kCnt]
            kCnt    +=      1
     od

            printks         "}\n", 0
            turnoff
endin


</CsInstruments>

<CsScore>
i1  0 0.1
e
</CsScore>

</CsoundSynthesizer>

See also

Array opcodes

Credits

Author: John ffitch 2018

New in version 6.12