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.

Statistics Download as Zip
| Branch: | Revision:

root / 7-synthesize-stereo-sines-with-numpy.py @ 6:fd0f9d0615b2

History | View | Annotate | Download (1.44 KB)

1
import numpy as np
2
import pylab as plt
3
from scikits.audiolab import Sndfile
4
from scikits.audiolab import Format
5

    
6

    
7
#################################################
8
############## CREATE A SINE TONE ###############
9
#################################################
10

    
11
# set our sampling frequency
12
fs = 44100
13

    
14
# create a numpy array that can hold 3 seconds of sound
15
tone = np.zeros((3*fs,2))
16

    
17
# set frequency to 440Hz
18
freq = 440
19

    
20
# set volume to 0.3
21
amp = 0.3
22

    
23
# set values of each channel
24
for i in range(tone.shape[0]):
25
    
26
    # calculate phase value
27
    phaseVal = np.float(i)/np.float(fs)
28
    
29
    # generate tone and set volume for left and right
30
    tone[i][0] = np.sin(2*np.pi*freq*phaseVal)*amp
31
    tone[i][1] = np.sin(2*np.pi*(freq*2)*phaseVal)*amp
32

    
33

    
34
#################################################
35
########## WRITING TONES TO AUDIO FILE ##########
36
#################################################
37

    
38
# create a name for the new file
39
new_filename = 'tone.wav'
40

    
41
# Create a Sndfile instance for writing wav files @ 44100 Hz
42
format = Format('wav')
43
f = Sndfile(new_filename, 'w', format, 2, fs)
44

    
45
# Write out the samples to the file
46
f.write_frames(tone)
47

    
48
# close the audio file
49
f.close()
50

    
51

    
52
#################################################
53
############### PLOT USING PYLAB ################
54
#################################################   
55

    
56
toneL = tone[:,0]
57
toneR = tone[:,1]
58

    
59
plt.subplot(211)
60
plt.plot(toneL[0:200])
61
plt.subplot(212)
62
plt.plot(toneR[0:200])
63
plt.show()