Cabbage Logo
Back to Cabbage Site

Arrays and / or Function Tables, copyf2array issue

Hello fellow csounders,

noob here. I’m still confused about csound - to a degree where it’s even hard to formulate proper questions. :sweat_smile: This one is about arrays and function tables, I’ve been reading the manual, floss, tried google, searched the forum and can’t get the most basic thing done: loading a audio sample into an array :slight_smile:

Now, what I would do in any of the other languages that I’m somewhat familiar with (java, js, python etc.) is:

  1. read audio file and split it into snippets, save all snippets into array
  2. iterate over snippet-array, analyze dominant pitch of each snippet and save to pitch-array (index of pitch array = index of respective snippet in snippet-array, or use a 2dim-array)
  3. find all snippets with a given pitch by iterating over pitch-array and play back as desired

now here the question / problem:
afaik in csound I can not directly read a soundfile to an array, correct? the only way I found to get audio sample data into an array is by loading it into a function table first, then copy it to array with copyf2array-opcode (which alone raises quite some init / perf time confusion for me).

this is what I tried (in various ways):

-n -d 
; Initialize the global variables. 
sr = 44100
ksmps = 32
nchnls = 2
0dbfs  = 1

gkFirstCyclePassed = 0

gSSamplePath = "any_sound_file.wav" ; maybe fox.wav ;)
giSampleTable ftgen 0, 0, 0, -1, gSSamplePath, 0, 0, 0

instr 99

    gkSampleArray[] init ftlen(giSampleTable)

    if gkFirstCyclePassed == 0 then 
        copyf2array gkSampleArray, giSampleTable
        printks "----- array length: %i\n", 1, lenarray(gkSampleArray)
        gkFirstCyclePassed = 1


i99 0 z

it throws an error at startup:

Deferred-size ftable 101.000000 load not available at perf time.INIT ERROR in instr 99 (opcode copyf2array.k) line 25: No table for copy2ftab

this suggests that the function table is not available at perf time? is it really? I can very well play it back in another instrument using poscil3 for example, is this not perf time? what am I missing?

or am I approaching this wrong? would a seasoned csounder instantly come up with a more appropriate way of dealing with the sample data in this case? Mixing ftables and arrays also seems a bit wrong but after reading about ftables, I strongly wish to be working with arrays (as this is what I’m more familiar with).

any hints will be highly appreciated! thanks in advance!

kind regards,

I just tested here and it looks like that table copy opcode doesn’t like GEN01 tables with deferred lengths. On the other hand, can diskin2 not be used to fill an array of samples? Looks like that options was added some time ago.

edit - no, it looks like you can just send the channel data to different array on each k-cycle. Not much good here…

What about something like this:

instr 99
    iCnt init 0
    iLen = ftlen(giSampleTable)
    aSamples[] init iLen
    while iCnt < iLen do
        aSamples[iCnt] = tab(iCnt, giSampleTable)
        iCnt +=1 

dear @rorywalsh ,
(humbly and mildly embarassed:) Thank you, this saves my day!
Didn’t see the wood for the trees…