c/app/metronome.c · 8355 字节 · 下载原文
/* metronome.c —— 节拍器(40~220 BPM,4/4 重拍),跑在任意 VML 宿主上
* metronome.c -- Metronome (40..220 BPM, 4/4 with an accent), runs on any VML host
*
* vml run Examples/c/app/metronome.c
*
* ## 跨平台口径
* ## Cross-platform notes
*
* 节拍按**墙钟**(`ui_time_get_ms`)排程,不依赖定时消息的精度;只用稳定 `ui_*`;
* 布局每帧按画布尺寸现算,覆盖 240x240 ~ 1920x1080;触摸/鼠标同坐标去重。
*
* ## 用法
* ## How to use
*
* −10 / −1 / +1 / +10 调 BPM;「开始/停止」控制;「点拍」连续点几下即可测出速度。
*/
#include <vml_ui.h>
int W, H;
int g_lang;
int bpm;
int running;
int beat; /* 0..3 */
int nextMs;
int lastTapMs;
int tapCount;
int taps[4];
char g_buf[24];
char g_num[16];
int clampi(int v, int lo, int hi)
{
if (v < lo) return lo;
if (v > hi) return hi;
return v;
}
int now_ms(void) { return ui_time_get_ms(); }
char* num_str(int v)
{
int n = 0, i = 0;
char t[12];
if (v == 0) { g_num[0] = '0'; g_num[1] = 0; return g_num; }
while (v > 0 && i < 11) { t[i] = (char)('0' + v % 10); v = v / 10; i = i + 1; }
while (i > 0) { i = i - 1; g_num[n] = t[i]; n = n + 1; }
g_num[n] = 0;
return g_num;
}
/* -1..1 三角波 */
int tri(int t, int period, int phase)
{
int p = (t + phase) % (period * 2);
if (p < 0) p = p + period * 2;
if (p < period) return p * 200 / period - 100;
return 200 - p * 200 / period;
}
void do_beat(void)
{
int accent = (beat == 0);
ui_audio_beep(accent ? 1568 : 1046, accent ? 70 : 45);
beat = (beat + 1) % 4;
}
/* ── 布局 ── */
int g_m, g_titleY, g_titleFS, g_bpmY, g_bpmFS, g_dotsY, g_dotR;
int g_penY, g_penH, g_stepY, g_stepH, g_ctrlY, g_ctrlH;
void layout(void)
{
int mn = W < H ? W : H;
g_m = clampi(W / 30, 6, 30);
g_titleY = clampi(H * 4 / 100, 8, 28);
g_titleFS = clampi(mn / 24, 10, 22);
g_bpmY = clampi(H * 18 / 100, 44, 220);
g_bpmFS = clampi(mn / 6, 26, 110);
g_dotsY = g_bpmY + g_bpmFS + clampi(H * 3 / 100, 6, 24);
g_dotR = clampi(mn / 28, 5, 18);
g_penY = g_dotsY + g_dotR * 3 + clampi(H * 3 / 100, 6, 24);
g_penH = clampi(H * 22 / 100, 40, 220);
g_ctrlH = clampi(H * 9 / 100, 22, 52);
g_ctrlY = H - g_ctrlH - clampi(H * 3 / 100, 6, 18);
g_stepH = clampi(H * 7 / 100, 20, 42);
g_stepY = g_ctrlY - g_stepH - clampi(H * 2 / 100, 4, 14);
}
void chip(int x, int y, int w, int h, char* label, int on, int col)
{
int fs = clampi(h * 40 / 100, 9, 18);
ui_2d_rect(x, y, w, h, on ? 0xFF31405C : 0xFF202737, 1, 0, h / 3);
ui_2d_text_v(x + w / 2, y + h / 2, label, on ? col : 0xFFB9C4D6, fs,
VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
}
void draw(void)
{
int i, cw, x, y, t, swing;
layout();
t = now_ms();
ui_2d_clear(0xFF11141C);
ui_2d_text_v(g_m, g_titleY, g_lang == 0 ? "节拍器" : "Metronome", 0xFFE6EBF5,
g_titleFS, VML_ANCHOR_LEFT, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
ui_2d_text_v(W - g_m, g_titleY, "4 / 4", 0xFF8B94A7, g_titleFS,
VML_ANCHOR_RIGHT, VML_VANCHOR_MIDDLE, 0);
/* BPM */
g_buf[0] = 0;
strcat(g_buf, num_str(bpm));
ui_2d_text_v(W / 2, g_bpmY, g_buf, running ? 0xFF5EC8F0 : 0xFFE6EBF5, g_bpmFS,
VML_ANCHOR_CENTER, VML_VANCHOR_TOP, VML_FONT_BOLD);
ui_2d_text_v(W / 2, g_bpmY + g_bpmFS, "BPM", 0xFF8B94A7,
clampi(g_bpmFS / 5, 9, 20),
VML_ANCHOR_CENTER, VML_VANCHOR_TOP, 0);
/* 四拍指示灯 */
cw = g_dotR * 3;
x = W / 2 - cw * 2 + g_dotR / 2;
for (i = 0; i < 4; i++) {
int on = running && (beat == (i + 1) % 4 || (beat == 0 && i == 3));
/* beat 在响完后就 +1 ⇒ 上一拍是 (beat+3)%4 */
on = running && (i == (beat + 3) % 4);
ui_2d_circle(x + i * cw, g_dotsY + g_dotR, g_dotR,
on ? (i == 0 ? 0xFFF3D24B : 0xFF5EC8F0) : 0xFF2A3038, 1, 0);
}
/* 摆锤 */
swing = running ? tri(t, clampi(60000 / bpm, 250, 1500), 0) : 0;
{
int cx = W / 2;
int top = g_penY;
int len = g_penH;
int ex = cx + swing * clampi(len / 3, 10, 120) / 100;
int ey = top + len;
ui_2d_line(cx, top, ex, ey, 0xFFB9C4D6, clampi(len / 40, 2, 8));
ui_2d_circle(ex, ey, clampi(len / 12, 5, 22), 0xFFF3A64B, 1, 0);
ui_2d_circle(cx, top, clampi(len / 20, 3, 12), 0xFF5EC8F0, 1, 0);
}
/* BPM 步进 */
{
char* labels[4];
labels[0] = "-10"; labels[1] = "-1"; labels[2] = "+1"; labels[3] = "+10";
cw = (W - g_m * 2) / 4;
for (i = 0; i < 4; i++)
chip(g_m + i * cw + 2, g_stepY, cw - 4, g_stepH, labels[i], 0, 0xFFE6EBF5);
}
/* 开始/停止 + 点拍 */
{
int half = (W - g_m * 2 - 8) / 2;
chip(g_m, g_ctrlY, half, g_ctrlH,
running ? (g_lang == 0 ? "停止" : "Stop") : (g_lang == 0 ? "开始" : "Start"),
running, 0xFF4ADE80);
chip(g_m + half + 8, g_ctrlY, half, g_ctrlH, g_lang == 0 ? "点拍" : "Tap", 0, 0xFFF3D24B);
}
ui_2d_present();
}
/* ── 交互 ── */
void toggle(void)
{
if (running) {
running = 0;
ui_audio_beep(440, 60);
} else {
running = 1;
beat = 0;
nextMs = now_ms();
do_beat();
nextMs = now_ms() + clampi(60000 / bpm, 250, 1500);
ui_audio_beep(660, 40);
}
}
void tap_tempo(void)
{
int t = now_ms();
int i, sum, n;
if (lastTapMs > 0) {
int dt = t - lastTapMs;
if (dt >= 200 && dt <= 2000) {
taps[tapCount % 4] = dt;
tapCount = tapCount + 1;
if (tapCount >= 4) {
sum = 0; n = 0;
for (i = 0; i < 4; i++) { sum += taps[i]; n = n + 1; }
bpm = clampi(60000 / (sum / n), 40, 220);
}
} else if (dt < 200) {
/* 太快,忽略 */
} else {
tapCount = 0;
}
} else {
tapCount = 0;
}
lastTapMs = t;
ui_audio_beep(880, 35);
}
void tap(int x, int y)
{
int i, cw, half;
layout();
if (y >= g_stepY && y < g_stepY + g_stepH) {
cw = (W - g_m * 2) / 4;
i = (x - g_m) / cw;
if (i == 0) bpm = clampi(bpm - 10, 40, 220);
else if (i == 1) bpm = clampi(bpm - 1, 40, 220);
else if (i == 2) bpm = clampi(bpm + 1, 40, 220);
else if (i == 3) bpm = clampi(bpm + 10, 40, 220);
if (i >= 0 && i < 4) ui_audio_beep(520, 30);
return;
}
if (y >= g_ctrlY && y < g_ctrlY + g_ctrlH) {
half = (W - g_m * 2 - 8) / 2;
if (x < g_m + half) toggle();
else tap_tempo();
return;
}
}
int main(void)
{
int m[4];
int t;
int lastX = -1, lastY = -1, sawTouch = 0;
g_lang = ui_scr_get_language();
ui_win_open_ex(g_lang == 0 ? "节拍器" : "Metronome", 0, 0,
VML_WIN_ROTATABLE, VML_WIN_NO_GAMEPAD);
W = ui_scr_get_w();
H = ui_scr_get_h();
ui_scr_set_keep_on(1);
bpm = 100;
running = 0;
beat = 0;
nextMs = 0;
lastTapMs = 0;
tapCount = 0;
draw();
while (ui_win_is_closed() == 0) {
t = ui_msg_wait(m, 40);
if (t == VML_MSG_WINDOWCLOSE) break;
if (t == VML_MSG_WINDOWRESIZE) {
W = ui_scr_get_w();
H = ui_scr_get_h();
draw();
continue;
}
if (t == VML_MSG_TOUCHDOWN) {
lastX = m[1]; lastY = m[2]; sawTouch = 1;
tap(m[1], m[2]);
draw();
continue;
}
if (t == VML_MSG_MOUSEDOWN) {
if (sawTouch && m[1] == lastX && m[2] == lastY) { sawTouch = 0; continue; }
tap(m[1], m[2]);
draw();
continue;
}
/* 节拍排程:墙钟欠账可能跨多拍,一次补齐 */
if (running) {
int guard = 0;
while (now_ms() >= nextMs && guard < 8) {
do_beat();
nextMs = nextMs + clampi(60000 / bpm, 250, 1500);
guard = guard + 1;
}
if (guard > 0) draw();
} else {
draw();
}
}
ui_scr_set_keep_on(0);
return 0;
}