Cabbage Logo
Back to Cabbage Site

Passing arrays to instruments

So I’ve read that we can’t pass arrays in p-fields to instruments (is this still the case?)

I want to be able to pass a single instrument an argument to change it’s sequence, or rather have one sequencer instrument that is dynamic to different arrays (of different lengths). I’ve come up with a workaround (as suggested) using a udo that converts an array to and f-table, returning the table number that I can pass into the event, whereupon the instr converts it back to an array. It all seems very inefficient and long winded! I wonder whether there any benefit to using an array… is there a better way to do the below?

opcode ConvArr2Ft_i, i, k[]
    kArr[] xin
    iTab ftgen 0, 0, lenarray(kArr), -2, 0
    copya2ftab kArr, iTab
    xout iTab
endop

gkArrPt1[] fillarray 1,2,3,4,5
gkArrPt2[] fillarray 1,2,3,4,5,6,7,8,9,10

giFtPt1 = ConvArr2Ft_i(gkArrPt1)
giFtPt2 = ConvArr2Ft_i(gkArrPt2)

print giFtPt1

instr 1, Seq
	iTab = p4
	iTrans = p5
	kCnt init 0
	kArr[] init ftlen(iTab)
	copyf2array kArr, iTab
	if metro(4) == 1 then
		event("i", "Blip", 0, 0.4, 0.4, kArr[kCnt%lenarray(kArr)]+48+iTrans)
		kCnt+=1
	endif
endin

instr 10, Blip
	pset 0.2, 1, 60
	aenv expon p4, p3, 0.001
	ares poscil aenv, cpsmidinn(p5)
	     outs ares,ares
endin

schedule(1, 0, -1, giFtPt1, 0)
schedule(1.1, 0, -1, giFtPt2, 12)

Why not just stick to arrays and avoid the need to write to a function table? Or alternatively just use function tables and drop arrays? I’m not sure why you need to combine both?

Good question. I’m not against working with f-tables in the slightest. But I guess working with arrays is a) more familiar b) has access to the array operations and udo’s (palindrome etc), but the problem arises when I want to pass the array reference to the sequencer instrument.

I’m very much for writing something once. Are you suggesting that I create an instrument that’s unique to each array? If I’m overlooking something, please put me on the right path! :smile:

Why not use the global arrays and pass them around between instruments? So what I would do is to create a sequencer instrument much like the one you have. Then I would create a sequence generator instrument that will dynamically update the array the sequencer is reading. Would this approach work?

Yeah I getcha. Just approach it a different way. I’ll se how I get on :slight_smile: chrs

In the meantime, I’ve prepared a simple video toot.


The final code used it attached below. Note that I use the gkNoteCount variable in this code although I never use it in the video…

Sequencer.csd (1.6 KB)

as you do! :joy: cheers man

I’m under pressure to produce more video content from my students! It seems they don’t like to read much :joy:

Hi guys,

I’ve implemented the sequencer in the video in a score I’m working on but it gets realtime midi input and plays the sequence starting on that key, however I only want the sequence to change its starting note after its completed the cycle so that it stays in time with everything else if you understand what I mean. I’d also like it to keep playing until the note is changed (similar to the sequencer in the micro brute)

This is my first time using midi properly in csound so I’m kinda lost, I’ve attatched the .orc & .csd (I’m using the same orchestra for a couple of scores).

Sequencer Problem.zip (6.9 KB)

It’s usually a good idea to prepare as simple an example as you can. I’m not sure many people here have the time to read over hundreds of lines of code they didn’t write. I started, but just gave up and went back to the first example. I’ve updated it now so that it only moves to the new pattern after then of the 8 beat sequence.

Sequencer.csd (1.7 KB)

Note: you need to hit update in order to generate the first set of notes. Before that you won’t hear anything. :wink:

I’ll do that in future, never really thought about that! Will have a look now, Cheers Rory

As you are probably aware, there are lots and lots of ways to do this. The most challenging part is finding the way that works best for you. Let me know if you have any questions about that code.