File size: 2,232 Bytes
39e0270
0d9f09c
 
39e0270
0d9f09c
 
 
 
 
 
 
 
 
 
39e0270
 
 
 
 
 
0d9f09c
39e0270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pydub
from pydub import AudioSegment
from tqdm import tqdm
from pydub.playback import play
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)

    notes_list = text.split(" ")
    for i in range(len(notes_list)):
        note = notes_list[i]
        # deduplicate characters in note
        note = "".join(set(note))
        notes_list[i] = note

    for note in notes_list:
        to_add = None
        for char in note:
            if char in mappings:
                if not to_add:
                    to_add = AudioSegment.from_wav(mappings[char])
                else:
                    if char in mappings:
                        new_audio = AudioSegment.from_wav(mappings[char])
                        # get length of new_audio
                        new_length = len(new_audio)
                        # get length of to_add
                        to_add_length = len(to_add)
                        # if new_length is longer than to_add_length
                        if new_length > to_add_length:
                            # add silence to to_add
                            to_add += AudioSegment.silent(duration=new_length - to_add_length)
                        # if to_add_length is longer than new_length
                        elif to_add_length > new_length:
                            # add silence to new_audio
                            new_audio += AudioSegment.silent(duration=to_add_length - new_length)
                        
                        to_add = to_add.overlay(new_audio)
            elif char == "n":
                to_add = AudioSegment.silent(duration=buffer_length)
            else: # everything else is a clap
                print('could not find mapping for ' + char)
                to_add = AudioSegment.from_wav(mappings["l"])

        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