Files
2026-10-22 21:07:47 +08:00

88 lines
2.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""產生示範曲包 demo.zip(節拍音軌 + 漸層封面 + 三種音符的範例譜面),全部由程式產生、無版權疑慮
用法:python examples/make_demo_pack.py [輸出路徑]
"""
import os
import sys
import tempfile
import wave
import numpy as np
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, ".."))
from chart import Note # noqa: E402
from pack import SongPack, default_config, save_pack # noqa: E402
from placer import auto_place # noqa: E402
BPM = 120
FIRST_BEAT = 1000
BEATS = 64
SR = 44100
def click_track(path):
beat = 60.0 / BPM
length = FIRST_BEAT / 1000 + BEATS * beat + 1.0
data = np.zeros(int(SR * length), dtype=np.float32)
n = int(SR * 0.05)
t = np.arange(n) / SR
for b in range(BEATS):
freq = 1760 if b % 4 == 0 else 1320
start = int((FIRST_BEAT / 1000 + b * beat) * SR)
data[start:start + n] += np.sin(2 * np.pi * freq * t) * np.exp(-t * 60) * 0.6
pcm = (np.clip(data, -1, 1) * 32767).astype(np.int16)
with wave.open(path, "wb") as w:
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(SR)
w.writeframes(pcm.tobytes())
def cover(path):
from PIL import Image
x = np.linspace(0, 1, 512)
r = (40 + 180 * x)[None, :].repeat(512, 0)
g = np.full((512, 512), 60.0)
b = (200 - 120 * x)[:, None].repeat(512, 1)
Image.fromarray(np.stack([r, g, b], -1).astype(np.uint8)).save(path)
def demo_notes():
beat = 60000 / BPM
notes = []
for i in range(48): # 前 48 拍:左右交替,每 4 拍一顆中間
t = int(FIRST_BEAT + i * beat)
notes.append(Note(t, "M" if i % 4 == 3 else ("L" if i % 2 == 0 else "R"), "tap"))
t = int(FIRST_BEAT + 48 * beat)
notes.append(Note(t, "L", "hold", int(t + 2 * beat)))
notes.append(Note(int(t + 2 * beat), "R", "hold", int(t + 4 * beat)))
notes.append(Note(int(t + 6 * beat), "L", "slide", int(t + 8 * beat)))
notes.append(Note(int(t + 10 * beat), "R", "slide", int(t + 12 * beat)))
return notes
def main():
out = sys.argv[1] if len(sys.argv) > 1 else os.path.join(HERE, "demo.zip")
tmp = tempfile.mkdtemp()
music, image = os.path.join(tmp, "music.wav"), os.path.join(tmp, "cover.png")
click_track(music)
cover(image)
pack = SongPack(default_config(id="demo", name="Maker Demo", artist="Rhyculator Maker",
charter="Rhyculator Maker", bpm=BPM, beat_offset=FIRST_BEAT,
normal_num=1))
pack.set_music(music)
pack.set_image(image)
chart = pack.chart("normal", create=True)
chart.notes = demo_notes()
auto_place(chart)
save_pack(pack, out)
pack.close()
print(f"已產生 {out}(normal {len(chart.notes)} 顆)")
if __name__ == "__main__":
main()