Skip to content

Instantly share code, notes, and snippets.

@gsora
Created April 18, 2014 22:05
Show Gist options
  • Select an option

  • Save gsora/11066465 to your computer and use it in GitHub Desktop.

Select an option

Save gsora/11066465 to your computer and use it in GitHub Desktop.
4/4 with arbitrary BPM "metronome" in C
#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