|
| 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() |
0 commit comments