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.

Statistics Download as Zip
| Branch: | Revision:

root / 8-dsp-delay.py

History | View | Annotate | Download (1.46 KB)

1 4:2b996e1d64da adamstark
import numpy as np
2 6:fd0f9d0615b2 adamstark
from scikits.audiolab import wavread
3
from scikits.audiolab import Format
4 4:2b996e1d64da adamstark
from scikits.audiolab import Sndfile
5
6
#################################################
7
######## CREATING A SOUND FILE INSTANCE #########
8
#################################################
9
10 6:fd0f9d0615b2 adamstark
# extract audio from file
11
samples, fs, enc = wavread('viola.wav')
12 4:2b996e1d64da adamstark
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 6:fd0f9d0615b2 adamstark
out = np.zeros(samples.size)
25 4:2b996e1d64da adamstark
26
# for every sample in the array
27 6:fd0f9d0615b2 adamstark
for i in range(samples.size):
28 4:2b996e1d64da adamstark
29
    # if we are safe to apply the delay without negative indexing
30
    if (i >= delay):
31 6:fd0f9d0615b2 adamstark
        out[i] = samples[i] + samples[i-delay]*alpha
32 4:2b996e1d64da adamstark
    else:
33 6:fd0f9d0615b2 adamstark
        out[i] = samples[i] # hacky
34 4:2b996e1d64da adamstark
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
#################################################