File size: 1,991 Bytes
2158a6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import gradio as gr
import os
import re
import subprocess
import time
from symusic import Score, Synthesizer, BuiltInSF3, dump_wav
import torchaudio
import torch
import music21

def convert_midi_to_svg(midi_file_path, output_svg_path):
    # Load MIDI file
    midi_stream = music21.converter.parse(midi_file_path)

    # Convert MIDI to MusicXML
    musicxml_string = midi_stream.write('musicxml')

    # Load MusicXML string
    score = music21.converter.parseData(musicxml_string)

    # Convert MusicXML to SVG
    svg_string = score.write('musicxml.png', fmt='musicxml', subformats=['svg'])

    # Write SVG to file
    with open(output_svg_path, 'wb') as f:
        f.write(svg_string)
    return output_svg_path


def parse_abc_notation(text="", conversation_id='1'):
    os.makedirs(f"tmp/{conversation_id}", 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'tmp/{conversation_id}/{ts}.mp3'
        audio = Synthesizer().render(s, stereo=True)
        torchaudio.save(wav_file, torch.FloatTensor(audio), 44100)

        # Convert abc notation to SVG
        tmp_midi = f'tmp/{conversation_id}/{ts}.mid'
        midi_file = s.dump_midi(tmp_midi)
        svg_file = f'tmp/{conversation_id}/{ts}.svg'
        convert_midi_to_svg(tmp_midi, svg_file)
        return svg_file, wav_file
    else:
        return None, None 

gradio_app = gr.Interface(
    parse_abc_notation,
    inputs=["text"],
    outputs=[gr.Image(label="Processed Image"), gr.Audio(label="Result")],
    title="Hot Dog? Or Not?",
)

def greet(name, intensity):
    return "Hello, " + name + "!" * int(intensity)

demo = gr.Interface(
    fn=greet,
    inputs=["text", "slider"],
    outputs=["text"],
)

demo.launch()


if __name__ == "__main__":
    gradio_app.launch()