autodrummer / t2a.py
jspr's picture
Upload 3 files
0d9f09c
raw
history blame
1.35 kB
from pydub import AudioSegment
from tqdm import tqdm
import os
from mappings import mappings, replacements
def bpm_to_ms(bpm):
return 60000 / 2 / bpm
def text_to_audio(text, bpm):
buffer_length = bpm_to_ms(bpm)
audio = AudioSegment.silent(duration=0)
for key, value in replacements.items():
text = text.replace(key, value)
for note in text.split(" "):
if note in mappings:
to_add = AudioSegment.from_wav(mappings[note])
# slice to be of consistent length or add more silence
if len(to_add) < buffer_length:
to_add = to_add + AudioSegment.silent(duration=buffer_length - len(to_add))
elif len(to_add) > buffer_length:
to_add = to_add[:buffer_length]
audio = audio + to_add
elif note == "n":
audio = audio + AudioSegment.silent(duration=buffer_length)
else: # everything else is a clap
to_add = AudioSegment.from_wav(mappings["l"])
# slice to be of consistent length or add more silence
if len(to_add) < buffer_length:
to_add = to_add + AudioSegment.silent(duration=buffer_length - len(to_add))
elif len(to_add) > buffer_length:
to_add = to_add[:buffer_length]
audio = audio + to_add
return audio