-
Notifications
You must be signed in to change notification settings - Fork 3
/
7-synthesize-stereo-sines-with-numpy.py
executable file
·63 lines (44 loc) · 1.44 KB
/
7-synthesize-stereo-sines-with-numpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import numpy as np
import pylab as plt
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*2)*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()
#################################################
############### PLOT USING PYLAB ################
#################################################
toneL = tone[:,0]
toneR = tone[:,1]
plt.subplot(211)
plt.plot(toneL[0:200])
plt.subplot(212)
plt.plot(toneR[0:200])
plt.show()