-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjarvis_sounds.py
More file actions
149 lines (117 loc) · 3.79 KB
/
Copy pathjarvis_sounds.py
File metadata and controls
149 lines (117 loc) · 3.79 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import math
import os
import struct
import wave
import pygame
from config import SOUND_PATH
class JarvisSounds:
"""Handles JARVIS sound generation, loading and playback."""
def __init__(self):
pygame.mixer.init(
frequency=44100,
size=-16,
channels=2,
buffer=512,
)
os.makedirs(SOUND_PATH, exist_ok=True)
self.sounds = {}
self.power_channel = pygame.mixer.Channel(0)
self._generate_missing_sounds()
self._load_sounds()
def _generate_tone(
self,
filename,
frequency,
duration,
volume=0.3,
end_frequency=None,
):
sample_rate = 44100
total_samples = int(sample_rate * duration)
frames = []
for i in range(total_samples):
t = i / sample_rate
progress = i / total_samples
if end_frequency is not None:
current_frequency = (
frequency
+ (end_frequency - frequency) * progress
)
else:
current_frequency = frequency
sample = math.sin(
2 * math.pi * current_frequency * t
)
sample += 0.25 * math.sin(
2
* math.pi
* current_frequency
* 2
* t
)
fade_in = min(1.0, t / 0.03)
fade_out = min(1.0, (duration - t) / 0.08)
envelope = min(fade_in, fade_out)
sample *= volume * envelope
value = int(sample * 32767)
frames.append(struct.pack("<h", value))
filepath = os.path.join(SOUND_PATH, filename)
with wave.open(filepath, "wb") as wav:
wav.setnchannels(1)
wav.setsampwidth(2)
wav.setframerate(sample_rate)
wav.writeframes(b"".join(frames))
def _generate_missing_sounds(self):
sounds = {
"power.wav": (55, 1.4, 0.45, 180),
"beep.wav": (850, 0.08, 0.30, None),
"beep2.wav": (1200, 0.06, 0.25, None),
"scan.wav": (400, 0.7, 0.22, 1400),
"ready1.wav": (500, 0.12, 0.30, 900),
"ready2.wav": (1200, 0.20, 0.32, 1800),
"error.wav": (400, 0.25, 0.30, 180),
"listen.wav": (700, 0.10, 0.22, 1000),
}
for filename, values in sounds.items():
filepath = os.path.join(SOUND_PATH, filename)
# Important: do not regenerate WAV files every time JARVIS starts.
if not os.path.exists(filepath):
self._generate_tone(filename, *values)
def _load_sounds(self):
for filename in (
"power.wav",
"beep.wav",
"beep2.wav",
"scan.wav",
"ready1.wav",
"ready2.wav",
"error.wav",
"listen.wav",
):
filepath = os.path.join(SOUND_PATH, filename)
if os.path.exists(filepath):
name = os.path.splitext(filename)[0]
self.sounds[name] = pygame.mixer.Sound(filepath)
def play(self, name):
sound = self.sounds.get(name)
if sound is not None:
sound.play()
def play_power(self, stop_after=700, root=None):
sound = self.sounds.get("power")
if sound is None:
return
self.power_channel.play(sound)
if root is not None:
root.after(stop_after, self.power_channel.stop)
def stop(self):
try:
self.power_channel.stop()
pygame.mixer.stop()
except pygame.error:
pass
def shutdown(self):
self.stop()
try:
pygame.mixer.quit()
except pygame.error:
pass