GUSMIDI2.C 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. Copyright (C) 1994-1995 Apogee Software, Ltd.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // Module MUST be compiled with structure allignment set to a maximum
  16. // of 1 byte ( zp1 ).
  17. #include <conio.h>
  18. #include <dos.h>
  19. #include <stdio.h>
  20. #include <io.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include <malloc.h>
  24. #include <stdlib.h>
  25. #include <math.h>
  26. #include <limits.h>
  27. #include "gusmidi.h"
  28. #define TRUE ( 1 == 1 )
  29. #define FALSE ( !TRUE )
  30. #if defined(__WATCOMC__) && defined(__FLAT__)
  31. #define LOADDS _loadds
  32. #define FAR
  33. typedef void ( __interrupt __far *PRI )();
  34. #define outportb outp
  35. #define enable _enable
  36. #define disable _disable
  37. #define farfree free
  38. #define farmalloc malloc
  39. #else
  40. #define LOADDS
  41. #define FAR far
  42. typedef void ( interrupt *PRI )();
  43. #endif
  44. #include "newgf1.h"
  45. #define BUFFER 2048U /* size of DMA buffer for patch loading */
  46. #define MAX_MEM_CONFIG 3
  47. #define NUM_PATCHES 256 /* size of patch array (128 perc, 128 melodic) */
  48. #define BIGGEST_NAME 9 /* size of largest patch name */
  49. #define PATCH_LOADED 1 /* patch loading flags */
  50. #define UNUSED_PATCH -1
  51. static struct patch new_patch[ NUM_PATCHES ];
  52. static unsigned char FAR *patch_waves[ NUM_PATCHES ];
  53. static int patch_map[ NUM_PATCHES ][ MAX_MEM_CONFIG + 1 ];
  54. static char program_name[ NUM_PATCHES ][ BIGGEST_NAME ];
  55. static char patch_flags[ NUM_PATCHES ];
  56. // DEBUG
  57. // static char config_name[] = "ULTRASND.INI";
  58. static char config_name[] = "ULTRAMID.INI";
  59. static char config_dir[ 80 ] =
  60. {
  61. '\0'
  62. };
  63. static char ultradir[ 80 ]; /* The name of the configuration directory */
  64. static char *hold_buffer;
  65. static int GUSMIDI_Volume = 255;
  66. static unsigned long GUS_TotalMemory;
  67. static int GUS_MemConfig;
  68. static int GUS_Installed = 0;
  69. int GUS_ErrorCode = GUS_Ok;
  70. int GUS_AuxError = 0;
  71. #define GUS_SetErrorCode( status ) \
  72. GUS_ErrorCode = ( status );
  73. /*---------------------------------------------------------------------
  74. Function: GUS_ErrorString
  75. Returns a pointer to the error message associated with an error
  76. number. A -1 returns a pointer the current error.
  77. ---------------------------------------------------------------------*/
  78. char *GUS_ErrorString
  79. (
  80. int ErrorNumber
  81. )
  82. {
  83. char *ErrorString;
  84. switch( ErrorNumber )
  85. {
  86. case GUS_Error :
  87. ErrorString = GUS_ErrorString( GUS_ErrorCode );
  88. break;
  89. case GUS_Ok :
  90. ErrorString = "Ultrasound music ok.";
  91. break;
  92. case GUS_OutOfMemory :
  93. ErrorString = "Out of memory in GusMidi.";
  94. break;
  95. case GUS_OutOfDosMemory :
  96. ErrorString = "Out of conventional (640K) memory in GusMidi.";
  97. break;
  98. case GUS_GF1Error :
  99. ErrorString = gf1_error_str( GUS_AuxError );
  100. break;
  101. case GUS_InvalidIrq :
  102. ErrorString = "Ultrasound IRQ must be 7 or less.";
  103. break;
  104. case GUS_ULTRADIRNotSet :
  105. ErrorString = "ULTRADIR environment variable not set.";
  106. break;
  107. case GUS_MissingConfig :
  108. ErrorString = "Can't find ULTRASND.INI file.";
  109. break;
  110. case GUS_FileError :
  111. ErrorString = strerror( GUS_AuxError );
  112. break;
  113. default :
  114. ErrorString = "Unknown Ultrasound error code.";
  115. break;
  116. }
  117. return( ErrorString );
  118. }
  119. /*
  120. int GUS_GetPatchMap
  121. (
  122. char *name
  123. )
  124. {
  125. char text[ 80 ];
  126. char FAR *ud;
  127. int index;
  128. char pname[ 80 ];
  129. FILE *fp;
  130. ud = getenv( "ULTRADIR" );
  131. if ( ud == NULL )
  132. {
  133. GUS_SetErrorCode( GUS_ULTRADIRNotSet );
  134. return( GUS_Error );
  135. }
  136. strcpy( ultradir, ud );
  137. strcat( ultradir, "\\midi\\" );
  138. strcpy( config_dir, ud );
  139. strcat( config_dir, "\\" );
  140. strcpy( text, config_dir );
  141. strcat( text, name );
  142. fp = fopen( text, "r" );
  143. if ( fp == NULL )
  144. {
  145. GUS_SetErrorCode( GUS_MissingConfig );
  146. return( GUS_Error );
  147. }
  148. while( 1 )
  149. {
  150. if ( fgets( text, 80, fp ) == NULL )
  151. {
  152. break;
  153. }
  154. if ( strncmp( text, "[Drum Patches]", 13 ) == 0 )
  155. {
  156. while( 1 )
  157. {
  158. if ( fgets( text, 80, fp ) == NULL )
  159. {
  160. break;
  161. }
  162. if ( text[ 0 ] == ';' )
  163. {
  164. continue;
  165. }
  166. if ( text[ 0 ] == '[' )
  167. {
  168. break;
  169. }
  170. if ( sscanf( text, "%d=%s\n", &index, pname ) != 2 )
  171. {
  172. continue;
  173. }
  174. strcpy( program_name[ index + 128 ], pname );
  175. }
  176. break;
  177. }
  178. }
  179. if ( fseek( fp, 0L, SEEK_SET ) )
  180. {
  181. GUS_AuxError = errno;
  182. GUS_SetErrorCode( GUS_FileError );
  183. return( GUS_Error );
  184. }
  185. while( 1 )
  186. {
  187. if ( fgets( text, 80, fp ) == NULL )
  188. {
  189. break;
  190. }
  191. if ( strncmp( text, "[Melodic Patches]", 17 ) == 0 )
  192. {
  193. while( 1 )
  194. {
  195. if ( fgets( text, 80, fp ) == 0 )
  196. {
  197. break;
  198. }
  199. if ( text[ 0 ] == ';' )
  200. {
  201. continue;
  202. }
  203. if ( text[ 0 ] == '[' )
  204. {
  205. break;
  206. }
  207. if ( sscanf( text, "%d=%s\n", &index, pname ) != 2 )
  208. {
  209. continue;
  210. }
  211. strcpy( program_name[ index ], pname );
  212. }
  213. break;
  214. }
  215. }
  216. fclose( fp );
  217. return( GUS_Ok );
  218. }
  219. */
  220. int GUS_GetPatchMap
  221. (
  222. char *name
  223. )
  224. {
  225. char text[ 80 ];
  226. char FAR *ud;
  227. int index;
  228. int ignore;
  229. FILE *fp;
  230. for( index = 0; index < NUM_PATCHES; index++ )
  231. {
  232. patch_map[ index ][ 0 ] = UNUSED_PATCH;
  233. patch_map[ index ][ 1 ] = UNUSED_PATCH;
  234. patch_map[ index ][ 2 ] = UNUSED_PATCH;
  235. patch_map[ index ][ 3 ] = UNUSED_PATCH;
  236. program_name[ index ][ 0 ] = 0;
  237. }
  238. ud = getenv( "ULTRADIR" );
  239. if ( ud == NULL )
  240. {
  241. GUS_SetErrorCode( GUS_ULTRADIRNotSet );
  242. return( GUS_Error );
  243. }
  244. strcpy( ultradir, ud );
  245. strcat( ultradir, "\\midi\\" );
  246. strcpy( config_dir, ud );
  247. strcat( config_dir, "\\midi\\" );
  248. strcpy( text, ultradir );
  249. strcat( text, name );
  250. fp = fopen( text, "r" );
  251. if ( fp == NULL )
  252. {
  253. GUS_SetErrorCode( GUS_MissingConfig );
  254. return( GUS_Error );
  255. }
  256. while( 1 )
  257. {
  258. if ( fgets( text, 80, fp ) == NULL )
  259. {
  260. break;
  261. }
  262. if ( text[ 0 ] == '#' )
  263. {
  264. continue;
  265. }
  266. if ( sscanf( text, "%d", &index ) != 1 )
  267. {
  268. continue;
  269. }
  270. sscanf( text, "%d, %d, %d, %d, %d, %s\n", &ignore,
  271. &patch_map[ index ][ 0 ], &patch_map[ index ][ 1 ],
  272. &patch_map[ index ][ 2 ], &patch_map[ index ][ 3 ],
  273. program_name[ index ] );
  274. }
  275. fclose( fp );
  276. return( GUS_Ok );
  277. }
  278. int GUSMIDI_UnloadPatch
  279. (
  280. int prognum
  281. )
  282. {
  283. int prog;
  284. prog = patch_map[ prognum ][ GUS_MemConfig ];
  285. if ( patch_flags[ prog ] & PATCH_LOADED )
  286. {
  287. disable();
  288. gf1_unload_patch( &new_patch[ prog ] );
  289. if ( patch_waves[ prog ] != NULL )
  290. {
  291. free( patch_waves[ prog ] );
  292. patch_waves[ prog ] = NULL;
  293. }
  294. /* just in case sequence is still playing */
  295. new_patch[ prog ].nlayers = 0;
  296. patch_flags[ prog ] &= ~PATCH_LOADED;
  297. enable();
  298. }
  299. return( GUS_Ok );
  300. }
  301. int GUSMIDI_LoadPatch
  302. (
  303. int prognum
  304. )
  305. {
  306. int prog;
  307. char text[ 80 ];
  308. int ret;
  309. unsigned char FAR *wave_buff;
  310. struct patchinfo patchi;
  311. prog = patch_map[ prognum ][ GUS_MemConfig ];
  312. if ( ( patch_flags[ prog ] & PATCH_LOADED ) || ( prog == UNUSED_PATCH ) )
  313. {
  314. return( GUS_Ok );
  315. }
  316. if ( !program_name[ prog ][ 0 ] )
  317. {
  318. return( GUS_Ok );
  319. }
  320. strcpy( text, ultradir );
  321. strcat( text, program_name[ prog ] );
  322. strcat( text, ".pat" );
  323. ret = gf1_get_patch_info( text, &patchi );
  324. if ( ret != OK )
  325. {
  326. GUS_AuxError = ret;
  327. GUS_SetErrorCode( GUS_GF1Error );
  328. return( GUS_Error );
  329. }
  330. wave_buff = ( unsigned char FAR * )malloc( patchi.header.wave_forms *
  331. sizeof( struct wave_struct ) );
  332. if ( wave_buff == NULL )
  333. {
  334. GUS_SetErrorCode( GUS_OutOfMemory );
  335. return( GUS_Error );
  336. }
  337. ret = gf1_load_patch( text, &patchi, &new_patch[ prog ], hold_buffer,
  338. BUFFER, ( unsigned char FAR * )wave_buff, PATCH_LOAD_8_BIT );
  339. if ( ret != OK )
  340. {
  341. free( wave_buff );
  342. GUS_AuxError = ret;
  343. GUS_SetErrorCode( GUS_GF1Error );
  344. return( GUS_Error );
  345. }
  346. patch_waves[ prog ] = wave_buff;
  347. patch_flags[ prog ] |= PATCH_LOADED;
  348. return( GUS_Ok );
  349. }
  350. void *D32DosMemAlloc
  351. (
  352. unsigned size
  353. )
  354. {
  355. #if defined(__WATCOMC__) && defined(__FLAT__)
  356. union REGS r;
  357. r.x.eax = 0x0100; /* DPMI allocate DOS memory */
  358. r.x.ebx = ( size + 15 ) >> 4; /* Number of paragraphs requested */
  359. int386( 0x31, &r, &r );
  360. if ( r.x.cflag )
  361. {
  362. return( NULL ); /* Failed */
  363. }
  364. return( ( void * )( ( r.x.eax & 0xFFFF ) << 4 ) );
  365. #else
  366. return( ( void * )( malloc( size ) ) );
  367. #endif
  368. }
  369. void GUSMIDI_ProgramChange
  370. (
  371. int channel,
  372. int prognum
  373. )
  374. {
  375. int prog;
  376. prog = patch_map[ prognum ][ GUS_MemConfig ];
  377. if ( patch_flags[ prog ] & PATCH_LOADED )
  378. {
  379. gf1_midi_change_program( &new_patch[ prog ], channel );
  380. }
  381. else
  382. {
  383. gf1_midi_change_program( NULL, channel );
  384. }
  385. }
  386. void GUSMIDI_NoteOn
  387. (
  388. int chan,
  389. int note,
  390. int velocity
  391. )
  392. {
  393. int prog;
  394. if ( chan == 9 )
  395. {
  396. prog = patch_map[ note + 128 ][ GUS_MemConfig ];
  397. if ( patch_flags[ prog ] & PATCH_LOADED )
  398. {
  399. gf1_midi_note_on( &new_patch[ note + 128 ], 1,
  400. note, velocity, 9 );
  401. }
  402. }
  403. else
  404. {
  405. gf1_midi_note_on( 0L, 1, note, velocity, chan );
  406. }
  407. }
  408. #pragma warn -par
  409. void GUSMIDI_NoteOff
  410. (
  411. int chan,
  412. int note,
  413. int velocity
  414. )
  415. {
  416. gf1_midi_note_off( note, chan );
  417. }
  418. #pragma warn .par
  419. void GUSMIDI_ControlChange
  420. (
  421. int channel,
  422. int number,
  423. int value
  424. )
  425. {
  426. gf1_midi_parameter( channel, number, value );
  427. }
  428. void GUSMIDI_PitchBend
  429. (
  430. int channel,
  431. int lsb,
  432. int msb
  433. )
  434. {
  435. gf1_midi_pitch_bend( channel, lsb, msb );
  436. }
  437. void GUSMIDI_ReleasePatches
  438. (
  439. void
  440. )
  441. {
  442. int i;
  443. for( i = 0; i < 256; i++ )
  444. {
  445. GUSMIDI_UnloadPatch( i );
  446. }
  447. }
  448. void GUSMIDI_SetVolume
  449. (
  450. int volume
  451. )
  452. {
  453. volume = max( 0, volume );
  454. volume = min( volume, 255 );
  455. GUSMIDI_Volume = volume;
  456. gf1_midi_master_volume( volume >> 1 ); /* range = 0 to 127 */
  457. }
  458. int GUSMIDI_GetVolume
  459. (
  460. void
  461. )
  462. {
  463. return( GUSMIDI_Volume );
  464. }
  465. #pragma on (check_stack);
  466. int GUS_Init
  467. (
  468. void
  469. )
  470. {
  471. struct load_os os;
  472. int ret;
  473. if ( GUS_Installed > 0 )
  474. {
  475. GUS_Installed++;
  476. return( GUS_Ok );
  477. }
  478. GUS_SetErrorCode( GUS_Ok );
  479. GUS_Installed = 0;
  480. GetUltraCfg( &os );
  481. #if defined(__WATCOMC__) && defined(__FLAT__)
  482. if ( os.forced_gf1_irq > 7 )
  483. {
  484. GUS_SetErrorCode( GUS_InvalidIrq );
  485. return( GUS_Error );
  486. }
  487. #endif
  488. os.voices = 24;
  489. ret = gf1_load_os( &os );
  490. if ( ret )
  491. {
  492. GUS_AuxError = ret;
  493. GUS_SetErrorCode( GUS_GF1Error );
  494. return( GUS_Error );
  495. }
  496. GUS_Installed = 1;
  497. return( GUS_Ok );
  498. }
  499. void GUS_Shutdown
  500. (
  501. void
  502. )
  503. {
  504. GUS_SetErrorCode( GUS_Ok );
  505. if ( GUS_Installed > 0 )
  506. {
  507. //printf( "GUS_Installed--;\n" );
  508. GUS_Installed--;
  509. if ( GUS_Installed == 0 )
  510. {
  511. //printf( "gf1_unload_os();\n" );
  512. gf1_unload_os();
  513. }
  514. }
  515. //printf( "Gus shutdown exit\n" );
  516. }
  517. int GUSMIDI_Init
  518. (
  519. void
  520. )
  521. {
  522. int ret;
  523. int i;
  524. int startmem;
  525. ret = GUS_Init();
  526. if ( ret != GUS_Ok )
  527. {
  528. return( ret );
  529. }
  530. GUS_TotalMemory = gf1_mem_avail();
  531. GUS_MemConfig = ( GUS_TotalMemory - 1 ) >> 18;
  532. // //printf( "Totalmem = %d\n", GUS_TotalMemory );
  533. // //printf( "Config # = %d\n", GUS_MemConfig );
  534. // getch();
  535. if ( GUS_MemConfig < 0 )
  536. {
  537. GUS_MemConfig = 0;
  538. }
  539. if ( GUS_MemConfig > MAX_MEM_CONFIG )
  540. {
  541. GUS_MemConfig = MAX_MEM_CONFIG;
  542. }
  543. hold_buffer = D32DosMemAlloc( BUFFER );
  544. if ( hold_buffer == NULL )
  545. {
  546. gf1_unload_os();
  547. GUS_SetErrorCode( GUS_OutOfDosMemory );
  548. return( GUS_Error );
  549. }
  550. for( i = 0; i < NUM_PATCHES; i++ )
  551. {
  552. program_name[ i ][ 0 ] = '\0';
  553. patch_waves[ i ] = NULL;
  554. patch_flags[ i ] = 0;
  555. }
  556. GUSMIDI_SetVolume( 255 );
  557. /*
  558. ** GUS_GetPatchMap reads a list of patches and the
  559. ** associated program numbers out of a configuration file.
  560. */
  561. ret = GUS_GetPatchMap( config_name );
  562. if ( ret != GUS_Ok )
  563. {
  564. return( ret );
  565. }
  566. startmem = gf1_mem_avail();
  567. for( i = 0; i < NUM_PATCHES; i++ )
  568. {
  569. ret = GUSMIDI_LoadPatch( i );
  570. // //printf( "%d, %d\n", i, gf1_mem_avail() );
  571. if ( ret != GUS_Ok )
  572. {
  573. // //printf( "Error!\n" );
  574. // getch();
  575. // GUS_SetErrorCode( GUS_OutOfDRAM );
  576. // return( GUS_Error );
  577. return( ret );
  578. }
  579. }
  580. // //printf( "mem available %d\n", gf1_mem_avail() );
  581. // getch();
  582. return( GUS_Ok );
  583. }
  584. void GUSMIDI_Shutdown
  585. (
  586. void
  587. )
  588. {
  589. //printf( "Release Patches\n" );
  590. GUSMIDI_ReleasePatches();
  591. //printf( "Shutdown\n" );
  592. GUS_Shutdown();
  593. //printf( "Return\n" );
  594. }