patterncMinor
Musical Note Length Calculator in C
Viewed 0 times
lengthnotecalculatormusical
Problem
This is my first ever program in C and I was just tinkering around with this idea, as I'm on a music based course and the synth plugins all use milliseconds as its time.
Any improvements to make it more efficient or less dependant with a little explanation would be really appreciated.
Same for quaver (dividing crot by 2), semiquaver (dividing crot by 4) and demi semiquaver (dividing crot by 8).
Any additional explanation towards the music theory I will also add if requested.
Any improvements to make it more efficient or less dependant with a little explanation would be really appreciated.
/* declarations */
int crotchet(int);
int semibreve(int);
int minim(int);
int quaver(int);
int semiquaver(int);
int demisemiquaver(int);
int main() {
int bpm;
/* Ask for note bpm */
printf("Enter bpm value:\n");
scanf("%d", &bpm);
/* returns note lengths from functions */
printf("Semibreve would last for... ");
printf("%d", semibreve(bpm));
printf(" miliseconds! \n");
return 0;
}
int crotchet(int bpm)
{
/* 1 second = 1000
1 min = 60 seconds = 6000 miliseconds
*/
int crotchet;
crotchet = 6000 / bpm;
return crotchet;
}
int semibreve(int bpm)
{
int crot;
crot = crotchet(bpm);
int semibreve;
semibreve = crot * 4;
return semibreve;
}
int minim(int bpm)
{
int crot;
crot = crotchet(bpm);
int minim;
minim = crot * 2;
return minim;
}Same for quaver (dividing crot by 2), semiquaver (dividing crot by 4) and demi semiquaver (dividing crot by 8).
Any additional explanation towards the music theory I will also add if requested.
Solution
Your calculations are all off by a factor of 10: one minute is 60000 milliseconds, not 6000. Instead of explaining your
I don't recommend that design, defining one function per type of note. One function should cover everything, I think.
Since you use
It is customary to put
6000 using a comment, write it out in the code, and the compiler will figure it out for you. That calculation will not cost you any runtime performance at all.I don't recommend that design, defining one function per type of note. One function should cover everything, I think.
Since you use
printf() and scanf(), you should #include .It is customary to put
main() at the end, so that you don't have to declare the functions it uses in advance.#include
#define SEMIBREVE (1.0)
#define MINIM (1.0/2)
#define CROTCHET (1.0/4)
#define QUAVER (1.0/8)
#define SEMIQUAVER (1.0/16)
#define DEMISEMIQUAVER (1.0/32)
int millisec(int bpm, double note) {
return (int)(
60 /* seconds */
* 1000 /* milliseconds per second */
* 4 /* crotchets per semibreve */
* note
/ bpm
);
}
int main() {
int bpm;
printf("Enter tempo (crotchets per minute): ");
scanf("%d", &bpm);
printf("Semibreve would last for... %d milliseconds!\n", millisec(bpm, SEMIBREVE));
printf("Dotted crotchet would last for... %d milliseconds!\n", millisec(bpm, 1.5 * CROTCHET));
printf("Demisemiquaver would last for... %d milliseconds!\n", millisec(bpm, DEMISEMIQUAVER));
}Code Snippets
#include <stdio.h>
#define SEMIBREVE (1.0)
#define MINIM (1.0/2)
#define CROTCHET (1.0/4)
#define QUAVER (1.0/8)
#define SEMIQUAVER (1.0/16)
#define DEMISEMIQUAVER (1.0/32)
int millisec(int bpm, double note) {
return (int)(
60 /* seconds */
* 1000 /* milliseconds per second */
* 4 /* crotchets per semibreve */
* note
/ bpm
);
}
int main() {
int bpm;
printf("Enter tempo (crotchets per minute): ");
scanf("%d", &bpm);
printf("Semibreve would last for... %d milliseconds!\n", millisec(bpm, SEMIBREVE));
printf("Dotted crotchet would last for... %d milliseconds!\n", millisec(bpm, 1.5 * CROTCHET));
printf("Demisemiquaver would last for... %d milliseconds!\n", millisec(bpm, DEMISEMIQUAVER));
}Context
StackExchange Code Review Q#142190, answer score: 2
Revisions (0)
No revisions yet.