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 / 7-synthesize-stereo-sines-with-numpy.py @ 4:2b996e1d64da
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 |
|
| 5 |
|
| 6 |
#################################################
|
| 7 |
############## CREATE A SINE TONE ###############
|
| 8 |
#################################################
|
| 9 |
|
| 10 |
# set our sampling frequency
|
| 11 |
fs = 44100
|
| 12 |
|
| 13 |
# create a numpy array that can hold 3 seconds of sound
|
| 14 |
tone = np.zeros((3*fs,2)) |
| 15 |
|
| 16 |
# set frequency to 440Hz
|
| 17 |
freq = 440
|
| 18 |
|
| 19 |
# set volume to 0.3
|
| 20 |
amp = 0.3
|
| 21 |
|
| 22 |
# set values of each channel
|
| 23 |
for i in range(tone.shape[0]): |
| 24 |
|
| 25 |
# calculate phase value
|
| 26 |
phaseVal = np.float(i)/np.float(fs) |
| 27 |
|
| 28 |
# generate tone and set volume for left and right
|
| 29 |
tone[i][0] = np.sin(2*np.pi*freq*phaseVal)*amp |
| 30 |
tone[i][1] = np.sin(2*np.pi*freq*phaseVal)*amp |
| 31 |
|
| 32 |
|
| 33 |
#################################################
|
| 34 |
########## WRITING TONES TO AUDIO FILE ##########
|
| 35 |
#################################################
|
| 36 |
|
| 37 |
# create a name for the new file
|
| 38 |
new_filename = 'tone.wav'
|
| 39 |
|
| 40 |
# Create a Sndfile instance for writing wav files @ 44100 Hz
|
| 41 |
format = Format('wav')
|
| 42 |
f = Sndfile(new_filename, 'w', format, 2, fs) |
| 43 |
|
| 44 |
# Write out the samples to the file
|
| 45 |
f.write_frames(tone) |
| 46 |
|
| 47 |
# close the audio file
|
| 48 |
f.close() |
| 49 |
|