Revision 2:7b9a06f64ab1

View differences:

dsp-delay.py
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
#################################################
synthesize-mono-noise-with-numpy.py
1
import numpy as np
2
from scikits.audiolab import Sndfile
3
from scikits.audiolab import Format
4

  
5

  
6
#################################################
7
############ CREATE SOME RANDOM NOISE ###########
8
#################################################
9

  
10
# set our sampling frequency
11
fs = 44100
12

  
13
# create a numpy array that can hold 3 seconds of sound
14
noise = np.empty(3*fs)
15

  
16
# set each element to a random value (noise)
17
for i in range(noise.size):
18
    # generate random value and turn it down!
19
    noise[i] = np.random.random()*0.2
20

  
21

  
22
#################################################
23
########## WRITING NOISE TO AUDIO FILE ##########
24
#################################################
25

  
26
# create a name for the new file
27
new_filename = 'noise.wav'
28

  
29
# Create a Sndfile instance for writing wav files @ 44100 Hz
30
format = Format('wav')
31
f = Sndfile(new_filename, 'w', format, 1, fs)
32

  
33
# Write out the samples to the file
34
f.write_frames(noise)
35

  
36
# close the audio file
37
f.close()
38

  
synthesize-stereo-sines-with-numpy.py
1
import numpy as np
2
from scikits.audiolab import Sndfile
3
from scikits.audiolab import Format
4

  
5

  
6
#################################################
7
############## CREATE A SINE TONE ###############
8
#################################################
9

  
10
# set our sampling frequency
11
fs = 44100
12

  
13
# create a numpy array that can hold 3 seconds of sound
14
tone = np.zeros((3*fs,2))
15

  
16
# set frequency to 440Hz
17
freq = 440
18

  
19
# set volume to 0.3
20
amp = 0.3
21

  
22
# set values of each channel
23
for i in range(tone.shape[0]):
24
    
25
    # calculate phase value
26
    phaseVal = np.float(i)/np.float(fs)
27
    
28
    # generate tone and set volume for left and right
29
    tone[i][0] = np.sin(2*np.pi*freq*phaseVal)*amp
30
    tone[i][1] = np.sin(2*np.pi*freq*phaseVal)*amp
31

  
32

  
33
#################################################
34
########## WRITING TONES TO AUDIO FILE ##########
35
#################################################
36

  
37
# create a name for the new file
38
new_filename = 'tone.wav'
39

  
40
# Create a Sndfile instance for writing wav files @ 44100 Hz
41
format = Format('wav')
42
f = Sndfile(new_filename, 'w', format, 2, fs)
43

  
44
# Write out the samples to the file
45
f.write_frames(tone)
46

  
47
# close the audio file
48
f.close()
49

  

Also available in: Unified diff