def lfo_envelope(): synth = WaveSynth(samplerate=100) lfo = Linear(1000, samplerate=synth.samplerate) lfo = EnvelopeFilter(lfo, 2, 1, 4, 0.3, 2, stop_at_end=True) from matplotlib import pyplot as plot plot.title("LFO Envelope") plot.plot(list(lfo)) plot.show()
def echo_sample(): synth = WaveSynth(samplerate=44100) lfo = Linear(1, -0.0001, min_value=-99999) s = synth.pulse(220, .5, fm_lfo=lfo).fadeout(.2) with Output(s.samplerate, s.samplewidth, s.nchannels) as out: e = s.copy().echo(1, 4, 0.5, 0.4) # echo out.play_sample(e) e = s.copy().echo(1, 30, 0.15, 0.5) # simple "reverberation" (simulated using fast echos) out.play_sample(e) out.wait_all_played()
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, mixing="sequential") as out: synth = WaveSynth() 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) # s_all.write_wav("fmtestall.wav") out.wait_all_played()
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): values = list(itertools.islice(osc.blocks(), int(rate*duration/params.norm_osc_blocksize))) 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(5000)) 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))
def create_unfiltered_osc(): def create_chord_osc(clazz, **arguments): if is_audio and self.arp_filter_gui.input_mode.get().startswith("chords"): chord_keys = major_chord_keys(note, octave) if self.arp_filter_gui.input_mode.get() == "chords3": chord_keys = list(chord_keys)[:-1] a4freq = self.a4_choice.get() chord_freqs = [note_freq(n, o, a4freq) for n, o in chord_keys] self.statusbar["text"] = "major chord: "+" ".join(n for n, o in chord_keys) oscillators = [] arguments["amplitude"] /= len(chord_freqs) for f in chord_freqs: arguments["frequency"] = f oscillators.append(clazz(**arguments)) return MixingFilter(*oscillators) else: # no chord (or an LFO instead of audio output oscillator), return one osc for only the given frequency return clazz(**arguments) waveform = from_gui.input_waveformtype.get() amp = from_gui.input_amp.get() bias = from_gui.input_bias.get() if waveform == "noise": return WhiteNoise(freq, amplitude=amp, bias=bias, samplerate=self.synth.samplerate) elif waveform == "linear": startlevel = from_gui.input_lin_start.get() increment = from_gui.input_lin_increment.get() minvalue = from_gui.input_lin_min.get() maxvalue = from_gui.input_lin_max.get() return Linear(startlevel, increment, minvalue, maxvalue) else: phase = from_gui.input_phase.get() pw = from_gui.input_pw.get() fm_choice = from_gui.input_fm.get() pwm_choice = from_gui.input_pwm.get() if fm_choice in (None, "", "<none>"): fm = None elif fm_choice.startswith("osc"): osc_num = int(fm_choice.split()[1]) osc = all_oscillators[osc_num - 1] fm = self.create_osc(note, octave, osc.input_freq.get(), all_oscillators[osc_num-1], all_oscillators) else: raise ValueError("invalid fm choice") if pwm_choice in (None, "", "<none>"): pwm = None elif pwm_choice.startswith("osc"): osc_num = int(pwm_choice.split()[1]) osc = all_oscillators[osc_num-1] pwm = self.create_osc(note, octave, osc.input_freq.get(), osc, all_oscillators) else: raise ValueError("invalid fm choice") if waveform == "pulse": return create_chord_osc(Pulse, frequency=freq, amplitude=amp, phase=phase, bias=bias, pulsewidth=pw, fm_lfo=fm, pwm_lfo=pwm, samplerate=self.synth.samplerate) elif waveform == "harmonics": harmonics = self.parse_harmonics(from_gui.harmonics_text.get(1.0, tk.END)) return create_chord_osc(Harmonics, frequency=freq, harmonics=harmonics, amplitude=amp, phase=phase, bias=bias, fm_lfo=fm, samplerate=self.synth.samplerate) else: o = { "sine": Sine, "triangle": Triangle, "sawtooth": Sawtooth, "sawtooth_h": SawtoothH, "square": Square, "square_h": SquareH, "semicircle": Semicircle, "pointy": Pointy, }[waveform] return create_chord_osc(o, frequency=freq, amplitude=amp, phase=phase, bias=bias, fm_lfo=fm, samplerate=self.synth.samplerate)