Revision 4:2b996e1d64da
| 1-wavread.py | ||
|---|---|---|
| 1 |
from scikits.audiolab import wavread |
|
| 2 |
|
|
| 3 |
# specify a file name |
|
| 4 |
filename = "viola.wav" |
|
| 5 |
|
|
| 6 |
# extract audio from file |
|
| 7 |
x, fs, enc = wavread(filename) |
|
| 8 |
|
|
| 9 |
# print out the first 50 samples |
|
| 10 |
print x[0:50] |
|
| 11 |
|
|
| 12 |
|
|
| 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 | ||
|---|---|---|
| 1 |
from scikits.audiolab import Sndfile |
|
| 2 |
from scikits.audiolab import Format |
|
| 3 |
from scikits.audiolab import play |
|
| 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 and print the encoding format |
|
| 26 |
enc = f.encoding |
|
| 27 |
print "encoding format: ",enc |
|
| 28 |
|
|
| 29 |
# extract the number of frames - single samples for |
|
| 30 |
# mono and pairs of samples for stereo |
|
| 31 |
nsamples = f.nframes |
|
| 32 |
|
|
| 33 |
|
|
| 34 |
################################################# |
|
| 35 |
######## READ AUDIO SAMPLES FROM THE FILE ####### |
|
| 36 |
################################################# |
|
| 37 |
|
|
| 38 |
# we can read audio samples using the read_frame method |
|
| 39 |
data = f.read_frames(nsamples) |
|
| 40 |
|
|
| 41 |
|
|
| 42 |
################################################# |
|
| 43 |
############# PLAYING AN AUDIO FILE ############# |
|
| 44 |
################################################# |
|
| 45 |
|
|
| 46 |
# play the audio file data in 'data' at 44100Hz |
|
| 47 |
play(data,fs=44100) |
|
| 48 |
|
|
| 49 |
|
|
| 50 |
################################################# |
|
| 51 |
################################################# |
|
| 52 |
################################################# |
|
| 3-sndfile-write.py | ||
|---|---|---|
| 1 |
from scikits.audiolab import Sndfile |
|
| 2 |
from scikits.audiolab import Format |
|
| 3 |
from scikits.audiolab import play |
|
| 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 and print the encoding format |
|
| 26 |
enc = f.encoding |
|
| 27 |
print "encoding format: ",enc |
|
| 28 |
|
|
| 29 |
# extract the number of frames - single samples for |
|
| 30 |
# mono and pairs of samples for stereo |
|
| 31 |
nsamples = f.nframes |
|
| 32 |
|
|
| 33 |
|
|
| 34 |
################################################# |
|
| 35 |
######## READ AUDIO SAMPLES FROM THE FILE ####### |
|
| 36 |
################################################# |
|
| 37 |
|
|
| 38 |
# we can read audio samples using the read_frame method |
|
| 39 |
data = f.read_frames(nsamples) |
|
| 40 |
|
|
| 41 |
|
|
| 42 |
################################################# |
|
| 43 |
########## WRITING TO A NEW AUDIO FILE ########## |
|
| 44 |
################################################# |
|
| 45 |
|
|
| 46 |
# create a name for the new file |
|
| 47 |
new_filename = 'output_file.wav' |
|
| 48 |
|
|
| 49 |
# create the output audio data, in this case a simple copy |
|
| 50 |
output_data = data |
|
| 51 |
|
|
| 52 |
# Create a Sndfile instance for writing wav files @ 44100 Hz |
|
| 53 |
format = Format('wav')
|
|
| 54 |
f = Sndfile(new_filename, 'w', format, 1, 44100) |
|
| 55 |
|
|
| 56 |
# Write out the first 3 seconds worth of samples (fs*3) |
|
| 57 |
f.write_frames(output_data[:fs*3]) |
|
| 58 |
|
|
| 59 |
# close the audio file |
|
| 60 |
f.close() |
|
| 61 |
|
|
| 62 |
################################################# |
|
| 63 |
################################################# |
|
| 64 |
################################################# |
|
| 3a-sndfile-convert-wav-to-aiff-in-folder.py | ||
|---|---|---|
| 1 |
import os |
|
| 2 |
from scikits.audiolab import Sndfile |
|
| 3 |
from scikits.audiolab import Format |
|
| 4 |
|
|
| 5 |
# get the current working directory |
|
| 6 |
path = os.getcwd() |
|
| 7 |
|
|
| 8 |
# get all file names in the current directory |
|
| 9 |
files = os.listdir(path) |
|
| 10 |
|
|
| 11 |
# for each file name |
|
| 12 |
for filename in files: |
|
| 13 |
|
|
| 14 |
# if the file ends with a .wav extension |
|
| 15 |
if filename.endswith('.wav'):
|
|
| 16 |
|
|
| 17 |
# create a Sndfile instance for the file |
|
| 18 |
f_in = Sndfile(filename, 'r') |
|
| 19 |
|
|
| 20 |
# extract the number of frames |
|
| 21 |
numframes = f_in.nframes |
|
| 22 |
|
|
| 23 |
# read all audio samples into 'data' |
|
| 24 |
data = f_in.read_frames(numframes) |
|
| 25 |
|
|
| 26 |
# extract the name (without extension from the file name) |
|
| 27 |
name,extension = os.path.splitext(filename) |
|
| 28 |
|
|
| 29 |
# create a new filename with a .aiff extension |
|
| 30 |
new_filename = name + '.aiff' |
|
| 31 |
|
|
| 32 |
# create the new format based on aiff |
|
| 33 |
format = Format('aiff')
|
|
| 34 |
|
|
| 35 |
# create a new Sndfile instance for the output file |
|
| 36 |
f_out = Sndfile(new_filename, 'w', format, f_in.channels, f_in.samplerate) |
|
| 37 |
|
|
| 38 |
# write out audio samples to the new file |
|
| 39 |
f_out.write_frames(data[:numframes]) |
|
| 40 |
|
|
| 41 |
# close the audio file |
|
| 42 |
f_out.close() |
|
| 43 |
|
|
| 44 |
|
|
| 4-lists-and-python-arrays-for-audio.py | ||
|---|---|---|
| 1 |
import array as array |
|
| 2 |
|
|
| 3 |
|
|
| 4 |
################################################# |
|
| 5 |
############# PYTHON LISTS FOR AUDIO? ########### |
|
| 6 |
################################################# |
|
| 7 |
|
|
| 8 |
# I am a list of audio samples |
|
| 9 |
myAudio = [-0.25,0.0,0.25,0.5] |
|
| 10 |
|
|
| 11 |
# turn it up! |
|
| 12 |
myAudio = myAudio*2 |
|
| 13 |
|
|
| 14 |
# oh dear! |
|
| 15 |
print myAudio |
|
| 16 |
|
|
| 17 |
# also, look what got into our array |
|
| 18 |
myAudio.append("hello!")
|
|
| 19 |
|
|
| 20 |
# oh dear again! - Python stores type information for every entry? Do we want |
|
| 21 |
# to do that 44100 times a second? |
|
| 22 |
print myAudio |
|
| 23 |
|
|
| 24 |
|
|
| 25 |
################################################# |
|
| 26 |
############# PYTHON ARRAYS FOR AUDIO? ########## |
|
| 27 |
################################################# |
|
| 28 |
|
|
| 29 |
# I am an array of audio samples |
|
| 30 |
myAudio = array.array('d',[-0.25,0.0,0.25,0.5])
|
|
| 31 |
|
|
| 32 |
# this doesn't work, that's good |
|
| 33 |
# myAudio.append("hello")
|
|
| 34 |
|
|
| 35 |
# but what about this! |
|
| 36 |
myAudio = myAudio*2 |
|
| 37 |
|
|
| 38 |
# oh dear oh dear oh dear |
|
| 39 |
print myAudio |
|
| 5-basic-numpy.py | ||
|---|---|---|
| 1 |
import numpy as np |
|
| 2 |
|
|
| 3 |
|
|
| 4 |
################################################# |
|
| 5 |
############# NUMPY ARRAYS FOR AUDIO? ########### |
|
| 6 |
################################################# |
|
| 7 |
|
|
| 8 |
# I am a numpy array containing some audio |
|
| 9 |
myAudio = np.array([-0.25,0.0,0.25,0.5]) |
|
| 10 |
|
|
| 11 |
# turn it up! |
|
| 12 |
myAudio = myAudio*2. |
|
| 13 |
|
|
| 14 |
# great! |
|
| 15 |
print myAudio |
|
| 16 |
|
|
| 17 |
|
|
| 18 |
# what can we find out about our numpy audio? |
|
| 19 |
|
|
| 20 |
|
|
| 21 |
print "Dimensions: ",myAudio.ndim |
|
| 22 |
|
|
| 23 |
print "Size of each dimension: ",myAudio.shape |
|
| 24 |
|
|
| 25 |
print "Total number of elements: ", myAudio.size # this is product of shape dimensions |
|
| 26 |
|
|
| 27 |
print "Data type: ",myAudio.dtype # ...or dtype.name |
|
| 28 |
|
|
| 29 |
print "Bytes per element: ",myAudio.itemsize |
|
| 30 |
|
|
| 31 |
print "Second element: ",myAudio[1] |
|
| 32 |
|
|
| 33 |
print "All elements up to (but not including) 2: ",myAudio[:2] |
|
| 34 |
|
|
| 35 |
print "All elements from index 2 to end: ",myAudio[2:] |
|
| 36 |
|
|
| 37 |
print "Abs!: ",np.abs(myAudio) |
|
| 38 |
|
|
| 39 |
print "Min: ",np.min(myAudio) |
|
| 40 |
|
|
| 41 |
print "Max: ",np.max(myAudio) |
|
| 42 |
|
|
| 43 |
print "Sum: ",np.sum(myAudio) |
|
| 44 |
|
|
| 45 |
print "Sqrt: ",np.sqrt(9) |
|
| 46 |
|
|
| 47 |
print "FFT!: ",np.fft.fft(myAudio) |
|
| 48 |
|
|
| 49 |
print "Random!: ",np.random.random(4) |
|
| 50 |
|
|
| 51 |
print "Ones: ",np.ones(4) |
|
| 52 |
|
|
| 53 |
print "Zeros: ",np.zeros(4) |
|
| 6-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 |
|
|
| 7-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 |
|
|
| 8-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 |
################################################# |
|
| 9-dsp-block-by-block.py | ||
|---|---|---|
| 1 |
import numpy as np |
|
| 2 |
from scikits.audiolab import wavread |
|
| 3 |
|
|
| 4 |
################################################# |
|
| 5 |
############ EXTRACT AUDIO FROM FILE ############ |
|
| 6 |
################################################# |
|
| 7 |
|
|
| 8 |
x, fs, enc = wavread("viola.wav")
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
################################################# |
|
| 12 |
#### CALCULATE RMS OF EACH AUDIO BLOCK #### |
|
| 13 |
################################################# |
|
| 14 |
|
|
| 15 |
hop_size = 2048 # set hop size |
|
| 16 |
frame_size = 4096 # set frame size |
|
| 17 |
frame = np.zeros(frame_size) # initialise frame with zeros |
|
| 18 |
window = np.hanning(frame_size) # create window of the same length as the hop size |
|
| 19 |
|
|
| 20 |
# create empty numpy array to hold our |
|
| 21 |
rms = np.array([]) |
|
| 22 |
|
|
| 23 |
# run through signal frame by frame |
|
| 24 |
for n in range(0,x.size-hop_size,hop_size): |
|
| 25 |
|
|
| 26 |
# extract a segment of length hop_size |
|
| 27 |
buffer = x[n:n+hop_size] |
|
| 28 |
|
|
| 29 |
# add new segment to frame, shifting back samples of frame |
|
| 30 |
frame = np.append(frame[hop_size:frame_size],buffer) |
|
| 31 |
|
|
| 32 |
# calculate RMS |
|
| 33 |
rms_val = np.sqrt(np.power(frame,2).mean()) |
|
| 34 |
|
|
| 35 |
# add amplitude to our numpy array |
|
| 36 |
rms = np.append(rms,rms_val) |
|
| 37 |
|
|
| 38 |
print rms |
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
Also available in: Unified diff