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 / 8-dsp-delay.py @ 6:fd0f9d0615b2
History | View | Annotate | Download (1.46 KB)
| 1 |
import numpy as np |
|---|---|
| 2 |
from scikits.audiolab import wavread |
| 3 |
from scikits.audiolab import Format |
| 4 |
from scikits.audiolab import Sndfile |
| 5 |
|
| 6 |
#################################################
|
| 7 |
######## CREATING A SOUND FILE INSTANCE #########
|
| 8 |
#################################################
|
| 9 |
|
| 10 |
# extract audio from file
|
| 11 |
samples, fs, enc = wavread('viola.wav')
|
| 12 |
|
| 13 |
#################################################
|
| 14 |
########## APPLY A DELAY TO THE SAMPLES #########
|
| 15 |
#################################################
|
| 16 |
|
| 17 |
# delay in samples
|
| 18 |
delay = fs/2
|
| 19 |
|
| 20 |
# volume of delayed sound
|
| 21 |
alpha = 0.75
|
| 22 |
|
| 23 |
# create an empty array for the output
|
| 24 |
out = np.zeros(samples.size) |
| 25 |
|
| 26 |
# for every sample in the array
|
| 27 |
for i in range(samples.size): |
| 28 |
|
| 29 |
# if we are safe to apply the delay without negative indexing
|
| 30 |
if (i >= delay):
|
| 31 |
out[i] = samples[i] + samples[i-delay]*alpha |
| 32 |
else:
|
| 33 |
out[i] = samples[i] # hacky
|
| 34 |
|
| 35 |
|
| 36 |
#################################################
|
| 37 |
########## WRITING TO A NEW AUDIO FILE ##########
|
| 38 |
#################################################
|
| 39 |
|
| 40 |
# create a name for the new file
|
| 41 |
new_filename = 'delayed_audio.wav'
|
| 42 |
|
| 43 |
# Create a Sndfile instance for writing wav files @ 44100 Hz
|
| 44 |
format = Format('wav')
|
| 45 |
f = Sndfile(new_filename, 'w', format, 1, 44100) |
| 46 |
|
| 47 |
# Write out the samples to an audio file
|
| 48 |
f.write_frames(out) |
| 49 |
|
| 50 |
# close the audio file
|
| 51 |
f.close() |
| 52 |
|
| 53 |
#################################################
|
| 54 |
#################################################
|
| 55 |
#################################################
|