# Thread: Techno pad?

1. ## 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. 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.

3. 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. 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.

5. 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.

6. 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. It's still cool. Very nice. A Simon program would be a pretty good next challenge.

8. I think i'll go for it.

Popular pages Recent additions