Mercurial > hg > audio-bootcamp-planning
changeset 2:7b9a06f64ab1
synthesis and delay added
| author | Adam <adamstark.uk@gmail.com> |
|---|---|
| date | Wed, 12 Sep 2012 20:44:28 +0100 |
| parents | 99434d86405a |
| children | 8af0cef242b8 |
| files | dsp-delay.py synthesize-mono-noise-with-numpy.py synthesize-stereo-sines-with-numpy.py |
| diffstat | 3 files changed, 166 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dsp-delay.py Wed Sep 12 20:44:28 2012 +0100 @@ -0,0 +1,79 @@ +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() + +################################################# +################################################# +#################################################
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/synthesize-mono-noise-with-numpy.py Wed Sep 12 20:44:28 2012 +0100 @@ -0,0 +1,38 @@ +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() +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/synthesize-stereo-sines-with-numpy.py Wed Sep 12 20:44:28 2012 +0100 @@ -0,0 +1,49 @@ +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() +
