File size: 1,485 Bytes
4c43480
 
 
 
 
 
 
 
 
 
ec2c213
4c43480
 
 
 
 
 
 
 
 
 
 
5ea23fb
4c43480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efb2746
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import re
import subprocess
import time
from symusic import Score, Synthesizer
import torchaudio
import torch

# for rendering abc notation
os.environ['QT_QPA_PLATFORM']='offscreen' 

default_abc = """
X:1
L:1/8
M:2/4
K:G
|:"G" G>A Bc | dB dB |"C" ce ce |"D7" dB A2 |"G" G>A Bc | dB dB |"Am" cA"D7" FA |"G" AG G2 :: 
"Em" g2"D" f>e | de Bd |"C" ce ce |"D7" dB A2 |"G" g2"D" f>e | de Bd |"Am" cA"D7" FA |"G" AG G2 :| 
"""

def parse_abc_notation(text='', conversation_id='debug'):
    os.makedirs(f"tmp/", exist_ok=True)
    ts = time.time()
    abc_pattern = r'(X:\d+\n(?:[^\n]*\n)+)'
    abc_notation = re.findall(abc_pattern, text+'\n')
    print(f'extract abc block: {abc_notation}')
    if abc_notation:
        # Convert ABC to midi
        s = Score.from_abc(abc_notation[0])
        wav_file = f'{ts}.mp3'
        audio = Synthesizer().render(s, stereo=True)
        torchaudio.save(wav_file, torch.FloatTensor(audio), 44100)

        # Convert abc notation to SVG
        tmp_midi = f'{ts}.mid'
        s.dump_midi(tmp_midi)
        svg_file = f'{ts}.svg'
        subprocess.run(["./MuseScore-4.1.1.232071203-x86_64.AppImage", "-f", "-o", svg_file, tmp_midi])
        return svg_file, wav_file
    else:
        return None, None 

gradio_app = gr.Interface(
    parse_abc_notation,
    inputs=["text"],
    outputs=[gr.Image(label="svg"), gr.Audio(label="audio")],
    title="ABC notation parse",
    examples=[default_abc]
)

gradio_app.launch()