Dear all,
Inspired by the following thread,
I have written the two codes below the post. I would like to know
- which one is more user friendly (= more convenient) for you to use;
- if there is a way to reduce the response rate of
canReceiveDragHandler_
in code 2; - if there is a way to have the message appear on/above the
DragBoth
instead of posting to the Post window.
I think code 2 is more convenient, but .postln
in the canReceiveDragHandler_
posts the message too often.
code 1
(
s.waitForBoot{
var win, buffer;
win = Window("accepts all items and posts messages", Rect(200,200,500,40)).front;
DragBoth(win, Rect(10,10,480,20))
.align_(\center)
.string_("drag an audio file here")
.action_{ |dropped|
("\nThe dropped item:" + dropped.object).postln;
if(PathName(dropped.object).isFile) {
var aSoundFile = SoundFile();
if(aSoundFile.openRead(dropped.object)){
aSoundFile.close;
if (buffer.class.asSymbol == \Buffer) {
buffer.free;
"The previous buffer has been freed.".postln
};
buffer = Buffer.read(s, dropped.object, action: { |buf|
buf.postln;
buf.play
});
} {
"The item you have dropped is not a supported sound file for SuperCollider. Please drop a sound file that is supported.".postln
}
} {
"The item you have dropped is not a file. Please drop a supported sound file.".postln
}
};
win.onClose_{
buffer.free;
"\nThe buffer you loaded is freed.".postln;
s.cachedBuffersDo { |buf| buf.postln };
}
}
)
code 2
(
s.waitForBoot{
var soundFileOrNot, win, buffer;
soundFileOrNot = { SoundFile.openRead(View.currentDrag).asString == "a SoundFile" };
win = Window("accepts only one of the sound files supported by SuperCollider", Rect(200,200,500,40))
.acceptsMouseOver_(true)
.front;
DragBoth(win, Rect(10,10,480,20))
.align_(\center)
.string_("drag an audio file here")
.canReceiveDragHandler_{
if(soundFileOrNot.().not) {
"\nThe item you are dropping is not a supported sound file. Please drag a supported sound file.".postln
};
soundFileOrNot.()
}
.action_{ |dropped|
"".postln;
if (buffer.class.asSymbol == \Buffer) {
buffer.free;
"The previous buffer has been freed.".postln
};
buffer = Buffer.read(s, dropped.object, action: { |buf|
buf.postln;
buf.play
})
};
win.onClose_{
buffer.free;
"\nThe buffer you loaded is freed.".postln;
s.cachedBuffersDo { |buf| buf.postln };
}
}
)