/* File: WOODS.C - Sound example */ #include #include #include #include typedef unsigned char far *display_buffer; typedef unsigned char byte; typedef unsigned word; static enum {BIRD1 =1, BIRD2, BIRD3, BIRD4}; void who_is_singing(const int bird); void singing_bird(const int bird, const word duration, const int pitch, const int note); #define _calcaddr(x, y) \ (display_buffer)MK_FP(_displaybufferbase, \ 160 * ((y) - 1) + 2 * ((x) - 1)) #define _outch(addr, c, attr) \ (*(word far *)(addr) = (byte)(c) | (byte)(attr) << 8) word _displaybufferbase; int main(void) { int bird; struct text_info info; gettextinfo(&info); _displaybufferbase = (info.currmode == C80) ? 0xb800 : 0xb000; cprintf("Press any key to stop singing\r\n"); randomize(); do { bird = random(4) + 1; who_is_singing(bird); singing_bird(bird, random(2000) + 300, random(4000) + 200, random(2)); delay(random(500) + 50); } while (!kbhit()); getch(); /* clear key input buffer */ nosound(); putch('\r'); clreol(); return(0); } void who_is_singing(const int bird) { const static byte *order[] = { "", "pretty", "small", "charming", "lovely" }; putch('\r'); clreol(); cprintf("A %s bird is now singing...", order[bird]); } void song(const byte note) { _outch(_calcaddr(wherex(), wherey()), (note == 0) ? '\x0e' : '\x0d', 0x0f); gotoxy(wherex() + 1, wherey()); } void singing_bird(const int bird, const word duration, const int pitch, const int note) { int i = 1, j, k; switch (bird) { case BIRD1: for (; i < duration; i++) { sound(pitch + i); if (i % 50 == 0) song(note); } break; case BIRD2: for (; i < duration; i++) { sound(pitch + i); if (i % 50 == 0) song(note); } nosound(); delay(100); for (j = i + i / 10; i > j; i--) { sound(pitch + i); if (i % 50 == 0) song(note); } break; case BIRD3: for (k = 1; k < random(20) + 10; k++) { sound(pitch); delay(30); song(note); sound(pitch + pitch / 10); delay(30); } break; case BIRD4: k = pitch + pitch / 10; for (i = random(5) + 10; i > 1; i--) { for (j = pitch; j < k; j += 10) { sound(j); delay(6); } nosound(); song(note); delay(40); } break; } /* switch */ nosound(); }