Skip to content

Commit 1939574

Browse files
committed
Add further examples
1 parent 9b39de4 commit 1939574

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

examples/buffer-read.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Example: Read and play an audio file.
5+
6+
Before running this script, the SC server must be started, and the following
7+
SynthDef stored:
8+
9+
SynthDef(\playbuf, { |out = 0, bufnum = 0, gain = 0.0|
10+
var data = PlayBuf.ar(1, bufnum, loop: 1) * gain.dbamp;
11+
Out.ar(out, Pan2.ar(data));
12+
}).store;
13+
14+
You will also need to download some sample audio:
15+
curl https://nssdc.gsfc.nasa.gov/planetary/sound/apollo_13_problem.wav -o apollo.wav
16+
"""
17+
18+
AUDIO_FILE = "apollo.wav"
19+
20+
from supercollider import Server, Synth, Buffer
21+
import time
22+
23+
#-------------------------------------------------------------------------------
24+
# Create connection to default server on localhost:57110
25+
#-------------------------------------------------------------------------------
26+
server = Server()
27+
28+
#-------------------------------------------------------------------------------
29+
# Read sample data into a Buffer.
30+
#-------------------------------------------------------------------------------
31+
buf = Buffer.read(server, AUDIO_FILE)
32+
buf_info = buf.info
33+
print("Read buffer, sample rate %d, duration %.1fs" % (buf_info["sample_rate"], buf_info["num_frames"] / buf_info["sample_rate"]))
34+
35+
#-------------------------------------------------------------------------------
36+
# Calculate the required playback rate (akin to SC BufRateScale.kr)
37+
# and begin playback.
38+
#-------------------------------------------------------------------------------
39+
server_sample_rate = server.status["sample_rate_nominal"]
40+
buffer_sample_rate = buf_info["sample_rate"]
41+
rate = buffer_sample_rate / server_sample_rate
42+
synth = Synth(server, 'playbuf', { "buffer" : buf, "rate": rate })
43+
44+
try:
45+
while True:
46+
time.sleep(0.1)
47+
except KeyboardInterrupt:
48+
synth.free()
49+
buf.free()

examples/buffer-write.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Example: Write the contents of a Buffer to disk.
5+
"""
6+
7+
from supercollider import Server, Synth, Buffer, HEADER_FORMAT_WAV
8+
import math
9+
10+
OUTPUT_FILE = "/tmp/440.wav"
11+
12+
#-------------------------------------------------------------------------------
13+
# Create connection to default server on localhost:57110
14+
#-------------------------------------------------------------------------------
15+
server = Server()
16+
17+
#-------------------------------------------------------------------------------
18+
# Create a Buffer, generate a 440Hz sine, and write to a .wav.
19+
#-------------------------------------------------------------------------------
20+
length = 1024
21+
buf = Buffer.alloc(server, length)
22+
buf.set([ math.sin(n * math.pi * 2.0 * 440.0 / sample_rate) for n in range(int(length)) ])
23+
buf.write(OUTPUT_FILE, HEADER_FORMAT_WAV)

examples/bus-example.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Example: Audio routing with buses.
5+
6+
Before running this script, the SC server must be started, and the following
7+
SynthDefs stored:
8+
9+
SynthDef(\dust, { |out = 0, gain = 0.0|
10+
var data = Dust.ar(10) * gain.dbamp;
11+
Out.ar(out, Pan2.ar(data));
12+
}).store;
13+
14+
SynthDef(\reverb, { |in = 0, out = 2|
15+
var data = In.ar(in, 2);
16+
data = FreeVerb.ar(data, 0.7, 0.8, 0.5);
17+
Out.ar(out, data);
18+
}).store;
19+
"""
20+
21+
from supercollider import Server, Synth, Buffer, AudioBus, ADD_TO_TAIL
22+
23+
import time
24+
25+
#-------------------------------------------------------------------------------
26+
# Create connection to default server on localhost:57110
27+
#-------------------------------------------------------------------------------
28+
server = Server()
29+
30+
#-------------------------------------------------------------------------------
31+
# Create a Buffer, read and play a sample.
32+
#-------------------------------------------------------------------------------
33+
bus = AudioBus(server, 2)
34+
synth = Synth(server, 'dust', { "out": bus })
35+
reverb = Synth(server, 'reverb', { "in": bus, "out": 0 }, target=server, action=ADD_TO_TAIL)
36+
37+
try:
38+
while True:
39+
time.sleep(0.1)
40+
except KeyboardInterrupt:
41+
bus.free()
42+
synth.free()
43+
reverb.free()

0 commit comments

Comments
 (0)