To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
The primary repository for this project is hosted at https://github.com/Codasign/york-software-bootcamp-audio-day.git .
This repository is a read-only copy which is updated automatically every hour.
root / 6-synthesize-mono-noise-with-numpy.py @ 6:fd0f9d0615b2
History | View | Annotate | Download (1.14 KB)
| 1 |
import numpy as np |
|---|---|
| 2 |
from scikits.audiolab import Sndfile |
| 3 |
from scikits.audiolab import Format |
| 4 |
import pylab as plt |
| 5 |
|
| 6 |
|
| 7 |
#################################################
|
| 8 |
############ CREATE SOME RANDOM NOISE ###########
|
| 9 |
#################################################
|
| 10 |
|
| 11 |
# set our sampling frequency
|
| 12 |
fs = 44100
|
| 13 |
|
| 14 |
# create a numpy array that can hold 3 seconds of sound
|
| 15 |
noise = np.empty(3*fs)
|
| 16 |
|
| 17 |
# set each element to a random value (noise)
|
| 18 |
for i in range(noise.size): |
| 19 |
# generate random value and turn it down!
|
| 20 |
noise[i] = np.random.random()*0.2
|
| 21 |
|
| 22 |
#################################################
|
| 23 |
########## WRITING NOISE TO AUDIO FILE ##########
|
| 24 |
#################################################
|
| 25 |
|
| 26 |
# create a name for the new file
|
| 27 |
new_filename = 'noise.wav'
|
| 28 |
|
| 29 |
# Create a Sndfile instance for writing wav files @ 44100 Hz
|
| 30 |
format = Format('wav')
|
| 31 |
f = Sndfile(new_filename, 'w', format, 1, fs) |
| 32 |
|
| 33 |
# Write out the samples to the file
|
| 34 |
f.write_frames(noise) |
| 35 |
|
| 36 |
# close the audio file
|
| 37 |
f.close() |
| 38 |
|
| 39 |
|
| 40 |
#################################################
|
| 41 |
############### PLOTTING THE NOISE ##############
|
| 42 |
#################################################
|
| 43 |
|
| 44 |
plt.plot(noise[0:512]) |
| 45 |
plt.show() |