/* * Touch sensitive keyboard */ #include "midiIO.h" #include "glut.h" #include /* function defs */ void idle(void); void handleKeys(unsigned char key, int, int); int drawObjects(void); double myRand(void); /* globals */ int pad[4] = {0, 0, 0, 0}; int state[4] = {0, 0, 0, 0}; int note[4] = {50, 55, 60, 65}; double x0, y0, x1, y1, x2, y2, x3, y3; /**********************************************************************/ void handleKeys(unsigned char key, int x, int y) { if (key == 'q') { exit(0); } } /* end handleKeys */ double myRand(void) { double sign; if (rand() > 16384) { sign = -1.0; } else { sign = 1.0; } return ((double)rand() / 32767.0) * sign; } /**********************************************************************/ void idle(void) { MDevent event; int channel = 1; int index, noteOn = 0, noteOff = 0; if (getMidiEvent(&event) > 0) /* retrieve most current event */ { switch (event.msg[1]) { case 16: pad[0] = event.msg[2]; if ((pad[0] > 0) && (state[0] == 0)) { noteOn = 1; index = 0; } else if (pad[0] == 0) { state[0] = 0; noteOff = 1; index = 0; } break; case 17: pad[1] = event.msg[2]; if ((pad[1] > 0) && (state[1] == 0)) { noteOn = 1; index = 1; } else if (pad[1] == 0) { state[1] = 0; noteOff = 1; index = 1; } break; case 18: pad[2] = event.msg[2]; if ((pad[2] > 0) && (state[2] == 0)) { noteOn = 1; index = 2; } else if (pad[2] == 0) { state[2] = 0; noteOff = 1; index = 2; } break; case 19: pad[3] = event.msg[2]; if ((pad[3] > 0) && (state[3] == 0)) { noteOn = 1; index = 3; } else if (pad[3] == 0) { state[3] = 0; noteOff = 1; index = 3; } break; case 75: /* send instrument type */ event.msg[0] = MD_PROGRAMCHANGE|channel; event.msg[1] = event.msg[2]; sendMidiEvent(&event); break; default: break; } /* end switch */ /* process note change */ if (noteOn) { /* send note on */ event.msg[0] = MD_NOTEON|channel; event.msg[1] = note[index]; event.msg[2] = 100; sendMidiEvent(&event); } if (noteOff) { /* Now send a note off */ event.msg[0] = MD_NOTEOFF|channel; event.msg[1] = note[index]; event.msg[2] = 0; sendMidiEvent(&event); } glutPostRedisplay(); } /* end if */ } /* end idle */ /**********************************************************************/ int drawObjects(void) { double size; int sign; if (pad[0] > 0) { if (state[0] == 0) { x0 = myRand() * 3.0; y0 = myRand() * 2.0; state[0] = 1; } size = ((double)pad[0] / 127.0) * 2.0; glPushMatrix(); glColor3f(1.0, 0.0, 0.0); glTranslatef(x0, y0, 0.0); glutSolidSphere(size, 8, 8); glPopMatrix(); } if (pad[1] > 0) { if (state[1] == 0) { x1 = myRand() * 3.0; y1 = myRand() * 2.0; state[1] = 1; } size = ((double)pad[1] / 127.0) * 2.0; glPushMatrix(); glColor3f(0.0, 1.0, 1.0); glTranslatef(x1, y1, 0.0); glutSolidSphere(size, 8, 8); glPopMatrix(); } if (pad[2] > 0) { if (state[2] == 0) { x2 = myRand() * 3.0; y2 = myRand() * 2.0; state[2] = 1; } size = ((double)pad[2] / 127.0) * 2.0; glPushMatrix(); glColor3f(0.0, 1.0, 0.0); glTranslatef(x2, y2, 0.0); glutSolidSphere(size, 8, 8); glPopMatrix(); } if (pad[3] > 0) { if (state[3] == 0) { x3 = myRand() * 3.0; y3 = myRand() * 2.0; state[3] = 1; } size = ((double)pad[3] / 127.0) * 2.0; glPushMatrix(); glColor3f(0.0, 0.0, 1.0); glTranslatef(x3, y3, 0.0); glutSolidSphere(size, 8, 8); glPopMatrix(); } return 1; } /* end drawObject */ /**********************************************************************/ void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90, 1.33, 0.01, 1000); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); drawObjects(); glutSwapBuffers(); glFlush(); } /* end display */ /**********************************************************************/ int main(int argc, char** argv) { printf("\nInitializing midi...\n"); initMidi("Serial Port 2\0", "Software Synth\0"); printf("Initializing graphics...\n"); glutInit(&argc, argv); glutInitWindowSize(640, 480); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutCreateWindow("demo"); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glShadeModel(GL_SMOOTH); glFlush(); glutKeyboardFunc(handleKeys); glutIdleFunc(idle); glutDisplayFunc(display); glutMainLoop(); return 0; } /* end main */