File size: 1,350 Bytes
0d9f09c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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