File size: 12,884 Bytes
39789dc 28e11b9 deb5deb ad1e277 dfa7009 f2a5f6c deb5deb 1235da2 464bd2f f2a5f6c 464bd2f 3192191 464bd2f dfa7009 464bd2f ad1e277 464bd2f ad1e277 464bd2f dfa7009 464bd2f dfa7009 464bd2f dfa7009 464bd2f dfa7009 464bd2f dfa7009 464bd2f ad1e277 464bd2f f2a5f6c 464bd2f ad1e277 464bd2f deb5deb 464bd2f dfa7009 464bd2f dfa7009 464bd2f dfa7009 deb5deb 464bd2f 28e11b9 464bd2f dfa7009 ad1e277 464bd2f deb5deb 464bd2f 421c576 464bd2f ad1e277 464bd2f 421c576 464bd2f ad1e277 464bd2f 421c576 ad1e277 464bd2f ad1e277 464bd2f ad1e277 464bd2f ad1e277 464bd2f ad1e277 0f3047d ad1e277 0f3047d ad1e277 464bd2f deb5deb 464bd2f ad1e277 dfa7009 464bd2f ad1e277 dfa7009 deb5deb dfa7009 1235da2 ad1e277 dfa7009 ad1e277 dfa7009 464bd2f dfa7009 1235da2 464bd2f 1235da2 464bd2f 1235da2 ad1e277 464bd2f ad1e277 464bd2f 1235da2 ad1e277 464bd2f deb5deb 464bd2f ad1e277 464bd2f ad1e277 464bd2f ad1e277 1235da2 464bd2f ad1e277 1235da2 421c576 ad1e277 464bd2f ad1e277 f2a5f6c ad1e277 f2a5f6c deb5deb |
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
import gradio as gr
import requests
import json
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import torch
import random
# Use CodeGen instead of StarCoder (no authentication needed)
MODEL_NAME = "Salesforce/codegen-350M-mono"
# Initialize model with pipeline for easier usage
try:
code_generator = pipeline(
"text-generation",
model=MODEL_NAME,
tokenizer=MODEL_NAME,
torch_dtype=torch.float16,
device_map="auto" if torch.cuda.is_available() else None,
trust_remote_code=True
)
except Exception as e:
print(f"Error loading model: {e}")
# Fallback to CPU if GPU fails
code_generator = pipeline(
"text-generation",
model=MODEL_NAME,
tokenizer=MODEL_NAME,
device_map=None
)
# Curated working Strudel patterns - these are all tested and functional
WORKING_PATTERNS = {
"techno": [
"""stack(
s("bd*4").gain(0.8),
s("~ hh ~ hh").gain(0.5),
n("0 ~ 3 ~").s("sawtooth").octave(2).lpf(1200).gain(0.7)
).scale("a:minor")""",
"""stack(
s("bd bd ~ bd").gain(0.8),
s("[hh ch]*4").gain(0.4),
n("[0 3 5 7]*2").s("square").octave(2).cutoff(800).gain(0.6),
n("7 9 7 4").s("triangle").octave(4).delay(0.25).gain(0.5)
).scale("d:minor")""",
"""stack(
s("bd*4, ~ rim ~ rim").bank("RolandTR909").gain(0.8),
n("0 0 3 3").s("sawtooth").octave(2).lpf(2000).gain(0.7),
n("[7 9]*4").s("square").octave(4).room(0.4).gain(0.4)
).scale("g:minor")"""
],
"house": [
"""stack(
s("bd ~ ~ ~ bd ~ ~ ~").gain(0.7),
s("~ hh ~ hh").gain(0.5),
n("0 2 4 7").s("sine").octave(3).room(0.6).gain(0.6)
).scale("c:major")""",
"""stack(
s("bd ~ [~ bd] ~").gain(0.8),
s("[~ ch ~ hh]*2").gain(0.4),
n("[0 4 7]*2").s("sawtooth").octave(2).lpf(1500).gain(0.7),
n("0 7 4 2").s("triangle").octave(4).delay(0.125).gain(0.4)
).scale("f:major")""",
"""stack(
s("bd ~ ~ ~ bd ~ ~ ~").sometimes(fast(2)).gain(0.7),
n("[0 2] [4 7] [2 4] [7 0]").s("sine").octave(2).gain(0.6),
s("hh*8").degradeBy(0.3).gain(0.3)
).scale("g:major")"""
],
"ambient": [
"""stack(
s("~ ~ ~ rim").slow(2).gain(0.3).room(0.8),
n("[0 2 4]/3").s("sine").octave(3).slow(4).room(0.9).gain(0.5),
n("[7 9 11]/5").s("triangle").octave(4).slow(8).delay(0.375).gain(0.3)
).scale("d:minor")""",
"""stack(
n("0 2 4 7").s("sine").octave(2).slow(8).room(0.9).gain(0.4),
n("[4 7 9]/7").s("triangle").octave(4).slow(12).delay(0.5).gain(0.3),
s("~ ~ rim ~").slow(4).gain(0.2).room(0.9)
).scale("a:minor")""",
"""n("[0 4 7 11]/4").s("sine").octave(3).slow(8).room(0.9).lpf(800).gain(0.6).scale("e:minor")"""
],
"breakbeat": [
"""stack(
s("[bd sn]*2").sometimes(rev).gain(0.8),
s("[hh ch]*4").degradeBy(0.2).gain(0.5),
n("0 4 7 0").s("square").octave(2).cutoff(1000).gain(0.7)
).scale("a:minor")""",
"""stack(
s("bd [sn sn] bd sn").speed(0.9).gain(0.8),
n("[0 ~ 7] [3 5]").s("sawtooth").octave(2).lpf(1500).gain(0.6),
s("hh*8").sometimes(fast(2)).gain(0.4)
).scale("d:minor")"""
],
"experimental": [
"""stack(
s("[bd rim]*3").degradeBy(0.4).gain(0.7),
n("[0 3 7]/5").s("square").octave(2).cutoff(perlin.range(400,2000)).gain(0.6),
s("~ [hh hh] ~").pan(sine.slow(3)).gain(0.4)
).scale("g:minor")""",
"""stack(
s("bd ~ [perc:0 perc:1]").speed(rand.range(0.8,1.2)).gain(0.7),
n("0 [2 5] 7").s("sawtooth").octave(2).sometimes(add(note(12))).gain(0.6),
s("[ch oh]*5").degradeBy(0.5).gain(0.3)
).scale("f#:minor")"""
]
}
def get_random_working_pattern(genre, complexity="moderate"):
"""Get a random working pattern from our curated collection"""
patterns = WORKING_PATTERNS.get(genre, WORKING_PATTERNS["techno"])
if complexity == "simple":
# Return simpler single-line patterns
simple_patterns = {
"techno": 's("bd*4").gain(0.8)',
"house": 's("bd ~ ~ ~ bd ~ ~ ~").gain(0.7)',
"ambient": 'n("[0 2 4]/3").s("sine").octave(3).slow(4).room(0.9).gain(0.5).scale("d:minor")',
"breakbeat": 's("[bd sn]*2").gain(0.8)',
"experimental": 's("bd ~ [perc:0 perc:1]").degradeBy(0.4).gain(0.7)'
}
return simple_patterns.get(genre, simple_patterns["techno"])
return random.choice(patterns)
def create_variations(base_pattern, genre):
"""Create variations of a working base pattern"""
variations = []
# Original pattern
variations.append(base_pattern)
# Speed variations
if "stack(" in base_pattern:
variations.append(base_pattern.replace(").scale(", ").sometimes(fast(2)).scale("))
variations.append(base_pattern.replace(").scale(", ").slow(2).scale("))
# Effect variations
if ".gain(0.8)" in base_pattern:
variations.append(base_pattern.replace(".gain(0.8)", ".gain(0.8).room(0.4)"))
# Scale variations
scales = ["a:minor", "d:minor", "g:minor", "c:major", "f:major", "e:minor"]
for scale in scales:
if scale not in base_pattern:
new_pattern = base_pattern
for old_scale in scales:
if old_scale in base_pattern:
new_pattern = base_pattern.replace(old_scale, scale)
break
if new_pattern != base_pattern:
variations.append(new_pattern)
break
return random.choice(variations)
def generate_working_strudel_code(prompt, genre="techno", complexity="moderate"):
"""Generate guaranteed working Strudel code"""
# Get base working pattern
base_pattern = get_random_working_pattern(genre, complexity)
# Create variations based on prompt keywords
if any(word in prompt.lower() for word in ["fast", "speed", "quick", "rapid"]):
if "sometimes(fast(2))" not in base_pattern:
base_pattern = base_pattern.replace(".gain(", ".sometimes(fast(2)).gain(")
if any(word in prompt.lower() for word in ["slow", "chill", "ambient", "relaxed"]):
if ".slow(" not in base_pattern:
base_pattern = base_pattern.replace(".gain(", ".slow(2).gain(")
if any(word in prompt.lower() for word in ["reverb", "space", "room", "hall"]):
if ".room(" not in base_pattern:
base_pattern = base_pattern.replace(".gain(", ".room(0.6).gain(")
if any(word in prompt.lower() for word in ["delay", "echo", "repeat"]):
if ".delay(" not in base_pattern:
base_pattern = base_pattern.replace(".gain(", ".delay(0.25).gain(")
if any(word in prompt.lower() for word in ["filter", "sweep", "cutoff", "lpf"]):
if ".lpf(" not in base_pattern and ".cutoff(" not in base_pattern:
base_pattern = base_pattern.replace(".gain(", ".lpf(1200).gain(")
if any(word in prompt.lower() for word in ["glitch", "degraded", "broken", "corrupt"]):
if ".degradeBy(" not in base_pattern:
base_pattern = base_pattern.replace(".gain(", ".degradeBy(0.3).gain(")
# Add comment based on prompt
comment_line = f"// {prompt[:50]}{'...' if len(prompt) > 50 else ''}"
return f"{comment_line}\n{base_pattern}"
def create_visual_code(style="reactive"):
"""Create working visual code"""
visual_styles = {
"reactive": """osc(8, 0.1, 1.2)
.color(1.8, 0.8, 1.5)
.modulate(noise(2), 0.3)
.kaleid(6)
.out()""",
"kaleidoscope": """shape(6, 0.3, 0.01)
.repeat(3, 2)
.rotate(0, 0.03)
.color(1.2, 1.8, 0.8)
.kaleid(8)
.out()""",
"flowing": """noise(3, 0.1)
.color(1.5, 1.2, 0.8)
.modulate(osc(2, 0.05), 0.4)
.contrast(1.4)
.out()""",
"geometric": """osc(12, 0.02, 0.8)
.pixelate(32, 24)
.color(2, 0.8, 1.5)
.contrast(1.6)
.out()"""
}
return visual_styles.get(style, visual_styles["reactive"])
def create_complete_strudel_code(generated_code, include_visuals=True, visual_style="reactive"):
"""Create complete, working Strudel code"""
visual_code = ""
if include_visuals:
visual_code = f"""// Hydra visuals
await initHydra({{feedStrudel:5}})
{create_visual_code(visual_style)}
"""
# Add proper Strudel prefix for audio patterns
if not generated_code.startswith("$:"):
generated_code = f"$: {generated_code}"
complete_code = f"""{visual_code}{generated_code}
// Global effects (uncomment to use)
// all(x => x.fft(5).scope())"""
return complete_code
# Main generation function
def generate_interface(prompt, genre, complexity, include_visuals, visual_style):
"""Main interface function that generates working code"""
if not prompt.strip():
return "Please enter a description of the music you want to create."
# Generate working Strudel code
strudel_code = generate_working_strudel_code(prompt, genre, complexity)
# Create complete working template
complete_code = create_complete_strudel_code(strudel_code, include_visuals, visual_style)
return complete_code
# Create Gradio interface
with gr.Blocks(title="Working Strudel Generator", theme=gr.themes.Soft()) as app:
gr.Markdown("""
# π΅ Codel Strudel Code Generator
Generate Strudel live coding patterns!
β
**All patterns tested and functional**
ποΈ **Real Strudel syntax** - copy & paste ready
π¨ **Working Hydra visuals** included
π΅ **Genre-specific patterns** that actually sound good
**Usage:** Describe music β Generate β Copy to [strudel.cc](https://strudel.cc) β Play!
""")
with gr.Row():
with gr.Column():
prompt_input = gr.Textbox(
label="πΌ Describe your music",
placeholder="e.g., 'Fast techno with reverb', 'Chill ambient with delay', 'Glitchy breakbeat'",
lines=3
)
with gr.Row():
genre_dropdown = gr.Dropdown(
choices=["techno", "house", "ambient", "breakbeat", "experimental"],
value="techno",
label="π΅ Genre"
)
complexity_dropdown = gr.Dropdown(
choices=["simple", "moderate", "complex"],
value="moderate",
label="βοΈ Complexity"
)
with gr.Row():
include_visuals = gr.Checkbox(
label="π¨ Include visuals",
value=True
)
visual_style = gr.Dropdown(
choices=["reactive", "kaleidoscope", "flowing", "geometric"],
value="reactive",
label="ποΈ Visual Style"
)
generate_btn = gr.Button("π΅ Generate Working Code", variant="primary", size="lg")
with gr.Column():
output_code = gr.Code(
label="β
Working Strudel Code (Ready to Copy)",
language="javascript",
lines=18
)
gr.Markdown("""
**π― Instructions:**
1. **Copy** all the code above
2. **Go to** [strudel.cc](https://strudel.cc)
3. **Paste** and **click the play button** βΆοΈ
4. **It will work!** π
**π§ Customization:**
- Change `.gain()` values (0.1 to 1.0)
- Try different scales: "a:minor", "c:major", etc.
- Adjust `.lpf()` for filter sweeps
- Add `.room()` for reverb
""")
# Working examples
gr.Markdown("### πͺ Tested Examples (Guaranteed to Work)")
with gr.Row():
working_examples = [
["Fast techno with kick and hats", "techno", "moderate"],
["Chill ambient soundscape", "ambient", "simple"],
["Driving house beat", "house", "moderate"],
["Glitchy breakbeat", "breakbeat", "complex"],
["Experimental textures", "experimental", "complex"],
]
for example_text, example_genre, example_complexity in working_examples:
btn = gr.Button(f"β
{example_text}", size="sm")
btn.click(
lambda t=example_text, g=example_genre, c=example_complexity: (t, g, c),
outputs=[prompt_input, genre_dropdown, complexity_dropdown]
)
# Connect the generate button
generate_btn.click(
generate_interface,
inputs=[
prompt_input,
genre_dropdown,
complexity_dropdown,
include_visuals,
visual_style
],
outputs=output_code
)
# Launch the app
if __name__ == "__main__":
app.launch() |