FACT_internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /* FAudio - XAudio Reimplementation for FNA
  2. *
  3. * Copyright (c) 2011-2021 Ethan Lee, Luigi Auriemma, and the MonoGame Team
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * In no event will the authors be held liable for any damages arising from
  7. * the use of this software.
  8. *
  9. * Permission is granted to anyone to use this software for any purpose,
  10. * including commercial applications, and to alter it and redistribute it
  11. * freely, subject to the following restrictions:
  12. *
  13. * 1. The origin of this software must not be misrepresented; you must not
  14. * claim that you wrote the original software. If you use this software in a
  15. * product, an acknowledgment in the product documentation would be
  16. * appreciated but is not required.
  17. *
  18. * 2. Altered source versions must be plainly marked as such, and must not be
  19. * misrepresented as being the original software.
  20. *
  21. * 3. This notice may not be removed or altered from any source distribution.
  22. *
  23. * Ethan "flibitijibibo" Lee <flibitijibibo@flibitijibibo.com>
  24. *
  25. */
  26. #include "FACT.h"
  27. #include "FACT3D.h"
  28. #include "FAudio_internal.h"
  29. /* Internal AudioEngine Types */
  30. typedef struct FACTAudioCategory
  31. {
  32. uint8_t instanceLimit;
  33. uint16_t fadeInMS;
  34. uint16_t fadeOutMS;
  35. uint8_t maxInstanceBehavior;
  36. int16_t parentCategory;
  37. float volume;
  38. uint8_t visibility;
  39. uint8_t instanceCount;
  40. float currentVolume;
  41. } FACTAudioCategory;
  42. typedef struct FACTVariable
  43. {
  44. uint8_t accessibility;
  45. float initialValue;
  46. float minValue;
  47. float maxValue;
  48. } FACTVariable;
  49. typedef struct FACTRPCPoint
  50. {
  51. float x;
  52. float y;
  53. uint8_t type;
  54. } FACTRPCPoint;
  55. typedef enum FACTRPCParameter
  56. {
  57. RPC_PARAMETER_VOLUME,
  58. RPC_PARAMETER_PITCH,
  59. RPC_PARAMETER_REVERBSEND,
  60. RPC_PARAMETER_FILTERFREQUENCY,
  61. RPC_PARAMETER_FILTERQFACTOR,
  62. RPC_PARAMETER_COUNT /* If >=, DSP Parameter! */
  63. } FACTRPCParameter;
  64. typedef struct FACTRPC
  65. {
  66. uint16_t variable;
  67. uint8_t pointCount;
  68. uint16_t parameter;
  69. FACTRPCPoint *points;
  70. } FACTRPC;
  71. typedef struct FACTDSPParameter
  72. {
  73. uint8_t type;
  74. float value;
  75. float minVal;
  76. float maxVal;
  77. uint16_t unknown;
  78. } FACTDSPParameter;
  79. typedef struct FACTDSPPreset
  80. {
  81. uint8_t accessibility;
  82. uint16_t parameterCount;
  83. FACTDSPParameter *parameters;
  84. } FACTDSPPreset;
  85. typedef enum FACTNoticationsFlags
  86. {
  87. NOTIFY_CUEPREPARED = 0x00000001,
  88. NOTIFY_CUEPLAY = 0x00000002,
  89. NOTIFY_CUESTOP = 0x00000004,
  90. NOTIFY_CUEDESTROY = 0x00000008,
  91. NOTIFY_MARKER = 0x00000010,
  92. NOTIFY_SOUNDBANKDESTROY = 0x00000020,
  93. NOTIFY_WAVEBANKDESTROY = 0x00000040,
  94. NOTIFY_LOCALVARIABLECHANGED = 0x00000080,
  95. NOTIFY_GLOBALVARIABLECHANGED = 0x00000100,
  96. NOTIFY_GUICONNECTED = 0x00000200,
  97. NOTIFY_GUIDISCONNECTED = 0x00000400,
  98. NOTIFY_WAVEPREPARED = 0x00000800,
  99. NOTIFY_WAVEPLAY = 0x00001000,
  100. NOTIFY_WAVESTOP = 0x00002000,
  101. NOTIFY_WAVELOOPED = 0x00004000,
  102. NOTIFY_WAVEDESTROY = 0x00008000,
  103. NOTIFY_WAVEBANKPREPARED = 0x00010000
  104. } FACTNoticationsFlags;
  105. /* Internal SoundBank Types */
  106. typedef enum
  107. {
  108. FACTEVENT_STOP = 0,
  109. FACTEVENT_PLAYWAVE = 1,
  110. FACTEVENT_PLAYWAVETRACKVARIATION = 3,
  111. FACTEVENT_PLAYWAVEEFFECTVARIATION = 4,
  112. FACTEVENT_PLAYWAVETRACKEFFECTVARIATION = 6,
  113. FACTEVENT_PITCH = 7,
  114. FACTEVENT_VOLUME = 8,
  115. FACTEVENT_MARKER = 9,
  116. FACTEVENT_PITCHREPEATING = 16,
  117. FACTEVENT_VOLUMEREPEATING = 17,
  118. FACTEVENT_MARKERREPEATING = 18
  119. } FACTEventType;
  120. typedef struct FACTEvent
  121. {
  122. uint16_t type;
  123. uint16_t timestamp;
  124. uint16_t randomOffset;
  125. FAUDIONAMELESS union
  126. {
  127. /* Play Wave Event */
  128. struct
  129. {
  130. uint8_t flags;
  131. uint8_t loopCount;
  132. uint16_t position;
  133. uint16_t angle;
  134. /* Track Variation */
  135. uint8_t isComplex;
  136. FAUDIONAMELESS union
  137. {
  138. struct
  139. {
  140. uint16_t track;
  141. uint8_t wavebank;
  142. } simple;
  143. struct
  144. {
  145. uint16_t variation;
  146. uint16_t trackCount;
  147. uint16_t *tracks;
  148. uint8_t *wavebanks;
  149. uint8_t *weights;
  150. } complex;
  151. };
  152. /* Effect Variation */
  153. int16_t minPitch;
  154. int16_t maxPitch;
  155. float minVolume;
  156. float maxVolume;
  157. float minFrequency;
  158. float maxFrequency;
  159. float minQFactor;
  160. float maxQFactor;
  161. uint16_t variationFlags;
  162. } wave;
  163. /* Set Pitch/Volume Event */
  164. struct
  165. {
  166. uint8_t settings;
  167. uint16_t repeats;
  168. uint16_t frequency;
  169. FAUDIONAMELESS union
  170. {
  171. struct
  172. {
  173. float initialValue;
  174. float initialSlope;
  175. float slopeDelta;
  176. uint16_t duration;
  177. } ramp;
  178. struct
  179. {
  180. uint8_t flags;
  181. float value1;
  182. float value2;
  183. } equation;
  184. };
  185. } value;
  186. /* Stop Event */
  187. struct
  188. {
  189. uint8_t flags;
  190. } stop;
  191. /* Marker Event */
  192. struct
  193. {
  194. uint32_t marker;
  195. uint16_t repeats;
  196. uint16_t frequency;
  197. } marker;
  198. };
  199. } FACTEvent;
  200. typedef struct FACTTrack
  201. {
  202. uint32_t code;
  203. float volume;
  204. uint8_t filter;
  205. uint8_t qfactor;
  206. uint16_t frequency;
  207. uint8_t rpcCodeCount;
  208. uint32_t *rpcCodes;
  209. uint8_t eventCount;
  210. FACTEvent *events;
  211. } FACTTrack;
  212. typedef struct FACTSound
  213. {
  214. uint8_t flags;
  215. uint16_t category;
  216. float volume;
  217. int16_t pitch;
  218. uint8_t priority;
  219. uint8_t trackCount;
  220. uint8_t rpcCodeCount;
  221. uint8_t dspCodeCount;
  222. FACTTrack *tracks;
  223. uint32_t *rpcCodes;
  224. uint32_t *dspCodes;
  225. } FACTSound;
  226. typedef struct FACTCueData
  227. {
  228. uint8_t flags;
  229. uint32_t sbCode;
  230. uint32_t transitionOffset;
  231. uint8_t instanceLimit;
  232. uint16_t fadeInMS;
  233. uint16_t fadeOutMS;
  234. uint8_t maxInstanceBehavior;
  235. uint8_t instanceCount;
  236. } FACTCueData;
  237. typedef struct FACTVariation
  238. {
  239. FAUDIONAMELESS union
  240. {
  241. struct
  242. {
  243. uint16_t track;
  244. uint8_t wavebank;
  245. } simple;
  246. uint32_t soundCode;
  247. };
  248. float minWeight;
  249. float maxWeight;
  250. uint32_t linger;
  251. } FACTVariation;
  252. typedef struct FACTVariationTable
  253. {
  254. uint8_t flags;
  255. int16_t variable;
  256. uint8_t isComplex;
  257. uint16_t entryCount;
  258. FACTVariation *entries;
  259. } FACTVariationTable;
  260. typedef struct FACTTransition
  261. {
  262. int32_t soundCode;
  263. uint32_t srcMarkerMin;
  264. uint32_t srcMarkerMax;
  265. uint32_t dstMarkerMin;
  266. uint32_t dstMarkerMax;
  267. uint16_t fadeIn;
  268. uint16_t fadeOut;
  269. uint16_t flags;
  270. } FACTTransition;
  271. typedef struct FACTTransitionTable
  272. {
  273. uint32_t entryCount;
  274. FACTTransition *entries;
  275. } FACTTransitionTable;
  276. /* Internal WaveBank Types */
  277. typedef struct FACTSeekTable
  278. {
  279. uint32_t entryCount;
  280. uint32_t *entries;
  281. } FACTSeekTable;
  282. /* Internal Cue Types */
  283. typedef struct FACTInstanceRPCData
  284. {
  285. float rpcVolume;
  286. float rpcPitch;
  287. float rpcReverbSend;
  288. float rpcFilterFreq;
  289. float rpcFilterQFactor;
  290. } FACTInstanceRPCData;
  291. typedef struct FACTEventInstance
  292. {
  293. uint32_t timestamp;
  294. uint16_t loopCount;
  295. uint8_t finished;
  296. FAUDIONAMELESS union
  297. {
  298. float value;
  299. uint32_t valuei;
  300. };
  301. } FACTEventInstance;
  302. typedef struct FACTTrackInstance
  303. {
  304. /* Tracks which events have fired */
  305. FACTEventInstance *events;
  306. /* RPC instance data */
  307. FACTInstanceRPCData rpcData;
  308. /* SetPitch/SetVolume data */
  309. float evtPitch;
  310. float evtVolume;
  311. /* Wave playback */
  312. struct
  313. {
  314. FACTWave *wave;
  315. float baseVolume;
  316. int16_t basePitch;
  317. float baseQFactor;
  318. float baseFrequency;
  319. } activeWave, upcomingWave;
  320. FACTEvent *waveEvt;
  321. FACTEventInstance *waveEvtInst;
  322. } FACTTrackInstance;
  323. typedef struct FACTSoundInstance
  324. {
  325. /* Base Sound reference */
  326. FACTSound *sound;
  327. /* Per-instance track information */
  328. FACTTrackInstance *tracks;
  329. /* RPC instance data */
  330. FACTInstanceRPCData rpcData;
  331. /* Fade data */
  332. uint32_t fadeStart;
  333. uint16_t fadeTarget;
  334. uint8_t fadeType; /* In (1), Out (2), Release RPC (3) */
  335. /* Engine references */
  336. FACTCue *parentCue;
  337. } FACTSoundInstance;
  338. /* Internal Wave Types */
  339. typedef struct FACTWaveCallback
  340. {
  341. FAudioVoiceCallback callback;
  342. FACTWave *wave;
  343. } FACTWaveCallback;
  344. /* Public XACT Types */
  345. struct FACTAudioEngine
  346. {
  347. uint32_t refcount;
  348. FACTNotificationCallback notificationCallback;
  349. FACTReadFileCallback pReadFile;
  350. FACTGetOverlappedResultCallback pGetOverlappedResult;
  351. uint16_t categoryCount;
  352. uint16_t variableCount;
  353. uint16_t rpcCount;
  354. uint16_t dspPresetCount;
  355. uint16_t dspParameterCount;
  356. char **categoryNames;
  357. char **variableNames;
  358. uint32_t *rpcCodes;
  359. uint32_t *dspPresetCodes;
  360. FACTAudioCategory *categories;
  361. FACTVariable *variables;
  362. FACTRPC *rpcs;
  363. FACTDSPPreset *dspPresets;
  364. /* Engine references */
  365. LinkedList *sbList;
  366. LinkedList *wbList;
  367. FAudioMutex sbLock;
  368. FAudioMutex wbLock;
  369. float *globalVariableValues;
  370. /* FAudio references */
  371. FAudio *audio;
  372. FAudioMasteringVoice *master;
  373. FAudioSubmixVoice *reverbVoice;
  374. /* Engine thread */
  375. FAudioThread apiThread;
  376. FAudioMutex apiLock;
  377. uint8_t initialized;
  378. /* Allocator callbacks */
  379. FAudioMallocFunc pMalloc;
  380. FAudioFreeFunc pFree;
  381. FAudioReallocFunc pRealloc;
  382. /* Peristent Notifications */
  383. FACTNoticationsFlags notifications;
  384. void *cue_context;
  385. void *sb_context;
  386. void *wb_context;
  387. void *wave_context;
  388. /* Settings handle */
  389. void *settings;
  390. };
  391. struct FACTSoundBank
  392. {
  393. /* Engine references */
  394. FACTAudioEngine *parentEngine;
  395. FACTCue *cueList;
  396. uint8_t notifyOnDestroy;
  397. void *usercontext;
  398. /* Array sizes */
  399. uint16_t cueCount;
  400. uint8_t wavebankCount;
  401. uint16_t soundCount;
  402. uint16_t variationCount;
  403. uint16_t transitionCount;
  404. /* Strings, strings everywhere! */
  405. char **wavebankNames;
  406. char **cueNames;
  407. /* Actual SoundBank information */
  408. char *name;
  409. FACTCueData *cues;
  410. FACTSound *sounds;
  411. uint32_t *soundCodes;
  412. FACTVariationTable *variations;
  413. uint32_t *variationCodes;
  414. FACTTransitionTable *transitions;
  415. uint32_t *transitionCodes;
  416. };
  417. struct FACTWaveBank
  418. {
  419. /* Engine references */
  420. FACTAudioEngine *parentEngine;
  421. LinkedList *waveList;
  422. FAudioMutex waveLock;
  423. uint8_t notifyOnDestroy;
  424. void *usercontext;
  425. /* Actual WaveBank information */
  426. char *name;
  427. uint32_t entryCount;
  428. FACTWaveBankEntry *entries;
  429. uint32_t *entryRefs;
  430. FACTSeekTable *seekTables;
  431. char *waveBankNames;
  432. /* I/O information */
  433. uint32_t packetSize;
  434. uint16_t streaming;
  435. uint8_t *packetBuffer;
  436. uint32_t packetBufferLen;
  437. void* io;
  438. };
  439. struct FACTWave
  440. {
  441. /* Engine references */
  442. FACTWaveBank *parentBank;
  443. FACTCue *parentCue;
  444. uint16_t index;
  445. uint8_t notifyOnDestroy;
  446. void *usercontext;
  447. /* Playback */
  448. uint32_t state;
  449. float volume;
  450. int16_t pitch;
  451. uint8_t loopCount;
  452. /* Stream data */
  453. uint32_t streamSize;
  454. uint32_t streamOffset;
  455. uint8_t *streamCache;
  456. /* FAudio references */
  457. uint16_t srcChannels;
  458. FAudioSourceVoice *voice;
  459. FACTWaveCallback callback;
  460. };
  461. struct FACTCue
  462. {
  463. /* Engine references */
  464. FACTSoundBank *parentBank;
  465. FACTCue *next;
  466. uint8_t managed;
  467. uint16_t index;
  468. uint8_t notifyOnDestroy;
  469. void *usercontext;
  470. /* Sound data */
  471. FACTCueData *data;
  472. FAUDIONAMELESS union
  473. {
  474. FACTVariationTable *variation;
  475. /* This is only used in scenarios where there is only one
  476. * Sound; XACT does not generate variation tables for
  477. * Cues with only one Sound.
  478. */
  479. FACTSound *sound;
  480. };
  481. /* Instance data */
  482. float *variableValues;
  483. float interactive;
  484. /* Playback */
  485. uint32_t state;
  486. FACTWave *simpleWave;
  487. FACTSoundInstance *playingSound;
  488. FACTVariation *playingVariation;
  489. uint32_t maxRpcReleaseTime;
  490. /* 3D Data */
  491. uint8_t active3D;
  492. uint32_t srcChannels;
  493. uint32_t dstChannels;
  494. float matrixCoefficients[2 * 8]; /* Stereo input, 7.1 output */
  495. /* Timer */
  496. uint32_t start;
  497. uint32_t elapsed;
  498. };
  499. /* Internal functions */
  500. void FACT_INTERNAL_GetNextWave(
  501. FACTCue *cue,
  502. FACTSound *sound,
  503. FACTTrack *track,
  504. FACTTrackInstance *trackInst,
  505. FACTEvent *evt,
  506. FACTEventInstance *evtInst
  507. );
  508. uint8_t FACT_INTERNAL_CreateSound(FACTCue *cue, uint16_t fadeInMS);
  509. void FACT_INTERNAL_DestroySound(FACTSoundInstance *sound);
  510. void FACT_INTERNAL_BeginFadeOut(FACTSoundInstance *sound, uint16_t fadeOutMS);
  511. void FACT_INTERNAL_BeginReleaseRPC(FACTSoundInstance *sound, uint16_t releaseMS);
  512. void FACT_INTERNAL_SendCueNotification(FACTCue *cue, FACTNoticationsFlags flag, uint8_t type);
  513. /* RPC Helper Functions */
  514. FACTRPC* FACT_INTERNAL_GetRPC(FACTAudioEngine *engine, uint32_t code);
  515. /* FACT Thread */
  516. int32_t FAUDIOCALL FACT_INTERNAL_APIThread(void* enginePtr);
  517. /* FAudio callbacks */
  518. void FACT_INTERNAL_OnBufferEnd(FAudioVoiceCallback *callback, void* pContext);
  519. void FACT_INTERNAL_OnStreamEnd(FAudioVoiceCallback *callback);
  520. /* FAudioIOStream functions */
  521. int32_t FACTCALL FACT_INTERNAL_DefaultReadFile(
  522. void *hFile,
  523. void *buffer,
  524. uint32_t nNumberOfBytesToRead,
  525. uint32_t *lpNumberOfBytesRead,
  526. FACTOverlapped *lpOverlapped
  527. );
  528. int32_t FACTCALL FACT_INTERNAL_DefaultGetOverlappedResult(
  529. void *hFile,
  530. FACTOverlapped *lpOverlapped,
  531. uint32_t *lpNumberOfBytesTransferred,
  532. int32_t bWait
  533. );
  534. /* Parsing functions */
  535. uint32_t FACT_INTERNAL_ParseAudioEngine(
  536. FACTAudioEngine *pEngine,
  537. const FACTRuntimeParameters *pParams
  538. );
  539. uint32_t FACT_INTERNAL_ParseSoundBank(
  540. FACTAudioEngine *pEngine,
  541. const void *pvBuffer,
  542. uint32_t dwSize,
  543. FACTSoundBank **ppSoundBank
  544. );
  545. uint32_t FACT_INTERNAL_ParseWaveBank(
  546. FACTAudioEngine *pEngine,
  547. void* io,
  548. uint32_t offset,
  549. uint32_t packetSize,
  550. FACTReadFileCallback pRead,
  551. FACTGetOverlappedResultCallback pOverlap,
  552. uint16_t isStreaming,
  553. FACTWaveBank **ppWaveBank
  554. );
  555. /* vim: set noexpandtab shiftwidth=8 tabstop=8: */