File size: 6,152 Bytes
b362624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c45a6c
 
 
b362624
 
 
 
1c45a6c
 
b362624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c45a6c
 
b362624
 
 
 
1c45a6c
 
 
 
 
 
 
 
 
 
 
 
 
b362624
 
 
 
 
 
 
 
 
 
 
bf8488d
b362624
 
 
 
 
 
 
 
1c45a6c
 
 
 
 
 
b362624
 
 
1c45a6c
b362624
 
 
f644682
5e15e63
 
b362624
1c45a6c
 
 
 
 
 
b362624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import torch
import einops
import gradio as gr
import datetime
import numpy as np
import spaces
import soundfile
import os
import sys
import zipfile
from pathlib import Path
from huggingface_hub import hf_hub_download

sys.path.append("sf-creator-fork")
from main import sfz, decentsampler

decoder_path = "erl-j/soundfont-generator-assets/decoder.pt"
model_path = "erl-j/soundfont-generator-assets/synth_lfm_modern_bfloat16.pt"
# Download models from Hugging Face Hub
decoder_path = hf_hub_download("erl-j/soundfont-generator-assets", "decoder.pt")
model_path = hf_hub_download(
    "erl-j/soundfont-generator-assets", "synth_lfm_modern_bfloat16.pt"
)

# Load models once at startup
device = "cuda"
decoder = torch.load(decoder_path, map_location=device).half().eval()
model = torch.load(model_path, map_location=device).half().eval()


@spaces.GPU
def generate_and_export_soundfont(text, steps=20, instrument_name=None):
    sample_start = datetime.datetime.now()

    # Generate audio as before
    z = model.sample(1, text=[text], steps=steps)
    z_reshaped = einops.rearrange(z, "b t c d -> (b c) d t")

    with torch.no_grad():
        audio = decoder.decode(z_reshaped)

    audio_output = einops.rearrange(audio, "b c t -> c (b t)").cpu().numpy()
    audio_output = audio_output / np.max(np.abs(audio_output))

    # Export individual wav files
    export_audio = audio.cpu().numpy().astype(np.float32)
    output_dir = "output"
    os.makedirs(output_dir, exist_ok=True)

    # Create instrument name if not provided
    if not instrument_name:
        instrument_name = text.replace(" ", "_")[:20]

    # Save individual WAV files
    pitches = [
        "C1",
        "F#1",
        "C2",
        "F#2",
        "C3",
        "F#3",
        "C4",
        "F#4",
        "C5",
        "F#5",
        "C6",
        "F#6",
        "C7",
        "F#7",
        "C8",
    ]
    wav_files = []
    for i in range(audio.shape[0]):
        wav_path = f"{output_dir}/{pitches[i]}.wav"
        soundfile.write(wav_path, export_audio[i].T, 44100)
        wav_files.append(wav_path)

    # Generate SFZ file
    sfz(
        directory=output_dir,
        lowkey="21",
        highkey="108",
        instrument=instrument_name,
        loopmode="no_loop",
        polyphony=None,
    )

    # Create zip file containing SFZ and WAV files for the complete soundfont
    zip_path = f"{output_dir}/{instrument_name}_package.zip"
    with zipfile.ZipFile(zip_path, "w") as zipf:
        # Add SFZ file
        sfz_file = f"{output_dir}/{instrument_name}.sfz"
        zipf.write(sfz_file, os.path.basename(sfz_file))
        # Add all WAV files
        for wav_file in wav_files:
            if os.path.exists(wav_file):
                zipf.write(wav_file, os.path.basename(wav_file))

    total_time = (datetime.datetime.now() - sample_start).total_seconds()

    return (
        (44100, audio_output.T),
        f"Generation took {total_time:.2f}s\nFiles saved in {output_dir}",
        zip_path,
        wav_files,
    )


custom_js = open("custom.js").read()
custom_css = open("custom.css").read()

demo = gr.Blocks(
    title="Erl-j's Soundfont Generator",
    theme=gr.themes.Default(
        primary_hue="green",
        font=[gr.themes.GoogleFont("Inconsolata"), "Arial", "sans-serif"],
    ),
    js=custom_js,
    css=custom_css,
)

with demo:
    gr.Markdown(open("intro.md").read())

    with gr.Row():
        steps = gr.Slider(
            minimum=1, maximum=50, value=20, step=1, label="Generation steps"
        )

    with gr.Row():
        text_input = gr.Textbox(
            label="Prompt",
            placeholder="Enter text description (e.g. 'hard bass', 'sparkly bells')",
            lines=2,
        )

    with gr.Row():
        generate_btn = gr.Button("Generate Soundfont", variant="primary")

    with gr.Row():
        audio_output = gr.Audio(label="Generated Audio Preview", visible=False)
        status_output = gr.Textbox(label="Status", lines=2, visible=False)

    with gr.Row():
        wav_files = gr.File(
            label="Individual WAV Files",
            file_count="multiple",
            visible=False,
            elem_id="individual-wav-files",
        )

    html = """
    <div id="custom-player"
    style="width: 100%; height: 600px; border: 1px solid #f8f9fa; border-radius: 5px; margin-top: 10px;"
    ></div>
    """

    gr.HTML(html, min_height=1000, max_height=1000)

    gr.Markdown("## Download Soundfont Package here:")
    with gr.Row():
        sf = gr.File(
            label="Download SFZ Soundfont Package",
            type="filepath",
            visible=True,
            elem_id="sfz",
        )

    gr.Markdown("""
    # About            
    The model is a modified version of [stable audio open](https://huggingface.co/stabilityai/stable-audio-open-1.0).
                
    Unlike the original model, this version uses latent flow matching rather than latent diffusion.
    Secondly, the pitches are stacked in a channel dimension rather than concatenated in the time dimension. 
    This allows for faster generation.
                
    Soundfont export code is based on the [sf-creator](https://github.com/paulwellnerbou/sf-creator) project.
                
    Similar work by Nercessian and Imort: [InstrumentGen](https://instrumentgen.netlify.app/).
                
    Thank you @carlthome for coming up with the name.
                
    To cite this work, please use the following BibTeX entry:
    ```bibtex   
    @misc{erl-j-soundfont-generator,
        title={Erl-j's Soundfont Generator},
        author={Nicolas Jonason},
        year={2024},
        publisher={Huggingface},
    }
    ```
    """)

    generate_btn.click(
        fn=generate_and_export_soundfont,
        inputs=[text_input, steps],
        outputs=[audio_output, status_output, sf, wav_files],
    ).success(js="() => console.log('Success')")

    text_input.submit(
        fn=generate_and_export_soundfont,
        inputs=[text_input, steps],
        outputs=[audio_output, status_output, sf, wav_files],
    )

if __name__ == "__main__":
    print("Starting demo...")
    demo.launch()