Created
April 18, 2014 22:05
-
-
Save gsora/11066465 to your computer and use it in GitHub Desktop.
4/4 with arbitrary BPM "metronome" in C
This file contains hidden or 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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <unistd.h> | |
| #define MINUTE_MS 60000 | |
| void tick(useconds_t); | |
| int main(int argc, char *argv[]){ | |
| if(argc != 2) { | |
| fprintf(stderr, "usage: %s bpm\n", argv[0]); | |
| return EXIT_FAILURE; | |
| } | |
| long int tempo = atoi(argv[1]); | |
| if(tempo > 320) { | |
| fprintf(stderr, "U mad bro?\n"); | |
| return EXIT_FAILURE; | |
| } | |
| useconds_t sleepValue = ((MINUTE_MS / tempo) * 1000); | |
| tick(sleepValue); | |
| return 0; | |
| } | |
| void tick(useconds_t tock) { | |
| int fract = 1; | |
| while(1) { | |
| printf("Tick! %d/4\n", fract); | |
| usleep(tock); | |
| if(fract == 4) { | |
| fract = 0; | |
| printf("\n"); | |
| } | |
| fract++; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment