Created
October 19, 2022 22:06
-
-
Save niclashoyer/d65125780c446d0e374c1bde82ff6fed to your computer and use it in GitHub Desktop.
Simple script to convert arduino music to music for the "sensor watch". Run this in apps/buzzer-test/!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
# simple script to convert notes from https://github.com/robsoncouto/arduino-songs | |
# to note/durations for use in apps/buzzer-test/ | |
# from https://github.com/joeycastillo/Sensor-Watch | |
tempo = int(input("tempo in bpm (e.g. 120): ")) | |
print("melody string (e.g. \"NOTE_B4,2,NOTE_E5,2\")") | |
print("Ctrl-D or Ctrl-Z ( windows ) to continue") | |
melody_str = "" | |
while True: | |
try: | |
line = input() | |
except EOFError: | |
break | |
if line.startswith("//"): | |
continue | |
line = line.split("//")[0] | |
if line.strip() != "": | |
melody_str += " " + line | |
sharp_map = { | |
"AS1": "A1SHARP_B1FLAT", | |
"CS2": "C2SHARP_D2FLAT", | |
"DS2": "D2SHARP_E2FLAT", | |
"FS2": "F2SHARP_G2FLAT", | |
"GS2": "G2SHARP_A2FLAT", | |
"AS2": "A2SHARP_B2FLAT", | |
"CS3": "C3SHARP_D3FLAT", | |
"DS3": "D3SHARP_E3FLAT", | |
"FS3": "F3SHARP_G3FLAT", | |
"GS3": "G3SHARP_A3FLAT", | |
"AS3": "A3SHARP_B3FLAT", | |
"CS4": "C4SHARP_D4FLAT", | |
"DS4": "D4SHARP_E4FLAT", | |
"FS4": "F4SHARP_G4FLAT", | |
"GS4": "G4SHARP_A4FLAT", | |
"AS4": "A4SHARP_B4FLAT", | |
"CS5": "C5SHARP_D5FLAT", | |
"DS5": "D5SHARP_E5FLAT", | |
"FS5": "F5SHARP_G5FLAT", | |
"GS5": "G5SHARP_A5FLAT", | |
"AS5": "A5SHARP_B5FLAT", | |
"CS6": "C6SHARP_D6FLAT", | |
"DS6": "D6SHARP_E6FLAT", | |
"FS6": "F6SHARP_G6FLAT", | |
"GS6": "G6SHARP_A6FLAT", | |
"AS6": "A6SHARP_B6FLAT", | |
"CS7": "C7SHARP_D7FLAT", | |
"DS7": "D7SHARP_E7FLAT", | |
"FS7": "F7SHARP_G7FLAT", | |
"GS7": "G7SHARP_A7FLAT", | |
"AS7": "A7SHARP_B7FLAT", | |
"CS8": "C8SHARP_D8FLAT", | |
"DS8": "D8SHARP_E8FLAT", | |
"FS8": "F8SHARP_G8FLAT", | |
"GS8": "G8SHARP_A8FLAT", | |
"AS8": "A8SHARP_B8FLAT", | |
} | |
def toduration(x): | |
x = int(x) | |
wholenote = (60000 * 2) / tempo; | |
duration = 0 | |
if x > 0: | |
duration = wholenote / x | |
else: | |
duration = wholenote / abs(x) | |
duration *= 1.5 | |
return str(int(duration)) | |
def tobuzzer(x): | |
s = x.split("_") | |
if s[0] == "NOTE": | |
if s[1] in sharp_map: | |
x = "NOTE_" + sharp_map.get(s[1]) | |
return "BUZZER_" + x | |
else: | |
return "BUZZER_NOTE_REST" | |
melody = list(filter(lambda s: s != "", map(str.strip, melody_str.split(",")))) | |
notes = list(map(tobuzzer, melody[::2])) | |
durations = list(map(toduration, melody[1::2])) | |
file = open("app.c", "w"); | |
file.write("""#include <stdio.h> | |
#include <string.h> | |
#include "watch.h" | |
typedef struct ApplicationState { | |
bool play; | |
} ApplicationState; | |
ApplicationState application_state; | |
void cb_alarm_pressed(void) { | |
application_state.play = true; | |
} | |
void app_init(void) { | |
memset(&application_state, 0, sizeof(application_state)); | |
} | |
void app_wake_from_backup(void) { | |
} | |
void app_setup(void) { | |
watch_register_extwake_callback(BTN_ALARM, cb_alarm_pressed, true); | |
watch_enable_display(); | |
watch_enable_buzzer(); | |
} | |
void app_prepare_for_standby(void) { | |
watch_display_string(" music ", 2); | |
} | |
void app_wake_from_standby(void) { | |
} | |
bool app_loop(void) { | |
if (application_state.play) { | |
printf("Playing song...\\n"); | |
""") | |
file.write(" const BuzzerNote melody[] = {\n " + ", \n ".join(list(notes)) + "\n};\n") | |
file.write(" const uint16_t durations[] = {\n " + ", \n ".join(list(durations)) + "\n};\n") | |
file.write(""" | |
application_state.play = false; | |
for(size_t i = 0, count = sizeof(melody) / sizeof(melody[0]); i < count; i++) { | |
char buf[9] = {0}; | |
if (melody[i] == BUZZER_NOTE_REST) { | |
printf("rest for %d ms\\n", durations[i]); | |
sprintf(buf, "%2drESt ", i); | |
} else { | |
printf("playing note %2d: %3.0f Hz for %d ms\\n", i, 1000000.0 / (float)NotePeriods[melody[i]], durations[i]); | |
sprintf(buf, "%2d%6d", i, NotePeriods[melody[i]]); | |
} | |
watch_display_string(buf, 2); | |
watch_buzzer_play_note(melody[i], durations[i]); | |
} | |
printf("done!\\n\\n"); | |
} | |
return true; | |
} | |
""") | |
file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment