Intervals and inversions
eharetea

This code will determine the musical relationship between different notes based on a tonic. Includes inversions.

Inversions, intervals, music and programming.

Date Created:Sunday March 04th, 2007 09:29 PM
Date Modified:Saturday August 02nd, 2008 02:55 PM


#include <stdio.h>

void modsortarray();
void swap();

int main()
{
    int inversion0[3] = {1,3,5};
    int inversion1[3] = {3,5,1};
    int inversion2[3] = {5,1,3};
    int inversion3[3] = {5,3,1};
                //  1      3      5
    int makeflatIII[6] = { 0, -20, -100};
    int makeIV[6]      = { 0,  00,  102};
    int makesharpV[6]  = { 0,  00, -101};
    int makeVI[6]       = {-1,  00,  000};

    int chord[3][2] = { {1,40}, {3,44}, {5,47} };
    int inote;
    
    int note1note2note3c_note1c_note2c_note3;
    
    int orignotes[3];
    int usernotes[3];
    

    char note_name[12][6] = {     
                {"A"},
                {"A#/Bb"},
                {"B"},
                {"C"},
                {"C#/Db"},
                {"D"},
                {"D#/Eb"},
                {"E"},
                {"F"},
                {"F#/Gb"},
                {"G"},
                {"G#/Ab"}
                };

    char intervals[25][40] = {
                {"Perfect Unison"},
                {"minor 2nd"},
                {"Major 2nd"},
                {"Augumented 2nd/minor 3rd"},
                {"Major 3rd"},
                {"Perfect 4th"},
                {"Augumented 4th/Diminished 5th"},
                {"Perfect 5th"},
                {"Augumented 5th/Diminished 6th"},
                {"Diminished 7th/Major 6th"},
                {"minor 7th"},
                {"Major 7th"},
                {"Octave"},
                {"minor 9th"},
                {"major 9th"},
                {"Augmented 9th"},
                {"Major 10th"},
                {"Perfect 11th"},
                {"Augumented 11th"},
                {"Perfect 12th"},
                {"Augumented 12th"},
                {"Major 13th"},
                {"minor 14th"},
                {"Major 14th"},
                {"Perfect 15th"}
                };

    for (0<3i++)
    {
        note = (chord[i][1]-1)%12;
        printf("%d\t%s\n\n"chord[i][1] , note_name[note] );
    }
    
    printf("Enter in 3 numbers between 1-88\n");
    scanf("%d %d %d", &note1, &note2, &note3);    

    c_note1 = (note1)%12-1;
    c_note2 = (note2)%12-1;
    c_note3 = (note3)%12-1;

    if (c_note1 0c_note1 += 12; 
    if (c_note2 0c_note2 += 12; 
    if (c_note3 0c_note3 += 12; 
    
    printf("%d %d %d\n\n"note1note2note3);

    usernotes[0] = c_note1+1;
    usernotes[1] = c_note2+1;
    usernotes[2] = c_note3+1;
    
    orignotes[0] = note1;
    orignotes[1] = note2;
    orignotes[2] = note3;

    modsortarray(usernotes,3);
    modsortarray(orignotes,3);

    printf("You Entered:\n");
    for ( 0<3i++)
    {
        printf("Original:%d New: %d Note:%s\n\n"orignotes[i], usernotes[i], note_nameusernotes[i]-] );
    }
    
    printf("Choose a root note: %s = 1, %s = 2, %s = 3\n",note_nameusernotes[0]-], note_nameusernotes[1]-],note_nameusernotes[2]-] );
    scanf("%d", &note);

    if (note == 1note usernotes[0];
    if (note == 2note usernotes[1];
    if (note == 3note usernotes[2];

    printf("The root note %d\n",note);
    
    note1 = ((13-note)+usernotes[0])%12;
    note2 = ((13-note)+usernotes[1])%12;
    note3 = ((13-note)+usernotes[2])%12;
    note = ((12-note)+note)%12+1;


    printf("Which now is %d\n",note);
    printf("%d => %d\t%s\n%d => %d\t%s\n%d => %d\t%s\n\n"orignotes[0], note1intervals[note1-1],orignotes[1], note2,intervals[note2-1], orignotes[2], note3intervals[note3-1]); 

    return 0;
}


void swap(int *aint *b) {
    
    int temp = *a;
    
    *= *b; 
    *temp;

}


void modsortarray(int *inputarrayint size) {

    int i;
    int j;
    for ( 0size 1i++ ) {

        for ( 1sizej++ ) {

            if ( inputarray[i]%12 inputarray[j]%12 ) {

                swap(&inputarray[i], &inputarray[j]);

            }

        }

    }

} 




Downloads:
Download: intervals.c 3 KB

Please login or Click Here to register for downloads
Creative Commons License
Intervals and inversions by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com