Techno pad?

This is a discussion on Techno pad? within the C Programming forums, part of the General Programming Boards category; a couple of you may have remembered my jeopardy song program J3Op4RdY Any who, a few weeks ago my friend ...

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230

    Techno pad?

    a couple of you may have remembered my jeopardy song program J3Op4RdY
    Any who, a few weeks ago my friend came over and he said "I want to learn how to program". Well as you all know programming does NOT get taught in one after noon, more like years of experience and mistakes. But I attempted and of course we got sidetracked when I got to the Beep() function. So sidetracked that we made a whole techno pad if you will out of a key board. (One of the most useful wastes of time ever). Enough chit chat and lets get to the program and how to use it.

    1. each key corresponds with an actual musical note.
    2. to make the not longer press '[' shorter ']'
    3. to make length of note the longest it can be press '=' shortest '-'
    4.whole note press '
    5. half note ;
    6. quarter note .
    7. eighth note ,

    Have fun, and i hope this works for you.





    Code:
    #include <stdio.h>
    int length = 200;
    
    int eight = 113;
    int quarter = 225;
    int half = 450;
    int whole = 900;
    
    int beeper(int freq);
    
    main()
    {
          char letter;
          
          
          while(1==1)
          {
                   letter = getch() ;
                    
                    if (letter== 'p')
                    {
                                 
                                 beeper(2637);
                                 printf("p");
                                
                                 
                    }
                    
                    else if(letter == 'o')
                    {
                                   printf("o");
                                   beeper(2349);
                    }
                    
                    else if (letter == 'i')
                    {
                                    printf("i");
                                    beeper(2093);
                                    
                    }
                    
                    else if (letter == 'u')
                    {
                                    printf("u");
                                    beeper(1976);
                                    
                    }
                    
                    else if (letter == 'y')
                    {
                                    printf("y");
                                    beeper(1760);
                                    
                    }
                    
                    else if (letter == 't')
                    {
                                    printf("t");
                                    beeper(1568);
                                    
                    }
                    
                    else if (letter == 'r')
                    {
                                    printf("r");
                                    beeper(1397);
                                    
                    }
                    
                    else if (letter == 'e')
                    {
                                    printf("e");
                                    beeper(1319);
                                    
                    }
                    
                    else if (letter == 'w')
                    {
                                    printf("w");
                                    beeper(1175);
                                    
                    }
                    
                    else if (letter == 'q')
                    {
                                    printf("q");
                                    beeper(1047);
                                    
                    }
                    
                    else if (letter == 'l')
                    {
                                    printf("l");
                                    beeper(988);
                                    
                    }
                    
                    
                    else if (letter == 'k')
                    {
                                    printf("k");
                                    beeper(880);
                                    
                    }
                    
                    
                    else if (letter == 'j')
                    {
                                    printf("j");
                                    beeper(784);
                                    
                    }
                    
                    
                    else if (letter == 'h')
                    {
                                    printf("h");
                                    beeper(698);
                                    
                    }
                    
                    else if (letter == 'g')
                    {
                                    printf("g");
                                    beeper(659);
                                    
                    }
                    
                    else if (letter == 'f')
                    {
                                    printf("f");
                                    beeper(588);
                                    
                    }
                    
                    else if (letter == 'd')
                    {
                                    printf("d");
                                    beeper(523);
                                    
                    }
                    
                    else if (letter == 's')
                    {
                                    printf("s");
                                    beeper(494);
                                    
                    }
                    
                    else if (letter == 'a')
                    {
                                    printf("a");
                                    beeper(440);
                                    
                    }
                    
                    else if (letter == 'm')
                    {
                                    printf("m");
                                    beeper(392);
                                    
                    }
                    
                    else if (letter == 'n')
                    {
                                    printf("n");
                                    beeper(349);
                                    
                    }
                    
                    else if (letter == 'b')
                    {
                                    printf("b");
                                    beeper(330);
                                    
                    }
                    
                    else if (letter == 'v')
                    {
                                    printf("v");
                                    beeper(294);
                                    
                    }
                              
                    else if (letter == 'c')
                    {
                                    printf("c");
                                    beeper(262);
                                    
                    }
                    
                    else if (letter == 'x')
                    {
                                    printf("x");
                                    beeper(247);
                                    
                    }
                    
                    else if (letter == 'z')
                    {
                                    printf("z");
                                    beeper(220);
                                    
                    }          
                    
                    else if (letter == '\\')
                    {
                                    system("cls");
                                    
                    }
          
                    else if (letter == '[' && length <= 10000)
                    {
                         
                                   printf("\nspeed: %d\n",length);
                                   length = length + 25;
                    }
          
                    else if (letter == ']' && length >= 25)
                    {
                                    
                                   
                                   printf("\nspeed: %d\n",length);
                                   length = length - 25;
                    }
                    
                    else if (letter == '=')
                    {
                         length = 10000;
                         printf("\n MAX Set\n");
                    }
                    
                    else if (letter == '-')
                    {
                         length = 25;
                         printf("\n MIN Set\n");
                    }
          
                    else if (letter == '\'')
                    {
                         length = whole;
                         
                         printf("whole note (900 speed)\n");
                    }
                    
                    else if (letter == '.')
                    {
                         length = quarter;
                         
                         printf("quarter note (%d speed)\n",length);
                    }
                    
                    else if (letter == ';')
                    {
                         length = half;
                         
                         printf("half note (450 speed)\n");
                         
                    }
                         
                    else if (letter == ',')
                    {
                         length = eight;
                         
                         printf("eighth note (%d speed)\n", length);
                         
                    } 
          
          
          }
          
          
          
          
    
    }
    
    
    int beeper(int freq)
    {
        
        beep(freq,length);
        
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could try using a switch statement, instead of a big if-else chain:
    Code:
    switch( letter )
    {
        case 'o': printf("something"); beep(stuff); break;
        ...
        default: /* nothing valid, ignore it. */ break;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Why not use a lookup table?
    BTW, you are forgetting to include appropriate header file for those non-standard beeper,getch functions. (conio.h perhaps?)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not all of his cases are just beeps. He'll need to broaden it a bit with an array (lookup table) of structures probably if he wants to use a lookup table. Plus, he doesn't have an option for every character in his example posted here, so he'll have to manually initialize them all. At which point, he may as well just do it as-is, or use a switch. But it might be a fun exercise.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Hmmm, that's strange. You've got 6 notes per octave... Usually there are 12.
    There is no need for look up table or hard coded frequency values. Just generate the numbers using appropriate multiplication fractor exp(2.0, 1.0 / 12.0) starting from A at 220 Hz.

    But still a very nice programming exercise for an afternoon. Very good mix of theory, practice, and fun result.
    Last edited by nonoob; 12-15-2010 at 07:18 AM.

  6. #6
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    As for the look up table, I showed my friend the if else if and else statements already. I didn't want to over complicate things for him.

    Hmmm, that's strange. You've got 6 notes per octave... Usually there are 12.
    There is no need for look up table or hard coded frequency values. Just generate the numbers using appropriate multiplication fractor exp(2.0, 1.0 / 12.0) starting from A at 220 Hz.
    I guess I didn't thing of that, although the first version didn't have real notes. We just used four keys and some low notes (He wanted to make Simon)

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    It's still cool. Very nice. A Simon program would be a pretty good next challenge.

  8. #8
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    I think i'll go for it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. one time pad and caesar cipher
    By nik2 in forum C++ Programming
    Replies: 8
    Last Post: 09-15-2010, 11:14 AM
  2. one time pad breakable debate
    By kryptkat in forum General Discussions
    Replies: 264
    Last Post: 04-10-2010, 07:58 AM
  3. Direction Pad - movement
    By samuelmoneill in forum C Programming
    Replies: 1
    Last Post: 04-30-2009, 05:28 AM
  4. what's on _your_ mouse pad?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 02-22-2002, 09:54 PM

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