Spaces:
Runtime error
Runtime error
Update t2a.py
Browse files
t2a.py
CHANGED
@@ -1,5 +1,7 @@
|
|
|
|
1 |
from pydub import AudioSegment
|
2 |
from tqdm import tqdm
|
|
|
3 |
import os
|
4 |
from mappings import mappings, replacements
|
5 |
|
@@ -10,26 +12,46 @@ def text_to_audio(text, bpm):
|
|
10 |
buffer_length = bpm_to_ms(bpm)
|
11 |
audio = AudioSegment.silent(duration=0)
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
for note in
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
return audio
|
|
|
1 |
+
import pydub
|
2 |
from pydub import AudioSegment
|
3 |
from tqdm import tqdm
|
4 |
+
from pydub.playback import play
|
5 |
import os
|
6 |
from mappings import mappings, replacements
|
7 |
|
|
|
12 |
buffer_length = bpm_to_ms(bpm)
|
13 |
audio = AudioSegment.silent(duration=0)
|
14 |
|
15 |
+
notes_list = text.split(" ")
|
16 |
+
for i in range(len(notes_list)):
|
17 |
+
note = notes_list[i]
|
18 |
+
# deduplicate characters in note
|
19 |
+
note = "".join(set(note))
|
20 |
+
notes_list[i] = note
|
21 |
|
22 |
+
for note in notes_list:
|
23 |
+
to_add = None
|
24 |
+
for char in note:
|
25 |
+
if char in mappings:
|
26 |
+
if not to_add:
|
27 |
+
to_add = AudioSegment.from_wav(mappings[char])
|
28 |
+
else:
|
29 |
+
if char in mappings:
|
30 |
+
new_audio = AudioSegment.from_wav(mappings[char])
|
31 |
+
# get length of new_audio
|
32 |
+
new_length = len(new_audio)
|
33 |
+
# get length of to_add
|
34 |
+
to_add_length = len(to_add)
|
35 |
+
# if new_length is longer than to_add_length
|
36 |
+
if new_length > to_add_length:
|
37 |
+
# add silence to to_add
|
38 |
+
to_add += AudioSegment.silent(duration=new_length - to_add_length)
|
39 |
+
# if to_add_length is longer than new_length
|
40 |
+
elif to_add_length > new_length:
|
41 |
+
# add silence to new_audio
|
42 |
+
new_audio += AudioSegment.silent(duration=to_add_length - new_length)
|
43 |
+
|
44 |
+
to_add = to_add.overlay(new_audio)
|
45 |
+
elif char == "n":
|
46 |
+
to_add = AudioSegment.silent(duration=buffer_length)
|
47 |
+
else: # everything else is a clap
|
48 |
+
print('could not find mapping for ' + char)
|
49 |
+
to_add = AudioSegment.from_wav(mappings["l"])
|
50 |
+
|
51 |
+
if len(to_add) < buffer_length:
|
52 |
+
to_add = to_add + AudioSegment.silent(duration=buffer_length - len(to_add))
|
53 |
+
elif len(to_add) > buffer_length:
|
54 |
+
to_add = to_add[:buffer_length]
|
55 |
+
|
56 |
+
audio = audio + to_add
|
57 |
return audio
|