From cdad8e3446456e9eceb38eacc0273f13d824c01b Mon Sep 17 00:00:00 2001 From: Lucas Berezy Date: Wed, 2 Mar 2016 00:28:38 +1100 Subject: [PATCH 1/3] Added small song parser to play Super Mario Bros. theme --- main.c | 85 +++++++++----- smb.song | 351 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 408 insertions(+), 28 deletions(-) create mode 100644 smb.song diff --git a/main.c b/main.c index c2a859e..0814af9 100644 --- a/main.c +++ b/main.c @@ -7,7 +7,10 @@ #include #include #include +#include + +#define am_delay(x) (square_am_signal(x, 0)) __m128i reg; __m128i reg_zero; __m128i reg_one; @@ -33,7 +36,12 @@ static inline void square_am_signal(float time, float frequency) { } } -int main() +int prefix(const char *pre, const char *str) +{ + return strncmp(pre, str, strlen(pre)) == 0; +} + +int main(int argc, char* argv[]) { mach_timebase_info_data_t theTimeBaseInfo; mach_timebase_info(&theTimeBaseInfo); @@ -46,33 +54,54 @@ int main() reg_zero = _mm_set_epi32(0, 0, 0, 0); reg_one = _mm_set_epi32(-1, -1, -1, -1); - + FILE* fp; + if (argc == 2) { + fp = fopen(argv[1], "r"); + } else { + puts("Supply file to read song from."); + exit(1); + } + char buffer[256]; while (1) { - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2093); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2673); - square_am_signal(0.790, 2673); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2349); - square_am_signal(0.790, 2349); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 3136); - square_am_signal(0.790, 3136); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2093); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2349); - square_am_signal(0.790, 2093); + fgets(buffer, 256 - 1, fp); + if (strlen(buffer) == 0) { + /* skip blank lines */ + continue; + } + + if (prefix(":beep", buffer)) { + int t; + int f; + if(sscanf(buffer, ":beep frequency=%d length=%dms", &f, &t) == 0) { + continue; + } + printf("F: %d, T: %d\n", f, t); + square_am_signal(t / 1000.0, f); + } else if (prefix(":delay", buffer)) { + int d; + if (sscanf(buffer, ":delay %dms", &d) == 0) { + continue; + } + printf("D: %d\n", d); + square_am_signal(d / 1000.0, 0); + } + /* Super Mario Bros. Theme + square_am_signal(0.100, 660); + am_delay(0.150); + square_am_signal(0.100, 660); + am_delay(0.300); + square_am_signal(0.100, 660); + am_delay(0.300); + square_am_signal(0.100, 510); + am_delay(0.100); + square_am_signal(0.100, 660); + am_delay(0.300); + square_am_signal(0.100, 770); + am_delay(0.550); + square_am_signal(0.100, 380); + am_delay(0.575);*/ + + + } } diff --git a/smb.song b/smb.song new file mode 100644 index 0000000..d8709df --- /dev/null +++ b/smb.song @@ -0,0 +1,351 @@ +:beep frequency=660 length=100ms; +:delay 150ms; +:beep frequency=660 length=100ms; +:delay 300ms; +:beep frequency=660 length=100ms; +:delay 300ms; +:beep frequency=510 length=100ms; +:delay 100ms; +:beep frequency=660 length=100ms; +:delay 300ms; +:beep frequency=770 length=100ms; +:delay 550ms; +:beep frequency=380 length=100ms; +:delay 575ms; + +:beep frequency=510 length=100ms; +:delay 450ms; +:beep frequency=380 length=100ms; +:delay 400ms; +:beep frequency=320 length=100ms; +:delay 500ms; +:beep frequency=440 length=100ms; +:delay 300ms; +:beep frequency=480 length=80ms; +:delay 330ms; +:beep frequency=450 length=100ms; +:delay 150ms; +:beep frequency=430 length=100ms; +:delay 300ms; +:beep frequency=380 length=100ms; +:delay 200ms; +:beep frequency=660 length=80ms; +:delay 200ms; +:beep frequency=760 length=50ms; +:delay 150ms; +:beep frequency=860 length=100ms; +:delay 300ms; +:beep frequency=700 length=80ms; +:delay 150ms; +:beep frequency=760 length=50ms; +:delay 350ms; +:beep frequency=660 length=80ms; +:delay 300ms; +:beep frequency=520 length=80ms; +:delay 150ms; +:beep frequency=580 length=80ms; +:delay 150ms; +:beep frequency=480 length=80ms; +:delay 500ms; + +:beep frequency=510 length=100ms; +:delay 450ms; +:beep frequency=380 length=100ms; +:delay 400ms; +:beep frequency=320 length=100ms; +:delay 500ms; +:beep frequency=440 length=100ms; +:delay 300ms; +:beep frequency=480 length=80ms; +:delay 330ms; +:beep frequency=450 length=100ms; +:delay 150ms; +:beep frequency=430 length=100ms; +:delay 300ms; +:beep frequency=380 length=100ms; +:delay 200ms; +:beep frequency=660 length=80ms; +:delay 200ms; +:beep frequency=760 length=50ms; +:delay 150ms; +:beep frequency=860 length=100ms; +:delay 300ms; +:beep frequency=700 length=80ms; +:delay 150ms; +:beep frequency=760 length=50ms; +:delay 350ms; +:beep frequency=660 length=80ms; +:delay 300ms; +:beep frequency=520 length=80ms; +:delay 150ms; +:beep frequency=580 length=80ms; +:delay 150ms; +:beep frequency=480 length=80ms; +:delay 500ms; + +:beep frequency=500 length=100ms; +:delay 300ms; + +:beep frequency=760 length=100ms; +:delay 100ms; +:beep frequency=720 length=100ms; +:delay 150ms; +:beep frequency=680 length=100ms; +:delay 150ms; +:beep frequency=620 length=150ms; +:delay 300ms; + +:beep frequency=650 length=150ms; +:delay 300ms; +:beep frequency=380 length=100ms; +:delay 150ms; +:beep frequency=430 length=100ms; +:delay 150ms; + +:beep frequency=500 length=100ms; +:delay 300ms; +:beep frequency=430 length=100ms; +:delay 150ms; +:beep frequency=500 length=100ms; +:delay 100ms; +:beep frequency=570 length=100ms; +:delay 220ms; + +:beep frequency=500 length=100ms; +:delay 300ms; + +:beep frequency=760 length=100ms; +:delay 100ms; +:beep frequency=720 length=100ms; +:delay 150ms; +:beep frequency=680 length=100ms; +:delay 150ms; +:beep frequency=620 length=150ms; +:delay 300ms; + +:beep frequency=650 length=200ms; +:delay 300ms; + +:beep frequency=1020 length=80ms; +:delay 300ms; +:beep frequency=1020 length=80ms; +:delay 150ms; +:beep frequency=1020 length=80ms; +:delay 300ms; + +:beep frequency=380 length=100ms; +:delay 300ms; +:beep frequency=500 length=100ms; +:delay 300ms; + +:beep frequency=760 length=100ms; +:delay 100ms; +:beep frequency=720 length=100ms; +:delay 150ms; +:beep frequency=680 length=100ms; +:delay 150ms; +:beep frequency=620 length=150ms; +:delay 300ms; + +:beep frequency=650 length=150ms; +:delay 300ms; +:beep frequency=380 length=100ms; +:delay 150ms; +:beep frequency=430 length=100ms; +:delay 150ms; + +:beep frequency=500 length=100ms; +:delay 300ms; +:beep frequency=430 length=100ms; +:delay 150ms; +:beep frequency=500 length=100ms; +:delay 100ms; +:beep frequency=570 length=100ms; +:delay 420ms; + +:beep frequency=585 length=100ms; +:delay 450ms; + +:beep frequency=550 length=100ms; +:delay 420ms; + +:beep frequency=500 length=100ms; +:delay 360ms; + +:beep frequency=380 length=100ms; +:delay 300ms; +:beep frequency=500 length=100ms; +:delay 300ms; +:beep frequency=500 length=100ms; +:delay 150ms; +:beep frequency=500 length=100ms; +:delay 300ms; + +:beep frequency=500 length=100ms; +:delay 300ms; + +:beep frequency=760 length=100ms; +:delay 100ms; +:beep frequency=720 length=100ms; +:delay 150ms; +:beep frequency=680 length=100ms; +:delay 150ms; +:beep frequency=620 length=150ms; +:delay 300ms; + +:beep frequency=650 length=150ms; +:delay 300ms; +:beep frequency=380 length=100ms; +:delay 150ms; +:beep frequency=430 length=100ms; +:delay 150ms; + +:beep frequency=500 length=100ms; +:delay 300ms; +:beep frequency=430 length=100ms; +:delay 150ms; +:beep frequency=500 length=100ms; +:delay 100ms; +:beep frequency=570 length=100ms; +:delay 220ms; + +:beep frequency=500 length=100ms; +:delay 300ms; + +:beep frequency=760 length=100ms; +:delay 100ms; +:beep frequency=720 length=100ms; +:delay 150ms; +:beep frequency=680 length=100ms; +:delay 150ms; +:beep frequency=620 length=150ms; +:delay 300ms; + +:beep frequency=650 length=200ms; +:delay 300ms; + +:beep frequency=1020 length=80ms; +:delay 300ms; +:beep frequency=1020 length=80ms; +:delay 150ms; +:beep frequency=1020 length=80ms; +:delay 300ms; + +:beep frequency=380 length=100ms; +:delay 300ms; +:beep frequency=500 length=100ms; +:delay 300ms; + +:beep frequency=760 length=100ms; +:delay 100ms; +:beep frequency=720 length=100ms; +:delay 150ms; +:beep frequency=680 length=100ms; +:delay 150ms; +:beep frequency=620 length=150ms; +:delay 300ms; + +:beep frequency=650 length=150ms; +:delay 300ms; +:beep frequency=380 length=100ms; +:delay 150ms; +:beep frequency=430 length=100ms; +:delay 150ms; + +:beep frequency=500 length=100ms; +:delay 300ms; +:beep frequency=430 length=100ms; +:delay 150ms; +:beep frequency=500 length=100ms; +:delay 100ms; +:beep frequency=570 length=100ms; +:delay 420ms; + +:beep frequency=585 length=100ms; +:delay 450ms; + +:beep frequency=550 length=100ms; +:delay 420ms; + +:beep frequency=500 length=100ms; +:delay 360ms; + +:beep frequency=380 length=100ms; +:delay 300ms; +:beep frequency=500 length=100ms; +:delay 300ms; +:beep frequency=500 length=100ms; +:delay 150ms; +:beep frequency=500 length=100ms; +:delay 300ms; + +:beep frequency=500 length=60ms; +:delay 150ms; +:beep frequency=500 length=80ms; +:delay 300ms; +:beep frequency=500 length=60ms; +:delay 350ms; +:beep frequency=500 length=80ms; +:delay 150ms; +:beep frequency=580 length=80ms; +:delay 350ms; +:beep frequency=660 length=80ms; +:delay 150ms; +:beep frequency=500 length=80ms; +:delay 300ms; +:beep frequency=430 length=80ms; +:delay 150ms; +:beep frequency=380 length=80ms; +:delay 600ms; + +:beep frequency=500 length=60ms; +:delay 150ms; +:beep frequency=500 length=80ms; +:delay 300ms; +:beep frequency=500 length=60ms; +:delay 350ms; +:beep frequency=500 length=80ms; +:delay 150ms; +:beep frequency=580 length=80ms; +:delay 150ms; +:beep frequency=660 length=80ms; +:delay 550ms; + +:beep frequency=870 length=80ms; +:delay 325ms; +:beep frequency=760 length=80ms; +:delay 600ms; + +:beep frequency=500 length=60ms; +:delay 150ms; +:beep frequency=500 length=80ms; +:delay 300ms; +:beep frequency=500 length=60ms; +:delay 350ms; +:beep frequency=500 length=80ms; +:delay 150ms; +:beep frequency=580 length=80ms; +:delay 350ms; +:beep frequency=660 length=80ms; +:delay 150ms; +:beep frequency=500 length=80ms; +:delay 300ms; +:beep frequency=430 length=80ms; +:delay 150ms; +:beep frequency=380 length=80ms; +:delay 600ms; + +:beep frequency=660 length=100ms; +:delay 150ms; +:beep frequency=660 length=100ms; +:delay 300ms; +:beep frequency=660 length=100ms; +:delay 300ms; +:beep frequency=510 length=100ms; +:delay 100ms; +:beep frequency=660 length=100ms; +:delay 300ms; +:beep frequency=770 length=100ms; +:delay 550ms; +:beep frequency=380 length=100ms; +:delay 575ms; From f30f3dfc4fecd79584218d7440b4f57d2bf4f6cd Mon Sep 17 00:00:00 2001 From: Lucas Berezy Date: Wed, 2 Mar 2016 00:37:10 +1100 Subject: [PATCH 2/3] Cleanup and attribution --- main.c | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/main.c b/main.c index 0814af9..3c2e9b2 100644 --- a/main.c +++ b/main.c @@ -9,8 +9,6 @@ #include #include - -#define am_delay(x) (square_am_signal(x, 0)) __m128i reg; __m128i reg_zero; __m128i reg_one; @@ -36,11 +34,6 @@ static inline void square_am_signal(float time, float frequency) { } } -int prefix(const char *pre, const char *str) -{ - return strncmp(pre, str, strlen(pre)) == 0; -} - int main(int argc, char* argv[]) { mach_timebase_info_data_t theTimeBaseInfo; @@ -68,8 +61,7 @@ int main(int argc, char* argv[]) /* skip blank lines */ continue; } - - if (prefix(":beep", buffer)) { + if (!strncmp(":beep", buffer, strlen(":beep"))) { int t; int f; if(sscanf(buffer, ":beep frequency=%d length=%dms", &f, &t) == 0) { @@ -77,7 +69,7 @@ int main(int argc, char* argv[]) } printf("F: %d, T: %d\n", f, t); square_am_signal(t / 1000.0, f); - } else if (prefix(":delay", buffer)) { + } else if (!strncmp(":delay", buffer, strlen(":delay"))) { int d; if (sscanf(buffer, ":delay %dms", &d) == 0) { continue; @@ -85,23 +77,5 @@ int main(int argc, char* argv[]) printf("D: %d\n", d); square_am_signal(d / 1000.0, 0); } - /* Super Mario Bros. Theme - square_am_signal(0.100, 660); - am_delay(0.150); - square_am_signal(0.100, 660); - am_delay(0.300); - square_am_signal(0.100, 660); - am_delay(0.300); - square_am_signal(0.100, 510); - am_delay(0.100); - square_am_signal(0.100, 660); - am_delay(0.300); - square_am_signal(0.100, 770); - am_delay(0.550); - square_am_signal(0.100, 380); - am_delay(0.575);*/ - - - } } From b4a10970cb01a9d07d6b5c34905bef6d60f9cdf0 Mon Sep 17 00:00:00 2001 From: Lucas Berezy Date: Wed, 2 Mar 2016 10:03:23 +1100 Subject: [PATCH 3/3] Added song repeat and :end token to song, added usage info also. --- main.c | 11 ++++++++++- smb.song | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 3c2e9b2..39f4f35 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,12 @@ // https://github.com/fulldecent/system-bus-radio // Copyright 2016 William Entriken + +/* Note: Included Super Mario Bros. theme song was found online at: +http://wiki.mikrotik.com/wiki/Super_Mario_Theme +annotated by user "IanH" + */ + #include #include #include @@ -51,7 +57,7 @@ int main(int argc, char* argv[]) if (argc == 2) { fp = fopen(argv[1], "r"); } else { - puts("Supply file to read song from."); + printf("No song file given!\nUsage: %s file.song\n", argv[0]); exit(1); } char buffer[256]; @@ -76,6 +82,9 @@ int main(int argc, char* argv[]) } printf("D: %d\n", d); square_am_signal(d / 1000.0, 0); + } else if (!strncmp(":end", buffer, strlen(":end"))) { + puts("End of song, repeating."); + rewind(fp); } } } diff --git a/smb.song b/smb.song index d8709df..5392ee1 100644 --- a/smb.song +++ b/smb.song @@ -349,3 +349,4 @@ :delay 550ms; :beep frequency=380 length=100ms; :delay 575ms; +:end