|
| 1 | +# ECHOSYNC |
| 2 | +# This script demonstrates one example to create a feedforward, |
| 3 | +# tempo-synchronized echo effect. |
| 4 | +# |
| 5 | +# See also CONVERTTEMPOSAMPLES |
| 6 | + |
| 7 | +import soundfile |
| 8 | +import numpy as np |
| 9 | +import matplotlib.pyplot as plt |
| 10 | +from IPython.display import Audio |
| 11 | + |
| 12 | +# Import our audio file |
| 13 | +[x, Fs] = soundfile.read('sw20.wav') |
| 14 | +Ts = 1/Fs |
| 15 | + |
| 16 | +# Known tempo of recording |
| 17 | +beatsPerMin = 102 # units of beats/minute |
| 18 | + |
| 19 | +# Calculate beats fore second |
| 20 | +beatsPerSec = beatsPerMin / 60 # 1 minute/60 seconds |
| 21 | + |
| 22 | +# Calculate # of seconds per beat |
| 23 | +secPerBeat = 1/beatsPerSec |
| 24 | + |
| 25 | +# Note division |
| 26 | +# 4 = whole, 2 = half, 1 = quarter, 0.5 = 8th, 0.25 = 16th |
| 27 | +noteDiv = 0.5 |
| 28 | +# Calculate delay time in seconds |
| 29 | +timeSec = noteDiv * secPerBeat |
| 30 | + |
| 31 | +# Convert to units of samples |
| 32 | +d = int(np.fix(timeSec * Fs)) # round to nearest integer sample |
| 33 | + |
| 34 | +b = 0.75 # amplitude of delay branch |
| 35 | + |
| 36 | +# Total number of samples |
| 37 | +N = len(x) |
| 38 | +y = np.zeros([N, 1]) |
| 39 | + |
| 40 | +# Index each element of our signal to create the output |
| 41 | +for n in range(N): |
| 42 | + # When the sample number is less than the time delay |
| 43 | + # Avoid indexing a negative number |
| 44 | + if n < d + 1: |
| 45 | + # output = input |
| 46 | + y[n] = x[n] |
| 47 | + |
| 48 | + # Now add in the delayed signal |
| 49 | + else: |
| 50 | + # output = input + delayed version of input |
| 51 | + # reduce relative amplitude of delay to 3/4 |
| 52 | + echo = n - d |
| 53 | + y[n] = x[n] + b * x[echo] |
| 54 | + |
| 55 | +t = np.arange(0, N) * Ts |
| 56 | + |
| 57 | +plt.plot(t, x, t, y) |
| 58 | +plt.axis([0, 1, -1.1, 1.1]) |
| 59 | +plt.xlabel('Time (sec.)') |
| 60 | +plt.ylabel('Amplitude') |
| 61 | +plt.title('Waveform') |
| 62 | +plt.show() |
| 63 | + |
| 64 | +Audio(y, rate=Fs) |
0 commit comments