-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmf2usb_midi.c
356 lines (298 loc) · 11.7 KB
/
fmf2usb_midi.c
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
// this code was tested in Flipper Zero SDK v50.1 to v54.0
//It is still under development.
//I would like to add various functions when we feel like it.
//It's unstable sometimes, so please tolerate it.
//The license for this FAP shall be WTFPL.
#include <furi.h>
#include <string.h>
#include <furi_hal.h>
#include "usb/usb_midi_driver.h"
#include "midi/message.h"
#include <gui/gui.h>
#include <dialogs/dialogs.h>
#include <storage/storage.h>
#define MUSIC_PLAYER_APP_EXTENSION "*"
#define TAG "FMF2USB_MIDI"
#define MUSIC_PLAYER_APP_PATH_FOLDER "/ext/music_player"
typedef enum { FmStatus_Idle, FmStatus_Loading, FmStatus_Sending } FmStatus;
typedef struct {
Gui* gui;
ViewPort* view_port;
FmStatus status;
bool should_exit;
uint8_t beat;
} Fmf2UsbMidiApp;
bool convert_fmf_to_midi(
const char* file_content,
uint8_t* midi_data,
size_t* midi_data_size,
int* bpm);
void fmf2usb_midi_play_file(const char* file_path, Fmf2UsbMidiApp* app);
void fmf2usb_midi_render_callback(Canvas* canvas, void* ctx);
void fmf2usb_midi_input_callback(InputEvent* input_event, void* ctx);
void fmf2usb_midi_input_callback(InputEvent* input_event, void* ctx) {
furi_assert(ctx);
Fmf2UsbMidiApp* app = ctx;
if(input_event->key == InputKeyBack && input_event->type == InputTypeShort) {
app->should_exit = true;
}
}
Fmf2UsbMidiApp* fmf2usb_midi_app_alloc() {
Fmf2UsbMidiApp* app = malloc(sizeof(Fmf2UsbMidiApp));
app->gui = furi_record_open(RECORD_GUI);
app->view_port = view_port_alloc();
view_port_draw_callback_set(app->view_port, fmf2usb_midi_render_callback, app);
view_port_input_callback_set(app->view_port, fmf2usb_midi_input_callback, app);
app->status = FmStatus_Idle;
app->should_exit = false;
return app;
}
void fmf2usb_midi_render_callback(Canvas* canvas, void* ctx) {
furi_assert(ctx);
Fmf2UsbMidiApp* app = ctx;
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
switch(app->status) {
case FmStatus_Idle:
canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, "Select an FMF file");
break;
case FmStatus_Loading:
canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, "Hold on...");
break;
case FmStatus_Sending:
canvas_draw_str_aligned(canvas, 64, 20, AlignCenter, AlignCenter, "Sending MIDI via USB");
canvas_draw_str_aligned(canvas, 64, 44, AlignCenter, AlignCenter, "Press Back to exit");
break;
}
}
bool convert_fmf_to_midi(
const char* file_content,
uint8_t* midi_data,
size_t* midi_data_size,
int* bpm) {
FURI_LOG_D(TAG, "Converting FMF to MIDI...");
// analyze the fmf file
char* lines = strdup(file_content);
char* line = strtok(lines, "\n");
int duration = 0;
int octave = 0;
while(line != NULL) {
if(strncmp(line, "BPM:", 4) == 0) {
sscanf(line, "BPM: %d", bpm);
} else if(strncmp(line, "Duration:", 9) == 0) {
sscanf(line, "Duration: %d", &duration);
} else if(strncmp(line, "Octave:", 7) == 0) {
sscanf(line, "Octave: %d", &octave);
} else if(strncmp(line, "Notes:", 6) == 0) {
char* notes = line + 7;
char* note = strtok(notes, ", ");
while(note != NULL) {
int note_duration = duration;
int note_octave = octave;
int dots = 0;
bool is_sharp = false;
// note duration analyze
if(isdigit((unsigned char)*note)) {
char* duration_str = note;
while(isdigit((unsigned char)*duration_str)) {
duration_str++;
}
if(duration_str > note) {
note_duration = atoi(note);
note = duration_str;
}
} else {
note_duration = duration;
}
// note name
char note_name = *note++;
// sharp
if(*note == '#') {
is_sharp = true;
note++;
}
// octave
if(isdigit((unsigned char)*note)) {
note_octave = *note++ - '0';
}
// dots
while(*note == '.') {
dots++;
note++;
}
int note_delay = (60000 / (*bpm * 2)) * (duration / note_duration);
int total_delay = note_delay;
for(int i = 0; i < dots; i++) {
total_delay += note_delay / (1 << (i + 1));
}
if(note_name == 'P') {
// when P is used, insert a delay
midi_data[(*midi_data_size)++] = 0x00; // Delay
midi_data[(*midi_data_size)++] = (total_delay >> 8) & 0xFF;
midi_data[(*midi_data_size)++] = total_delay & 0xFF;
} else {
int note_number = 0;
switch(note_name) {
case 'C':
note_number = 0;
break;
case 'D':
note_number = 2;
break;
case 'E':
note_number = 4;
break;
case 'F':
note_number = 5;
break;
case 'G':
note_number = 7;
break;
case 'A':
note_number = 9;
break;
case 'B':
note_number = 11;
break;
}
if(is_sharp) {
note_number++;
}
note_number += (note_octave * 12);
midi_data[(*midi_data_size)++] = 0x90; // Note On
midi_data[(*midi_data_size)++] = note_number;
midi_data[(*midi_data_size)++] = 100; // Velocity
int note_on_delay = total_delay * 9 / 10;
midi_data[(*midi_data_size)++] = 0x00; // Delay
midi_data[(*midi_data_size)++] = (note_on_delay >> 8) & 0xFF;
midi_data[(*midi_data_size)++] = note_on_delay & 0xFF;
midi_data[(*midi_data_size)++] = 0x80; // Note Off
midi_data[(*midi_data_size)++] = note_number;
midi_data[(*midi_data_size)++] = 0; // Velocity
int note_off_delay = total_delay - note_on_delay;
midi_data[(*midi_data_size)++] = 0x00; // Delay
midi_data[(*midi_data_size)++] = (note_off_delay >> 8) & 0xFF;
midi_data[(*midi_data_size)++] = note_off_delay & 0xFF;
}
note = strtok(NULL, ", ");
}
}
line = strtok(NULL, "\n");
}
free(lines);
FURI_LOG_D(TAG, "Conversion complete. MIDI data size: %d", *midi_data_size);
return true;
}
void fmf2usb_midi_play_file(const char* file_path, Fmf2UsbMidiApp* app) {
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
if(!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING)) {
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return;
}
char* file_content = malloc(storage_file_size(file) + 1);
storage_file_read(file, file_content, storage_file_size(file));
file_content[storage_file_size(file)] = '\0';
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
uint8_t* midi_data = malloc(1024);
size_t midi_data_size = 0;
int bpm = 0;
if(convert_fmf_to_midi(file_content, midi_data, &midi_data_size, &bpm)) {
FURI_LOG_D(TAG, "Playing MIDI data...");
app->status = FmStatus_Sending;
app->beat = 0;
view_port_update(app->view_port);
size_t pos = 0;
uint32_t led_update_time = furi_get_tick();
while(pos < midi_data_size && !app->should_exit) {
if(midi_data[pos] == 0x00) { // Delay
uint32_t delay = (midi_data[pos + 1] << 8) | midi_data[pos + 2];
if(delay > 0) {
FURI_LOG_D(TAG, "Delay: %ld ms", delay);
furi_delay_ms(delay);
}
pos += 3;
} else {
FURI_LOG_D(
TAG,
"Sending MIDI event: %02X %02X %02X",
midi_data[pos],
midi_data[pos + 1],
midi_data[pos + 2]);
uint8_t midi_event[4];
midi_event[0] = 0x09;
midi_event[1] = midi_data[pos];
midi_event[2] = midi_data[pos + 1];
midi_event[3] = midi_data[pos + 2];
midi_usb_tx(midi_event, 4);
pos += 3;
}
uint32_t current_time = furi_get_tick();
if(current_time - led_update_time >= (uint32_t)(60000 / bpm)) {
led_update_time = current_time;
app->beat = (app->beat + 1) % 4;
if(app->beat == 0) {
furi_hal_light_set(LightGreen, 0xFF);
furi_hal_light_set(LightBlue, 0x00);
} else {
furi_hal_light_set(LightGreen, 0x00);
furi_hal_light_set(LightBlue, 0xFF);
}
}
}
furi_hal_light_set(LightGreen, 0x00);
furi_hal_light_set(LightBlue, 0x00);
FURI_LOG_D(TAG, "Playback complete");
FURI_LOG_D(TAG, "Playback complete");
} else {
FURI_LOG_E(TAG, "Failed to convert FMF to MIDI");
}
furi_delay_ms(500);
free(midi_data);
free(file_content);
}
void fmf2usb_midi_app_free(Fmf2UsbMidiApp* app) {
furi_assert(app);
view_port_enabled_set(app->view_port, false);
gui_remove_view_port(app->gui, app->view_port);
view_port_free(app->view_port);
furi_record_close(RECORD_GUI);
app->gui = NULL;
free(app);
}
int32_t fmf2usb_midi_app(void* p) {
UNUSED(p);
FURI_LOG_D(TAG, "Starting fmf2usb_midi_app");
Fmf2UsbMidiApp* app = fmf2usb_midi_app_alloc();
gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
furi_hal_usb_set_config(&midi_usb_interface, NULL);
furi_hal_usb_lock();
FuriString* file_path;
file_path = furi_string_alloc();
furi_string_set(file_path, MUSIC_PLAYER_APP_PATH_FOLDER);
DialogsFileBrowserOptions browser_options;
dialog_file_browser_set_basic_options(&browser_options, MUSIC_PLAYER_APP_EXTENSION, NULL);
browser_options.hide_ext = false;
browser_options.base_path = MUSIC_PLAYER_APP_PATH_FOLDER;
DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
bool res = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
furi_record_close(RECORD_DIALOGS);
if(res) {
FURI_LOG_D(TAG, "File selected: %s", furi_string_get_cstr(file_path));
app->status = FmStatus_Loading;
view_port_update(app->view_port);
fmf2usb_midi_play_file(furi_string_get_cstr(file_path), app);
} else {
FURI_LOG_D(TAG, "File selection cancelled");
}
furi_string_free(file_path);
fmf2usb_midi_app_free(app);
furi_hal_usb_unlock();
furi_hal_usb_set_config(usb_mode_prev, NULL);
FURI_LOG_D(TAG, "Exiting fmf2usb_midi_app");
return 0;
}