-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtts.py
63 lines (45 loc) · 1.64 KB
/
tts.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
from gtts import gTTS
import os
import time
from openal.audio import SoundSink, SoundSource
from openal.loaders import load_wav_file
from pydub import AudioSegment
import wave
import contextlib
import math
temp_path = "C:\\Users\\YICHIY~1\\Documents\\Others\\HackSC\\HackSC-2020\\temp_audio"
frame_width = 640
frame_height = 480
frame_area = frame_width * frame_height
def make_sounds(items):
sink = SoundSink()
sink.activate()
print("Frame with these items: " + str([item.label for item in items]))
for item in items:
label = item.label.strip()
position = item.box.center
area = item.box.area
source_x = (position[0] - frame_width / 2) / (frame_width / 2) * 5
source_z = -1 / math.sqrt(area / frame_area)
print("{label} @ ({x:2f}, {z:2f})".format(
label=label, x=source_x, z=source_z))
base_name = os.path.join(temp_path, label)
wav_file = base_name + ".wav"
if(not os.path.exists(wav_file)):
tts = gTTS(label)
tts.save(base_name + '.mp3')
sound = AudioSegment.from_mp3(base_name + '.mp3')
sound.export(wav_file, format="wav")
data = load_wav_file(wav_file)
duration = 0.0
with contextlib.closing(wave.open(wav_file, 'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
source = SoundSource(position=[0, 0, 0])
source.looping = False
source.queue(data)
sink.play(source)
source.position = [source_x, 0, source_z]
sink.update()
time.sleep(duration + 0.1)