I have a program that I want to create a randomly generated bar of music (4 beats to a bar, using C Major scale). However, I'm having trouble understanding the math and keep overflowing my do while loop, creating more than 4 notes to the bar which I want to avoid.
I am using aServe, which was created by my tutor, but basically opens a stream to an Oscillator that plays the arguments I've commented.
/* Program for randomly written bar of 4/4 in C Major */
#include "aservelibs/aservelib.h"
#include <stdio.h>
#include <stdlib.h>
//macros
#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)
#define C (261.63)
#define D (293.66)
#define E (329.63)
#define F (349.23)
#define G (391.99)
#define A (440.00)
#define B (493.88)
int millisec(int bpm, double note) {
return (int)(
60 /* seconds */
* 1000 /* milliseconds per second */
* 4 /* crotchets per semibreve */
* note
/ bpm
);
}
int main()
{
int bpm = 120; //BPM Value
double Length[] = {SEMIBREVE, MINIM, CROTCHET, QUAVER, SEMIQUAVER, DEMISEMIQUAVER}; //Array of Note Lengths
double Pitch[] = {C, D, E,F, G, A, B}; //Array of CMajor Scale Freq
int randLength = (rand() % 6); //random positions for note length
int randPitch = ( rand() % 7); //random positions for note pitch
double barTotal = 0; //amount of bar currently completed
do {
if(barTotal < 1) //if bar total is smaller than 1
{
barTotal = Length[randLength] + barTotal; //add note to total
aserveOscillator(0, Pitch[randPitch], 1, 2); //Starts stream to oscialltor
//aserveOscillator(Index,Frequency,Amplitude,WaveType);
aserveSleep(millisec(bpm, Length[randLength])); //play the notes for the length of time specified in milliseconds
randLength = (rand() % 6); //prepare next random note
randPitch = (rand() % 7); //prepare next random pitch
//Output
printf("Note: ");
printf("%lf", Pitch[randPitch]);
printf("\n For: ");
printf("%lf", Length[millisec(bpm,randLength)]);
printf("\n With Bar Total: ");
printf("%lf", barTotal);
printf("\n\n");
}
else
{
if(barTotal != 1) //if bar total is bigger than 4
{
randLength = (rand() % 6); //try another number
}
}
} while (barTotal != 1); //will stop once reaches 4
return 0;
}