-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSFPlay.sc
56 lines (54 loc) · 1.84 KB
/
SFPlay.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
SFPlay {
classvar <playfunc, <buffer;
*play {arg path, starttime = 0, outbus = 0, server;
var c, soundfile, info, numChannels, numFrames, sampleRate, duration;
c = Condition.new;
server = server ? Server.default;
path.notNil.if({soundfile = path;
info = SoundFile.new;
info.openRead(soundfile);
numChannels = info.numChannels;
numFrames = info.numFrames;
sampleRate = info.sampleRate;
info.close;
duration = numFrames / sampleRate;
(starttime > duration).if({
("Starttime is greater than file duration of "++ duration.asString).postln},
{
buffer = Buffer.read(server, soundfile, starttime * sampleRate);
playfunc = SynthDef(\sfplay, {
Out.ar(outbus,
PlayBuf.ar(numChannels, buffer.bufnum, BufRateScale.kr(buffer.bufnum)) *
EnvGen.kr(Env([1,1], [(duration - starttime) +1]),
doneAction: 2)); }).play(server);
AppClock.sched((duration-starttime), {this.stop});
})},
{CocoaDialog.getPaths({ arg paths;
paths.do({ arg p;
soundfile = p;
info = SoundFile.new;
info.openRead(soundfile);
numChannels = info.numChannels;
numFrames = info.numFrames;
sampleRate = info.sampleRate;
info.close;
duration = numFrames / sampleRate;
(starttime > duration).if({
("Starttime is greater than file duration of "++ duration.asString).postln},{
buffer = Buffer.read(server, soundfile, starttime * sampleRate, -1);
playfunc = SynthDef(\sfplay, {
Out.ar(outbus,
PlayBuf.ar(numChannels, buffer.bufnum, BufRateScale.kr(buffer.bufnum)) *
EnvGen.kr(Env([1,1], [(duration - starttime) +1]),
doneAction: 2)); }).play(server);
AppClock.sched((duration-starttime), {this.stop});
})
})
},{
"cancelled".postln})});
}
*stop {
this.playfunc.free;
this.buffer.free;
}
}