Attached is a simple example that will let you drag samples onto the midi keyboard, and have them then associated with that key. Itās really just a proof of concept. You may need the latest beta which wonāt be cooked for about 20 minutes or so. It worked without my latest changes on Windows, but if youāre OSX you will probably need the beta.
SamplePlay.csd (1.9 KB)
Hereās the highlights:
instr 1000
SFile chngetks "LAST_FILE_DROPPED"
if changed(SFile) == 1 then
event "i", 2000, 0, 1
endif
endin
This instrument is always on and listens for a for a file drop. When a file is dropped, it triggers this instrument:
instr 2000
SFile chnget "LAST_FILE_DROPPED"
iX chnget "MOUSE_X"
iY chnget "MOUSE_Y"
if iY > 228 && iY < 250 then
if iX > 13 && iX < 30 then
prints "C1"
gi1 ftgen 36, 0, 0, 1, SFile, 0, 4, 0
elseif iX > 30 && iX < 60 then
prints "D1"
gi1 ftgen 38, 0, 0, 1, SFile, 0, 4, 0
elseif iX > 60 && iX < 90 then
prints "E1"
gi1 ftgen 40, 0, 0, 1, SFile, 0, 4, 0
elseif iX > 90 && iX < 120 then
prints "F1"
gi1 ftgen 41, 0, 0, 1, SFile, 0, 4, 0
elseif iX > 120 && iX < 150 then
prints "G1"
gi1 ftgen 43, 0, 0, 1, SFile, 0, 4, 0
elseif iX > 150 && iX < 180 then
prints "A1"
gi1 ftgen 45, 0, 0, 1, SFile, 0, 4, 0
endif
endif
endin
All this does is check the coordinates of the mouse when the file was dropped, and match it to the key under it. Iād probably make a UDO for this. As you can see, each keyās width is a multiple of 30, so this check could be done more succinctly in a loop. But you get the idea. When we enter an if block we put the sample into a corresponding function table.
For simplicity, Iāve numbered each function table to match the corresponding MIDI note number. Itās clear why I did this in the next instrument, the one that gets triggered when you press a note:
instr 1
iFreq = sr/ftlen(p4)
aPhs phasor iFreq
aTab tab aPhs, p4, 1
outs aTab, aTab
endin
p4 is the current MIDI note, which is also the number of the corresponding table. Youāll see that I create some dummy function tables in the score. This just stops Csound from giving out if you play a note before you have loaded a samples.
Some things to note:
- this only works for the first few notes, youāll need to add hook for the rest
- it would probably be simpler to use images/pads to drop samples on, at least you would be working with a larger landing space.
- Iāve attached below another file that uses a UDO to check if the mouse is over a pad. It might be handy for you.
DragPad.csd (1.7 KB)