ex_music.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* EX_MUSIC.C - Example program plays "Happy Birthday" to the remote user, */
  2. /* if possible. See the manual for instructions on how to */
  3. /* compile this program. */
  4. /* */
  5. /* This program shows how to do the following: */
  6. /* */
  7. /* - Demonstrates how to play sounds effects or music on a */
  8. /* remote terminal program that supports the so-called */
  9. /* "ANSI music" standard. */
  10. /* - Shows how to send text to the remote system without it */
  11. /* being displayed on the local screen. */
  12. /* The opendoor.h file must be included by any program using OpenDoors. */
  13. #include "OpenDoor.h"
  14. #include <string.h>
  15. /* Functions for playing "ANSI music" and testing "ANSI music" capabilities. */
  16. void PlayANSISound(char *pszSounds);
  17. char TestSound(void);
  18. /* Variable indicates whether or not sound is on */
  19. char bSoundEnabled = TRUE;
  20. /* The main() or WinMain() function: program execution begins here. */
  21. #ifdef ODPLAT_WIN32
  22. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  23. LPSTR lpszCmdLine, int nCmdShow)
  24. #else
  25. int main(int argc, char *argv[])
  26. #endif
  27. {
  28. /* Handle standard command-line options and perform other needed setup. */
  29. #ifdef ODPLAT_WIN32
  30. od_control.od_cmd_show = nCmdShow;
  31. od_parse_cmd_line(lpszCmdLine);
  32. #else
  33. od_parse_cmd_line(argc, argv);
  34. #endif
  35. /* Display introductory message. */
  36. od_printf("This is a simple door program that will play the song Happy Birthday\n\r");
  37. od_printf("tune on the remote system, if the user's terminal program supports ANSI\n\r");
  38. od_printf("music. Music is not played on the local speaker, as BBS system operators\n\r");
  39. od_printf("do not wish to have the BBS computer making sounds at any time of the day\n\r");
  40. od_printf("or night. However, the program can easily be modified to also echo sound to\n\r");
  41. od_printf("the local speaker.\n\r\n\r");
  42. /* Test whether user's terminal supports "ANSI music". */
  43. TestSound();
  44. /* Send birthday greetings to the remote user. */
  45. /* Clear the screen. */
  46. od_clr_scr();
  47. /* Display a message. */
  48. od_printf("\n\rHappy Birthday!\n\r");
  49. /* If "ANSI music" is available, play "Happy Birthday". */
  50. PlayANSISound("MBT120L4MFMNO4C8C8DCFE2C8C8DCGF2C8C8O5CO4AFED2T90B-8B-8AFGF2");
  51. /* Reset sound after finished playing. */
  52. PlayANSISound("00m");
  53. /* Wait for user to press a key before returning to the BBS. */
  54. od_printf("\n\rPress any key to return to BBS...\n\r");
  55. od_get_key(TRUE);
  56. od_exit(0, FALSE);
  57. return(0);
  58. }
  59. /* Function to test whether the user's terminal program supports ANSI music. */
  60. /* You can either do this every time the user uses your program, or only the */
  61. /* first time they use the program, saving the result in a data file. */
  62. char TestSound(void)
  63. {
  64. /* Variable to store user's response to question. */
  65. char chResponse;
  66. /* Display description of test to user. */
  67. od_printf("We need to know whether or not your terminal program supports ANSI music.\n\r");
  68. od_printf("In order to test this, we will send a short ANSI music sequence. We will then\n\r");
  69. od_printf("ask whether or not you heard any sound.\n\r");
  70. od_printf("Press any key to begin this test... ");
  71. /* Wait for user to press a key to begin. */
  72. od_get_key(TRUE);
  73. od_printf("\n\r\n\r");
  74. /* Temporarily enable sound. */
  75. bSoundEnabled = TRUE;
  76. /* Send sound test sequence. */
  77. PlayANSISound("MBT120L4MFMNO4C8C8DC");
  78. /* Reset sound after finished test. */
  79. PlayANSISound("00m");
  80. /* Clear screen and ask whether user heard the sound. */
  81. od_clr_scr();
  82. od_printf("Did you just hear sound from your speaker? (Y/n)");
  83. chResponse = od_get_answer("YN");
  84. /* Set ANSI music on/off according to user's response. */
  85. bSoundEnabled = (chResponse == 'Y');
  86. return(bSoundEnabled);
  87. }
  88. /* Function to play "ANSI" music or sound effects. The play_sound() function
  89. * can be called with a string of 0 to 250 characters. The caracters of the
  90. * string define what sounds should be played on the remote speaker, as
  91. * follows:
  92. *
  93. * A - G Musical Notes
  94. * # or + Following A-G note means sharp
  95. * - Following A-G note means flat
  96. * < Move down one octave
  97. * > Move up one octave
  98. * . Period acts as dotted note (extend note duration by 3/2)
  99. * MF Music Foreground (pause until finished playing music)
  100. * MB Music Background (continue while music plays)
  101. * MN Music note duration Normal (7/8 of interval between notes)
  102. * MS Music note duration Staccato
  103. * ML Music note duration Legato
  104. * Ln Length of note (n=1-64, 1=whole note, 4=quarter note, etc)
  105. * Pn Pause length (same n values as Ln above)
  106. * Tn Tempo, n=notes/minute (n=32-255, default n=120)
  107. * On Octave number (n=0-6, default n=4)
  108. */
  109. void PlayANSISound(char *pszSounds)
  110. {
  111. /* Beginning of sound sequence. */
  112. char szStartSound[255] = {27, '[', '\0'};
  113. /* Abort if sound is not enabled. */
  114. if(!bSoundEnabled) return;
  115. /* Send sequence to start playing sound to remote system only. */
  116. od_disp(szStartSound, strlen(szStartSound), FALSE);
  117. /* Send the sounds codes to the remote system only. */
  118. od_disp(pszSounds, strlen(pszSounds), FALSE);
  119. }