/************************************************************************** EXAMPLE.C Written by: Eric Jorgensen (Feb, 1995) This code was written using Turbo C. It is intended to demonstrate the use of Varmint's Audio Tools and to provide a example code for program who wish to use VAT. This code is FREEWARE You are free to distribute without any restrictions as long as you charge no fee. **************************************************************************/ #include #include #include #include #include #include #include "sound.h" #include #define getbit(x,y) ((x>>y) & 0x01) #define setbit(x,y) x = x & (0x01<= 0; i-= 1) { // quiet down slowly for(j = 0; j < 8; j++) { FM_SetVol(j,i); } msdelay(i*2+40); if(kbhit() & i) i = 1; } } for(i = 0; i < 9; i++) { // Silence them all FM_SetVol(i,0); // volume off FM_SetVoice(i,inst[0]); // this instrument has a decay, so it // gets all the way quiet FM_SetFreq(i,1); // Low freq = quiet } } /************************************************************************** void mididemo(void) DESCRIPTION: Example function that loads and "plays" a midi file **************************************************************************/ void mididemo(void) { int i,err; char *errstring[256],r=0; static MIDI *bach = NULL; clrscr(); // clear screen FM_Reset(); for(i = 0 ; i < 9; i++) { FM_SetVol(i,0); FM_SetVoice ( i,inst[0]) ; // initialize voice } if(!bach) { // load a midi file err = ReadMidi("bach.mid",&bach,(char *)errstring); if(err) { printf("MIDI read ERROR: %s\n",errstring); printf("Exit?\n"); r = getch(); if(r != 'n') return; } } //printf("FORMAT: %d TRACKS: %d DIVISION: %d\n", // bach->format,bach->num_tracks,bach->divisions); midi_data = bach; midi_reset = TRUE; textcolor(MAGENTA); gotoxy(25,2); cprintf("MIDI demo menu"); // Draw the menu textcolor(CYAN); gotoxy(25,4); cprintf("V,v - Change music volume"); gotoxy(25,5); cprintf("T,t - Change music tempo"); gotoxy(25,6); cprintf("B - Begin music"); gotoxy(25,7); cprintf("S - Stop Music"); gotoxy(25,8); cprintf("R - Reset midi"); gotoxy(25,9); cprintf("Q - Quit"); while(toupper(r) != 'Q') { gotoxy(30,11); // print temppo and volume stats textcolor(WHITE); cprintf("Music Volume: %d ",music_volume); gotoxy(30,12); cprintf("Music Tempo: %.2f ",midi_usertempo); while(!kbhit()) { gotoxy(1,1); //printf("D: %d",debugnum); } r = getch(); // get keystroke if(r == 'V') { music_volume++; if(music_volume > 0x3f) music_volume = 0x3f; } else if(r == 'v') { if(music_volume > 0) music_volume--; } else if(r == 'T') { midi_usertempo *= 1.02; if(midi_usertempo > 10.0) midi_usertempo = 10.0; } else if(r == 't') { midi_usertempo *= 0.98; if(midi_usertempo < 0.1) midi_usertempo = 0.1; } else if(toupper(r) == 'B') { midi_on = TRUE; } else if(toupper(r) == 'S') { midi_on = FALSE; } else if(toupper(r) == 'R') { midi_reset = TRUE; } } } /************************************************************************** void bitprint(char byte) DESCRIPTION: Prints individual bits and then a HEX value **************************************************************************/ void bitprint(unsigned char byte) { int i; cprintf(" "); // go through bits for(i = 7; i >= 0; i--) { if(getbit(byte,i)) { // 1's are yellow textcolor(YELLOW); cprintf("1"); } else { // 0's are dark grey textcolor(DARKGRAY); cprintf("0"); } } textcolor(LIGHTBLUE); // print the hex value cprintf(" %c%c",hexchar[byte/16],hexchar[byte%16]); textcolor(WHITE); } /************************************************************************** void fmdemo() DESCRIPTION: Allows th user to mess around with instruments and play a few notes. **************************************************************************/ void fmdemo(void) { unsigned int i,cx,cy,voice=0,instrument = 0; int drawvoice = 1,rythm = 0; char r = 0; clrscr(); // clear screen printf("FM demonstration screen"); FM_Reset(); for(i = 0; i < 9; i++) FM_KeyOff(i); gotoxy(1,16); cprintf("IJKL = cursor movement\r\n"); cprintf(" = toggle bit\r\n"); cprintf("numbers = play notes\r\n"); cprintf("v = change voice\r\n"); cprintf("n = Change instrument\r\n"); cprintf("r = toggle rythm mode\r\n"); cprintf("q = quit"); _setcursortype(_SOLIDCURSOR); for( i = 0; i < 8; i++) { // initialize voices FM_SetVoice ( i,inst[instrument]) ; FM_SetVol(i,0); } cx = 54;cy = 5; // init cursor position for(i = 0; i < 11; i++) { // print register names gotoxy(50-strlen(regname[i]),i+5); textcolor(MAGENTA); cprintf("%s",regname[i]); } while(r != 'Q') { // main input loop if(drawvoice) { for(i = 0; i < 11; i++) { // display instrument data gotoxy(52,i+5); bitprint(inst[instrument][i]); } for(i = 0; i < 9; i++) { FM_SetVoice ( i,inst[instrument]) ; // initialize new voice } drawvoice = 0; textcolor(LIGHTGRAY); gotoxy(52,4); cprintf("Instrument #%d ",instrument); } gotoxy(cx,cy); // put cursor in right spot while(!kbhit()){ } r = toupper(getch()); if(r >= '0' && r <= '9') { FM_KeyOff(voice); FM_SetVoice(voice,inst[instrument]); FM_SetNote ( voice, (r-'0'+3) * 8) ; FM_SetVol(voice,music_volume); FM_KeyOn(voice); } else if(r == 'I') { // i,j,k,l = cursor movement cy = cy -1; if(cy<5) cy = 5; } else if(r == 'K') { cy = cy +1; if(cy >15) cy = 15; } else if(r == 'J') { cx = cx -1; if(cx<54) cx = 54; } else if(r == 'L') { cx = cx +1; if(cx>61) cx = 61; } else if(r == ' ') { // space = toggle bit togbit(inst[instrument][cy-5],(7-(cx-54))); drawvoice = 1; } else if(r == 'N') { // I = change instrument instrument++; if(instrument > 8) instrument = 0; drawvoice = 1; } else if(r == 'R') { // r = toggle rythm mode if(rythm) rythm = 0; else rythm = 1; FM_RythmMode(rythm); // do it FM_RythmOn(FM_HIHAT); gotoxy(1,2); // tell the user if(rythm) cprintf("Rythm Mode ON (voices 6,7,8 only) "); else cprintf("Rythm Mode OFF "); } else if(r == 'V') { // v = change voice FM_KeyOff(voice); FM_SetVol(voice,0); voice ++; if(voice > 8) voice = 0; gotoxy(1,3); cprintf("Voice: %d ",voice); } else if(r == 'W') { // Frequency ramp for(i = 0; i < 0xf000; i+= 0x10) { FM_SetFreq(voice,i); FM_KeyOn(voice); msdelay(5); } } } for(i = 0; i < 9; i++) { FM_SetVol(i,0); } }