summaryrefslogtreecommitdiff
path: root/src/c/metronome.c
blob: f7a5bd35acbde67af1e8b44ee9286f402b332914 (plain)
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
/* Copyright (C) 2026 Reiner Herrmann
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <pebble.h>

static Window *metronome_window;
static Layer *draw_layer;
static TextLayer *bpm_text;

static AppTimer *beat_timer;

#define BPM_TO_MS(bpm) (60000/(bpm))
#define IS_FIRST_BEAT() (current_beat % beats == 0)

#define COLOR_BG GColorWhite
#define COLOR_BG_INV GColorBlack
#define COLOR_FG GColorBlack

#define NOTE_C4 60
#define NOTE_C5 72

static int bpm = 90;
static int beats = 4;
static int current_beat = -1;

static bool playing = true;

static bool flash_enabled = true;
static bool vibrate_enabled = true;
static bool beep_enabled = true;

static void restore_background_color() {
	window_set_background_color(metronome_window, COLOR_BG);
}

static void metronome_flash() {
	static const int flash_duration = 50;
	if (!flash_enabled) {
		return;
	}
	window_set_background_color(metronome_window, COLOR_BG_INV);
	app_timer_register(flash_duration, restore_background_color, NULL);
}

static void metronome_vibrate() {
	if (!vibrate_enabled) {
		return;
	}
	if (IS_FIRST_BEAT()) {
		// TODO: slightly longer pulse
		vibes_short_pulse();
	} else {
		vibes_short_pulse();
	}
}

static void metronome_beep() {
#if defined(PBL_SPEAKER)
	if (!beep_enabled || speaker_is_muted()) {
		return;
	}
	const SpeakerNote note = {
		.midi_note = IS_FIRST_BEAT() ? NOTE_C5 : NOTE_C4,
		.waveform = SpeakerWaveformSquare,
		.duration_ms = 150,
		.velocity = 0,
	};

	speaker_play_notes(&note, 1, 0);
#endif
}

static void beat_timer_callback() {
	beat_timer = app_timer_register(BPM_TO_MS(bpm), beat_timer_callback, NULL);

	current_beat++;

	metronome_flash();
	metronome_vibrate();
	metronome_beep();

	layer_mark_dirty(draw_layer);
}

static void update_bpm_text() {
	static char bpm_buf[6];
	snprintf(bpm_buf, sizeof(bpm_buf), "%d", bpm);
	text_layer_set_text(bpm_text, bpm_buf);
}

static void beat_indicators_update_proc(Layer *layer, GContext *ctx) {
	GRect bounds = layer_get_unobstructed_bounds(layer);

	const int rect_radius = 10;
	const int width = bounds.size.w / beats;
	const int height = bounds.size.h;

	for (int i=0; i<beats; i++) {
		GColor color1, color2;
		if (current_beat % (2 * beats) >= beats) {
			color1 = GColorGreen;
			color2 = GColorOrange;
		} else {
			color1 = GColorOrange;
			color2 = GColorGreen;
		}
		GRect rect = GRect(5 + (i * width), 10, width - 10, height - 10);
		if (current_beat % beats < i) {
			graphics_context_set_fill_color(ctx, color1);
		} else {
			graphics_context_set_fill_color(ctx, color2);
		}
		graphics_fill_rect(ctx, rect, rect_radius, GCornersAll);

		graphics_context_set_stroke_color(ctx, COLOR_FG);
		graphics_draw_round_rect(ctx, rect, rect_radius);
	}
}

bool is_paused() {
	return !playing;
}

void start_metronome() {
	beat_timer = app_timer_register(0, beat_timer_callback, NULL);
	playing = true;
}

void stop_metronome() {
	app_timer_cancel(beat_timer);
	playing = false;
}

static void metronome_window_load(Window *window) {
	Layer *window_layer = window_get_root_layer(window);
	GRect bounds = layer_get_unobstructed_bounds(window_layer);

	const int font_height = 38;

	/* layer for drawing beat bars */
	draw_layer = layer_create(GRect(0, 0, bounds.size.w - ACTION_BAR_WIDTH, bounds.size.h - font_height - 2*20));
	layer_set_update_proc(draw_layer, beat_indicators_update_proc);
	layer_add_child(window_layer, draw_layer);

	/* BPM text */
	bpm_text = text_layer_create(GRect(bounds.origin.x, bounds.size.h - font_height - 20, bounds.size.w - ACTION_BAR_WIDTH, font_height));
	text_layer_set_background_color(bpm_text, GColorClear);
	text_layer_set_text_color(bpm_text, COLOR_FG);
	text_layer_set_text_alignment(bpm_text, GTextAlignmentCenter);
	text_layer_set_font(bpm_text, fonts_get_system_font(FONT_KEY_LECO_38_BOLD_NUMBERS));
	update_bpm_text();
	layer_add_child(window_layer, text_layer_get_layer(bpm_text));

	start_metronome();
}

static void metronome_window_unload(Window *window) {
	text_layer_destroy(bpm_text);
}

Window *create_metronome_window() {
	metronome_window = window_create();
	window_set_window_handlers(metronome_window, (WindowHandlers) {
		.load = metronome_window_load,
		.unload = metronome_window_unload,
	});
	window_set_background_color(metronome_window, COLOR_BG);

	return metronome_window;
}

void increase_bpm() {
	bpm++;
	if (bpm > 600) {
		bpm = 600;
	}
	APP_LOG(APP_LOG_LEVEL_DEBUG, "increasing bpm to: %d", bpm);
	update_bpm_text();
}

void decrease_bpm() {
	bpm--;
	if (bpm <= 0) {
		bpm = 0;
	}
	APP_LOG(APP_LOG_LEVEL_DEBUG, "decreasing bpm to: %d", bpm);
	update_bpm_text();
}

void increase_beats() {
	beats++;
	if (beats > 16) {
		beats = 16;
	}
	APP_LOG(APP_LOG_LEVEL_DEBUG, "increasing beats to: %d", beats);
}

void decrease_beats() {
	beats--;
	if (beats < 1) {
		beats = 1;
	}
	APP_LOG(APP_LOG_LEVEL_DEBUG, "decreasing beats to: %d", beats);
}