def stereo_pan(): synth = WaveSynth() # panning a stereo source: wave = Sample("samples/SOS 020.wav").clip( 6, 12).normalize().fadein(0.5).fadeout(0.5).lock() osc = Sine(0.4) panning = wave.copy().pan(lfo=osc).fadeout(0.2) with Output.for_sample(panning) as out: out.play_sample(panning) # panning a generated mono source: fm = Sine(0.5, 0.1999, bias=0.2) wave = synth.triangle(220, 5, fm_lfo=fm).lock() osc = Sine(0.4) panning = wave.copy().pan(lfo=osc).fadeout(0.2) with Output.for_sample(panning) as out: out.play_sample(panning)
def pwm(): from matplotlib import pyplot as plot synth = WaveSynth(samplerate=1000) pwm_lfo = Sine(0.05, amplitude=0.49, bias=0.5, samplerate=synth.samplerate) s1 = synth.pulse(4, amplitude=0.6, duration=20, pwm_lfo=pwm_lfo) plot.figure(figsize=(16, 4)) plot.title("Pulse width modulation") plot.ylim([-35000, 35000]) plot.plot(s1.get_frame_array()) plot.show() with Output(nchannels=1) as out: synth = WaveSynth() lfo2 = Sine(0.2, amplitude=0.48, bias=0.5) s1 = synth.pulse(440 / 6, amplitude=0.5, duration=6, fm_lfo=None, pwm_lfo=lfo2) out.play_sample(s1)
def lfo_func(): rate = 1000 s = Sine(1, amplitude=100, bias=40, samplerate=rate) s = AbsFilter(s) s = ClipFilter(s, minimum=20, maximum=80) s = DelayFilter(s, 0.5) s = iter(s) s = [next(s) for _ in range(rate * 2)] import matplotlib.pyplot as plot a = plot.subplot(111) a.set_ylim([-50, 100]) a.plot(s) plot.show()
def echo_lfo(): synth = WaveSynth(22050) s = Sine(440, amplitude=25000, samplerate=synth.samplerate) s = EnvelopeFilter(s, .2, .2, 0, 0, 1.5, stop_at_end=True) s = EchoFilter(s, .15, 5, 0.3, 0.6) s = ClipFilter(s, -32000, 32000) frames = [int(v) for v in s] import matplotlib.pyplot as plot plot.plot(frames) plot.show() samp = Sample.from_array(frames, synth.samplerate, 1) with Output.for_sample(samp) as out: out.play_sample(samp)
def filter(self, source): wave = self.input_waveform.get() freq = self.input_rate.get() amp = self.input_depth.get() / 2.0 samplerate = self.gui.synth.samplerate bias = 1.0 - amp if amp == 0.0 or freq == 0.0 or wave in (None, "", "<none>", "<off>"): return source if wave == "sine": modulator = Sine(freq, amp, bias=bias, samplerate=samplerate) elif wave == "triangle": modulator = Triangle(freq, amp, bias=bias, samplerate=samplerate) elif wave == "sawtooth": modulator = SawtoothH(freq, 9, amp, bias=bias, samplerate=samplerate) elif wave == "square": modulator = SquareH(freq, 9, amp, bias=bias, samplerate=samplerate) return AmpMudulationFilter(source, iter(modulator))
def modulate_amp(): from matplotlib import pyplot as plot synth = WaveSynth() freq = 220 s1 = synth.triangle(freq, duration=2) m = synth.sine(2, duration=2, amplitude=0.4, bias=0.5) s1.modulate_amp(m) plot.title("Amplitude modulation by another waveform") plot.plot(s1.get_frame_array()) plot.show() with Output(nchannels=1) as out: out.play_sample(s1) s1 = synth.triangle(freq, duration=2) m = Sine(3, amplitude=0.4, bias=0.5) s1.modulate_amp(m) plot.title("Amplitude modulation by an oscillator") plot.plot(s1.get_frame_array()) plot.show() with Output(nchannels=1) as out: out.play_sample(s1)
def fm(): synth = WaveSynth(samplerate=8000) from matplotlib import pyplot as plot freq = 2000 lfo1 = Sine(1, amplitude=0.4, samplerate=synth.samplerate) s1 = synth.sine(freq, duration=3, fm_lfo=lfo1) plot.title("Spectrogram") plot.ylabel("Freq") plot.xlabel("Time") plot.specgram(s1.get_frame_array(), Fs=synth.samplerate, noverlap=90, cmap=plot.cm.gist_heat) plot.show() with Output(nchannels=1, samplerate=22050) as out: synth = WaveSynth(samplerate=22050) freq = 440 lfo1 = Linear(5, samplerate=synth.samplerate) lfo1 = EnvelopeFilter(lfo1, 1, 0.5, 0.5, 0.5, 1) s1 = synth.sine(freq, duration=3, fm_lfo=lfo1) s_all = s1.copy() out.play_sample(s1) lfo1 = Sine(1, amplitude=0.2, samplerate=synth.samplerate) s1 = synth.sine(freq, duration=2, fm_lfo=lfo1) s_all.join(s1) out.play_sample(s1) lfo1 = Sine(freq / 17, amplitude=0.5, samplerate=synth.samplerate) s1 = synth.sine(freq, duration=2, fm_lfo=lfo1) s_all.join(s1) out.play_sample(s1) lfo1 = Sine(freq / 6, amplitude=0.5, samplerate=synth.samplerate) s1 = synth.sine(freq, duration=2, fm_lfo=lfo1) s_all.join(s1) out.play_sample(s1) lfo1 = Sine(1, amplitude=0.4, samplerate=synth.samplerate) s1 = synth.triangle(freq, duration=2, fm_lfo=lfo1) s_all.join(s1) out.play_sample(s1) freq = 440 * 2 lfo1 = Sine(freq / 80, amplitude=0.4, samplerate=synth.samplerate) s1 = synth.triangle(freq, duration=2, fm_lfo=lfo1) s_all.join(s1) out.play_sample(s1)
def make_sample(freq): fmfm = Linear(0, 0.002, max_value=99999) fm = Sine(0.05, amplitude=0.5, fm_lfo=fmfm) s1 = synth.sawtooth(freq, duration, amplitude=0.6, fm_lfo=fm) s1.envelope(0.01, 0.1, 0.6, 2) return s1
def osc_bench(): rate = 44100 duration = 2.0 def get_values(osc): oscv = iter(osc) values = [next(oscv) for _ in range(int(rate * duration))] fm = FastSine(220) print("GENERATING {:g} SECONDS SAMPLE DATA {:d} HZ USING LFO.".format( duration, rate)) print(" WAVEFORM: with-FM / no-FM / optimized") # sine print(" Sine: ", end="") start = time.time() get_values(Sine(440, fm_lfo=fm)) duration1 = time.time() - start start = time.time() get_values(Sine(440)) duration2 = time.time() - start start = time.time() get_values(FastSine(440)) duration3 = time.time() - start print("{:.3f} / {:.3f} / {:.3f}".format(duration1, duration2, duration3)) # triangle print(" Triangle: ", end="") start = time.time() get_values(Triangle(440, fm_lfo=fm)) duration1 = time.time() - start start = time.time() get_values(Triangle(440)) duration2 = time.time() - start start = time.time() get_values(FastTriangle(440)) duration3 = time.time() - start print("{:.3f} / {:.3f} / {:.3f}".format(duration1, duration2, duration3)) # square print(" Square: ", end="") start = time.time() get_values(Square(440, fm_lfo=fm)) duration1 = time.time() - start start = time.time() get_values(Square(440)) duration2 = time.time() - start start = time.time() get_values(FastSquare(440)) duration3 = time.time() - start print("{:.3f} / {:.3f} / {:.3f}".format(duration1, duration2, duration3)) # sawtooth print(" Sawtooth: ", end="") start = time.time() get_values(Sawtooth(440, fm_lfo=fm)) duration1 = time.time() - start start = time.time() get_values(Sawtooth(440)) duration2 = time.time() - start start = time.time() get_values(FastSawtooth(440)) duration3 = time.time() - start print("{:.3f} / {:.3f} / {:.3f}".format(duration1, duration2, duration3)) # pulse print(" Pulse: ", end="") start = time.time() get_values(Pulse(440, fm_lfo=fm)) duration1 = time.time() - start start = time.time() get_values(Pulse(440)) duration2 = time.time() - start start = time.time() get_values(FastPulse(440)) duration3 = time.time() - start print("{:.3f} / {:.3f} / {:.3f}".format(duration1, duration2, duration3)) # square_h print(" Square_H: ", end="") start = time.time() get_values(SquareH(440, fm_lfo=fm)) duration1 = time.time() - start start = time.time() get_values(SquareH(440)) duration2 = time.time() - start print("{:.3f} / {:.3f}".format(duration1, duration2)) print("Sawtooth_H: ", end="") start = time.time() get_values(SawtoothH(440, fm_lfo=fm)) duration1 = time.time() - start start = time.time() get_values(SawtoothH(440)) duration2 = time.time() - start print("{:.3f} / {:.3f}".format(duration1, duration2)) print(" Noise: ", end="") start = time.time() get_values(WhiteNoise()) duration1 = time.time() - start print(" {:.3f}".format(duration1)) print(" Linear: ", end="") start = time.time() get_values(Linear(0, 0.0001)) duration1 = time.time() - start print(" {:.3f}".format(duration1))