Guest User

Untitled

a guest
Jul 22nd, 2020
298
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.15 KB | None | 1 0
  1. #define WOPENFMT  "<%d>|"
  2. #define WFSTRFMT  "-<%d>-|"
  3. #define WUSTRFMT  "-----|"
  4. #define WMAXFRET 13
  5. #define NOPENFMT " %d |"
  6. #define NFSTRFMT "-%d-|"
  7. #define NUSTRFMT "---|"
  8. #define NMAXFRET 17
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. char * openfmt = WOPENFMT;
  14. char * fstrfmt = WFSTRFMT;
  15. char * ustrfmt = WUSTRFMT;
  16. int maxfret = WMAXFRET;
  17. int capo = 0;
  18. int transp = 0;
  19. int tune[6] = { 7,2,10,5,0,7 };
  20. int notes[12];                  /* the available notes */
  21.  
  22. int main(void);
  23. void chord( char *cp );
  24. void string( int b );
  25. int has( char * cp, char c );
  26. void retune( char * ap );
  27. int getword( char * wp );
  28.  
  29. int main(void)
  30. {
  31.     char line[80], *ap;
  32.     while ( getword(line) ) {
  33.     if ( *(ap = line) == '-' ) {
  34.         ap += 2;
  35.         switch( ap[-1] ) {
  36.         case 'c':               /* capo */
  37.             capo = atoi( ap ); break;
  38.         case 't':
  39.             retune( ap ); break;
  40.         case 'T':
  41.             if ( *ap == 'D' || *ap == 'd' ) {
  42.                 tune[0] = tune[5] = 5;
  43.             }
  44.             break;
  45.         case 'n':
  46.             maxfret = NMAXFRET;
  47.             openfmt = NOPENFMT;
  48.             fstrfmt = NFSTRFMT;
  49.             ustrfmt = NUSTRFMT;
  50.             break;
  51.         case 'w':
  52.             maxfret = WMAXFRET;
  53.             openfmt = WOPENFMT;
  54.             fstrfmt = WFSTRFMT;
  55.             ustrfmt = WUSTRFMT;
  56.             break;
  57.         case 's':
  58.             if ( *ap == '-' )
  59.                 transp = -atoi( ap+1 );
  60.             else
  61.                 transp = atoi( ap );
  62.             break;
  63.         }
  64.     } else
  65.         chord( ap );
  66.     }
  67.     return 0;
  68. }
  69. void chord( char *cp )
  70. {
  71.     int i;
  72.     int n;                  /* which note: A=0,A#=1,...G#=11 */
  73.  
  74.     int     mflag;          /* minor chord */
  75.     int     sflag;          /* sharp or flat */
  76.     int     flag7;          /* seventh chord */
  77.     int     flag9;          /* ninth chord */
  78.     int     flag6;
  79.  
  80.     int t1[7] = {0,2,3,5,7,8,10};
  81.     char *names[] = {
  82.         "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#" };
  83.     char *bnames[] = {
  84.         "A", "Bb", "B", "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab" };
  85.  
  86.     if ( cp[0] == '\0' )
  87.         return;
  88.  
  89.     mflag = sflag = flag7 = flag9 = flag6 = 0;
  90.     for ( i = 0; i < 12; i++ )
  91.         notes[i] = 0;
  92.  
  93.     if ( has( cp+1, '#' ) ) sflag = 1;
  94.     if ( has( cp+1, 'b' ) ) sflag = -1;
  95.     if ( has( cp+1, '7' ) ) flag7 = 1;
  96.     if ( has( cp+1, '6' ) ) flag6 = 1;
  97.     if ( has( cp+1, '9' ) ) flag9 = 1;
  98.     if ( has( cp+1, 'm' ) ) mflag = 1;
  99.     n = transp + 48 + t1[cp[0]-'a'] + sflag;
  100.     n %= 12;
  101.     notes[n] = 1;
  102.     notes[(n+4-mflag)%12] = 3;
  103.     notes[(n+7)%12] = 5;
  104.     notes[(n+9)%12] = flag6 ? 6 : 0;
  105.     notes[(n+10)%12] = flag7 ? 7 : 0;
  106.     notes[(n+13)%12] = flag9 ? 9 : 0;
  107.  
  108.     printf( "%s", sflag < 0 ? bnames[n] : names[n] );
  109.     if ( mflag ) printf( "m" );
  110.     if ( flag6 ) printf( "6" );
  111.     if ( flag7 ) printf( "7" );
  112.     if ( flag9 ) printf( "9" );
  113.     printf( ": " );
  114.     for ( i = 0; i < 12; i++ )
  115.         if ( notes[(i+n)%12] )
  116.             printf( " %s", sflag < 0 ? bnames[(i+n)%12] : names[(i+n)%12] );
  117.     printf( "\n" );
  118.     if ( capo ) printf( "capo up %d frets\n", capo );
  119.     for ( i = 0; i < 6; i++ )
  120.         string( tune[i]+capo );
  121. }
  122.  
  123. void string( int b )
  124. {
  125.     int i;
  126.     b %= 12;
  127.     if ( notes[b] )
  128.         printf( openfmt, notes[b] );
  129.     else
  130.         printf( "   |" );
  131.     for ( i=1,++b; i<maxfret; i++,b++ ) {
  132.         b %= 12;
  133.         if ( notes[b] )
  134.             printf( fstrfmt, notes[b] );
  135.         else
  136.             printf( "%s", ustrfmt );
  137.     }
  138.     printf( "\n" );
  139. }
  140.  
  141. int has( char * cp, char c )
  142. {
  143.     while ( *cp )
  144.     if ( *cp++ == c )
  145.         return 1;
  146.     return 0;
  147. }
  148.  
  149. void retune( char * ap )
  150. {
  151.     int i;
  152.     for ( i = 0; i < 6; i++ ) {
  153.         if ( ! *ap )
  154.             break;
  155.         tune[i] = *ap++ - 'a';
  156.         if ( tune[i] < 0 || tune[i] > 7 )
  157.             break;
  158.         if ( *ap == '#' ) tune[i]++,ap++;
  159.         if ( *ap == '-' ) tune[i]--,ap++;
  160.         tune[i] = (tune[i]+12)%12;
  161.     }
  162. }
  163.  
  164. int getword( char * wp )
  165. {
  166.     int c;
  167.     while ( (c=getchar()) != EOF ) {
  168.         if ( c == ' ' || c == '\t' || c == '\n' ) {
  169.             *wp = 0;
  170.             return 1;
  171.         }
  172.         *wp++ = c;
  173.     }
  174.     return 0;
  175. }
  176. #ifdef NOTDEF
  177. if ( argc == 1 ) {
  178.     printf( "Usage: %s [options] chord ...\n", argv[0] );
  179.     printf( "Options are:\n" );
  180.     printf( "  -cN:      capo up N frets\n" );
  181.     printf( "  -tRSTUVW: strings tuned to R,S,T,U,V,W\n" );
  182.     printf( "            R,etc are note name with optional # or -\n" );
  183.     printf( "            to indicate sharp or flat.  Order is string 1 to 6\n");
  184.     printf( "  -Td:      strings 1 and 6 are tuned to D\n" );
  185.     printf( "  -n:       Draw frets in narrower format\n" );
  186.     printf( "  -sN:      transpose by N half steps\n" );
  187.     printf( "chord is a note name {a,b,c,d,e,f,g}.  The note name can be\n" );
  188.     printf( "followed be the following modifiers, in any order:\n" );
  189.     printf( "  7: 7th chord\n" );
  190.     printf( "  #: sharp\n" );
  191.     printf( "  b: flat\n" );
  192.     printf( "  m: minor\n" );
  193.     exit(0);
  194. }
  195. #endif
Advertisement
Add Comment
Please, Sign In to add comment