Mercurial > hg > audio-bootcamp-planning
changeset 5:2042d278a4e1
Removed some filed
| author | Adam <adamstark.uk@gmail.com> |
|---|---|
| date | Fri, 14 Sep 2012 17:14:59 +0100 |
| parents | 2b996e1d64da |
| children | fd0f9d0615b2 |
| files | basic-numpy.py dsp-block-by-block.py dsp-delay.py lists-and-python-arrays-for-audio.py plotting.py sndfile-convert-wav-to-aiff-in-folder.py sndfile-read-and-play.py sndfile-write.py synthesize-mono-noise-with-numpy.py synthesize-stereo-sines-with-numpy.py wavread.py |
| diffstat | 11 files changed, 0 insertions(+), 519 deletions(-) [+] |
line wrap: on
line diff
--- a/basic-numpy.py Fri Sep 14 17:11:22 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -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)
--- a/dsp-block-by-block.py Fri Sep 14 17:11:22 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ -import numpy as np -from scikits.audiolab import wavread - -################################################# -############ EXTRACT AUDIO FROM FILE ############ -################################################# - -x, fs, enc = wavread("viola.wav") - - -################################################# -#### CALCULATE RMS OF EACH AUDIO BLOCK #### -################################################# - -hop_size = 2048 # set hop size -frame_size = 4096 # 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 - - - \ No newline at end of file
--- a/dsp-delay.py Fri Sep 14 17:11:22 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,79 +0,0 @@ -import numpy as np -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) - - -################################################# -########## APPLY A DELAY TO THE SAMPLES ######### -################################################# - -# delay in samples -delay = fs/2 - -# volume of delayed sound -alpha = 0.75 - -# create an empty array for the output -out = np.zeros(data.size) - -# for every sample in the array -for i in range(data.size): - - # if we are safe to apply the delay without negative indexing - if (i >= delay): - out[i] = data[i] + data[i-delay]*alpha - else: - out[i] = data[i] # hacky - - -################################################# -########## WRITING TO A NEW AUDIO FILE ########## -################################################# - -# create a name for the new file -new_filename = 'delayed_audio.wav' - -# Create a Sndfile instance for writing wav files @ 44100 Hz -format = Format('wav') -f = Sndfile(new_filename, 'w', format, 1, 44100) - -# Write out the samples to an audio file -f.write_frames(out) - -# close the audio file -f.close() - -################################################# -################################################# -#################################################
--- a/lists-and-python-arrays-for-audio.py Fri Sep 14 17:11:22 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ -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
--- a/plotting.py Fri Sep 14 17:11:22 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/sndfile-convert-wav-to-aiff-in-folder.py Fri Sep 14 17:11:22 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -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() - -
--- a/sndfile-read-and-play.py Fri Sep 14 17:11:22 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -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) - - -################################################# -################################################# -#################################################
--- a/sndfile-write.py Fri Sep 14 17:11:22 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -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() - -################################################# -################################################# -#################################################
--- a/synthesize-mono-noise-with-numpy.py Fri Sep 14 17:11:22 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,38 +0,0 @@ -import numpy as np -from scikits.audiolab import Sndfile -from scikits.audiolab import Format - - -################################################# -############ CREATE SOME RANDOM NOISE ########### -################################################# - -# set our sampling frequency -fs = 44100 - -# create a numpy array that can hold 3 seconds of sound -noise = np.empty(3*fs) - -# set each element to a random value (noise) -for i in range(noise.size): - # generate random value and turn it down! - noise[i] = np.random.random()*0.2 - - -################################################# -########## WRITING NOISE TO AUDIO FILE ########## -################################################# - -# create a name for the new file -new_filename = 'noise.wav' - -# Create a Sndfile instance for writing wav files @ 44100 Hz -format = Format('wav') -f = Sndfile(new_filename, 'w', format, 1, fs) - -# Write out the samples to the file -f.write_frames(noise) - -# close the audio file -f.close() -
--- a/synthesize-stereo-sines-with-numpy.py Fri Sep 14 17:11:22 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -import numpy as np -from scikits.audiolab import Sndfile -from scikits.audiolab import Format - - -################################################# -############## CREATE A SINE TONE ############### -################################################# - -# set our sampling frequency -fs = 44100 - -# create a numpy array that can hold 3 seconds of sound -tone = np.zeros((3*fs,2)) - -# set frequency to 440Hz -freq = 440 - -# set volume to 0.3 -amp = 0.3 - -# set values of each channel -for i in range(tone.shape[0]): - - # calculate phase value - phaseVal = np.float(i)/np.float(fs) - - # 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 - - -################################################# -########## WRITING TONES TO AUDIO FILE ########## -################################################# - -# create a name for the new file -new_filename = 'tone.wav' - -# Create a Sndfile instance for writing wav files @ 44100 Hz -format = Format('wav') -f = Sndfile(new_filename, 'w', format, 2, fs) - -# Write out the samples to the file -f.write_frames(tone) - -# close the audio file -f.close() -
--- a/wavread.py Fri Sep 14 17:11:22 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ -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] - -
