C for Guitar

This is a discussion on C for Guitar within the C Programming forums, part of the General Programming Boards category; Hi, I am a C newbie. My first project is to model my guitar (strings + frets = notes) in ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    8

    C for Guitar

    Hi, I am a C newbie. My first project is to model my guitar (strings + frets = notes) in C and then write algorithms to create music. This post attempts a simple for loop to assign a char array containing musical letters (i.e., E, F, F#...n) to another char array of 19 possible notes on my bottom (i.e., low) E string.

    Code:
    #include <stdio.h>
    
    main() {
    char e_string[19];
    char notes[12] = {E, F, F#Gb, G, G#Ab, A, A#Bb, B, C, C#Db, D, D#Eb};
    int i;
    
    for (i=0; i<19; i++) {
    	e_string[i] = notes[i];
    	print f("%c\n", e_string[i]);
    	}
    return void;
    }
    Questions:
    1) Have I correctly declared and initialized my variables?
    2) Have I correctly looped through i and assigned the notes to the 19 possible positions on the E string?
    3) Is their a simpler way to do this operation?

    Thanks for your interest and comments! IRNO

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    You might want to compile your code first before asking. That act alone would answer your first question, as it stands.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Some comments:
    1. There's no space in "printf".
    2. You need to put single quotes around characters like 'E', or else the compiler thinks you're referring to the variable E. Likewise, strings with multiple characters must be surrounded by double quotes.
    3. Your char[12] array is an array of 12 chars -- you can't put "A#Bb" into a single char.
    4. void is a keyword for e.g. declaring functions which return nothing. You shouldn't return it with a return statement; use "return 0" from main.
    5. main() should return int, as in "int main()" instead of just "main()" (the latter is outdated K&R code).


    3) Is their a simpler way to do this operation?
    I'm not really sure what you're trying to do, but maybe you could start with something like this.
    Code:
    #include <stdio.h>
    
    int main() {
        char notes[12][5] = {"E", "F", "F#Gb", "G", "G#Ab", "A", "A#Bb", "B", "C", "C#Db", "D", "D#Eb"};
        int i;
    
        for(i = 0; i < 12; i++) {
            printf("%s\n", notes[i]);
        }
        
        return 0;
    }
    [Syntax highlighting by codeform.]

    Notice that I've stored each note as a char[5], an array of up to 5 characters. That's what you need to store strings like "E" and "F#Gb".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    A while ago I wrote a program to convert online guitar tabs to a more piano-appropriate format. I found that when dealing with guitar-style music, the best data structure was a linked-list of "chords", and each chord contained an array of between 1 and 8 notes, and a chord-length. Each note was represented as a number ( 0 being the lowest possible note, 88 being the highest), and I made functions to convert from a number to a string ("F# - 3rd Octave"), etc.. The chord-length was also a number (for instance, I could measure it number of 1/16 beats, or something).

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sean View Post
    A while ago I wrote a program to convert online guitar tabs to a more piano-appropriate format. I found that when dealing with guitar-style music, the best data structure was a linked-list of "chords", and each chord contained an array of between 1 and 8 notes, and a chord-length. Each note was represented as a number ( 0 being the lowest possible note, 88 being the highest), and I made functions to convert from a number to a string ("F# - 3rd Octave"), etc.. The chord-length was also a number (for instance, I could measure it number of 1/16 beats, or something).
    Something that would be great and AFAIK does not exist (very surprisingly) would be a kind of "reverse tabbing" GUI where you could just point and click at frets to assemble a chord, and find out what chord(s) that could be. I would love to do a web app like that. I have never even seen a book that attempted to do that, although it could be done in a few hundred pages methinks.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I would love to do a web app like that.
    Perfect. I'll talk to Paul Graham about some funding, and we'll get moving on this.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sean View Post
    Perfect. I'll talk to Paul Graham about some funding, and we'll get moving on this.
    It does seem to me that you could pick up a few ad dollars this way. If only I took myself seriously...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by MK27 View Post
    it could be done in a few hundred pages methinks.
    I fail to see why it would be that complicated
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ಠ_ಠ View Post
    I fail to see why it would be that complicated
    Okay Mr. SmartyPants.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by MK27 View Post
    Okay Mr. SmartyPants.
    why do you think it would be that complicated?
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by ಠ_ಠ View Post
    why do you think it would be that complicated?
    Because books are not interactive at all, and therefore there probably wouldn't be an intuitive way to search through the different notes to find chords. Of course the total number of pages it took would be dependent upon the font size and the amount of pictures in the book

  12. #12
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by MK27 View Post
    Something that would be great and AFAIK does not exist (very surprisingly) would be a kind of "reverse tabbing" GUI where you could just point and click at frets to assemble a chord, and find out what chord(s) that could be. I would love to do a web app like that. I have never even seen a book that attempted to do that, although it could be done in a few hundred pages methinks.
    Years ago I wrote a program in AMOS on the Commodore Amgia called "FretKnot" which was a learning tool/utility for guitar players. It would display chords and scales in all keys all over the fretboard, consisting of both preset fingerings and algorithm generated fingerings. Basically you could specify a range of frets and have it compute every possible fingering of a chord within those frets. You could also click on frets to make a chord and have it analyze those notes, so it would tell you which chords those notes matched exactly, which chords contained those notes among others and which chords were contained in those notes. It was actually pretty useful for jazz guitarists, for learning about chord substitution and for composing. You could also give it a chord progression and it would give you tons of suggestions as to which chords the progression could go to next, etc.

    I tell you, it wasn't half bad for my first ever program! It had a fully featured 3D GUI (programmed from scratch!) and enabled you to save and load chord fingerings and progressions. I think it ended up on a couple of Amiga magazine cover CD's. I've long since lost the Amiga and the program and would love to see it again for old times sake. I've asked on Amiga forums if anyone has the cover disks but never got a response.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sharke View Post
    I've asked on Amiga forums if anyone has the cover disks but never got a response.
    You almost have me in tears about that Sharke. Honest.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by MK27 View Post
    You almost have me in tears about that Sharke. Honest.
    The funny thing is, I just went back to the thread on which I asked and it turns out someone actually replied months later with an attachment containing my program. They'd found it on an old cover disk. Now I'm just going through the frustrating process of trying to get an Amiga emulator set up so I can check out my old handiwork.

  15. #15
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    For the OP:

    Instead of assigning a character string for every fret of every guitar string, why not just represent notes as ints with C as 0 up to B as 11, that way you can simply represent the guitar strings as an array of 6 ints and calculate the note for each fret on the fly, using the note value as a subscript on an array of char pointers which point to string representations of each note. Probably easier to show some code than explain it. This prints the notes on each string up to the 12th fret, for example:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *notes[] = {"C", "C#/Db", "D", "D#/Eb", "E", "F", 
                         "F#/Gb", "G", "G#/Ab", "A", "A#/Bb", "B"};
    
        int strings[6] = {4, 9, 2, 7, 11, 4};
        int i, j;
    
        for (i = 5; i > -1; i--)
        {
            printf("%s string:  ", notes[strings[i]]);
            for (j = 0; j < 13; j++)
                printf("%-8s", notes[(strings[i] + j) % 12]);
            printf("\n");
        }
    
        return 0;
    }
    It doesn't help to explain things when you're dealing with both C strings and guitar strings btw!
    Last edited by Sharke; 06-06-2009 at 11:34 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21