/* * The main function - the program starts executing here */ int main(void) { /* Declare integer variables for specifying a note */ int pitch, channel, velocity; /* initialize the midi functions */ midi_start(); /* Set the pitch variable to 60, which is middle C */ pitch = 60; /* We will play the note on MIDI channel 1 */ channel = 1; /* The note will have a medium velocity (volume) */ velocity = 64; /* Start playing a middle C at moderate volume */ midi_note(pitch, channel, velocity); /* Wait, for 1 second, so that we can hear the note playing */ pause(1000); /* Turn the note off by setting its volume to 0 */ midi_note(pitch, channel, 0); /* close down all midi functions */ midi_close(); return 0; }
int main(void) { /* Declare integer variables for specifying a note */ int pitch, channel, velocity, offset; /* Set the pitch variable to 60, which is middle C */ pitch = 60; /* We will play the note on MIDI channel 1 */ channel = 1; /* The note will have a medium velocity (volume) */ velocity = 64; /* initialize the midi functions */ midi_start(); /* Play an octave's worth of chromatic scale */ for (offset = 0; offset <= 12; offset++) { /* Start playing a note */ midi_note(pitch + offset, channel, velocity); /* Wait, so that we can hear the note playing */ pause(400); /* Turn the note off */ midi_note(pitch + offset, channel, 0); } /* close down all midi functions */ midi_close(); return 0; }
Tset rockbass(Tset vars) { // Declare variables for the note played and whether veloctiy has been changed. int newbass, vel; // Declare an array of integers to create a bassline. int offset[7] = {0, 0, 12, 0, 0, 12, 0}; /* * Increase the velocity of every other note and decrease the other notes. Set the * variable vel to 1 if increased to be used later. */ if(vars.instb_velocity >= 10) { if(vars.time == 0 || vars.time == 1200 || vars.time == 2400) { vars.instb_velocity = vars.instb_velocity + 5; vel = 1; } else { vars.instb_velocity = vars.instb_velocity - 5; vel = 0; } } /* * Set the new note to be played to be the key minus 12 to take into account the bass * playing an octave lower plus the offset from the corresponding place in the array * determined by the time. */ newbass = (vars.key - 12) + offset[vars.time/400]; // At certain times play the new note and assign it to be the variable oldbass. if(vars.time == 0 || vars.time == 400 || vars.time == 800 || vars.time == 1200 || vars.time == 2000 || vars.time == 2400 || vars.time == 2800) { vars.oldbass = newbass; midi_note(newbass, 3, vars.instb_velocity); } // At certain times turn off the note at the key of the variable oldbass. if(vars.time == 380 || vars.time == 780 || vars.time == 1180 || vars.time == 1580 || vars.time == 2380 || vars.time == 2780 || vars.time == 3180) { midi_note(vars.oldbass, 3, 0); } /* * Use the variable vel to check if velocity has increased or decreased and return it * to its original value unless velocity is 0 at which point the note should be off. * This means that velocity doesnt randomly keep rising or falling but fluctuates around * its given value. */ if(vars.instb_velocity != 0) { if(vel == 1) vars.instb_velocity = vars.instb_velocity - 5; else vars.instb_velocity = vars.instb_velocity + 5; } return(vars); // Return vars to keep track of the variable oldbass. }
Tset keypress(Tset vars) { // Declare an integer variable assign the value of the users key press to it. int input; input = getch(); /* * Use a switch to check if the key pressed corresponds to any control keys and if * so edit the approriate variable. */ switch(input) { case '1': { vars.instl_velocity = 0; break; } case 'q': { vars.instc_velocity = 0; break; } case 'a': { vars.instb_velocity = 0; break; } case 'z': { vars.drum_vel = 0; break; } case '3': { vars.instl_velocity += 5; break; } case 'e': { vars.instc_velocity += 5; break; } case 'd': { vars.instb_velocity += 5; break; } case 'c': { vars.drum_vel += 5; break; } /* * In cases where velocity is decreased check that the decreased value does not go * negative before decreasing the variable. */ case '2': { if(vars.instl_velocity - 5 > 0) vars.instl_velocity -= 5; break; } case 'w': { if(vars.instc_velocity - 5 > 0) vars.instc_velocity -= 5; break; } case 's': { if(vars.instb_velocity - 5 > 0) vars.instb_velocity -= 5; break; } case 'x': { if(vars.drum_vel - 5 > 0) vars.drum_vel -= 5; break; } // Set the instruments velocitys to be at a standard value declared in the header file case '4': { vars.instl_velocity = LEAD; break; } case 'r': { vars.instc_velocity = CHORD; break; } case 'f': { vars.instb_velocity = BASS; fflush(stdin); break; } case 'v': { vars.drum_vel = DRUM; break; } /* * In cases where tempo is decreased check that the decreased value does not go negative * before decreasing the variable. */ case 'o': { if(vars.tempo - 2 > 0) vars.tempo--; break; } case 'i': { vars.tempo ++; break; } // This sets the variable loop to 1, stopping the main loop and terminating the program. case ' ': { vars.loop = 1; break; } case '5': { /* * When changing the instrument the variable scroll checks which instrument * is currently playing, changes it to the next instrument and adds 1 to scroll. * At the last instrument scroll is reset and so as the key 5 is repeatedly pressed * its cycles through the possible instruments. */ switch(vars.instl_scroll) { case 0: { program_change(1, 5); vars.instl_scroll++; break; } case 1: { program_change(1, 31); vars.instl_scroll++; break; } case 2: { program_change(1, 27); vars.instl_scroll++; break; } case 3: { program_change(1, 67); vars.instl_scroll++; break; } case 4: { program_change(1, 10); vars.instl_scroll = 5; break; } case 5: { program_change(1, 115); vars.instl_scroll = 0; break; } } break; } case 't': { switch(vars.instc_scroll) { case 0: { program_change(2, 5); vars.instc_scroll++; break; } case 1: { program_change(2, 27); vars.instc_scroll++; break; } case 2: { program_change(2, 29 ); vars.instc_scroll = 0; break; } } break; } case 'g': { switch(vars.instb_scroll) { case 0: { program_change(3, 33); vars.instb_scroll++; break; } case 1: { program_change(3, 34); vars.instb_scroll = 0; break; } } break; } case 'l': { vars.key++; break; } case 'k': { vars.key--; break; } /* * Here the user has chosen to restart the program so turn off all midi notes, * clear the screen and print a message. Calls the user input functions * again and display the user input messages. */ case 8: { midi_note(vars.oldlead, 1, 0); midi_note(vars.key, 1, 0); midi_note(vars.oldbass, 3, 0); midi_note(vars.key, 3, 0); midi_note(vars.key, 2, 0); midi_note(vars.key + 7, 2, 0); midi_note(vars.key + 9, 2, 0); midi_note(vars.key + 10, 2, 0); midi_note(vars.key + 12, 2, 0); midi_note(vars.key + 15, 2, 0); midi_note(vars.key + 16, 2, 0); system ("cls"); printf("Starting again...\n\nRe-enter values:\n\n"); vars.rhythm = rhythm(); vars.key = pitch(); vars.tempo = speed(); vars = input_instrument(vars); vars.loop = 0; vars.time = 0; vars.count = 0; vars.luck = 0; vars.shift = 0; messages( vars); break; } /* * Here the user is changing the rhythm so set the key back to its original value, * reset shift, time and count and set the variable rhythm to the approriate value. */ case '8': { vars.key = vars.original; vars.shift = 0; vars.rhythm = 1; vars.time = 0; vars.count = 0; break; } case '9': { vars.key = vars.original; vars.shift = 0; vars.rhythm = 2; vars.time = 0; vars.count = 0; break; } case '0': { vars.key = vars.original; vars.shift = 0; vars.rhythm = 3; vars.time = 0; vars.count = 0; break; } } fflush(stdin); // Clear the standard input memory return(vars); // Return the altered vars }
void midi_player::play_note(unsigned note, unsigned frames) { notes.push_back(midi_note(note, frames)); midiOutShortMsg(midi_handle, (note << 8) | 0x400090); }
Tset lead(Tset vars) { /* * Declare integer variables for a random number, the offset pitch, the new note to be * played and whether velocity was changed or not. Also declare an array of 12 integers * to hold the offsets in the scale. As these offsets depend on which rhythm is being * played assign the integers in if functions. */ int rand, offset, newlead, vel; int scale[12]; /* * Depending on the rhythm chosen by the user assign the integers in the array scale to be * offset pitchs of a scale. */ if(vars.rhythm == 1) { scale[0] = 0; scale[1] = 3; scale[2] = 5; scale[3] = 7; scale[4] = 10; scale[5] = 12; scale[6] = 15; scale[7] = 17; scale[8] = 19; scale[9] = 22; scale[10] = 24; scale[11] = 27; } if(vars.rhythm == 2) { scale[0] = 0; scale[1] = 2; scale[2] = 4; scale[3] = 7; scale[4] = 9; scale[5] = 12; scale[6] = 14; scale[7] = 16; scale[8] = 19; scale[9] = 21; scale[10] = 24; scale[11] = 26; } if(vars.rhythm == 3) { scale[0] = 12; scale[1] = 14; scale[2] = 16; scale[3] = 19; scale[4] = 21; scale[5] = 24; scale[6] = 26; scale[7] = 28; scale[8] = 31; scale[9] = 33; scale[10] = 36; scale[11] = 38; } /* * Assign a random number to the variable rand and choose an integer from the array scale * at that random number. */ rand = random_number(0, 11); offset = scale[rand]; // Assign the variable newlead to be the current key plus the offset chosen. newlead = vars.key + offset; /* * If velocity is not 0 and therefore the lead is being played decide whether to randomly * increase or decrease velocity to add a sense of improvisation. If increased set the * variable vel to 1. */ if(vars.instl_velocity >= 10) { if(rand <= 5) { vars.instl_velocity = vars.instl_velocity + 5; vel = 1; } else { vars.instl_velocity = vars.instl_velocity - 5; vel = 0; } } // Turn off the old note, set the old note to be the new note and play the new note. midi_note(vars.oldlead, 1, 0); vars.oldlead = newlead; midi_note(newlead, 1, vars.instl_velocity); /* * Using vel to check how the velocity was changed, set velocity back to its starting point * so that it doesnt randomly keep increasing or decreasing. */ if(vars.instl_velocity != 0) { if(vel == 1) vars.instl_velocity = vars.instl_velocity - 5; else vars.instl_velocity = vars.instl_velocity + 5; } return(vars); // Return vars to store the old note so that it can be turned off next time. }