Revision 6:fd0f9d0615b2

View differences:

1-wavread.py
1 1
from scikits.audiolab import wavread
2
from scikits.audiolab import play
2 3

  
3 4
# specify a file name
4 5
filename = "viola.wav"
5 6

  
6 7
# extract audio from file
7
x, fs, enc = wavread(filename)     
8
samples, fs, enc = wavread(filename)     
8 9

  
9 10
# print out the first 50 samples
10
print x[0:50]
11
print samples[0:50]
11 12

  
12 13

  
14
#################################################
15
############# PLAYING AN AUDIO FILE #############
16
#################################################
17

  
18
# play the audio file data in 'data' at 44100Hz
19
play(samples,fs=44100)
20

  
21

  
22
#################################################
23
#################################################
24
#################################################
10-plotting.py
1
import numpy as np
2
from scikits.audiolab import wavread
3
from matplotlib import pylab as plt
4

  
5
#################################################
6
############ EXTRACT AUDIO FROM FILE ############
7
#################################################
8

  
9
x, fs, enc = wavread("drums_mono.wav")
10

  
11

  
12
#################################################
13
#### CALCULATE RMS OF EACH AUDIO BLOCK ####
14
#################################################
15

  
16
hop_size = 1024                              # set hop size
17
frame_size = hop_size*2                     # set frame size
18
frame = np.zeros(frame_size)                # initialise frame with zeros
19
window = np.hanning(frame_size)             # create window of the same length as the hop size
20

  
21
# create empty numpy array to hold our 
22
rms = np.array([])
23

  
24
# run through signal frame by frame 
25
for n in range(0,x.size-hop_size,hop_size):
26
    
27
    # extract a segment of length hop_size
28
    buffer = x[n:n+hop_size]                               
29
    
30
    # add new segment to frame, shifting back samples of frame
31
    frame = np.append(frame[hop_size:frame_size],buffer)  
32
    
33
    # calculate RMS
34
    rms_val = np.sqrt(np.power(frame,2).mean())
35
    
36
    # add amplitude to our numpy array
37
    rms = np.append(rms,rms_val)
38

  
39
print rms
40

  
41

  
42
plt.plot(rms)
43
plt.title("RMS")
44
plt.xlabel("time")
45
plt.ylabel("value")
46
plt.show()
47
    
48
                                   
2-sndfile-read-and-play.py
28 28

  
29 29
# extract the number of frames - single samples for
30 30
# mono and pairs of samples for stereo
31
nsamples = f.nframes
31
num_samples = f.nframes
32 32

  
33 33

  
34 34
#################################################
......
36 36
#################################################
37 37

  
38 38
# we can read audio samples using the read_frame method
39
data = f.read_frames(nsamples)
39
samples = f.read_frames(num_samples)
40 40

  
41 41

  
42 42
#################################################
43 43
############# PLAYING AN AUDIO FILE #############
44 44
#################################################
45 45

  
46
# play the audio file data in 'data' at 44100Hz
47
play(data,fs=44100)
46
# play the audio file data in 'samples' at the sampling frequency 'fs' 
47
play(samples,fs)
48 48

  
49 49

  
50 50
#################################################
3-sndfile-write.py
28 28

  
29 29
# extract the number of frames - single samples for
30 30
# mono and pairs of samples for stereo
31
nsamples = f.nframes
31
num_samples = f.nframes
32 32

  
33 33

  
34 34
#################################################
......
36 36
#################################################
37 37

  
38 38
# we can read audio samples using the read_frame method
39
data = f.read_frames(nsamples)
39
samples = f.read_frames(num_samples)
40 40

  
41 41

  
42 42
#################################################
......
47 47
new_filename = 'output_file.wav'
48 48

  
49 49
# create the output audio data, in this case a simple copy
50
output_data = data
50
output_samples = samples
51 51

  
52 52
# Create a Sndfile instance for writing wav files @ 44100 Hz
53 53
format = Format('wav')
54 54
f = Sndfile(new_filename, 'w', format, 1, 44100)
55 55

  
56 56
# Write out the first 3 seconds worth of samples (fs*3)
57
f.write_frames(output_data[:fs*3])
57
f.write_frames(output_samples)
58 58

  
59 59
# close the audio file
60 60
f.close()
4-lists-and-python-arrays-for-audio.py
21 21
# to do that 44100 times a second?
22 22
print myAudio
23 23

  
24

  
24
"""
25 25
#################################################
26 26
############# PYTHON ARRAYS FOR AUDIO? ##########
27 27
#################################################
......
36 36
myAudio = myAudio*2
37 37

  
38 38
# oh dear oh dear oh dear
39
print myAudio
39
print myAudio
40
"""
6-synthesize-mono-noise-with-numpy.py
1 1
import numpy as np
2 2
from scikits.audiolab import Sndfile
3 3
from scikits.audiolab import Format
4
import pylab as plt
4 5

  
5 6

  
6 7
#################################################
......
18 19
    # generate random value and turn it down!
19 20
    noise[i] = np.random.random()*0.2
20 21

  
21

  
22 22
#################################################
23 23
########## WRITING NOISE TO AUDIO FILE ##########
24 24
#################################################
......
36 36
# close the audio file
37 37
f.close()
38 38

  
39

  
40
#################################################
41
############### PLOTTING THE NOISE ##############
42
#################################################
43

  
44
plt.plot(noise[0:512])
45
plt.show()
7-synthesize-stereo-sines-with-numpy.py
1 1
import numpy as np
2
import pylab as plt
2 3
from scikits.audiolab import Sndfile
3 4
from scikits.audiolab import Format
4 5

  
......
27 28
    
28 29
    # generate tone and set volume for left and right
29 30
    tone[i][0] = np.sin(2*np.pi*freq*phaseVal)*amp
30
    tone[i][1] = np.sin(2*np.pi*freq*phaseVal)*amp
31
    tone[i][1] = np.sin(2*np.pi*(freq*2)*phaseVal)*amp
31 32

  
32 33

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

  
51

  
52
#################################################
53
############### PLOT USING PYLAB ################
54
#################################################   
55

  
56
toneL = tone[:,0]
57
toneR = tone[:,1]
58

  
59
plt.subplot(211)
60
plt.plot(toneL[0:200])
61
plt.subplot(212)
62
plt.plot(toneR[0:200])
63
plt.show()
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
#################################################
9-dsp-block-by-block.py
1 1
import numpy as np
2
import pylab as plt
2 3
from scikits.audiolab import wavread
3 4

  
4 5
#################################################
......
9 10

  
10 11

  
11 12
#################################################
12
#### CALCULATE RMS OF EACH AUDIO BLOCK ####
13
####### CALCULATE RMS OF EACH AUDIO BLOCK #######
13 14
#################################################
14 15

  
15
hop_size = 2048                             # set hop size
16
frame_size = 4096                           # set frame size
16
hop_size = 512                             # set hop size
17
frame_size = hop_size*2                     # set frame size
17 18
frame = np.zeros(frame_size)                # initialise frame with zeros
18 19
window = np.hanning(frame_size)             # create window of the same length as the hop size
19 20

  
......
24 25
for n in range(0,x.size-hop_size,hop_size):
25 26
    
26 27
    # extract a segment of length hop_size
28
    frame = x[n:n+hop_size]                               
29
        
30
    # calculate RMS
31
    rms_val = np.sqrt(np.power(frame,2).mean())
32
    
33
    # add amplitude to our numpy array
34
    rms = np.append(rms,rms_val)
35

  
36
print rms
37

  
38
""" I AM THE OVERLAP VERSION
39
# run through signal frame by frame 
40
for n in range(0,x.size-hop_size,hop_size):
41
    
42
    # extract a segment of length hop_size
27 43
    buffer = x[n:n+hop_size]                               
28 44
    
29 45
    # add new segment to frame, shifting back samples of frame
......
36 52
    rms = np.append(rms,rms_val)
37 53

  
38 54
print rms
39
    
55
"""
56

  
57
plt.plot(rms)
58
plt.title("RMS")
59
plt.xlabel("time")
60
plt.ylabel("value")
61
plt.show()
40 62
                                   
41 63
 

Also available in: Unified diff