# activation gate tone_out_T0 = 1 tone_out_N0 = tone_out_T0 * SR tone_out_N1 = N_out gate_0 = np.zeros_like(y) gate_0[tone_out_N0:tone_out_N1] = 1 rs = envelopes.region_segmenter(H) wl = windowed_lookup(x, W) pv = pvoc_synth(signal.get_window('hann', W), signal.get_window('hann', W), W, H, lambda n: wl.access(n)) av = time_map_tstretch.attack_avoider(attack_times, -3 * H, W + 2 * H, H) aaa = time_map_tstretch.attack_avoid_access( lambda t, r: pv.process(int(np.round(t)), r), av) ps = pitch_shift.pitch_shifter(aaa, B=H) adsr = envelopes.gate_to_adsr(200, 1, 1, 200) # control signals pos_sig = np.linspace(3 * SR, 3 * SR, N_out) ps_sig = np.linspace(ps_0, ps_0, N_out) ts_sig = np.linspace(ts_0, ts_0, N_out) for n_ in range(0, N_out, H): H_ = min(N_out - n_, H) en, st, ed, ac, sta = adsr.gate_to_adsr_env_start_end_active(gate_0[n_:n_ + H_]) ant, ans, ane, regs = rs.region_segmenter_update(st, ed, ac) for s, e in regs: if e > s: l = e - s if st[s] > 0:
import numpy as np import matplotlib.pyplot as plt import envelopes import common N = 30 n = np.arange(N) adsr = envelopes.gate_to_adsr(5, 1, 1, 5) gate0 = np.zeros(N) gate0[5:20] = 1 states0 = adsr.gate_to_adsr_seq(gate0) gate1 = np.zeros(N) gate1[5:8] = 1 gate1[9:19] = 1 states1 = adsr.gate_to_adsr_seq(gate1) plt.figure() common.logic_plot(n, states0) plt.figure() common.logic_plot(n, states1) plt.show()
from window_tools import windowed_lookup import matplotlib.pyplot as plt # prepare input signal SR = 16000 note_params = [dict(T=2, f=440), dict(T=3, f=660)] for note in note_params: note['N'] = note['T'] * SR note['v'] = note['f'] / SR note['x'] = signal.chirp(np.arange(note['N']), note['v'], note['N'], note['v']) note['g'] = lfo.chirp(note['N'], 1 / note['N'], 1 / note['N'], phase=-0.25).squarewave() adsr = envelopes.gate_to_adsr(100, 1000, 0.5, 4000) en, st, ed, ac, sta = adsr.gate_to_adsr_env_start_end_active(note['g']) note['e'] = en['adsr'] note['y'] = note['x'] * note['e'] N = 0 for note in note_params: N += note['N'] y = np.zeros(N) n = 0 for note in note_params: y[n:n + note['N']] = note['y'] n += note['N'] y += np.random.standard_normal(N) * 1e-6 y.tofile('/tmp/in.f64')
# up a window's worth of samples pv = pvoc_synth(signal.get_window('hann', W), signal.get_window('hann', W), W, H, wl_access) # estimate attack times attack_time_pairs = attack_finder.attacks_from_spectral_diff( x, lmax_filt_rate=LMAX_FILT_RATE) attack_times = np.array([b for a, b in attack_time_pairs]) # make attack avoider av = attack_avoider(attack_times, -H, H + W, H) # synthesize gate signal gate_sig = lfo.chirp(N, GATE_F0, GATE_F1).squarewave() # make ADSR object adsr = envelopes.gate_to_adsr(SR * 0.1, SR * 0.1, 0.5, SR * .1) # make region segmenter rs = envelopes.region_segmenter(H) class time_stretch_ts_access: """ The pitch shifter needs a way to get samples to fill a ring buffer, which it then resamples to effect the pitch shift. This callable object (like a function) provides n samples requested at a time t. This particular one uses a phase vocoder to do it in a way so that the phase of the signal is smooth from one block to the next, even if the two consecutive times looked up are not separated by a hop size. This also adjusts the requested times to keep attacks from getting "smeared" if an attack_avoider is registered with it.
B=256 # Gate frequency gate_f0=5 gate_f1=7.5 # Length in samples N=int(np.round(T*SR)) # frequency of synthesizer (Hz) f0=10 f1=70 fm_f0=4.5 blss=bl_square_synth() f=lfo.chirp(N,fm_f0/SR,fm_f0/SR,x_min=f0/SR,x_max=f1/SR).sinusoid() gate=lfo.chirp(N,gate_f0/SR,gate_f1/SR).squarewave() adsr=envelopes.gate_to_adsr(SR*0.01, SR*0.01, 1, SR*0.01) x=np.zeros(N) rs=envelopes.region_segmenter(B) for n in np.arange(0,N-B,B): (adsr_dict, start, end, state, adsr_states)=adsr.gate_to_adsr_env_start_end_active(gate[n:n+B]) ant,ans,ane,regs=rs.region_segmenter_update(start,end,state) for s,e in regs: if e>s: l=e-s x[n+s:n+e]=blss.synth(f[n+s:n+e],start[s]>0)*adsr_dict['adsr'][s:e] plt.plot(np.arange(N),x)
import numpy as np import matplotlib.pyplot as plt from envelopes import gate_to_adsr from common import logic_plot N = 1000 thresh = 1 gate = np.random.standard_normal(N) gate = (gate > thresh).astype('float') gate = np.cumsum(gate) gate %= 2 #gate=np.zeros(N) #gate[25:N-25]=1 gate_to_adsr_inst = gate_to_adsr(3, 4, 0.5, 5, decay_min_dB=-24) adsr_sig = gate_to_adsr_inst.gate_to_adsr_seq(gate) adsr_env = gate_to_adsr_inst.adsr_seq_to_env(adsr_sig) logic_plot(np.arange(N), gate, label='gate') logic_plot(np.arange(N), adsr_sig, color='purple', label='ADSR states') logic_plot(np.arange(N), adsr_env, color='orange', label='ADSR envelope') plt.legend() plt.show()