To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
The primary repository for this project is hosted at https://github.com/Codasign/york-software-bootcamp-audio-day.git .
This repository is a read-only copy which is updated automatically every hour.
root / dsp-delay.py @ 2:7b9a06f64ab1
History | View | Annotate | Download (2.02 KB)
| 1 |
import numpy as np |
|---|---|
| 2 |
from scikits.audiolab import Sndfile |
| 3 |
from scikits.audiolab import Format |
| 4 |
|
| 5 |
#################################################
|
| 6 |
######## CREATING A SOUND FILE INSTANCE #########
|
| 7 |
#################################################
|
| 8 |
|
| 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 |
|
| 36 |
|
| 37 |
#################################################
|
| 38 |
########## APPLY A DELAY TO THE SAMPLES #########
|
| 39 |
#################################################
|
| 40 |
|
| 41 |
# delay in samples
|
| 42 |
delay = fs/2
|
| 43 |
|
| 44 |
# volume of delayed sound
|
| 45 |
alpha = 0.75
|
| 46 |
|
| 47 |
# create an empty array for the output
|
| 48 |
out = np.zeros(data.size) |
| 49 |
|
| 50 |
# for every sample in the array
|
| 51 |
for i in range(data.size): |
| 52 |
|
| 53 |
# if we are safe to apply the delay without negative indexing
|
| 54 |
if (i >= delay):
|
| 55 |
out[i] = data[i] + data[i-delay]*alpha |
| 56 |
else:
|
| 57 |
out[i] = data[i] # hacky
|
| 58 |
|
| 59 |
|
| 60 |
#################################################
|
| 61 |
########## WRITING TO A NEW AUDIO FILE ##########
|
| 62 |
#################################################
|
| 63 |
|
| 64 |
# create a name for the new file
|
| 65 |
new_filename = 'delayed_audio.wav'
|
| 66 |
|
| 67 |
# Create a Sndfile instance for writing wav files @ 44100 Hz
|
| 68 |
format = Format('wav')
|
| 69 |
f = Sndfile(new_filename, 'w', format, 1, 44100) |
| 70 |
|
| 71 |
# Write out the samples to an audio file
|
| 72 |
f.write_frames(out) |
| 73 |
|
| 74 |
# close the audio file
|
| 75 |
f.close() |
| 76 |
|
| 77 |
#################################################
|
| 78 |
#################################################
|
| 79 |
#################################################
|