GUSWAVE.C 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  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. file: GUSWAVE.C
  17. author: James R. Dose
  18. date: March 23, 1994
  19. Digitized sound playback routines for the Gravis Ultrasound.
  20. (c) Copyright 1994 James R. Dose. All Rights Reserved.
  21. **********************************************************************/
  22. #include <stdlib.h>
  23. #include <conio.h>
  24. #include <dos.h>
  25. #include <stdio.h>
  26. #include <io.h>
  27. #include <string.h>
  28. #include "debugio.h"
  29. #include "interrup.h"
  30. #include "ll_man.h"
  31. #include "pitch.h"
  32. #include "user.h"
  33. #include "multivoc.h"
  34. #include "_guswave.h"
  35. #include "newgf1.h"
  36. #include "gusmidi.h"
  37. #include "guswave.h"
  38. #define ATR_INDEX 0x3c0
  39. #define STATUS_REGISTER_1 0x3da
  40. #define SetBorderColor(color) \
  41. { \
  42. inp (STATUS_REGISTER_1); \
  43. outp (ATR_INDEX,0x31); \
  44. outp (ATR_INDEX,color); \
  45. }
  46. static const int GUSWAVE_PanTable[ 32 ] =
  47. {
  48. 8, 9, 10, 11, 11, 12, 13, 14,
  49. 15, 14, 13, 12, 11, 10, 9, 8,
  50. 7, 6, 5, 4, 4, 3, 2, 1,
  51. 0, 1, 2, 3, 4, 5, 6, 7
  52. };
  53. static voicelist VoiceList;
  54. static voicelist VoicePool;
  55. static voicestatus VoiceStatus[ MAX_VOICES ];
  56. //static
  57. VoiceNode GUSWAVE_Voices[ VOICES ];
  58. static int GUSWAVE_VoiceHandle = GUSWAVE_MinVoiceHandle;
  59. static int GUSWAVE_MaxVoices = VOICES;
  60. //static
  61. int GUSWAVE_Installed = FALSE;
  62. static void ( *GUSWAVE_CallBackFunc )( unsigned long ) = NULL;
  63. // current volume for dig audio - from 0 to 4095
  64. static int GUSWAVE_Volume = MAX_VOLUME;
  65. static int GUSWAVE_SwapLeftRight = FALSE;
  66. static int GUS_Debug = FALSE;
  67. extern int GUSMIDI_Installed;
  68. int GUSWAVE_ErrorCode = GUSWAVE_Ok;
  69. #define GUSWAVE_SetErrorCode( status ) \
  70. GUSWAVE_ErrorCode = ( status );
  71. /*---------------------------------------------------------------------
  72. Function: GUSWAVE_ErrorString
  73. Returns a pointer to the error message associated with an error
  74. number. A -1 returns a pointer the current error.
  75. ---------------------------------------------------------------------*/
  76. char *GUSWAVE_ErrorString
  77. (
  78. int ErrorNumber
  79. )
  80. {
  81. char *ErrorString;
  82. switch( ErrorNumber )
  83. {
  84. case GUSWAVE_Warning :
  85. case GUSWAVE_Error :
  86. ErrorString = GUSWAVE_ErrorString( GUSWAVE_ErrorCode );
  87. break;
  88. case GUSWAVE_Ok :
  89. ErrorString = "GUSWAVE ok.";
  90. break;
  91. case GUSWAVE_GUSError :
  92. ErrorString = GUS_ErrorString( GUS_Error );
  93. break;
  94. case GUSWAVE_NotInstalled :
  95. ErrorString = "GUSWAVE not installed.";
  96. break;
  97. case GUSWAVE_NoVoices :
  98. ErrorString = "No free voices available to GUSWAVE.";
  99. break;
  100. case GUSWAVE_UltraNoMem :
  101. ErrorString = "Not enough Ultrasound memory available for GUSWAVE.";
  102. break;
  103. case GUSWAVE_UltraNoMemMIDI :
  104. ErrorString = "Not enough Ultrasound memory available for GUSWAVE. "
  105. "Try initializing Sound FX before Music.";
  106. break;
  107. case GUSWAVE_VoiceNotFound :
  108. ErrorString = "No voice with matching handle found.";
  109. break;
  110. case GUSWAVE_InvalidVOCFile :
  111. ErrorString = "Invalid VOC file passed in to GUSWAVE.";
  112. break;
  113. case GUSWAVE_InvalidWAVFile :
  114. ErrorString = "Invalid WAV file passed in to GUSWAVE.";
  115. break;
  116. default :
  117. ErrorString = "Unknown GUSWAVE error code.";
  118. break;
  119. }
  120. return( ErrorString );
  121. }
  122. /*---------------------------------------------------------------------
  123. Function: GUSWAVE_CallBack
  124. GF1 callback service routine.
  125. ---------------------------------------------------------------------*/
  126. char GUS_Silence8[ 1024 ] = //256 ] =
  127. {
  128. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  129. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  130. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  131. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  132. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  133. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  134. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  135. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  136. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  137. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  138. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  139. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  140. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  141. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  142. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  143. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  144. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  145. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  146. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  147. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  148. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  149. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  150. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  151. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  152. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  153. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  154. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  155. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  156. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  157. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  158. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  159. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  160. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  161. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  162. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  163. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  164. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  165. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  166. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  167. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  168. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  169. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  170. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  171. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  172. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  173. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  174. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  175. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  176. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  177. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  178. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  179. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  180. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  181. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  182. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  183. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  184. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  185. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  186. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  187. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  188. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  189. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  190. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  191. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80
  192. };
  193. //unsigned short GUS_Silence16[ 128 ] =
  194. unsigned short GUS_Silence16[ 512 ] =
  195. {
  196. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  197. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  198. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  199. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  200. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  201. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  202. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  203. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  204. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  205. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  206. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  207. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  208. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  209. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  210. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  211. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  212. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  213. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  214. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  215. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  216. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  217. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  218. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  219. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  220. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  221. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  222. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  223. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  224. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  225. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  226. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  227. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  228. };
  229. static int LOADDS GUSWAVE_CallBack
  230. (
  231. int reason,
  232. int voice,
  233. unsigned char **buf,
  234. unsigned long *size
  235. )
  236. {
  237. VoiceNode *Voice;
  238. playbackstatus status;
  239. // this function is called from an interrupt
  240. // remember not to make any DOS or BIOS calls from here
  241. // also don't call any C library functions unless you are sure that
  242. // they are reentrant
  243. // restore our DS register
  244. if ( VoiceStatus[ voice ].playing == FALSE )
  245. {
  246. return( DIG_DONE );
  247. }
  248. if ( reason == DIG_MORE_DATA )
  249. {
  250. // SetBorderColor(16);
  251. Voice = VoiceStatus[ voice ].Voice;
  252. if ( ( Voice != NULL ) && ( Voice->Playing ) )
  253. /*
  254. {
  255. *buf = ( unsigned char * )GUS_Silence16;
  256. *size = 1024;
  257. SetBorderColor(0);
  258. return( DIG_MORE_DATA );
  259. }
  260. */
  261. {
  262. status = Voice->GetSound( Voice );
  263. if ( status != SoundDone )
  264. {
  265. if ( ( Voice->sound == NULL ) || ( status == NoMoreData ) )
  266. {
  267. if ( Voice->bits == 8 )
  268. {
  269. *buf = GUS_Silence8;
  270. }
  271. else
  272. {
  273. *buf = ( unsigned char * )GUS_Silence16;
  274. }
  275. *size = 256;
  276. }
  277. else
  278. {
  279. *buf = Voice->sound;
  280. *size = Voice->length;
  281. }
  282. return( DIG_MORE_DATA );
  283. }
  284. }
  285. // SetBorderColor(16);
  286. return( DIG_DONE );
  287. }
  288. if ( reason == DIG_DONE )
  289. {
  290. Voice = VoiceStatus[ voice ].Voice;
  291. VoiceStatus[ voice ].playing = FALSE;
  292. if ( Voice != NULL )
  293. {
  294. Voice->Active = FALSE;
  295. Voice->Playing = FALSE;
  296. // I'm commenting this out because a -1 could cause a crash if it
  297. // is sent to the GF1 code. This shouldn't be necessary since
  298. // Active should be false when GF1voice is -1, but this is just
  299. // a precaution. Adjust the pan on the wrong voice is a lot
  300. // more pleasant than a crash!
  301. // Voice->GF1voice = -1;
  302. LL_Remove( VoiceNode, &VoiceList, Voice );
  303. LL_AddToTail( VoiceNode, &VoicePool, Voice );
  304. }
  305. if ( GUSWAVE_CallBackFunc )
  306. {
  307. GUSWAVE_CallBackFunc( Voice->callbackval );
  308. }
  309. }
  310. return( DIG_DONE );
  311. }
  312. /*---------------------------------------------------------------------
  313. Function: GUSWAVE_DebugCallBack
  314. GF1 callback service routine with debugging info.
  315. ---------------------------------------------------------------------*/
  316. static int LOADDS GUSWAVE_DebugCallBack
  317. (
  318. int reason,
  319. int voice,
  320. unsigned char **buf,
  321. unsigned long *size
  322. )
  323. {
  324. VoiceNode *Voice;
  325. // this function is called from an interrupt
  326. // remember not to make any DOS or BIOS calls from here
  327. // also don't call any C library functions unless you are sure that
  328. // they are reentrant
  329. // restore our DS register
  330. if ( VoiceStatus[ voice ].playing == FALSE )
  331. {
  332. // DB_printf( "GUS Voice %d not playing.\n", voice );
  333. DB_printf( "GUS Voice " );
  334. DB_PrintNum( voice );
  335. DB_printf( " not playing.\n" );
  336. return( DIG_DONE );
  337. }
  338. if ( reason == DIG_MORE_DATA )
  339. {
  340. Voice = VoiceStatus[ voice ].Voice;
  341. // DB_printf( "Voice %d : More data -- ", Voice );
  342. DB_printf( "Voice " );
  343. DB_PrintNum( voice );
  344. DB_printf( " : More data -- " );
  345. if ( Voice != NULL )
  346. {
  347. if ( Voice->Playing )
  348. {
  349. GUSWAVE_GetNextVOCBlock( Voice );
  350. if ( Voice->Playing )
  351. {
  352. // DB_printf( "More data -- size = %u blocklength = %u\n",
  353. // Voice->length, Voice->BlockLength );
  354. DB_printf( "More data -- size = " );
  355. DB_PrintNum( Voice->length );
  356. DB_printf( " blocklength = " );
  357. DB_PrintNum( Voice->BlockLength );
  358. DB_printf( "\n" );
  359. *buf = Voice->sound;
  360. *size = Voice->length;
  361. return( DIG_MORE_DATA );
  362. }
  363. else
  364. {
  365. DB_printf( "Voice done.\n" );
  366. }
  367. }
  368. else
  369. {
  370. DB_printf( "Voice not active.\n" );
  371. }
  372. }
  373. else
  374. {
  375. DB_printf( " NULL Voice\n" );
  376. }
  377. return( DIG_DONE );
  378. }
  379. if ( reason == DIG_DONE )
  380. {
  381. VoiceStatus[ voice ].playing = FALSE;
  382. Voice = VoiceStatus[ voice ].Voice;
  383. // DB_printf( "Voice %d : Done -- ", Voice );
  384. DB_printf( "Voice " );
  385. DB_PrintNum( voice );
  386. DB_printf( " : Done -- " );
  387. if ( Voice != NULL )
  388. {
  389. DB_printf( "Ok\n" );
  390. Voice->Active = FALSE;
  391. Voice->Playing = FALSE;
  392. // I'm commenting this out because a -1 could cause a crash if it
  393. // is sent to the GF1 code. This shouldn't be necessary since
  394. // Active should be false when GF1voice is -1, but this is just
  395. // a precaution. Adjust the pan on the wrong voice is a lot
  396. // more pleasant than a crash!
  397. // Voice->GF1voice = -1;
  398. LL_Remove( VoiceNode, &VoiceList, Voice );
  399. LL_AddToTail( VoiceNode, &VoicePool, Voice );
  400. }
  401. else
  402. {
  403. DB_printf( "Null voice\n" );
  404. }
  405. if ( GUSWAVE_CallBackFunc )
  406. {
  407. GUSWAVE_CallBackFunc( Voice->callbackval );
  408. }
  409. }
  410. return( DIG_DONE );
  411. }
  412. /*---------------------------------------------------------------------
  413. Function: GUSWAVE_GetVoice
  414. Locates the voice with the specified handle.
  415. ---------------------------------------------------------------------*/
  416. static VoiceNode *GUSWAVE_GetVoice
  417. (
  418. int handle
  419. )
  420. {
  421. VoiceNode *voice;
  422. unsigned flags;
  423. flags = DisableInterrupts();
  424. voice = VoiceList.start;
  425. while( voice != NULL )
  426. {
  427. if ( handle == voice->handle )
  428. {
  429. break;
  430. }
  431. voice = voice->next;
  432. }
  433. RestoreInterrupts( flags );
  434. if ( voice == NULL )
  435. {
  436. GUSWAVE_SetErrorCode( GUSWAVE_VoiceNotFound );
  437. }
  438. return( voice );
  439. }
  440. /*---------------------------------------------------------------------
  441. Function: GUSWAVE_VoicePlaying
  442. Checks if the voice associated with the specified handle is
  443. playing.
  444. ---------------------------------------------------------------------*/
  445. int GUSWAVE_VoicePlaying
  446. (
  447. int handle
  448. )
  449. {
  450. VoiceNode *voice;
  451. voice = GUSWAVE_GetVoice( handle );
  452. if ( voice != NULL )
  453. {
  454. return( voice->Active );
  455. }
  456. return( FALSE );
  457. }
  458. /*---------------------------------------------------------------------
  459. Function: GUSWAVE_VoicesPlaying
  460. Determines the number of currently active voices.
  461. ---------------------------------------------------------------------*/
  462. int GUSWAVE_VoicesPlaying
  463. (
  464. void
  465. )
  466. {
  467. int index;
  468. int NumVoices = 0;
  469. unsigned flags;
  470. flags = DisableInterrupts();
  471. for( index = 0; index < GUSWAVE_MaxVoices; index++ )
  472. {
  473. if ( GUSWAVE_Voices[ index ].Active )
  474. {
  475. NumVoices++;
  476. }
  477. }
  478. RestoreInterrupts( flags );
  479. if ( GUS_Debug )
  480. {
  481. DB_printf( "Number of voices = %d.\n", NumVoices );
  482. }
  483. return( NumVoices );
  484. }
  485. /*---------------------------------------------------------------------
  486. Function: GUSWAVE_Kill
  487. Stops output of the voice associated with the specified handle.
  488. ---------------------------------------------------------------------*/
  489. int GUSWAVE_Kill
  490. (
  491. int handle
  492. )
  493. {
  494. VoiceNode *voice;
  495. unsigned flags;
  496. flags = DisableInterrupts();
  497. voice = GUSWAVE_GetVoice( handle );
  498. if ( voice == NULL )
  499. {
  500. RestoreInterrupts( flags );
  501. GUSWAVE_SetErrorCode( GUSWAVE_VoiceNotFound );
  502. if ( GUS_Debug )
  503. {
  504. DB_printf( "Could not find voice to kill.\n" );
  505. }
  506. return( GUSWAVE_Warning );
  507. }
  508. RestoreInterrupts( flags );
  509. if ( !GUS_Debug )
  510. {
  511. if ( voice->Active )
  512. {
  513. gf1_stop_digital( voice->GF1voice );
  514. }
  515. }
  516. else
  517. {
  518. DB_printf( "Kill - GUS Voice %d ", voice->GF1voice );
  519. if ( voice->Active )
  520. {
  521. DB_printf( "active\n" );
  522. gf1_stop_digital( voice->GF1voice );
  523. }
  524. else
  525. {
  526. DB_printf( "inactive\n" );
  527. }
  528. }
  529. // RestoreInterrupts( flags );
  530. return( GUSWAVE_Ok );
  531. }
  532. /*---------------------------------------------------------------------
  533. Function: GUSWAVE_KillAllVoices
  534. Stops output of all currently active voices.
  535. ---------------------------------------------------------------------*/
  536. int GUSWAVE_KillAllVoices
  537. (
  538. void
  539. )
  540. {
  541. int i;
  542. unsigned flags;
  543. if ( !GUSWAVE_Installed )
  544. {
  545. return( GUSWAVE_Ok );
  546. }
  547. if ( GUS_Debug )
  548. {
  549. DB_printf( "Kill All Voices\n" );
  550. }
  551. flags = DisableInterrupts();
  552. // Remove all the voices from the list
  553. for( i = 0; i < GUSWAVE_MaxVoices; i++ )
  554. {
  555. if ( GUSWAVE_Voices[ i ].Active )
  556. {
  557. // GUSWAVE_Kill( GUSWAVE_Voices[ i ].handle );
  558. gf1_stop_digital( GUSWAVE_Voices[ i ].GF1voice );
  559. }
  560. }
  561. for( i = 0; i < MAX_VOICES; i++ )
  562. {
  563. VoiceStatus[ i ].playing = FALSE;
  564. VoiceStatus[ i ].Voice = NULL;
  565. }
  566. VoicePool.start = NULL;
  567. VoicePool.end = NULL;
  568. VoiceList.start = NULL;
  569. VoiceList.end = NULL;
  570. for( i = 0; i < GUSWAVE_MaxVoices; i++ )
  571. {
  572. GUSWAVE_Voices[ i ].Active = FALSE;
  573. if ( GUSWAVE_Voices[ i ].mem != NULL )
  574. {
  575. LL_AddToTail( VoiceNode, &VoicePool, &GUSWAVE_Voices[ i ] );
  576. }
  577. }
  578. RestoreInterrupts( flags );
  579. return( GUSWAVE_Ok );
  580. }
  581. /*---------------------------------------------------------------------
  582. Function: GUSWAVE_SetPitch
  583. Sets the pitch for the voice associated with the specified handle.
  584. ---------------------------------------------------------------------*/
  585. int GUSWAVE_SetPitch
  586. (
  587. int handle,
  588. int pitchoffset
  589. )
  590. {
  591. VoiceNode *voice;
  592. unsigned flags;
  593. flags = DisableInterrupts();
  594. voice = GUSWAVE_GetVoice( handle );
  595. if ( voice == NULL )
  596. {
  597. RestoreInterrupts( flags );
  598. GUSWAVE_SetErrorCode( GUSWAVE_VoiceNotFound );
  599. return( GUSWAVE_Warning );
  600. }
  601. if ( voice->Active )
  602. {
  603. voice->PitchScale = PITCH_GetScale( pitchoffset );
  604. voice->RateScale = ( voice->SamplingRate * voice->PitchScale ) >> 16;
  605. gf1_dig_set_freq( voice->GF1voice, voice->RateScale );
  606. }
  607. RestoreInterrupts( flags );
  608. return( GUSWAVE_Ok );
  609. }
  610. /*---------------------------------------------------------------------
  611. Function: GUSWAVE_SetPan3D
  612. Sets the pan position of the voice with the specified handle.
  613. ---------------------------------------------------------------------*/
  614. int GUSWAVE_SetPan3D
  615. (
  616. int handle,
  617. int angle,
  618. int distance
  619. )
  620. {
  621. VoiceNode *voice;
  622. int pan;
  623. unsigned flags;
  624. flags = DisableInterrupts();
  625. voice = GUSWAVE_GetVoice( handle );
  626. if ( voice == NULL )
  627. {
  628. RestoreInterrupts( flags );
  629. GUSWAVE_SetErrorCode( GUSWAVE_VoiceNotFound );
  630. return( GUSWAVE_Warning );
  631. }
  632. if ( voice->Active )
  633. {
  634. angle &= 31;
  635. pan = GUSWAVE_PanTable[ angle ];
  636. if ( GUSWAVE_SwapLeftRight )
  637. {
  638. pan = 15 - pan;
  639. }
  640. distance = max( 0, distance );
  641. distance = min( 255, distance );
  642. voice->Volume = 255 - distance;
  643. voice->Pan = pan;
  644. gf1_dig_set_pan( voice->GF1voice, pan );
  645. gf1_dig_set_vol( voice->GF1voice, GUSWAVE_Volume - distance * 4 );
  646. }
  647. RestoreInterrupts( flags );
  648. return( GUSWAVE_Ok );
  649. }
  650. /*---------------------------------------------------------------------
  651. Function: GUSWAVE_SetVolume
  652. Sets the total volume of the digitized sounds.
  653. ---------------------------------------------------------------------*/
  654. void GUSWAVE_SetVolume
  655. (
  656. int volume
  657. )
  658. {
  659. int i;
  660. volume = max( 0, volume );
  661. volume = min( 255, volume );
  662. GUSWAVE_Volume = MAX_VOLUME - ( 255 - volume ) * 4;
  663. for( i = 0; i < GUSWAVE_MaxVoices; i++ )
  664. {
  665. if ( GUSWAVE_Voices[ i ].Active )
  666. {
  667. gf1_dig_set_vol( GUSWAVE_Voices[ i ].GF1voice,
  668. GUSWAVE_Volume - ( 255 - GUSWAVE_Voices[ i ].Volume ) * 4 );
  669. }
  670. }
  671. }
  672. /*---------------------------------------------------------------------
  673. Function: GUSWAVE_GetVolume
  674. Returns the total volume of the digitized sounds.
  675. ---------------------------------------------------------------------*/
  676. int GUSWAVE_GetVolume
  677. (
  678. void
  679. )
  680. {
  681. return( 255 - ( ( MAX_VOLUME - GUSWAVE_Volume ) / 4 ) );
  682. }
  683. /*---------------------------------------------------------------------
  684. Function: GUSWAVE_AllocVoice
  685. Retrieve an inactive or lower priority voice for output.
  686. ---------------------------------------------------------------------*/
  687. static VoiceNode *GUSWAVE_AllocVoice
  688. (
  689. int priority
  690. )
  691. {
  692. VoiceNode *voice;
  693. VoiceNode *node;
  694. unsigned flags;
  695. // If we don't have any free voices, check if we have a higher
  696. // priority than one that is playing.
  697. if ( GUSWAVE_VoicesPlaying() >= GUSWAVE_MaxVoices )
  698. {
  699. flags = DisableInterrupts();
  700. node = VoiceList.start;
  701. voice = node;
  702. while( node != NULL )
  703. {
  704. if ( node->priority < voice->priority )
  705. {
  706. voice = node;
  707. }
  708. node = node->next;
  709. }
  710. RestoreInterrupts( flags );
  711. if ( priority >= voice->priority )
  712. {
  713. GUSWAVE_Kill( voice->handle );
  714. }
  715. }
  716. // Check if any voices are in the voice pool
  717. flags = DisableInterrupts();
  718. voice = VoicePool.start;
  719. if ( voice != NULL )
  720. {
  721. LL_Remove( VoiceNode, &VoicePool, voice );
  722. }
  723. RestoreInterrupts( flags );
  724. if ( voice != NULL )
  725. {
  726. do
  727. {
  728. GUSWAVE_VoiceHandle++;
  729. if ( GUSWAVE_VoiceHandle < GUSWAVE_MinVoiceHandle )
  730. {
  731. GUSWAVE_VoiceHandle = GUSWAVE_MinVoiceHandle;
  732. }
  733. }
  734. while( GUSWAVE_VoicePlaying( GUSWAVE_VoiceHandle ) );
  735. voice->handle = GUSWAVE_VoiceHandle;
  736. }
  737. return( voice );
  738. }
  739. /*---------------------------------------------------------------------
  740. Function: GUSWAVE_VoiceAvailable
  741. Checks if a voice can be play at the specified priority.
  742. ---------------------------------------------------------------------*/
  743. int GUSWAVE_VoiceAvailable
  744. (
  745. int priority
  746. )
  747. {
  748. VoiceNode *voice;
  749. VoiceNode *node;
  750. unsigned flags;
  751. if ( GUSWAVE_VoicesPlaying() < GUSWAVE_MaxVoices )
  752. {
  753. return( TRUE );
  754. }
  755. flags = DisableInterrupts();
  756. node = VoiceList.start;
  757. voice = node;
  758. while( node != NULL )
  759. {
  760. if ( node->priority < voice->priority )
  761. {
  762. voice = node;
  763. }
  764. node = node->next;
  765. }
  766. RestoreInterrupts( flags );
  767. if ( priority >= voice->priority )
  768. {
  769. return( TRUE );
  770. }
  771. return( FALSE );
  772. }
  773. /*---------------------------------------------------------------------
  774. Function: GUSWAVE_GetNextVOCBlock
  775. Interperate the information of a VOC format sound file.
  776. ---------------------------------------------------------------------*/
  777. playbackstatus GUSWAVE_GetNextVOCBlock
  778. (
  779. VoiceNode *voice
  780. )
  781. {
  782. unsigned char *ptr;
  783. int blocktype;
  784. int lastblocktype;
  785. unsigned long blocklength;
  786. unsigned long samplespeed;
  787. unsigned int tc;
  788. int packtype;
  789. int voicemode;
  790. int done;
  791. unsigned BitsPerSample;
  792. unsigned Channels;
  793. unsigned Format;
  794. if ( voice->BlockLength > 0 )
  795. {
  796. voice->sound += MAX_BLOCK_LENGTH;
  797. voice->length = min( voice->BlockLength, MAX_BLOCK_LENGTH );
  798. voice->BlockLength -= voice->length;
  799. return( KeepPlaying );
  800. }
  801. ptr = ( unsigned char * )voice->NextBlock;
  802. voice->Playing = TRUE;
  803. voicemode = 0;
  804. lastblocktype = 0;
  805. packtype = 0;
  806. done = FALSE;
  807. while( !done )
  808. {
  809. // Stop playing if we get a NULL pointer
  810. if ( ptr == NULL )
  811. {
  812. voice->Playing = FALSE;
  813. done = TRUE;
  814. break;
  815. }
  816. blocktype = ( int )*ptr;
  817. blocklength = ( *( unsigned long * )( ptr + 1 ) ) & 0x00ffffff;
  818. ptr += 4;
  819. switch( blocktype )
  820. {
  821. case 0 :
  822. // End of data
  823. voice->Playing = FALSE;
  824. done = TRUE;
  825. break;
  826. case 1 :
  827. // Sound data block
  828. voice->bits = 8;
  829. if ( lastblocktype != 8 )
  830. {
  831. tc = ( unsigned int )*ptr << 8;
  832. packtype = *( ptr + 1 );
  833. }
  834. ptr += 2;
  835. blocklength -= 2;
  836. samplespeed = 256000000L / ( 65536 - tc );
  837. // Skip packed or stereo data
  838. if ( ( packtype != 0 ) || ( voicemode != 0 ) )
  839. {
  840. ptr += blocklength;
  841. }
  842. else
  843. {
  844. done = TRUE;
  845. }
  846. voicemode = 0;
  847. break;
  848. case 2 :
  849. // Sound continuation block
  850. samplespeed = voice->SamplingRate;
  851. done = TRUE;
  852. break;
  853. case 3 :
  854. // Silence
  855. // Not implimented.
  856. ptr += blocklength;
  857. break;
  858. case 4 :
  859. // Marker
  860. // Not implimented.
  861. ptr += blocklength;
  862. break;
  863. case 5 :
  864. // ASCII string
  865. // Not implimented.
  866. ptr += blocklength;
  867. break;
  868. case 6 :
  869. // Repeat begin
  870. voice->LoopCount = *( unsigned short * )ptr;
  871. ptr += blocklength;
  872. voice->LoopStart = ptr;
  873. break;
  874. case 7 :
  875. // Repeat end
  876. ptr += blocklength;
  877. if ( lastblocktype == 6 )
  878. {
  879. voice->LoopCount = 0;
  880. }
  881. else
  882. {
  883. if ( ( voice->LoopCount > 0 ) && ( voice->LoopStart != NULL ) )
  884. {
  885. ptr = voice->LoopStart;
  886. if ( voice->LoopCount < 0xffff )
  887. {
  888. voice->LoopCount--;
  889. if ( voice->LoopCount == 0 )
  890. {
  891. voice->LoopStart = NULL;
  892. }
  893. }
  894. }
  895. }
  896. break;
  897. case 8 :
  898. // Extended block
  899. voice->bits = 8;
  900. tc = *( unsigned short * )ptr;
  901. packtype = *( ptr + 2 );
  902. voicemode = *( ptr + 3 );
  903. ptr += blocklength;
  904. break;
  905. case 9 :
  906. // New sound data block
  907. samplespeed = *( unsigned long * )ptr;
  908. BitsPerSample = ( unsigned )*( ptr + 4 );
  909. Channels = ( unsigned )*( ptr + 5 );
  910. Format = ( unsigned )*( unsigned short * )( ptr + 6 );
  911. if ( ( BitsPerSample == 8 ) && ( Channels == 1 ) &&
  912. ( Format == VOC_8BIT ) )
  913. {
  914. ptr += 12;
  915. blocklength -= 12;
  916. voice->bits = 8;
  917. done = TRUE;
  918. }
  919. else if ( ( BitsPerSample == 16 ) && ( Channels == 1 ) &&
  920. ( Format == VOC_16BIT ) )
  921. {
  922. ptr += 12;
  923. blocklength -= 12;
  924. voice->bits = 16;
  925. done = TRUE;
  926. }
  927. else
  928. {
  929. ptr += blocklength;
  930. }
  931. break;
  932. default :
  933. // Unknown data. Probably not a VOC file.
  934. voice->Playing = FALSE;
  935. done = TRUE;
  936. break;
  937. }
  938. lastblocktype = blocktype;
  939. }
  940. if ( voice->Playing )
  941. {
  942. voice->NextBlock = ptr + blocklength;
  943. voice->sound = ptr;
  944. voice->SamplingRate = samplespeed;
  945. voice->RateScale = ( voice->SamplingRate * voice->PitchScale ) >> 16;
  946. voice->length = min( blocklength, MAX_BLOCK_LENGTH );
  947. voice->BlockLength = blocklength - voice->length;
  948. return( KeepPlaying );
  949. }
  950. return( SoundDone );
  951. }
  952. /*---------------------------------------------------------------------
  953. Function: GUSWAVE_GetNextWAVBlock
  954. Controls playback of demand fed data.
  955. ---------------------------------------------------------------------*/
  956. playbackstatus GUSWAVE_GetNextWAVBlock
  957. (
  958. VoiceNode *voice
  959. )
  960. {
  961. if ( voice->BlockLength <= 0 )
  962. {
  963. if ( voice->LoopStart == NULL )
  964. {
  965. voice->Playing = FALSE;
  966. return( SoundDone );
  967. }
  968. voice->BlockLength = voice->LoopSize;
  969. voice->NextBlock = voice->LoopStart;
  970. voice->length = 0;
  971. }
  972. voice->sound = voice->NextBlock;
  973. voice->length = min( voice->BlockLength, 0x8000 );
  974. voice->NextBlock += voice->length;
  975. voice->BlockLength -= voice->length;
  976. return( KeepPlaying );
  977. }
  978. /*---------------------------------------------------------------------
  979. Function: GUSWAVE_GetNextDemandFeedBlock
  980. Controls playback of demand fed data.
  981. ---------------------------------------------------------------------*/
  982. playbackstatus GUSWAVE_GetNextDemandFeedBlock
  983. (
  984. VoiceNode *voice
  985. )
  986. {
  987. if ( voice->BlockLength > 0 )
  988. {
  989. voice->sound += voice->length;
  990. voice->length = min( voice->BlockLength, 0x8000 );
  991. voice->BlockLength -= voice->length;
  992. return( KeepPlaying );
  993. }
  994. if ( voice->DemandFeed == NULL )
  995. {
  996. return( SoundDone );
  997. }
  998. ( voice->DemandFeed )( &voice->sound, &voice->BlockLength );
  999. // voice->sound = GUS_Silence16;
  1000. // voice->BlockLength = 256;
  1001. voice->length = min( voice->BlockLength, 0x8000 );
  1002. voice->BlockLength -= voice->length;
  1003. if ( ( voice->length > 0 ) && ( voice->sound != NULL ) )
  1004. {
  1005. return( KeepPlaying );
  1006. }
  1007. return( NoMoreData );
  1008. }
  1009. /*---------------------------------------------------------------------
  1010. Function: GUSWAVE_Play
  1011. Begins playback of digitized sound.
  1012. ---------------------------------------------------------------------*/
  1013. int GUSWAVE_Play
  1014. (
  1015. VoiceNode *voice,
  1016. int angle,
  1017. int volume,
  1018. int channels
  1019. )
  1020. {
  1021. int VoiceNumber;
  1022. int type;
  1023. int pan;
  1024. unsigned flags;
  1025. int ( *servicefunction )( int reason, int voice, unsigned char **buf, unsigned long *size );
  1026. type = 0;
  1027. if ( channels != 1 )
  1028. {
  1029. type |= TYPE_STEREO;
  1030. }
  1031. if ( voice->bits == 8 )
  1032. {
  1033. type |= TYPE_8BIT;
  1034. type |= TYPE_INVERT_MSB;
  1035. }
  1036. voice->GF1voice = -1;
  1037. angle &= 31;
  1038. pan = GUSWAVE_PanTable[ angle ];
  1039. if ( GUSWAVE_SwapLeftRight )
  1040. {
  1041. pan = 15 - pan;
  1042. }
  1043. voice->Pan = pan;
  1044. volume = max( 0, volume );
  1045. volume = min( 255, volume );
  1046. voice->Volume = volume;
  1047. if ( !GUS_Debug )
  1048. {
  1049. servicefunction = GUSWAVE_CallBack;
  1050. }
  1051. else
  1052. {
  1053. servicefunction = GUSWAVE_DebugCallBack;
  1054. }
  1055. VoiceNumber = gf1_play_digital( 0, voice->sound, voice->length,
  1056. voice->mem, GUSWAVE_Volume - ( 255 - volume ) * 4, pan,
  1057. voice->RateScale, type, &GUS_HoldBuffer, servicefunction );
  1058. if ( VoiceNumber == NO_MORE_VOICES )
  1059. {
  1060. if ( GUS_Debug )
  1061. {
  1062. DB_printf( "Out of voices.\n" );
  1063. }
  1064. flags = DisableInterrupts();
  1065. LL_AddToTail( VoiceNode, &VoicePool, voice );
  1066. RestoreInterrupts( flags );
  1067. GUSWAVE_SetErrorCode( GUSWAVE_NoVoices );
  1068. return( GUSWAVE_Warning );
  1069. }
  1070. flags = DisableInterrupts();
  1071. voice->GF1voice = VoiceNumber;
  1072. voice->Active = TRUE;
  1073. LL_AddToTail( VoiceNode, &VoiceList, voice );
  1074. VoiceStatus[ VoiceNumber ].playing = TRUE;
  1075. VoiceStatus[ VoiceNumber ].Voice = voice;
  1076. if ( GUS_Debug )
  1077. {
  1078. DB_printf( "GUS voice %d playing\n", VoiceNumber );
  1079. }
  1080. RestoreInterrupts( flags );
  1081. return( voice->handle );
  1082. }
  1083. /*---------------------------------------------------------------------
  1084. Function: GUSWAVE_PlayVOC
  1085. Begins playback of digitized sound.
  1086. ---------------------------------------------------------------------*/
  1087. int GUSWAVE_PlayVOC
  1088. (
  1089. char *sample,
  1090. int pitchoffset,
  1091. int angle,
  1092. int volume,
  1093. int priority,
  1094. unsigned long callbackval
  1095. )
  1096. {
  1097. int handle;
  1098. int status;
  1099. playbackstatus soundstatus;
  1100. VoiceNode *voice;
  1101. unsigned flags;
  1102. // Make sure it's a valid VOC file.
  1103. status = strncmp( sample, "Creative Voice File", 19 );
  1104. if ( status != 0 )
  1105. {
  1106. // Tell multivoc that we had a bad VOC file
  1107. MV_ErrorCode = MV_InvalidVOCFile;
  1108. GUSWAVE_SetErrorCode( GUSWAVE_InvalidVOCFile );
  1109. return( GUSWAVE_Error );
  1110. }
  1111. // Request a voice from the voice pool
  1112. voice = GUSWAVE_AllocVoice( priority );
  1113. if ( voice == NULL )
  1114. {
  1115. if ( GUS_Debug )
  1116. {
  1117. DB_printf( "No more voices. Skipping sound.\n" );
  1118. }
  1119. GUSWAVE_SetErrorCode( GUSWAVE_NoVoices );
  1120. return( GUSWAVE_Warning );
  1121. }
  1122. voice->NextBlock = sample + *( unsigned short int * )( sample + 0x14 );
  1123. voice->LoopStart = NULL;
  1124. voice->LoopCount = 0;
  1125. voice->BlockLength = 0;
  1126. voice->PitchScale = PITCH_GetScale( pitchoffset );
  1127. voice->wavetype = VOC;
  1128. voice->bits = 8;
  1129. voice->GetSound = GUSWAVE_GetNextVOCBlock;
  1130. voice->length = 0;
  1131. voice->next = NULL;
  1132. voice->prev = NULL;
  1133. voice->priority = priority;
  1134. voice->callbackval = callbackval;
  1135. soundstatus = GUSWAVE_GetNextVOCBlock( voice );
  1136. if ( soundstatus == SoundDone )
  1137. {
  1138. flags = DisableInterrupts();
  1139. LL_AddToTail( VoiceNode, &VoicePool, voice );
  1140. RestoreInterrupts( flags );
  1141. if ( GUS_Debug )
  1142. {
  1143. DB_printf( "Voice ended before playback.\n" );
  1144. }
  1145. // Tell multivoc that we had a bad VOC file
  1146. MV_ErrorCode = MV_InvalidVOCFile;
  1147. GUSWAVE_SetErrorCode( GUSWAVE_InvalidVOCFile );
  1148. return( GUSWAVE_Error );
  1149. }
  1150. handle = GUSWAVE_Play( voice, angle, volume, 1 );
  1151. return( handle );
  1152. }
  1153. /*---------------------------------------------------------------------
  1154. Function: GUSWAVE_PlayWAV
  1155. Begins playback of digitized sound.
  1156. ---------------------------------------------------------------------*/
  1157. int GUSWAVE_PlayWAV
  1158. (
  1159. char *sample,
  1160. int pitchoffset,
  1161. int angle,
  1162. int volume,
  1163. int priority,
  1164. unsigned long callbackval
  1165. )
  1166. {
  1167. VoiceNode *voice;
  1168. int handle;
  1169. int channels;
  1170. int bits;
  1171. int length;
  1172. riff_header *riff;
  1173. format_header *format;
  1174. data_header *data;
  1175. riff = ( riff_header * )sample;
  1176. if ( ( strncmp( riff->RIFF, "RIFF", 4 ) != 0 ) ||
  1177. ( strncmp( riff->WAVE, "WAVE", 4 ) != 0 ) ||
  1178. ( strncmp( riff->fmt, "fmt ", 4) != 0 ) )
  1179. {
  1180. GUSWAVE_SetErrorCode( GUSWAVE_InvalidWAVFile );
  1181. return( GUSWAVE_Error );
  1182. }
  1183. format = ( format_header * )( riff + 1 );
  1184. data = ( data_header * )( ( ( char * )format ) + riff->format_size );
  1185. if ( format->wFormatTag != 1 )
  1186. {
  1187. GUSWAVE_SetErrorCode( GUSWAVE_InvalidWAVFile );
  1188. return( GUSWAVE_Error );
  1189. }
  1190. channels = format->nChannels;
  1191. if ( ( channels != 1 ) && ( channels != 2 ) )
  1192. {
  1193. GUSWAVE_SetErrorCode( GUSWAVE_InvalidWAVFile );
  1194. return( GUSWAVE_Error );
  1195. }
  1196. bits = format->nBitsPerSample;
  1197. if ( ( bits != 8 ) && ( bits != 16 ) )
  1198. {
  1199. GUSWAVE_SetErrorCode( GUSWAVE_InvalidWAVFile );
  1200. return( GUSWAVE_Error );
  1201. }
  1202. if ( strncmp( data->DATA, "data", 4 ) != 0 )
  1203. {
  1204. GUSWAVE_SetErrorCode( GUSWAVE_InvalidWAVFile );
  1205. return( GUSWAVE_Error );
  1206. }
  1207. // Request a voice from the voice pool
  1208. voice = GUSWAVE_AllocVoice( priority );
  1209. if ( voice == NULL )
  1210. {
  1211. if ( GUS_Debug )
  1212. {
  1213. DB_printf( "No more voices. Skipping sound.\n" );
  1214. }
  1215. GUSWAVE_SetErrorCode( GUSWAVE_NoVoices );
  1216. return( GUSWAVE_Warning );
  1217. }
  1218. voice->wavetype = WAV;
  1219. voice->bits = bits;
  1220. voice->GetSound = GUSWAVE_GetNextWAVBlock;
  1221. length = data->size;
  1222. voice->Playing = TRUE;
  1223. voice->DemandFeed = NULL;
  1224. voice->LoopStart = NULL;
  1225. voice->LoopCount = 0;
  1226. voice->length = min( length, 0x8000 );
  1227. voice->BlockLength = length - voice->length;// min( loopend + 1, data->size );
  1228. voice->sound = ( char * )( data + 1 );
  1229. voice->NextBlock = voice->sound + voice->length;
  1230. voice->next = NULL;
  1231. voice->prev = NULL;
  1232. voice->priority = priority;
  1233. voice->callbackval = callbackval;
  1234. voice->LoopStart = NULL;// voice->NextBlock + loopstart;
  1235. voice->LoopEnd = NULL;//voice->NextBlock + min( loopend, data->size - 1 );
  1236. voice->LoopSize = 0;//( voice->LoopEnd - voice->LoopStart ) + 1;
  1237. voice->PitchScale = PITCH_GetScale( pitchoffset );
  1238. voice->SamplingRate = format->nSamplesPerSec;
  1239. voice->RateScale = ( voice->SamplingRate * voice->PitchScale ) >> 16;
  1240. handle = GUSWAVE_Play( voice, angle, volume, channels );
  1241. return( handle );
  1242. }
  1243. /*---------------------------------------------------------------------
  1244. Function: GUSWAVE_StartDemandFeedPlayback
  1245. Begins playback of digitized sound.
  1246. ---------------------------------------------------------------------*/
  1247. int GUSWAVE_StartDemandFeedPlayback
  1248. (
  1249. void ( *function )( char **ptr, unsigned long *length ),
  1250. int channels,
  1251. int bits,
  1252. int rate,
  1253. int pitchoffset,
  1254. int angle,
  1255. int volume,
  1256. int priority,
  1257. unsigned long callbackval
  1258. )
  1259. {
  1260. VoiceNode *voice;
  1261. int handle;
  1262. // Request a voice from the voice pool
  1263. voice = GUSWAVE_AllocVoice( priority );
  1264. if ( voice == NULL )
  1265. {
  1266. if ( GUS_Debug )
  1267. {
  1268. DB_printf( "No more voices. Skipping sound.\n" );
  1269. }
  1270. GUSWAVE_SetErrorCode( GUSWAVE_NoVoices );
  1271. return( GUSWAVE_Warning );
  1272. }
  1273. voice->wavetype = DemandFeed;
  1274. voice->bits = bits;
  1275. voice->GetSound = GUSWAVE_GetNextDemandFeedBlock;
  1276. voice->Playing = TRUE;
  1277. voice->DemandFeed = function;
  1278. voice->LoopStart = NULL;
  1279. voice->LoopCount = 0;
  1280. voice->BlockLength = 0;
  1281. voice->length = 256;
  1282. voice->sound = ( bits == 8 ) ? GUS_Silence8 : ( char * )GUS_Silence16;
  1283. voice->NextBlock = NULL;
  1284. voice->next = NULL;
  1285. voice->prev = NULL;
  1286. voice->priority = priority;
  1287. voice->callbackval = callbackval;
  1288. voice->PitchScale = PITCH_GetScale( pitchoffset );
  1289. voice->SamplingRate = rate;
  1290. voice->RateScale = ( voice->SamplingRate * voice->PitchScale ) >> 16;
  1291. handle = GUSWAVE_Play( voice, angle, volume, channels );
  1292. return( handle );
  1293. }
  1294. /*---------------------------------------------------------------------
  1295. Function: GUSWAVE_SetReverseStereo
  1296. Set the orientation of the left and right channels.
  1297. ---------------------------------------------------------------------*/
  1298. void GUSWAVE_SetReverseStereo
  1299. (
  1300. int setting
  1301. )
  1302. {
  1303. GUSWAVE_SwapLeftRight = setting;
  1304. }
  1305. /*---------------------------------------------------------------------
  1306. Function: GUSWAVE_GetReverseStereo
  1307. Returns the orientation of the left and right channels.
  1308. ---------------------------------------------------------------------*/
  1309. int GUSWAVE_GetReverseStereo
  1310. (
  1311. void
  1312. )
  1313. {
  1314. return( GUSWAVE_SwapLeftRight );
  1315. }
  1316. /*---------------------------------------------------------------------
  1317. Function: GUSWAVE_InitVoices
  1318. Begins playback of digitized sound.
  1319. ---------------------------------------------------------------------*/
  1320. static int GUSWAVE_InitVoices
  1321. (
  1322. void
  1323. )
  1324. {
  1325. int i;
  1326. for( i = 0; i < MAX_VOICES; i++ )
  1327. {
  1328. VoiceStatus[ i ].playing = FALSE;
  1329. VoiceStatus[ i ].Voice = NULL;
  1330. }
  1331. VoicePool.start = NULL;
  1332. VoicePool.end = NULL;
  1333. VoiceList.start = NULL;
  1334. VoiceList.end = NULL;
  1335. for( i = 0; i < VOICES; i++ )
  1336. {
  1337. GUSWAVE_Voices[ i ].num = -1;
  1338. GUSWAVE_Voices[ i ].Active = FALSE;
  1339. GUSWAVE_Voices[ i ].GF1voice = -1;
  1340. GUSWAVE_Voices[ i ].mem = NULL;
  1341. }
  1342. for( i = 0; i < VOICES; i++ )
  1343. {
  1344. GUSWAVE_Voices[ i ].num = i;
  1345. GUSWAVE_Voices[ i ].Active = FALSE;
  1346. GUSWAVE_Voices[ i ].GF1voice = 0;
  1347. GUSWAVE_Voices[ i ].mem = gf1_malloc( GF1BSIZE );
  1348. if ( GUSWAVE_Voices[ i ].mem == NULL )
  1349. {
  1350. GUSWAVE_MaxVoices = i;
  1351. if ( i < 1 )
  1352. {
  1353. if ( GUSMIDI_Installed )
  1354. {
  1355. GUSWAVE_SetErrorCode( GUSWAVE_UltraNoMemMIDI );
  1356. }
  1357. else
  1358. {
  1359. GUSWAVE_SetErrorCode( GUSWAVE_UltraNoMem );
  1360. }
  1361. return( GUSWAVE_Error );
  1362. }
  1363. break;
  1364. }
  1365. LL_AddToTail( VoiceNode, &VoicePool, &GUSWAVE_Voices[ i ] );
  1366. }
  1367. return( GUSWAVE_Ok );
  1368. }
  1369. /*---------------------------------------------------------------------
  1370. Function: GUSWAVE_SetCallBack
  1371. Set the function to call when a voice stops.
  1372. ---------------------------------------------------------------------*/
  1373. void GUSWAVE_SetCallBack
  1374. (
  1375. void ( *function )( unsigned long )
  1376. )
  1377. {
  1378. GUSWAVE_CallBackFunc = function;
  1379. }
  1380. /*---------------------------------------------------------------------
  1381. Function: GUSWAVE_Init
  1382. Initializes the Gravis Ultrasound for digitized sound playback.
  1383. ---------------------------------------------------------------------*/
  1384. int GUSWAVE_Init
  1385. (
  1386. int numvoices
  1387. )
  1388. {
  1389. int status;
  1390. if ( GUSWAVE_Installed )
  1391. {
  1392. GUSWAVE_Shutdown();
  1393. }
  1394. GUSWAVE_SetErrorCode( GUSWAVE_Ok );
  1395. status = GUS_Init();
  1396. if ( status != GUS_Ok )
  1397. {
  1398. GUSWAVE_SetErrorCode( GUSWAVE_GUSError );
  1399. return( GUSWAVE_Error );
  1400. }
  1401. GUS_Debug = USER_CheckParameter( "DEBUGGUS" );
  1402. GUSWAVE_MaxVoices = min( numvoices, VOICES );
  1403. GUSWAVE_MaxVoices = max( GUSWAVE_MaxVoices, 0 );
  1404. status = GUSWAVE_InitVoices();
  1405. if ( status != GUSWAVE_Ok )
  1406. {
  1407. GUS_Shutdown();
  1408. return( status );
  1409. }
  1410. GUSWAVE_SetReverseStereo( FALSE );
  1411. GUSWAVE_CallBackFunc = NULL;
  1412. GUSWAVE_Installed = TRUE;
  1413. return( GUSWAVE_Ok );
  1414. }