-
Notifications
You must be signed in to change notification settings - Fork 0
/
tone.py
61 lines (50 loc) · 1.96 KB
/
tone.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
import math
import numpy
import pyaudio
from matplotlib import pyplot
class MusicBox:
def __init__(self):
self.p = pyaudio.PyAudio()
self.stream = self.p.open(format=pyaudio.paFloat32,
channels=1, rate=44100, output=1)
self.speed = 18
def sine(self, frequency, length, rate):
length = int(length * rate)
factor = float(frequency) * (math.pi * 2) / rate
wavelength = rate / float(frequency)
length -= length % wavelength # Sine wave must end at a zero
return numpy.sin(numpy.arange(length) * factor)
def tone(self, stream, frequency=440, length=1, rate=44100):
chunks = []
wave = self.sine(frequency, length, rate)
#print(wave[-10:-1])
#pyplot.plot(wave)
#pyplot.show()
chunks.append(wave)
chunk = numpy.concatenate(chunks) * 0.25
self.stream.write(chunk.astype(numpy.float32).tostring())
def dot(self, frequency):
self.tone(self.stream, frequency, (1.2/float(self.speed)))
def dash(self, frequency):
self.tone(self.stream, frequency, 3*(1.2/float(self.speed)))
def rest(self):
self.tone(self.stream, 1/float(1.2/float(self.speed)), (1.2/float(self.speed)))
def pause(self, number, FARNS):
self.tone(self.stream, 1/float(1.2/float(FARNS)), number * (1.2/float(FARNS)))
def play(self, code, frequency, speed):
words = code.lower().split()
for i in range(len(words)):
for j in range(len(words[i])):
if words[i][j] == ".":
self.dot(frequency)
elif words[i][j] == "-":
self.dash(frequency)
elif words[i][j] == "/":
self.pause(1, speed)
if j + 1 != len(words[i]):
self.rest()
if i + 1 != len(words):
self.pause(3, speed)
def close(self):
STREAM.close()
p.terminate()