| 1 |
Applying digital signal processing at a higher sampling rate |
| 2 |
============================================================================================================================================ |
| 3 |
|
| 4 |
|
| 5 |
**07-upsamp-processing.py** |
| 6 |
|
| 7 |
This script demonstrates how to use pyo to apply processing on a sound |
| 8 |
at a very high sampling rate. |
| 9 |
|
| 10 |
Pyo offers two functions to upsample and downsample an audio file:: |
| 11 |
|
| 12 |
upsamp(path, outfile, up, order) |
| 13 |
downsamp(path, outfile, down, order) |
| 14 |
|
| 15 |
Where: |
| 16 |
|
| 17 |
- `path` is the path to the file to process. |
| 18 |
- `outfile` is the path where to save the resampled file. |
| 19 |
- `up`, `down` is the resampling factor (new sampling rate will be |
| 20 |
this value times the current sampling rate). |
| 21 |
- `order` is the length, in samples, of the anti-aliasing lowpass filter. |
| 22 |
|
| 23 |
.. code-block:: python |
| 24 |
|
| 25 |
import os |
| 26 |
from pyo import * |
| 27 |
|
| 28 |
# Upsampling factor. |
| 29 |
SR_FACTOR = 8 |
| 30 |
|
| 31 |
# Size, in samples, of the anti-aliasing lowpass filters. |
| 32 |
SIZE = 512 |
| 33 |
|
| 34 |
# Sound to process. |
| 35 |
IN_SND = "../snds/baseballmajeur_m.aif" |
| 36 |
|
| 37 |
# Temporary audio files. |
| 38 |
UP_SND = os.path.join(os.path.expanduser("~"), "Desktop", "temp_sound_up_%i.aif" % SR_FACTOR) |
| 39 |
PROC_SND = os.path.join(os.path.expanduser("~"), "Desktop", "temp_sound_disto.aif") |
| 40 |
|
| 41 |
# Output sound file. |
| 42 |
DOWN_SND = os.path.join(os.path.expanduser("~"), "Desktop", "output_disto_down.aif") |
| 43 |
|
| 44 |
# Retrieve info about the source file. |
| 45 |
DUR = sndinfo(IN_SND)[1] |
| 46 |
SR = sndinfo(IN_SND)[2] |
| 47 |
|
| 48 |
print("Up sampling the source file %i times..." % SR_FACTOR) |
| 49 |
upsamp(IN_SND, UP_SND, SR_FACTOR, SIZE) |
| 50 |
|
| 51 |
print("Apply distortion at a sampling rate of %i Hz." % (SR * SR_FACTOR)) |
| 52 |
|
| 53 |
# Initialize the Server in offline mode, at the desired sampling rate. |
| 54 |
s = Server(sr=SR * SR_FACTOR, nchnls=1, duplex=0, audio="offline").boot() |
| 55 |
|
| 56 |
# Set the recording parameters. |
| 57 |
s.recordOptions(dur=DUR + 0.1, filename=PROC_SND, fileformat=0, sampletype=0) |
| 58 |
|
| 59 |
# Read the upsampled version of the source file. |
| 60 |
sf = SfPlayer(UP_SND, loop=False, interp=4, mul=0.7) |
| 61 |
# Apply distortion on it. |
| 62 |
dist = Disto(sf, drive=0.75, slope=0.7, mul=0.3) |
| 63 |
# Lowpass filtering. |
| 64 |
filt = Biquad(dist, freq=8000, q=0.7, type=0).out() |
| 65 |
|
| 66 |
# Start the offline processing. |
| 67 |
s.start() |
| 68 |
|
| 69 |
print("Down sampling the processed sound %i times..." % SR_FACTOR) |
| 70 |
downsamp(PROC_SND, DOWN_SND, SR_FACTOR, SIZE) |
| 71 |
|
| 72 |
# Cleanup the temporary files. |
| 73 |
os.remove(UP_SND) |
| 74 |
os.remove(PROC_SND) |
| 75 |
|
| 76 |
print("Done") |
| 77 |
|