home *** CD-ROM | disk | FTP | other *** search
/ Audio Version 4.94 / audioversion4.94knowledgemediaresourcelibraryoctober1994.iso / msdos / midi / mppdemo / mpudemo.c < prev   
Text File  |  1994-03-16  |  7KB  |  181 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *   FileName:  MPUDEMO1.BAS, MPUDEMO1.EXE - for IBM and ROLAND MPU-401    *
  4.  *                                                                         *
  5.  *      Demonstrates the speed of Borland's TURBO BASIC, and allows you    *
  6.  *        to experiment with the ROLAND MPU-401 and MIDI synthesizers.     *
  7.  *   MPUDEMO1 puts your MPU-401 into its "DUMB UART" mode and then plays   *
  8.  *    a quick scale and some chords on MIDI Channel 1.  To run, connect    *
  9.  *        your synth (on Chan.1) to MPU-401 OUT, type MPUDEMO1 <CR>        *
  10.  *   WARNING:  If you don't HAVE an MPU-401 hooked up, program hangs up!   *
  11.  *       Compile ONLY with TURBO BASIC V. 1.0 on IBM PC/XT or equiv.       *
  12.  *         Please feel free to tinker around to learn about MIDI!          *
  13.  *                                                                         *
  14.  *               By Gino F. Silvestri 06/19/87 1700 Hrs.                   *
  15.  *                                                                         *
  16.  ***************************************************************************/
  17.  
  18. /***************************************************************************
  19.  *                        D E F I N I T I O N S                            *
  20.  ***************************************************************************/
  21.  
  22. #define WAIT( port, flag )    while ( (inp(port)&flag) != 0 )
  23.  
  24. #define    COMDPORT   0x331        /* MPU-401 Command Port on IBM */
  25. #define    STATPORT   0x331        /* MPU-401 Status Port on IBM */
  26. #define    DATAPORT   0x330        /* MPU-401 Data I/O Port on IBM */
  27. #define    DRR        0x40            /* Mask for Data Read Reg. Bit */
  28. #define    DSR        0x80            /* Mask for Data Set Ready Bit */
  29. #define    ACK        0xFE            /* MPU-401 Acknowledge Response */
  30. #define    MASKFLIP   0xFF            /* WAIT Function Bit Mask XOR */
  31. #define    MPURESET   0xFF            /* MPU-401 Total Reset Command */
  32. #define    UARTMODE   0x3F            /* MPU-401 "Dumb UART Mode" */
  33. #define    NOTEON1    0x90            /* MIDI Note On for Channel 1 */
  34. #define    VELOCITY   64            /* MIDI Medium Key Velocity */
  35. #define    NOTEOFF    0            /* 0 Velocity = Note Off */
  36. #define    FIRSTNOTE  36            /* First note synth can play */
  37. #define    LASTNOTE   96            /* Last note synth can play */
  38.  
  39. /***************************************************************************
  40.  *                       I N I T I A L I Z A T I O N                       *
  41.  ***************************************************************************/
  42.  
  43. RstMPU()
  44. {
  45.     register char a;
  46.  
  47.     /* Clear Display */
  48.  
  49.     outp( COMDPORT, MPURESET );        /* Send MPU-401 RESET Command */
  50.     a = inp( DATAPORT );            /* Dummy read to clear buffer */
  51.  
  52.     WAIT( STATPORT, DRR );            /* wait for port ready */
  53.  
  54.     outp( COMDPORT, UARTMODE );        /* Set MPU-401 "Dumb UART" Mode */
  55.     a = inp(DATAPORT);                /* Dummy Read to clear buffer */
  56.  
  57.     WAIT( STATPORT, DSR );            /* Wait for "UART" port ready - */
  58.                                      /*  Really crucial! */
  59. }
  60.  
  61. /***************************************************************************
  62.  *                         M A I N   P R O G R A M                         *
  63.  ***************************************************************************/
  64.  
  65. main()
  66. {
  67.     register n;
  68.  
  69.  
  70.     RstMPU();
  71.  
  72.     printf( "\n\n\n\n\n" );
  73.  
  74.     printf( "\tMPUDEMO1 now playing a fast scale on MIDI Channel 1\n" );
  75.  
  76.     for ( n = FIRSTNOTE; n <= LASTNOTE; n++ )
  77.     {
  78.         PlayIt( n );        /* Play note */
  79.         Delay( 1 );            /* Duration of note ON */
  80.         OffIt( n );            /* Stop note */
  81.     }
  82.  
  83.     Delay( 1 );                /* Pause between scales */
  84.  
  85.     for ( n = LASTNOTE; n <= FIRSTNOTE; n-- )
  86.     {
  87.         PlayIt( n );        /* Play a note */
  88.         Delay( 1 );            /* Duration of note OFF */
  89.         OffIt( n );            /* Stop that same note */
  90.     }
  91.  
  92.     Delay( 1 );                /* Pause between demos */
  93.  
  94.     printf( "\n\n\n" );
  95.  
  96.     printf( "\tMPUDEMO1 now playing some chords on MIDI Channel 1\n" );
  97.  
  98.     for ( n = 1; n <= 3; n++ )
  99.     {
  100.         PlayIt( 65 );        /* Start a chord - F3 */
  101.         PlayIt( 69 );        /* A3 */
  102.         PlayIt( 72 );        /* C4 */
  103.  
  104.         Delay( 1 );            /* Duration of held chord */
  105.  
  106.         OffIt( 65 );        /* Stop the chord - F3 */
  107.         OffIt( 69 );        /* A3 */
  108.         OffIt( 72 );        /* C4 */
  109.  
  110.         Delay( 1 );            /* Duration of rest */
  111.     }
  112.  
  113.     printf( "\n\n\n" );
  114.  
  115.     printf( "\tMPUDEMO1 is through - Tinker with it!\n" );
  116. }
  117.  
  118. /***************************************************************************
  119.  *                          S U B R O U T I N E S                          *
  120.  ***************************************************************************/
  121.  
  122.  
  123. /***************************** Playit SUBROUTINE ***************************/
  124.  
  125. PlayIt( n )             /* Play a MIDI Note */
  126. int n;
  127. {
  128.     register char a;
  129.  
  130.     outp( DATAPORT, NOTEON1    );        /* Send Chan. 1 note ON code */
  131.     a = inp( DATAPORT );            /* Dummy Read to clear buffer */
  132.      WAIT( STATPORT, DRR );            /* Wait for port ready */
  133.     
  134.     outp( DATAPORT, n );            /* Send note Number to turn ON */
  135.     a = inp( DATAPORT );            /* Dummy Read to clear buffer */
  136.     WAIT( STATPORT, DRR );             /* Wait for port ready */
  137.  
  138.     outp( DATAPORT, VELOCITY );        /* Send medium velocity */
  139.     a = inp( DATAPORT );            /* Dummy Read to clear buffer */
  140.     WAIT( STATPORT, DRR );             /* Wait for port ready */
  141. }
  142.  
  143. /***************************** Offit SUBROUTINE ***************************/
  144.  
  145. OffIt( n )                    /* Turn off a MIDI Note */
  146. int n;
  147. {
  148.     register char a;
  149.  
  150.     outp( DATAPORT, NOTEON1    );        /* Send Chan. 1 note ON code */
  151.     a = inp( DATAPORT );            /* Dummy Read to clear buffer */
  152.     WAIT( STATPORT, DRR );            /* Wait for port ready */
  153.  
  154.     outp( DATAPORT, n );            /* Send note number to turn OFF */
  155.     a = inp( DATAPORT );            /* Dummy Read to clear buffer */
  156.      WAIT( STATPORT, DRR );            /* Wait for port ready */
  157.  
  158.     outp( DATAPORT, NOTEOFF    );        /* Send 0 Velocity = Note Off */
  159.     a = inp( DATAPORT );            /* Dummy Read to clear buffer */
  160.      WAIT( STATPORT, DRR );            /* Wait for port ready */
  161. }
  162.  
  163. /**************************** Delay SUBROUTINE ****************************/
  164.  
  165. Delay( n )
  166. int n;
  167. {
  168.     long x;
  169.  
  170.     while ( n-- > 0 )
  171.         for ( x = 1; x < 10000; x++ );
  172. }
  173.  
  174. /* Note: Read of DATAPORT prevents hang-up if MIDI IN from a keyboard is
  175.     connected and played - WAIT would stay FOREVER if you hit any key once!
  176. */
  177.  
  178. /************************* Last Line of MPUDEMO1.C ************************
  179.   Last Edited 07/1/87 2010 Hrs. M. D. Bratcher
  180.  **************************************************************************/
  181.