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:
SFile chngetks "LAST_FILE_DROPPED"
if changed(SFile) == 1 then
event "i", 2000, 0, 1
This instrument is always on and listens for a for a file drop. When a file is dropped, it triggers this instrument:
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
gi1 ftgen 36, 0, 0, 1, SFile, 0, 4, 0
elseif iX > 30 && iX < 60 then
gi1 ftgen 38, 0, 0, 1, SFile, 0, 4, 0
elseif iX > 60 && iX < 90 then
gi1 ftgen 40, 0, 0, 1, SFile, 0, 4, 0
elseif iX > 90 && iX < 120 then
gi1 ftgen 41, 0, 0, 1, SFile, 0, 4, 0
elseif iX > 120 && iX < 150 then
gi1 ftgen 43, 0, 0, 1, SFile, 0, 4, 0
elseif iX > 150 && iX < 180 then
gi1 ftgen 45, 0, 0, 1, SFile, 0, 4, 0
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:
iFreq = sr/ftlen(p4)
aPhs phasor iFreq
aTab tab aPhs, p4, 1
outs aTab, aTab
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)