Revision 1:99434d86405a
| 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) |
|
| 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 |
|
| 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 |
|
|
| 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 |
################################################# |
|
| 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 |
################################################# |
|
| wavread.py | ||
|---|---|---|
| 1 |
from scikits.audiolab import wavread |
|
| 2 |
|
|
| 3 |
# specify a file name |
|
| 4 |
filename = "viola3.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 |
|
|
Also available in: Unified diff