MUSIC.C 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. /**********************************************************************
  16. module: MUSIC.C
  17. author: James R. Dose
  18. date: March 25, 1994
  19. Device independant music playback routines.
  20. (c) Copyright 1994 James R. Dose. All Rights Reserved.
  21. **********************************************************************/
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include "task_man.h"
  25. #include "sndcards.h"
  26. #include "music.h"
  27. #include "midi.h"
  28. #include "al_midi.h"
  29. #include "pas16.h"
  30. #include "blaster.h"
  31. #include "gusmidi.h"
  32. #include "mpu401.h"
  33. #include "awe32.h"
  34. #include "sndscape.h"
  35. #include "ll_man.h"
  36. #include "user.h"
  37. #define TRUE ( 1 == 1 )
  38. #define FALSE ( !TRUE )
  39. void TextMode( void );
  40. #pragma aux TextMode = \
  41. "mov ax, 0003h", \
  42. "int 10h" \
  43. modify [ ax ];
  44. int MUSIC_SoundDevice = -1;
  45. int MUSIC_ErrorCode = MUSIC_Ok;
  46. static midifuncs MUSIC_MidiFunctions;
  47. static int MUSIC_FadeLength;
  48. static int MUSIC_FadeRate;
  49. static unsigned MUSIC_CurrentFadeVolume;
  50. static unsigned MUSIC_LastFadeVolume;
  51. static int MUSIC_EndingFadeVolume;
  52. static task *MUSIC_FadeTask = NULL;
  53. int MUSIC_InitAWE32( midifuncs *Funcs );
  54. int MUSIC_InitFM( int card, midifuncs *Funcs );
  55. int MUSIC_InitMidi( int card, midifuncs *Funcs, int Address );
  56. int MUSIC_InitGUS( midifuncs *Funcs );
  57. #define MUSIC_SetErrorCode( status ) \
  58. MUSIC_ErrorCode = ( status );
  59. /*---------------------------------------------------------------------
  60. Function: MUSIC_ErrorString
  61. Returns a pointer to the error message associated with an error
  62. number. A -1 returns a pointer the current error.
  63. ---------------------------------------------------------------------*/
  64. char *MUSIC_ErrorString
  65. (
  66. int ErrorNumber
  67. )
  68. {
  69. char *ErrorString;
  70. switch( ErrorNumber )
  71. {
  72. case MUSIC_Warning :
  73. case MUSIC_Error :
  74. ErrorString = MUSIC_ErrorString( MUSIC_ErrorCode );
  75. break;
  76. case MUSIC_Ok :
  77. ErrorString = "Music ok.";
  78. break;
  79. case MUSIC_ASSVersion :
  80. ErrorString = "Apogee Sound System Version " ASS_VERSION_STRING " "
  81. "Programmed by Jim Dose\n"
  82. "(c) Copyright 1996 James R. Dose. All Rights Reserved.\n";
  83. break;
  84. case MUSIC_SoundCardError :
  85. switch( MUSIC_SoundDevice )
  86. {
  87. case SoundBlaster :
  88. case WaveBlaster :
  89. ErrorString = BLASTER_ErrorString( BLASTER_Error );
  90. break;
  91. case ProAudioSpectrum :
  92. case SoundMan16 :
  93. ErrorString = PAS_ErrorString( PAS_Error );
  94. break;
  95. case Adlib :
  96. ErrorString = "Adlib error.";
  97. break;
  98. case GenMidi :
  99. case SoundCanvas :
  100. ErrorString = "Could not detect MPU-401.";
  101. break;
  102. case SoundScape :
  103. ErrorString = SOUNDSCAPE_ErrorString( SOUNDSCAPE_Error );
  104. break;
  105. case Awe32 :
  106. ErrorString = AWE32_ErrorString( AWE32_Error );
  107. break;
  108. case UltraSound :
  109. ErrorString = GUS_ErrorString( GUS_Error );
  110. break;
  111. default :
  112. ErrorString = MUSIC_ErrorString( MUSIC_InvalidCard );
  113. break;
  114. }
  115. break;
  116. case MUSIC_MPU401Error :
  117. ErrorString = "Could not detect MPU-401.";
  118. break;
  119. case MUSIC_InvalidCard :
  120. ErrorString = "Invalid Music device.";
  121. break;
  122. case MUSIC_MidiError :
  123. ErrorString = "Error playing MIDI file.";
  124. break;
  125. case MUSIC_TaskManError :
  126. ErrorString = "TaskMan error.";
  127. break;
  128. case MUSIC_FMNotDetected :
  129. ErrorString = "Could not detect FM chip.";
  130. break;
  131. case MUSIC_DPMI_Error :
  132. ErrorString = "DPMI Error in MUSIC.";
  133. break;
  134. default :
  135. ErrorString = "Unknown Music error code.";
  136. break;
  137. }
  138. return( ErrorString );
  139. }
  140. /*---------------------------------------------------------------------
  141. Function: MUSIC_Init
  142. Selects which sound device to use.
  143. ---------------------------------------------------------------------*/
  144. int MUSIC_Init
  145. (
  146. int SoundCard,
  147. int Address
  148. )
  149. {
  150. int i;
  151. int status;
  152. if ( USER_CheckParameter( "ASSVER" ) )
  153. {
  154. MUSIC_SetErrorCode( MUSIC_ASSVersion );
  155. return( MUSIC_Error );
  156. }
  157. status = LL_LockMemory();
  158. if ( status != LL_Ok )
  159. {
  160. MUSIC_SetErrorCode( MUSIC_DPMI_Error );
  161. return( MUSIC_Error );
  162. }
  163. for( i = 0; i < 128; i++ )
  164. {
  165. MIDI_PatchMap[ i ] = i;
  166. }
  167. status = MUSIC_Ok;
  168. MUSIC_SoundDevice = SoundCard;
  169. switch( SoundCard )
  170. {
  171. case SoundBlaster :
  172. case Adlib :
  173. case ProAudioSpectrum :
  174. case SoundMan16 :
  175. status = MUSIC_InitFM( SoundCard, &MUSIC_MidiFunctions );
  176. break;
  177. case GenMidi :
  178. case SoundCanvas :
  179. case WaveBlaster :
  180. case SoundScape :
  181. status = MUSIC_InitMidi( SoundCard, &MUSIC_MidiFunctions, Address );
  182. break;
  183. case Awe32 :
  184. status = MUSIC_InitAWE32( &MUSIC_MidiFunctions );
  185. break;
  186. case UltraSound :
  187. status = MUSIC_InitGUS( &MUSIC_MidiFunctions );
  188. break;
  189. case SoundSource :
  190. case PC :
  191. default :
  192. MUSIC_SetErrorCode( MUSIC_InvalidCard );
  193. status = MUSIC_Error;
  194. }
  195. if ( status != MUSIC_Ok )
  196. {
  197. LL_UnlockMemory();
  198. }
  199. return( status );
  200. }
  201. /*---------------------------------------------------------------------
  202. Function: MUSIC_Shutdown
  203. Terminates use of sound device.
  204. ---------------------------------------------------------------------*/
  205. int MUSIC_Shutdown
  206. (
  207. void
  208. )
  209. {
  210. int status;
  211. status = MUSIC_Ok;
  212. MIDI_StopSong();
  213. if ( MUSIC_FadeTask != NULL )
  214. {
  215. MUSIC_StopFade();
  216. }
  217. switch ( MUSIC_SoundDevice )
  218. {
  219. case Adlib :
  220. AL_Shutdown();
  221. break;
  222. case SoundBlaster :
  223. AL_Shutdown();
  224. BLASTER_RestoreMidiVolume();
  225. break;
  226. case GenMidi :
  227. case SoundCanvas :
  228. case SoundScape :
  229. MPU_Reset();
  230. break;
  231. case WaveBlaster :
  232. BLASTER_ShutdownWaveBlaster();
  233. MPU_Reset();
  234. BLASTER_RestoreMidiVolume();
  235. break;
  236. case Awe32 :
  237. AWE32_Shutdown();
  238. BLASTER_RestoreMidiVolume();
  239. break;
  240. case ProAudioSpectrum :
  241. case SoundMan16 :
  242. AL_Shutdown();
  243. PAS_RestoreMusicVolume();
  244. break;
  245. case UltraSound :
  246. GUSMIDI_Shutdown();
  247. break;
  248. }
  249. LL_UnlockMemory();
  250. return( status );
  251. }
  252. /*---------------------------------------------------------------------
  253. Function: MUSIC_SetMaxFMMidiChannel
  254. Sets the maximum MIDI channel that FM cards respond to.
  255. ---------------------------------------------------------------------*/
  256. void MUSIC_SetMaxFMMidiChannel
  257. (
  258. int channel
  259. )
  260. {
  261. AL_SetMaxMidiChannel( channel );
  262. }
  263. /*---------------------------------------------------------------------
  264. Function: MUSIC_SetVolume
  265. Sets the volume of music playback.
  266. ---------------------------------------------------------------------*/
  267. void MUSIC_SetVolume
  268. (
  269. int volume
  270. )
  271. {
  272. volume = max( 0, volume );
  273. volume = min( volume, 255 );
  274. if ( MUSIC_SoundDevice != -1 )
  275. {
  276. MIDI_SetVolume( volume );
  277. }
  278. }
  279. /*---------------------------------------------------------------------
  280. Function: MUSIC_SetMidiChannelVolume
  281. Sets the volume of music playback on the specified MIDI channel.
  282. ---------------------------------------------------------------------*/
  283. void MUSIC_SetMidiChannelVolume
  284. (
  285. int channel,
  286. int volume
  287. )
  288. {
  289. MIDI_SetUserChannelVolume( channel, volume );
  290. }
  291. /*---------------------------------------------------------------------
  292. Function: MUSIC_ResetMidiChannelVolumes
  293. Sets the volume of music playback on all MIDI channels to full volume.
  294. ---------------------------------------------------------------------*/
  295. void MUSIC_ResetMidiChannelVolumes
  296. (
  297. void
  298. )
  299. {
  300. MIDI_ResetUserChannelVolume();
  301. }
  302. /*---------------------------------------------------------------------
  303. Function: MUSIC_GetVolume
  304. Returns the volume of music playback.
  305. ---------------------------------------------------------------------*/
  306. int MUSIC_GetVolume
  307. (
  308. void
  309. )
  310. {
  311. if ( MUSIC_SoundDevice == -1 )
  312. {
  313. return( 0 );
  314. }
  315. return( MIDI_GetVolume() );
  316. }
  317. /*---------------------------------------------------------------------
  318. Function: MUSIC_SetLoopFlag
  319. Set whether the music will loop or end when it reaches the end of
  320. the song.
  321. ---------------------------------------------------------------------*/
  322. void MUSIC_SetLoopFlag
  323. (
  324. int loopflag
  325. )
  326. {
  327. MIDI_SetLoopFlag( loopflag );
  328. }
  329. /*---------------------------------------------------------------------
  330. Function: MUSIC_SongPlaying
  331. Returns whether there is a song playing.
  332. ---------------------------------------------------------------------*/
  333. int MUSIC_SongPlaying
  334. (
  335. void
  336. )
  337. {
  338. return( MIDI_SongPlaying() );
  339. }
  340. /*---------------------------------------------------------------------
  341. Function: MUSIC_Continue
  342. Continues playback of a paused song.
  343. ---------------------------------------------------------------------*/
  344. void MUSIC_Continue
  345. (
  346. void
  347. )
  348. {
  349. MIDI_ContinueSong();
  350. }
  351. /*---------------------------------------------------------------------
  352. Function: MUSIC_Pause
  353. Pauses playback of a song.
  354. ---------------------------------------------------------------------*/
  355. void MUSIC_Pause
  356. (
  357. void
  358. )
  359. {
  360. MIDI_PauseSong();
  361. }
  362. /*---------------------------------------------------------------------
  363. Function: MUSIC_StopSong
  364. Stops playback of current song.
  365. ---------------------------------------------------------------------*/
  366. int MUSIC_StopSong
  367. (
  368. void
  369. )
  370. {
  371. MUSIC_StopFade();
  372. MIDI_StopSong();
  373. MUSIC_SetErrorCode( MUSIC_Ok );
  374. return( MUSIC_Ok );
  375. }
  376. /*---------------------------------------------------------------------
  377. Function: MUSIC_PlaySong
  378. Begins playback of MIDI song.
  379. ---------------------------------------------------------------------*/
  380. int MUSIC_PlaySong
  381. (
  382. unsigned char *song,
  383. int loopflag
  384. )
  385. {
  386. int status;
  387. switch( MUSIC_SoundDevice )
  388. {
  389. case SoundBlaster :
  390. case Adlib :
  391. case ProAudioSpectrum :
  392. case SoundMan16 :
  393. case GenMidi :
  394. case SoundCanvas :
  395. case WaveBlaster :
  396. case SoundScape :
  397. case Awe32 :
  398. case UltraSound :
  399. MIDI_StopSong();
  400. status = MIDI_PlaySong( song, loopflag );
  401. if ( status != MIDI_Ok )
  402. {
  403. MUSIC_SetErrorCode( MUSIC_MidiError );
  404. return( MUSIC_Warning );
  405. }
  406. break;
  407. case SoundSource :
  408. case PC :
  409. default :
  410. MUSIC_SetErrorCode( MUSIC_InvalidCard );
  411. return( MUSIC_Warning );
  412. break;
  413. }
  414. return( MUSIC_Ok );
  415. }
  416. /*---------------------------------------------------------------------
  417. Function: MUSIC_SetContext
  418. Sets the song context.
  419. ---------------------------------------------------------------------*/
  420. void MUSIC_SetContext
  421. (
  422. int context
  423. )
  424. {
  425. MIDI_SetContext( context );
  426. }
  427. /*---------------------------------------------------------------------
  428. Function: MUSIC_GetContext
  429. Returns the current song context.
  430. ---------------------------------------------------------------------*/
  431. int MUSIC_GetContext
  432. (
  433. void
  434. )
  435. {
  436. return MIDI_GetContext();
  437. }
  438. /*---------------------------------------------------------------------
  439. Function: MUSIC_SetSongTick
  440. Sets the position of the song pointer.
  441. ---------------------------------------------------------------------*/
  442. void MUSIC_SetSongTick
  443. (
  444. unsigned long PositionInTicks
  445. )
  446. {
  447. MIDI_SetSongTick( PositionInTicks );
  448. }
  449. /*---------------------------------------------------------------------
  450. Function: MUSIC_SetSongTime
  451. Sets the position of the song pointer.
  452. ---------------------------------------------------------------------*/
  453. void MUSIC_SetSongTime
  454. (
  455. unsigned long milliseconds
  456. )
  457. {
  458. MIDI_SetSongTime( milliseconds );
  459. }
  460. /*---------------------------------------------------------------------
  461. Function: MUSIC_SetSongPosition
  462. Sets the position of the song pointer.
  463. ---------------------------------------------------------------------*/
  464. void MUSIC_SetSongPosition
  465. (
  466. int measure,
  467. int beat,
  468. int tick
  469. )
  470. {
  471. MIDI_SetSongPosition( measure, beat, tick );
  472. }
  473. /*---------------------------------------------------------------------
  474. Function: MUSIC_GetSongPosition
  475. Returns the position of the song pointer.
  476. ---------------------------------------------------------------------*/
  477. void MUSIC_GetSongPosition
  478. (
  479. songposition *pos
  480. )
  481. {
  482. MIDI_GetSongPosition( pos );
  483. }
  484. /*---------------------------------------------------------------------
  485. Function: MUSIC_GetSongLength
  486. Returns the length of the song.
  487. ---------------------------------------------------------------------*/
  488. void MUSIC_GetSongLength
  489. (
  490. songposition *pos
  491. )
  492. {
  493. MIDI_GetSongLength( pos );
  494. }
  495. int MUSIC_InitAWE32
  496. (
  497. midifuncs *Funcs
  498. )
  499. {
  500. int status;
  501. status = AWE32_Init();
  502. if ( status != AWE32_Ok )
  503. {
  504. MUSIC_SetErrorCode( MUSIC_SoundCardError );
  505. return( MUSIC_Error );
  506. }
  507. Funcs->NoteOff = AWE32_NoteOff;
  508. Funcs->NoteOn = AWE32_NoteOn;
  509. Funcs->PolyAftertouch = AWE32_PolyAftertouch;
  510. Funcs->ControlChange = AWE32_ControlChange;
  511. Funcs->ProgramChange = AWE32_ProgramChange;
  512. Funcs->ChannelAftertouch = AWE32_ChannelAftertouch;
  513. Funcs->PitchBend = AWE32_PitchBend;
  514. Funcs->ReleasePatches = NULL;
  515. Funcs->LoadPatch = NULL;
  516. Funcs->SetVolume = NULL;
  517. Funcs->GetVolume = NULL;
  518. if ( BLASTER_CardHasMixer() )
  519. {
  520. BLASTER_SaveMidiVolume();
  521. Funcs->SetVolume = BLASTER_SetMidiVolume;
  522. Funcs->GetVolume = BLASTER_GetMidiVolume;
  523. }
  524. status = MUSIC_Ok;
  525. MIDI_SetMidiFuncs( Funcs );
  526. return( status );
  527. }
  528. int MUSIC_InitFM
  529. (
  530. int card,
  531. midifuncs *Funcs
  532. )
  533. {
  534. int status;
  535. int passtatus;
  536. status = MIDI_Ok;
  537. if ( !AL_DetectFM() )
  538. {
  539. MUSIC_SetErrorCode( MUSIC_FMNotDetected );
  540. return( MUSIC_Error );
  541. }
  542. // Init the fm routines
  543. AL_Init( card );
  544. Funcs->NoteOff = AL_NoteOff;
  545. Funcs->NoteOn = AL_NoteOn;
  546. Funcs->PolyAftertouch = NULL;
  547. Funcs->ControlChange = AL_ControlChange;
  548. Funcs->ProgramChange = AL_ProgramChange;
  549. Funcs->ChannelAftertouch = NULL;
  550. Funcs->PitchBend = AL_SetPitchBend;
  551. Funcs->ReleasePatches = NULL;
  552. Funcs->LoadPatch = NULL;
  553. Funcs->SetVolume = NULL;
  554. Funcs->GetVolume = NULL;
  555. switch( card )
  556. {
  557. case SoundBlaster :
  558. if ( BLASTER_CardHasMixer() )
  559. {
  560. BLASTER_SaveMidiVolume();
  561. Funcs->SetVolume = BLASTER_SetMidiVolume;
  562. Funcs->GetVolume = BLASTER_GetMidiVolume;
  563. }
  564. else
  565. {
  566. Funcs->SetVolume = NULL;
  567. Funcs->GetVolume = NULL;
  568. }
  569. break;
  570. case Adlib :
  571. Funcs->SetVolume = NULL;
  572. Funcs->GetVolume = NULL;
  573. break;
  574. case ProAudioSpectrum :
  575. case SoundMan16 :
  576. Funcs->SetVolume = NULL;
  577. Funcs->GetVolume = NULL;
  578. passtatus = PAS_SaveMusicVolume();
  579. if ( passtatus == PAS_Ok )
  580. {
  581. Funcs->SetVolume = PAS_SetFMVolume;
  582. Funcs->GetVolume = PAS_GetFMVolume;
  583. }
  584. break;
  585. }
  586. MIDI_SetMidiFuncs( Funcs );
  587. return( status );
  588. }
  589. int MUSIC_InitMidi
  590. (
  591. int card,
  592. midifuncs *Funcs,
  593. int Address
  594. )
  595. {
  596. int status;
  597. status = MUSIC_Ok;
  598. if ( ( card == WaveBlaster ) || ( card == SoundCanvas ) ||
  599. ( card == GenMidi ) )
  600. {
  601. // Setup WaveBlaster Daughterboard clone
  602. // (ie. SoundCanvas DB, TurtleBeach Rio)
  603. BLASTER_SetupWaveBlaster();
  604. }
  605. if ( card == SoundScape )
  606. {
  607. Address = SOUNDSCAPE_GetMIDIPort();
  608. if ( Address < SOUNDSCAPE_Ok )
  609. {
  610. MUSIC_SetErrorCode( MUSIC_SoundCardError );
  611. return( MUSIC_Error );
  612. }
  613. }
  614. if ( MPU_Init( Address ) != MPU_Ok )
  615. {
  616. MUSIC_SetErrorCode( MUSIC_MPU401Error );
  617. return( MUSIC_Error );
  618. }
  619. Funcs->NoteOff = MPU_NoteOff;
  620. Funcs->NoteOn = MPU_NoteOn;
  621. Funcs->PolyAftertouch = MPU_PolyAftertouch;
  622. Funcs->ControlChange = MPU_ControlChange;
  623. Funcs->ProgramChange = MPU_ProgramChange;
  624. Funcs->ChannelAftertouch = MPU_ChannelAftertouch;
  625. Funcs->PitchBend = MPU_PitchBend;
  626. Funcs->ReleasePatches = NULL;
  627. Funcs->LoadPatch = NULL;
  628. Funcs->SetVolume = NULL;
  629. Funcs->GetVolume = NULL;
  630. if ( card == WaveBlaster )
  631. {
  632. if ( BLASTER_CardHasMixer() )
  633. {
  634. BLASTER_SaveMidiVolume();
  635. Funcs->SetVolume = BLASTER_SetMidiVolume;
  636. Funcs->GetVolume = BLASTER_GetMidiVolume;
  637. }
  638. }
  639. MIDI_SetMidiFuncs( Funcs );
  640. return( status );
  641. }
  642. int MUSIC_InitGUS
  643. (
  644. midifuncs *Funcs
  645. )
  646. {
  647. int status;
  648. status = MUSIC_Ok;
  649. if ( GUSMIDI_Init() != GUS_Ok )
  650. {
  651. MUSIC_SetErrorCode( MUSIC_SoundCardError );
  652. return( MUSIC_Error );
  653. }
  654. Funcs->NoteOff = GUSMIDI_NoteOff;
  655. Funcs->NoteOn = GUSMIDI_NoteOn;
  656. Funcs->PolyAftertouch = NULL;
  657. Funcs->ControlChange = GUSMIDI_ControlChange;
  658. Funcs->ProgramChange = GUSMIDI_ProgramChange;
  659. Funcs->ChannelAftertouch = NULL;
  660. Funcs->PitchBend = GUSMIDI_PitchBend;
  661. Funcs->ReleasePatches = NULL;//GUSMIDI_ReleasePatches;
  662. Funcs->LoadPatch = NULL;//GUSMIDI_LoadPatch;
  663. Funcs->SetVolume = GUSMIDI_SetVolume;
  664. Funcs->GetVolume = GUSMIDI_GetVolume;
  665. MIDI_SetMidiFuncs( Funcs );
  666. return( status );
  667. }
  668. /*---------------------------------------------------------------------
  669. Function: MUSIC_FadeRoutine
  670. Fades music volume from current level to another over a specified
  671. period of time.
  672. ---------------------------------------------------------------------*/
  673. static void MUSIC_FadeRoutine
  674. (
  675. task *Task
  676. )
  677. {
  678. int volume;
  679. MUSIC_CurrentFadeVolume += MUSIC_FadeRate;
  680. if ( MUSIC_FadeLength == 0 )
  681. {
  682. MIDI_SetVolume( MUSIC_EndingFadeVolume );
  683. TS_Terminate( Task );
  684. MUSIC_FadeTask = NULL;
  685. }
  686. else
  687. {
  688. MUSIC_FadeLength--;
  689. // if ( ( MUSIC_SoundDevice == GenMidi ) &&
  690. // ( ( MUSIC_FadeLength % 12 ) != 0 ) )
  691. // {
  692. // return;
  693. // }
  694. volume = MUSIC_CurrentFadeVolume >> 7;
  695. if ( MUSIC_LastFadeVolume != volume )
  696. {
  697. MUSIC_LastFadeVolume = volume;
  698. MIDI_SetVolume( volume );
  699. }
  700. }
  701. }
  702. /*---------------------------------------------------------------------
  703. Function: MUSIC_FadeVolume
  704. Fades music volume from current level to another over a specified
  705. period of time.
  706. ---------------------------------------------------------------------*/
  707. int MUSIC_FadeVolume
  708. (
  709. int tovolume,
  710. int milliseconds
  711. )
  712. {
  713. int fromvolume;
  714. if ( ( MUSIC_SoundDevice == ProAudioSpectrum ) ||
  715. ( MUSIC_SoundDevice == SoundMan16 ) ||
  716. ( MUSIC_SoundDevice == GenMidi ) ||
  717. ( MUSIC_SoundDevice == SoundScape ) ||
  718. ( MUSIC_SoundDevice == SoundCanvas ) )
  719. {
  720. MIDI_SetVolume( tovolume );
  721. return( MUSIC_Ok );
  722. }
  723. if ( MUSIC_FadeTask != NULL )
  724. {
  725. MUSIC_StopFade();
  726. }
  727. tovolume = max( 0, tovolume );
  728. tovolume = min( 255, tovolume );
  729. fromvolume = MUSIC_GetVolume();
  730. MUSIC_FadeLength = milliseconds / 25;
  731. MUSIC_FadeRate = ( ( tovolume - fromvolume ) << 7 ) / MUSIC_FadeLength;
  732. MUSIC_LastFadeVolume = fromvolume;
  733. MUSIC_CurrentFadeVolume = fromvolume << 7;
  734. MUSIC_EndingFadeVolume = tovolume;
  735. MUSIC_FadeTask = TS_ScheduleTask( MUSIC_FadeRoutine, 40, 1, NULL );
  736. if ( MUSIC_FadeTask == NULL )
  737. {
  738. MUSIC_SetErrorCode( MUSIC_TaskManError );
  739. return( MUSIC_Warning );
  740. }
  741. TS_Dispatch();
  742. return( MUSIC_Ok );
  743. }
  744. /*---------------------------------------------------------------------
  745. Function: MUSIC_FadeActive
  746. Returns whether the fade routine is active.
  747. ---------------------------------------------------------------------*/
  748. int MUSIC_FadeActive
  749. (
  750. void
  751. )
  752. {
  753. return( MUSIC_FadeTask != NULL );
  754. }
  755. /*---------------------------------------------------------------------
  756. Function: MUSIC_StopFade
  757. Stops fading the music.
  758. ---------------------------------------------------------------------*/
  759. void MUSIC_StopFade
  760. (
  761. void
  762. )
  763. {
  764. if ( MUSIC_FadeTask != NULL )
  765. {
  766. TS_Terminate( MUSIC_FadeTask );
  767. MUSIC_FadeTask = NULL;
  768. }
  769. }
  770. /*---------------------------------------------------------------------
  771. Function: MUSIC_RerouteMidiChannel
  772. Sets callback function to reroute MIDI commands from specified
  773. function.
  774. ---------------------------------------------------------------------*/
  775. void MUSIC_RerouteMidiChannel
  776. (
  777. int channel,
  778. int cdecl ( *function )( int event, int c1, int c2 )
  779. )
  780. {
  781. MIDI_RerouteMidiChannel( channel, function );
  782. }
  783. /*---------------------------------------------------------------------
  784. Function: MUSIC_RegisterTimbreBank
  785. Halts playback of all sounds.
  786. ---------------------------------------------------------------------*/
  787. void MUSIC_RegisterTimbreBank
  788. (
  789. unsigned char *timbres
  790. )
  791. {
  792. AL_RegisterTimbreBank( timbres );
  793. }