jspr commited on
Commit
39e0270
·
1 Parent(s): 9bc86e6

Update t2a.py

Browse files
Files changed (1) hide show
  1. t2a.py +43 -21
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
- for key, value in replacements.items():
14
- text = text.replace(key, value)
 
 
 
 
15
 
16
- for note in text.split(" "):
17
- if note in mappings:
18
- to_add = AudioSegment.from_wav(mappings[note])
19
- # slice to be of consistent length or add more silence
20
- if len(to_add) < buffer_length:
21
- to_add = to_add + AudioSegment.silent(duration=buffer_length - len(to_add))
22
- elif len(to_add) > buffer_length:
23
- to_add = to_add[:buffer_length]
24
- audio = audio + to_add
25
- elif note == "n":
26
- audio = audio + AudioSegment.silent(duration=buffer_length)
27
- else: # everything else is a clap
28
- to_add = AudioSegment.from_wav(mappings["l"])
29
- # slice to be of consistent length or add more silence
30
- if len(to_add) < buffer_length:
31
- to_add = to_add + AudioSegment.silent(duration=buffer_length - len(to_add))
32
- elif len(to_add) > buffer_length:
33
- to_add = to_add[:buffer_length]
34
- audio = audio + to_add
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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