| 1 |
""" |
| 2 |
01-multi-rate-synthesis.py - Doing synthesis 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 synthesis algorithm generates aliasing in |
| 7 |
the output signal. The solution is to increase the sampling rate, so the |
| 8 |
nyquist frequency, and to use anti-aliasing filters when converting from one |
| 9 |
rate to another. |
| 10 |
|
| 11 |
Pyo allows to compute chunks of code at different sampling rates than the |
| 12 |
one with which the server was started. You should do this only for the objects |
| 13 |
you need to process with a higher sampling rate, without changing the server's |
| 14 |
sampling rate, otherwise the program will be very CPU consuming. |
| 15 |
|
| 16 |
You start a new resampling block with the method: |
| 17 |
|
| 18 |
Server.beginResamplingBlock(x) |
| 19 |
|
| 20 |
where `x`, a power-of-two, is the resampling factor. A negative power-of-two |
| 21 |
will start a downsampling block of code. |
| 22 |
|
| 23 |
To close the block, simply call: |
| 24 |
|
| 25 |
Server.endResamplingBlock() |
| 26 |
|
| 27 |
Everything between the two calls will be computed with the new sampling rate. |
| 28 |
|
| 29 |
Audio signals must be resampled before used with a different sampling rate. |
| 30 |
The Resample object does this. Inside a resampling block, it will convert the |
| 31 |
signal to the new sampling rate, and outside the resampling block, it will |
| 32 |
convert the signal back to the original sampling rate. Its `mode` argument lets |
| 33 |
choose the quality of the interpolation/decimation filter used to resample the |
| 34 |
signal. |
| 35 |
|
| 36 |
""" |
| 37 |
from pyo import * |
| 38 |
|
| 39 |
s = Server().boot() |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class UpSampFM: |
| 44 |
""" |
| 45 |
Frequency modulation synthesis that can be computed a higher sampling rate. |
| 46 |
|
| 47 |
:Args: |
| 48 |
|
| 49 |
fmindex: float or PyoObject, optional |
| 50 |
The modulation index of the FM synthesis. |
| 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, fmindex=20, upfactor=8, filtmode=32): |
| 60 |
|
| 61 |
|
| 62 |
self.fmindex = Sig(fmindex) |
| 63 |
|
| 64 |
|
| 65 |
server = self.fmindex.getServer() |
| 66 |
|
| 67 |
|
| 68 |
server.beginResamplingBlock(upfactor) |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
self.fmindexUp = Resample(self.fmindex, mode=1) |
| 73 |
|
| 74 |
|
| 75 |
self.fmUp = FM(carrier=492, ratio=2, index=self.fmindexUp) |
| 76 |
|
| 77 |
|
| 78 |
server.endResamplingBlock() |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
self.output = Resample(self.fmUp, mode=filtmode) |
| 83 |
|
| 84 |
|
| 85 |
def out(self): |
| 86 |
self.output.out() |
| 87 |
return self |
| 88 |
|
| 89 |
def sig(self): |
| 90 |
return self.output |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
index = Sig(20) |
| 95 |
index.ctrl([SLMap(5, 50, "lin", "value", 20)], title="Modulation Index") |
| 96 |
|
| 97 |
|
| 98 |
fm1 = FM(carrier=492, ratio=2, index=index) |
| 99 |
|
| 100 |
|
| 101 |
fm2 = UpSampFM(index) |
| 102 |
|
| 103 |
|
| 104 |
output = Interp(fm1, fm2.sig(), 0, mul=0.5).out() |
| 105 |
output.ctrl([SLMap(0, 1, "lin", "interp", 0)], title="Up Sampling: without <=> with") |
| 106 |
|
| 107 |
sp = Spectrum(output) |
| 108 |
|
| 109 |
s.gui(locals()) |