自制的电子琴程序

哈哈,有了上一篇文章做为基础,并且还有另一篇非常有用的文章,已经传到了google上。尝试了几下,终于写了个电子琴程序:

#include #include typedef union _midi { DWORD dwData; BYTE bData[4]; } MIDI; int main(int argc,char **argv) { unsigned long result,device; HMIDIOUT handle; unsigned char key; MIDI midi; printf("initlizing the midi device.../n"); result=midiOutOpen(&handle,0,0,0,CALLBACK_NULL); if(result) { printf("There is an error opening the default MIDI out device!/n"); } else printf("now it can work/n"); if(argc==1) { printf("device num:"); scanf("%u",&device); } if(argc==2) { sscanf(argv[1],"%u",&device); } midi.bData[1]=device; midi.bData[0]=0xc0; midi.bData[3]=0; midi.bData[3]=0; midiOutShortMsg(handle,midi.dwData); while(TRUE) { key=getch(); switch(key) { case 'a': midiOutShortMsg(handle, 0x00403C90); // Sleep(1000); break; case 's': midiOutShortMsg(handle, 0x00403E90); // Sleep(1000); break; case 'd': midiOutShortMsg(handle, 0x00404090); // Sleep(1000); break; case 'f': midiOutShortMsg(handle, 0x00404190); // Sleep(1000); break; case 'j': midiOutShortMsg(handle, 0x00404390); // Sleep(1000); break; case 'k': midiOutShortMsg(handle, 0x00404590); // Sleep(1000); break; case 'l': midiOutShortMsg(handle, 0x00404790); // Sleep(1000); break; case ';': midiOutShortMsg(handle, 0x00404890); // Sleep(1000); break; case ' ': goto finish; default: break; } } /* Now let's turn off those 3 notes */ finish: midiOutShortMsg(handle, 0x00003C90); midiOutShortMsg(handle, 0x00004090); midiOutShortMsg(handle, 0x00004390); /* Close the MIDI device */ midiOutClose(handle); return 0; }

然后编译,别忘了$gcc -o midi midi.c -lwinmm

然后将需要模拟的乐器代号作为参数传递进去或者另行输入,然后就可以轻松弹奏了!!!哈哈,曾经很迷惑的程序现在很轻松的实现了啊,不必再艳羡其它的语言有相关的使用的类库了,windows给C语言程序带来了快乐。

你可能感兴趣的:(自制的电子琴程序)