GAP.C 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #ifdef PRECOMPILEDHEADERS
  2. #include "Tactical All.h"
  3. #else
  4. #include "debug.h"
  5. #include "types.h"
  6. //#include "mssw.h"
  7. #include "gap.h"
  8. #include "Sound Control.h"
  9. #include "soundman.h"
  10. #include "Timer Control.h"
  11. #include <stdio.h>
  12. #include "FileMan.h"
  13. #endif
  14. #if 0
  15. static void AILCALLBACK timer_func( UINT32 user )
  16. {
  17. AudioGapList *pGapList;
  18. pGapList = (AudioGapList*)user;
  19. pGapList->gap_monitor_timer += GAP_TIMER_INTERVAL;
  20. if ( pGapList->audio_gap_active )
  21. {
  22. if ( (pGapList->gap_monitor_timer / 1000) > pGapList->end[ pGapList->gap_monitor_current] )
  23. {
  24. pGapList->audio_gap_active = 0;
  25. pGapList->gap_monitor_current++;
  26. }
  27. }
  28. else
  29. {
  30. if ( pGapList->gap_monitor_current < pGapList->count )
  31. {
  32. if ( ( pGapList->gap_monitor_timer / 1000) >= pGapList->start[ pGapList->gap_monitor_current ])
  33. {
  34. pGapList->audio_gap_active = 1;
  35. }
  36. }
  37. }
  38. }
  39. #endif
  40. void AudioGapListInit( CHAR8 *zSoundFile, AudioGapList *pGapList )
  41. {
  42. // This procedure will load in the appropriate .gap file, corresponding
  43. // to the .wav file in szSoundEffects indexed by uiSampleNum
  44. // The procedure will then allocate and load in the AUDIO_GAP information,
  45. // while counting the number of elements loaded
  46. // FILE *pFile;
  47. HWFILE pFile;
  48. STR pSourceFileName;
  49. STR pDestFileName;
  50. char sFileName[256];
  51. UINT8 counter=0;
  52. AUDIO_GAP *pCurrentGap, *pPreviousGap;
  53. UINT32 Start;
  54. UINT32 uiNumBytesRead;
  55. UINT32 End;
  56. pSourceFileName= zSoundFile;
  57. pDestFileName=sFileName;
  58. //Initialize GapList
  59. pGapList->size=0;
  60. pGapList->current_time=0;
  61. pGapList->pHead=0;
  62. pGapList->pCurrent=0;
  63. pGapList->audio_gap_active=FALSE;
  64. pPreviousGap=pCurrentGap=0;
  65. //DebugMsg(TOPIC_JA2, DBG_LEVEL_3,String("File is %s", szSoundEffects[uiSampleNum]));
  66. // Get filename
  67. strcpy(pDestFileName, pSourceFileName);
  68. // strip .wav and change to .gap
  69. while(pDestFileName[counter] !='.')
  70. {
  71. counter++;
  72. }
  73. pDestFileName[counter+1]='g';
  74. pDestFileName[counter+2]='a';
  75. pDestFileName[counter+3]='p';
  76. pDestFileName[counter+4]='\0';
  77. pFile = FileOpen(pDestFileName, FILE_ACCESS_READ, FALSE );
  78. if( pFile )
  79. {
  80. counter=0;
  81. // gap file exists
  82. // now read in the AUDIO_GAPs
  83. //fread(&Start,sizeof(UINT32), 1, pFile);
  84. FileRead( pFile, &Start,sizeof(UINT32), &uiNumBytesRead );
  85. // while ( !feof(pFile) )
  86. while ( !FileCheckEndOfFile( pFile ) )
  87. {
  88. // can read the first element, there exists a second
  89. //fread(&End, sizeof(UINT32),1,pFile);
  90. FileRead( pFile, &End, sizeof(UINT32), &uiNumBytesRead );
  91. // allocate space for AUDIO_GAP
  92. pCurrentGap = MemAlloc( sizeof(AUDIO_GAP) );
  93. if (pPreviousGap !=0)
  94. pPreviousGap->pNext=pCurrentGap;
  95. else
  96. {
  97. // Start of list
  98. pGapList->pCurrent=pCurrentGap;
  99. pGapList->pHead=pCurrentGap;
  100. }
  101. pGapList->size++;
  102. pCurrentGap->pNext=0;
  103. pCurrentGap->uiStart=Start;
  104. pCurrentGap->uiEnd=End;
  105. DebugMsg( TOPIC_JA2, DBG_LEVEL_3, String("Gap Start %d and Ends %d", Start, End) );
  106. // Increment pointer
  107. pPreviousGap=pCurrentGap;
  108. // fread(&Start,sizeof(UINT32), 1, pFile);
  109. FileRead( pFile, &Start, sizeof(UINT32), &uiNumBytesRead );
  110. }
  111. pGapList->audio_gap_active=FALSE;
  112. pGapList->current_time=0;
  113. //fclose(pFile);
  114. FileClose( pFile );
  115. }
  116. DebugMsg( TOPIC_JA2, DBG_LEVEL_3, String("Gap List Started From File %s and has %d gaps", pDestFileName, pGapList->size) );
  117. }
  118. void AudioGapListDone( AudioGapList *pGapList )
  119. {
  120. // This procedure will go through the AudioGapList and free space/nullify pointers
  121. // for any allocated elements
  122. AUDIO_GAP *pCurrent, *pNext;
  123. if (pGapList->pHead !=0)
  124. {
  125. pCurrent=pGapList->pHead;
  126. pNext=pCurrent->pNext;
  127. // There are elements in the list
  128. while(pNext !=0)
  129. {
  130. // kill pCurrent
  131. MemFree(pCurrent);
  132. pCurrent=pNext;
  133. pNext=pNext->pNext;
  134. }
  135. // now kill the last element
  136. MemFree(pCurrent);
  137. pCurrent=0;
  138. }
  139. pGapList->pHead=0;
  140. pGapList->pCurrent=0;
  141. pGapList->size = 0;
  142. DebugMsg( TOPIC_JA2, DBG_LEVEL_3, String("Audio Gap List Deleted") );
  143. }
  144. void PollAudioGap( UINT32 uiSampleNum, AudioGapList *pGapList )
  145. {
  146. // This procedure will access the AudioGapList pertaining to the .wav about
  147. // to be played and sets the audio_gap_active flag. This is done by
  148. // going to the current AUDIO_GAP element in the AudioGapList, comparing to see if
  149. // the current time is between the uiStart and uiEnd. If so, set flag..if not and
  150. // the uiStart of the next element is not greater than current time, set current to next and repeat
  151. // ...if next elements uiStart is larger than current_time, or no more elements..
  152. // set flag FALSE
  153. UINT32 time;
  154. AUDIO_GAP *pCurrent;
  155. if(!pGapList)
  156. {
  157. // no gap list, return
  158. return;
  159. }
  160. if (pGapList->size > 0)
  161. {
  162. time=SoundGetPosition(uiSampleNum);
  163. // DebugMsg( TOPIC_JA2, DBG_LEVEL_3, String("Sound Sample Time is %d", time) );
  164. }
  165. else
  166. {
  167. pGapList->audio_gap_active=(FALSE);
  168. return;
  169. }
  170. // set current ot head of gap list for this sound
  171. pCurrent = pGapList -> pHead;
  172. // check to see if we have fallen behind
  173. if( ( time > pCurrent-> uiEnd ) )
  174. {
  175. // fallen behind
  176. // catchup
  177. while( time > pCurrent -> uiEnd )
  178. {
  179. pCurrent = pCurrent -> pNext;
  180. if( ! pCurrent )
  181. {
  182. pGapList->audio_gap_active=(FALSE);
  183. return;
  184. }
  185. }
  186. }
  187. // check to see if time is within the next AUDIO_GAPs start time
  188. if ( ( time > pCurrent ->uiStart ) && ( time < pCurrent->uiEnd ) )
  189. {
  190. if ((time >pCurrent->uiStart)&&(time < pCurrent->uiEnd))
  191. {
  192. // we are within the time frame
  193. DebugMsg( TOPIC_JA2, DBG_LEVEL_3, String("Gap Started at %d", time) );
  194. pGapList->audio_gap_active=(TRUE);
  195. }
  196. else if ( (time > pCurrent->uiEnd)&&(pGapList->audio_gap_active == TRUE) )
  197. {
  198. // reset if already set
  199. pGapList->audio_gap_active=(FALSE);
  200. DebugMsg( TOPIC_JA2, DBG_LEVEL_3, String("Gap Ended at %d", time) );
  201. }
  202. }
  203. else
  204. {
  205. pGapList->audio_gap_active=(FALSE);
  206. }
  207. }
  208. UINT32 PlayJA2GapSample( CHAR8 *zSoundFile, UINT32 usRate, UINT32 ubVolume, UINT32 ubLoops, UINT32 uiPan, AudioGapList* pData )
  209. {
  210. SOUNDPARMS spParms;
  211. memset(&spParms, 0xff, sizeof(SOUNDPARMS));
  212. spParms.uiSpeed = usRate;
  213. spParms.uiVolume = (UINT32) ( ( ubVolume / (FLOAT) HIGHVOLUME ) * GetSpeechVolume( ) );
  214. spParms.uiLoop = ubLoops;
  215. spParms.uiPan = uiPan;
  216. spParms.uiPriority=GROUP_PLAYER;
  217. // Setup Gap Detection, if it is not null
  218. if( pData != NULL )
  219. AudioGapListInit( zSoundFile, pData );
  220. return(SoundPlayStreamedFile( zSoundFile, &spParms));
  221. }