| 1 |
""" |
| 2 |
01-multi-rate-processing.py - Doing processing at very high sampling rate. |
| 3 |
|
| 4 |
In numerical audio computing, it is sometimes useful to be able to process |
| 5 |
a signal with much more timing precision than what the usual sampling rates |
| 6 |
offer. A typical case is when the effect applied to the sound adds a lot of |
| 7 |
harmonics. Higher harmonics will quickly wrap around the Nyquist frequency, |
| 8 |
producing aliasing in the output signal. The solution is to increase the |
| 9 |
sampling rate, so the nyquist frequency, and to use anti-aliasing filters |
| 10 |
when converting from one rate to another. |
| 11 |
|
| 12 |
Pyo allows to compute chunks of code at different sampling rates than the |
| 13 |
one with which the server was started. You should do this only for the objects |
| 14 |
you need to process with a higher sampling rate, without changing the server's |
| 15 |
sampling rate, otherwise the program will be very CPU consuming. |
| 16 |
|
| 17 |
You start a new resampling block with the method: |
| 18 |
|
| 19 |
Server.beginResamplingBlock(x) |
| 20 |
|
| 21 |
where `x`, a power-of-two, is the resampling factor. A negative power-of-two |
| 22 |
will start a downsampling block of code. |
| 23 |
|
| 24 |
To close the block, simply call: |
| 25 |
|
| 26 |
Server.endResamplingBlock() |
| 27 |
|
| 28 |
Everything between the two calls will be computed with the new sampling rate. |
| 29 |
|
| 30 |
Audio signals must be resampled before used with a different sampling rate. |
| 31 |
The Resample object does this. Inside a resampling block, it will convert the |
| 32 |
signal to the new sampling rate, and outside the resampling block, it will |
| 33 |
convert the signal back to the original sampling rate. Its `mode` argument lets |
| 34 |
choose the quality of the interpolation/decimation filter used to resample the |
| 35 |
signal. |
| 36 |
|
| 37 |
""" |
| 38 |
from pyo import * |
| 39 |
|
| 40 |
|
| 41 |
class UpSampDisto: |
| 42 |
""" |
| 43 |
Upsampled distortion effect. |
| 44 |
|
| 45 |
:Args: |
| 46 |
|
| 47 |
input: PyoObject |
| 48 |
The signal to process. |
| 49 |
drive: float or PyoObject, optional |
| 50 |
Amount of distortion applied to the signal, between 0 and 1. |
| 51 |
upfactor: int (power-of-two), optional |
| 52 |
Resampling factor. |
| 53 |
filtmode: int, optional |
| 54 |
The interpolation/decimation mode. See Resample's man page |
| 55 |
for details. |
| 56 |
|
| 57 |
""" |
| 58 |
|
| 59 |
def __init__(self, input, drive=0.5, upfactor=8, filtmode=32): |
| 60 |
|
| 61 |
self.input = InputFader(input) |
| 62 |
|
| 63 |
|
| 64 |
self.drive = Sig(drive) |
| 65 |
|
| 66 |
|
| 67 |
server = self.drive.getServer() |
| 68 |
|
| 69 |
|
| 70 |
server.beginResamplingBlock(upfactor) |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
self.inputUp = Resample(self.input, mode=filtmode) |
| 76 |
self.driveUp = Resample(self.drive, mode=1) |
| 77 |
|
| 78 |
|
| 79 |
self.disto = Disto(self.inputUp, drive=self.driveUp) |
| 80 |
|
| 81 |
|
| 82 |
server.endResamplingBlock() |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
self.output = Resample(self.disto, mode=filtmode, mul=0.5) |
| 87 |
|
| 88 |
|
| 89 |
def setInput(self, x, fadetime=0.05): |
| 90 |
self.input.setInput(x, fadetime) |
| 91 |
|
| 92 |
def setDrive(self, x): |
| 93 |
self.drive.value = x |
| 94 |
|
| 95 |
def out(self, chnl=0): |
| 96 |
self.output.out(chnl) |
| 97 |
return self |
| 98 |
|
| 99 |
def sig(self): |
| 100 |
return self.output |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
s = Server().boot() |
| 105 |
|
| 106 |
|
| 107 |
src1 = Sine(freq=722, mul=0.7) |
| 108 |
src1.ctrl([SLMapFreq(722)], title="Sine frequency") |
| 109 |
src2 = SfPlayer("../snds/flute.aif", loop=True) |
| 110 |
|
| 111 |
src = Interp(src1, src2, 0) |
| 112 |
src.ctrl([SLMap(0, 1, "lin", "interp", 0)], title="Source: sine <=> flute") |
| 113 |
|
| 114 |
|
| 115 |
drv = Sig(0) |
| 116 |
drv.ctrl(title="Drive") |
| 117 |
|
| 118 |
|
| 119 |
dist = Disto(src, drive=drv, mul=0.5) |
| 120 |
|
| 121 |
|
| 122 |
updist = UpSampDisto(src, drv) |
| 123 |
|
| 124 |
|
| 125 |
output = Interp(dist, updist.sig(), 0, mul=0.5).out() |
| 126 |
output.ctrl([SLMap(0, 1, "lin", "interp", 0)], title="Up Sampling: without <=> with") |
| 127 |
|
| 128 |
sp = Spectrum(output) |
| 129 |
|
| 130 |
s.gui(locals()) |