changeset 6:fd0f9d0615b2 tip master

Final commit
author Adam <adamstark.uk@gmail.com>
date Sat, 15 Sep 2012 09:18:07 +0100
parents 2042d278a4e1
children
files 1-wavread.py 10-plotting.py 2-sndfile-read-and-play.py 3-sndfile-write.py 4-lists-and-python-arrays-for-audio.py 6-synthesize-mono-noise-with-numpy.py 7-synthesize-stereo-sines-with-numpy.py 8-dsp-delay.py 9-dsp-block-by-block.py
diffstat 9 files changed, 82 insertions(+), 98 deletions(-) [+]
line wrap: on
line diff
--- a/1-wavread.py	Fri Sep 14 17:14:59 2012 +0100
+++ b/1-wavread.py	Sat Sep 15 09:18:07 2012 +0100
@@ -1,12 +1,24 @@
 from scikits.audiolab import wavread
+from scikits.audiolab import play
 
 # specify a file name
 filename = "viola.wav"
 
 # extract audio from file
-x, fs, enc = wavread(filename)     
+samples, fs, enc = wavread(filename)     
 
 # print out the first 50 samples
-print x[0:50]
+print samples[0:50]
 
 
+#################################################
+############# PLAYING AN AUDIO FILE #############
+#################################################
+
+# play the audio file data in 'data' at 44100Hz
+play(samples,fs=44100)
+
+
+#################################################
+#################################################
+#################################################
\ No newline at end of file
--- a/10-plotting.py	Fri Sep 14 17:14:59 2012 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-import numpy as np
-from scikits.audiolab import wavread
-from matplotlib import pylab as plt
-
-#################################################
-############ EXTRACT AUDIO FROM FILE ############
-#################################################
-
-x, fs, enc = wavread("drums_mono.wav")
-
-
-#################################################
-#### CALCULATE RMS OF EACH AUDIO BLOCK ####
-#################################################
-
-hop_size = 1024                              # set hop size
-frame_size = hop_size*2                     # set frame size
-frame = np.zeros(frame_size)                # initialise frame with zeros
-window = np.hanning(frame_size)             # create window of the same length as the hop size
-
-# create empty numpy array to hold our 
-rms = np.array([])
-
-# run through signal frame by frame 
-for n in range(0,x.size-hop_size,hop_size):
-    
-    # extract a segment of length hop_size
-    buffer = x[n:n+hop_size]                               
-    
-    # add new segment to frame, shifting back samples of frame
-    frame = np.append(frame[hop_size:frame_size],buffer)  
-    
-    # calculate RMS
-    rms_val = np.sqrt(np.power(frame,2).mean())
-    
-    # add amplitude to our numpy array
-    rms = np.append(rms,rms_val)
-
-print rms
-
-
-plt.plot(rms)
-plt.title("RMS")
-plt.xlabel("time")
-plt.ylabel("value")
-plt.show()
-    
-                                   
--- a/2-sndfile-read-and-play.py	Fri Sep 14 17:14:59 2012 +0100
+++ b/2-sndfile-read-and-play.py	Sat Sep 15 09:18:07 2012 +0100
@@ -28,7 +28,7 @@
 
 # extract the number of frames - single samples for
 # mono and pairs of samples for stereo
-nsamples = f.nframes
+num_samples = f.nframes
 
 
 #################################################
@@ -36,15 +36,15 @@
 #################################################
 
 # we can read audio samples using the read_frame method
-data = f.read_frames(nsamples)
+samples = f.read_frames(num_samples)
 
 
 #################################################
 ############# PLAYING AN AUDIO FILE #############
 #################################################
 
-# play the audio file data in 'data' at 44100Hz
-play(data,fs=44100)
+# play the audio file data in 'samples' at the sampling frequency 'fs' 
+play(samples,fs)
 
 
 #################################################
--- a/3-sndfile-write.py	Fri Sep 14 17:14:59 2012 +0100
+++ b/3-sndfile-write.py	Sat Sep 15 09:18:07 2012 +0100
@@ -28,7 +28,7 @@
 
 # extract the number of frames - single samples for
 # mono and pairs of samples for stereo
-nsamples = f.nframes
+num_samples = f.nframes
 
 
 #################################################
@@ -36,7 +36,7 @@
 #################################################
 
 # we can read audio samples using the read_frame method
-data = f.read_frames(nsamples)
+samples = f.read_frames(num_samples)
 
 
 #################################################
@@ -47,14 +47,14 @@
 new_filename = 'output_file.wav'
 
 # create the output audio data, in this case a simple copy
-output_data = data
+output_samples = samples
 
 # 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])
+f.write_frames(output_samples)
 
 # close the audio file
 f.close()
--- a/4-lists-and-python-arrays-for-audio.py	Fri Sep 14 17:14:59 2012 +0100
+++ b/4-lists-and-python-arrays-for-audio.py	Sat Sep 15 09:18:07 2012 +0100
@@ -21,7 +21,7 @@
 # to do that 44100 times a second?
 print myAudio
 
-
+"""
 #################################################
 ############# PYTHON ARRAYS FOR AUDIO? ##########
 #################################################
@@ -36,4 +36,5 @@
 myAudio = myAudio*2
 
 # oh dear oh dear oh dear
-print myAudio
\ No newline at end of file
+print myAudio
+"""
\ No newline at end of file
--- a/6-synthesize-mono-noise-with-numpy.py	Fri Sep 14 17:14:59 2012 +0100
+++ b/6-synthesize-mono-noise-with-numpy.py	Sat Sep 15 09:18:07 2012 +0100
@@ -1,6 +1,7 @@
 import numpy as np
 from scikits.audiolab import Sndfile
 from scikits.audiolab import Format
+import pylab as plt
 
 
 #################################################
@@ -18,7 +19,6 @@
     # generate random value and turn it down!
     noise[i] = np.random.random()*0.2
 
-
 #################################################
 ########## WRITING NOISE TO AUDIO FILE ##########
 #################################################
@@ -36,3 +36,10 @@
 # close the audio file
 f.close()
 
+
+#################################################
+############### PLOTTING THE NOISE ##############
+#################################################
+
+plt.plot(noise[0:512])
+plt.show()
--- a/7-synthesize-stereo-sines-with-numpy.py	Fri Sep 14 17:14:59 2012 +0100
+++ b/7-synthesize-stereo-sines-with-numpy.py	Sat Sep 15 09:18:07 2012 +0100
@@ -1,4 +1,5 @@
 import numpy as np
+import pylab as plt
 from scikits.audiolab import Sndfile
 from scikits.audiolab import Format
 
@@ -27,7 +28,7 @@
     
     # generate tone and set volume for left and right
     tone[i][0] = np.sin(2*np.pi*freq*phaseVal)*amp
-    tone[i][1] = np.sin(2*np.pi*freq*phaseVal)*amp
+    tone[i][1] = np.sin(2*np.pi*(freq*2)*phaseVal)*amp
 
 
 #################################################
@@ -47,3 +48,16 @@
 # close the audio file
 f.close()
 
+
+#################################################
+############### PLOT USING PYLAB ################
+#################################################   
+
+toneL = tone[:,0]
+toneR = tone[:,1]
+
+plt.subplot(211)
+plt.plot(toneL[0:200])
+plt.subplot(212)
+plt.plot(toneR[0:200])
+plt.show()
--- a/8-dsp-delay.py	Fri Sep 14 17:14:59 2012 +0100
+++ b/8-dsp-delay.py	Sat Sep 15 09:18:07 2012 +0100
@@ -1,38 +1,14 @@
 import numpy as np
+from scikits.audiolab import wavread
+from scikits.audiolab import Format
 from scikits.audiolab import Sndfile
-from scikits.audiolab import Format
 
 #################################################
 ######## 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 the number of samples
-nsamples = f.nframes
-
-
-#################################################
-######## READ AUDIO SAMPLES FROM THE FILE #######
-#################################################
-
-# we can read audio samples using the read_frame method
-data = f.read_frames(nsamples)
-
+# extract audio from file
+samples, fs, enc = wavread('viola.wav')  
 
 #################################################
 ########## APPLY A DELAY TO THE SAMPLES #########
@@ -45,16 +21,16 @@
 alpha = 0.75
 
 # create an empty array for the output
-out = np.zeros(data.size)
+out = np.zeros(samples.size)
 
 # for every sample in the array
-for i in range(data.size):
+for i in range(samples.size):
 
     # if we are safe to apply the delay without negative indexing
     if (i >= delay):
-        out[i] = data[i] + data[i-delay]*alpha
+        out[i] = samples[i] + samples[i-delay]*alpha
     else:
-        out[i] = data[i] # hacky
+        out[i] = samples[i] # hacky
 
 
 #################################################
--- a/9-dsp-block-by-block.py	Fri Sep 14 17:14:59 2012 +0100
+++ b/9-dsp-block-by-block.py	Sat Sep 15 09:18:07 2012 +0100
@@ -1,4 +1,5 @@
 import numpy as np
+import pylab as plt
 from scikits.audiolab import wavread
 
 #################################################
@@ -9,11 +10,11 @@
 
 
 #################################################
-#### CALCULATE RMS OF EACH AUDIO BLOCK ####
+####### CALCULATE RMS OF EACH AUDIO BLOCK #######
 #################################################
 
-hop_size = 2048                             # set hop size
-frame_size = 4096                           # set frame size
+hop_size = 512                             # set hop size
+frame_size = hop_size*2                     # set frame size
 frame = np.zeros(frame_size)                # initialise frame with zeros
 window = np.hanning(frame_size)             # create window of the same length as the hop size
 
@@ -24,6 +25,21 @@
 for n in range(0,x.size-hop_size,hop_size):
     
     # extract a segment of length hop_size
+    frame = x[n:n+hop_size]                               
+        
+    # calculate RMS
+    rms_val = np.sqrt(np.power(frame,2).mean())
+    
+    # add amplitude to our numpy array
+    rms = np.append(rms,rms_val)
+
+print rms
+
+""" I AM THE OVERLAP VERSION
+# run through signal frame by frame 
+for n in range(0,x.size-hop_size,hop_size):
+    
+    # extract a segment of length hop_size
     buffer = x[n:n+hop_size]                               
     
     # add new segment to frame, shifting back samples of frame
@@ -36,6 +52,12 @@
     rms = np.append(rms,rms_val)
 
 print rms
-    
+"""
+
+plt.plot(rms)
+plt.title("RMS")
+plt.xlabel("time")
+plt.ylabel("value")
+plt.show()
                                    
  
\ No newline at end of file