[go: up one dir, main page]

Menu

[d33b2f]: / sine wave.txt  Maximize  Restore  History

Download this file

66 lines (62 with data), 1.4 kB

 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
64
65
66
1.
# create a soundfile in AU format playing a sine wave
2.
# of a given frequency, duration and volume
3.
# tested with Python25 by vegaseat 29jan2008
4.
5.
from struct import pack
6.
from math import sin, pi
7.
8.
def au_file(name='test.au', freq=440, dur=1000, vol=0.5):
9.
"""
10.
creates an AU format audio file of a sine wave
11.
of frequency freq (Hz)
12.
for duration dur (milliseconds)
13.
at volume vol (max is 1.0)
14.
"""
15.
fout = open(name, 'wb')
16.
# header needs size, encoding=2, sampling_rate=8000, channel=1
17.
fout.write('.snd' + pack('>5L', 24, 8*dur, 2, 8000, 1))
18.
factor = 2 * pi * freq/8000
19.
# write data
20.
for seg in range(8 * dur):
21.
# sine wave calculations
22.
sin_seg = sin(seg * factor)
23.
fout.write(pack('b', vol * 127 * sin_seg))
24.
fout.close()
25.
26.
# test the module ...
27.
if __name__ == '__main__':
28.
au_file(name='sound800.au', freq=800, dur=2000, vol=0.8)
29.
30.
# if you have Windows, you can test the audio file
31.
# otherwise comment this code out
32.
import os
33.
os.startfile('sound800.au')