changeset 1:99434d86405a

Added some initial tutorial files
author Adam <adamstark.uk@gmail.com>
date Wed, 12 Sep 2012 20:02:32 +0100
parents b234c3417899
children 7b9a06f64ab1
files basic-numpy.py lists-and-python-arrays-for-audio.py sndfile-convert-wav-to-aiff-in-folder.py sndfile-read-and-play.py sndfile-write.py viola.wav wavread.py
diffstat 7 files changed, 264 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/basic-numpy.py	Wed Sep 12 20:02:32 2012 +0100
@@ -0,0 +1,53 @@
+import numpy as np
+
+
+#################################################
+############# NUMPY ARRAYS FOR AUDIO? ###########
+#################################################
+
+# I am a numpy array containing some audio
+myAudio = np.array([-0.25,0.0,0.25,0.5])
+
+# turn it up!
+myAudio = myAudio*2.
+
+# great!
+print myAudio
+
+
+# what can we find out about our numpy audio?
+
+
+print "Dimensions: ",myAudio.ndim
+
+print "Size of each dimension: ",myAudio.shape
+
+print "Total number of elements: ", myAudio.size # this is product of shape dimensions
+
+print "Data type: ",myAudio.dtype # ...or dtype.name
+
+print "Bytes per element: ",myAudio.itemsize 
+
+print "Second element: ",myAudio[1]
+
+print "All elements up to (but not including) 2: ",myAudio[:2]
+
+print "All elements from index 2 to end: ",myAudio[2:]
+
+print "Abs!: ",np.abs(myAudio)
+
+print "Min: ",np.min(myAudio)
+
+print "Max: ",np.max(myAudio)
+
+print "Sum: ",np.sum(myAudio)
+
+print "Sqrt: ",np.sqrt(9)
+
+print "FFT!: ",np.fft.fft(myAudio)
+
+print "Random!: ",np.random.random(4)
+
+print "Ones: ",np.ones(4)
+
+print "Zeros: ",np.zeros(4)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lists-and-python-arrays-for-audio.py	Wed Sep 12 20:02:32 2012 +0100
@@ -0,0 +1,39 @@
+import array as array
+
+
+#################################################
+############# PYTHON LISTS FOR AUDIO? ###########
+#################################################
+
+# I am a list of audio samples
+myAudio = [-0.25,0.0,0.25,0.5]
+
+# turn it up!
+myAudio = myAudio*2
+
+# oh dear!
+print myAudio
+
+# also, look what got into our array 
+myAudio.append("hello!")
+
+# oh dear again! - Python stores type information for every entry? Do we want
+# to do that 44100 times a second?
+print myAudio
+
+
+#################################################
+############# PYTHON ARRAYS FOR AUDIO? ##########
+#################################################
+
+# I am an array of audio samples
+myAudio = array.array('d',[-0.25,0.0,0.25,0.5])
+
+# this doesn't work, that's good
+# myAudio.append("hello")
+
+# but what about this!
+myAudio = myAudio*2
+
+# oh dear oh dear oh dear
+print myAudio
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sndfile-convert-wav-to-aiff-in-folder.py	Wed Sep 12 20:02:32 2012 +0100
@@ -0,0 +1,44 @@
+import os
+from scikits.audiolab import Sndfile
+from scikits.audiolab import Format
+
+# get the current working directory
+path = os.getcwd()
+
+# get all file names in the current directory
+files = os.listdir(path)
+
+# for each file name
+for filename in files:
+    
+    # if the file ends with a .wav extension
+    if filename.endswith('.wav'):
+        
+        # create a Sndfile instance for the file
+        f_in = Sndfile(filename, 'r')
+        
+        # extract the number of frames
+        numframes = f_in.nframes
+
+        # read all audio samples into 'data'
+        data = f_in.read_frames(numframes)
+
+        # extract the name (without extension from the file name)
+        name,extension = os.path.splitext(filename)
+
+        # create a new filename with a .aiff extension
+        new_filename = name + '.aiff'
+
+        # create the new format based on aiff
+        format = Format('aiff')
+        
+        # create a new Sndfile instance for the output file
+        f_out = Sndfile(new_filename, 'w', format, f_in.channels, f_in.samplerate)
+
+        # write out audio samples to the new file
+        f_out.write_frames(data[:numframes])
+
+        # close the audio file
+        f_out.close()
+
+        
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sndfile-read-and-play.py	Wed Sep 12 20:02:32 2012 +0100
@@ -0,0 +1,52 @@
+from scikits.audiolab import Sndfile
+from scikits.audiolab import Format
+from scikits.audiolab import play
+
+#################################################
+######## CREATING A SOUND FILE INSTANCE #########
+#################################################
+
+# create Sndfile instance with our example audio file
+f = Sndfile('viola.wav', 'r')
+
+
+#################################################
+######## EXTRACTING AUDIO FILE META-DATA ########
+#################################################
+
+# extract and print sample rate
+fs = f.samplerate
+print "sample rate: ",fs
+
+# extract and print the number of channels
+nc = f.channels
+print "number of channels: ",nc
+
+# extract and print the encoding format
+enc = f.encoding
+print "encoding format: ",enc
+
+# extract the number of frames - single samples for
+# mono and pairs of samples for stereo
+nsamples = f.nframes
+
+
+#################################################
+######## READ AUDIO SAMPLES FROM THE FILE #######
+#################################################
+
+# we can read audio samples using the read_frame method
+data = f.read_frames(nsamples)
+
+
+#################################################
+############# PLAYING AN AUDIO FILE #############
+#################################################
+
+# play the audio file data in 'data' at 44100Hz
+play(data,fs=44100)
+
+
+#################################################
+#################################################
+#################################################
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sndfile-write.py	Wed Sep 12 20:02:32 2012 +0100
@@ -0,0 +1,64 @@
+from scikits.audiolab import Sndfile
+from scikits.audiolab import Format
+from scikits.audiolab import play
+
+#################################################
+######## CREATING A SOUND FILE INSTANCE #########
+#################################################
+
+# create Sndfile instance with our example audio file
+f = Sndfile('viola.wav', 'r')
+
+
+#################################################
+######## EXTRACTING AUDIO FILE META-DATA ########
+#################################################
+
+# extract and print sample rate
+fs = f.samplerate
+print "sample rate: ",fs
+
+# extract and print the number of channels
+nc = f.channels
+print "number of channels: ",nc
+
+# extract and print the encoding format
+enc = f.encoding
+print "encoding format: ",enc
+
+# extract the number of frames - single samples for
+# mono and pairs of samples for stereo
+nsamples = f.nframes
+
+
+#################################################
+######## READ AUDIO SAMPLES FROM THE FILE #######
+#################################################
+
+# we can read audio samples using the read_frame method
+data = f.read_frames(nsamples)
+
+
+#################################################
+########## WRITING TO A NEW AUDIO FILE ##########
+#################################################
+
+# create a name for the new file
+new_filename = 'output_file.wav'
+
+# create the output audio data, in this case a simple copy
+output_data = data
+
+# Create a Sndfile instance for writing wav files @ 44100 Hz
+format = Format('wav')
+f = Sndfile(new_filename, 'w', format, 1, 44100)
+
+# Write out the first 3 seconds worth of samples (fs*3)
+f.write_frames(output_data[:fs*3])
+
+# close the audio file
+f.close()
+
+#################################################
+#################################################
+#################################################
Binary file viola.wav has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wavread.py	Wed Sep 12 20:02:32 2012 +0100
@@ -0,0 +1,12 @@
+from scikits.audiolab import wavread
+
+# specify a file name
+filename = "viola3.wav"
+
+# extract audio from file
+x, fs, enc = wavread(filename)     
+
+# print out the first 50 samples
+print x[0:50]
+
+