Revision 6:fd0f9d0615b2 8-dsp-delay.py
| 8-dsp-delay.py | ||
|---|---|---|
| 1 | 1 |
import numpy as np |
| 2 |
from scikits.audiolab import wavread |
|
| 3 |
from scikits.audiolab import Format |
|
| 2 | 4 |
from scikits.audiolab import Sndfile |
| 3 |
from scikits.audiolab import Format |
|
| 4 | 5 |
|
| 5 | 6 |
################################################# |
| 6 | 7 |
######## CREATING A SOUND FILE INSTANCE ######### |
| 7 | 8 |
################################################# |
| 8 | 9 |
|
| 9 |
# create Sndfile instance with our example audio file |
|
| 10 |
f = Sndfile('viola.wav', 'r')
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
################################################# |
|
| 14 |
######## EXTRACTING AUDIO FILE META-DATA ######## |
|
| 15 |
################################################# |
|
| 16 |
|
|
| 17 |
# extract and print sample rate |
|
| 18 |
fs = f.samplerate |
|
| 19 |
print "sample rate: ",fs |
|
| 20 |
|
|
| 21 |
# extract and print the number of channels |
|
| 22 |
nc = f.channels |
|
| 23 |
print "number of channels: ",nc |
|
| 24 |
|
|
| 25 |
# extract the number of samples |
|
| 26 |
nsamples = f.nframes |
|
| 27 |
|
|
| 28 |
|
|
| 29 |
################################################# |
|
| 30 |
######## READ AUDIO SAMPLES FROM THE FILE ####### |
|
| 31 |
################################################# |
|
| 32 |
|
|
| 33 |
# we can read audio samples using the read_frame method |
|
| 34 |
data = f.read_frames(nsamples) |
|
| 35 |
|
|
| 10 |
# extract audio from file |
|
| 11 |
samples, fs, enc = wavread('viola.wav')
|
|
| 36 | 12 |
|
| 37 | 13 |
################################################# |
| 38 | 14 |
########## APPLY A DELAY TO THE SAMPLES ######### |
| ... | ... | |
| 45 | 21 |
alpha = 0.75 |
| 46 | 22 |
|
| 47 | 23 |
# create an empty array for the output |
| 48 |
out = np.zeros(data.size)
|
|
| 24 |
out = np.zeros(samples.size)
|
|
| 49 | 25 |
|
| 50 | 26 |
# for every sample in the array |
| 51 |
for i in range(data.size):
|
|
| 27 |
for i in range(samples.size):
|
|
| 52 | 28 |
|
| 53 | 29 |
# if we are safe to apply the delay without negative indexing |
| 54 | 30 |
if (i >= delay): |
| 55 |
out[i] = data[i] + data[i-delay]*alpha
|
|
| 31 |
out[i] = samples[i] + samples[i-delay]*alpha
|
|
| 56 | 32 |
else: |
| 57 |
out[i] = data[i] # hacky
|
|
| 33 |
out[i] = samples[i] # hacky
|
|
| 58 | 34 |
|
| 59 | 35 |
|
| 60 | 36 |
################################################# |
Also available in: Unified diff