EASGlue.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Copyright (C) 2009-2011 id Software LLC, a ZeniMax Media company.
  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. See the
  10. 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. #include "EASGlue.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <assert.h>
  20. #include "eas.h"
  21. #include "eas_wave.h"
  22. #include "eas_report.h"
  23. #define NUM_BUFFERS 1
  24. #ifndef NDEBUG
  25. static EAS_BOOL EASLibraryCheck (const S_EAS_LIB_CONFIG *pLibConfig);
  26. #endif
  27. static EAS_DATA_HANDLE pEASData;
  28. static const S_EAS_LIB_CONFIG *pLibConfig;
  29. static int polyphony;
  30. static int bufferSize;
  31. static EAS_FILE file;
  32. static EAS_HANDLE handle;
  33. void EASGlueInit(void) {
  34. EAS_RESULT result;
  35. /* get the library configuration */
  36. pLibConfig = EAS_Config();
  37. assert( EASLibraryCheck(pLibConfig) );
  38. if (polyphony > pLibConfig->maxVoices)
  39. polyphony = pLibConfig->maxVoices;
  40. EAS_I32 mixSize = pLibConfig->mixBufferSize;
  41. bufferSize = mixSize * pLibConfig->numChannels * (EAS_I32)sizeof(EAS_PCM) * NUM_BUFFERS;
  42. /* calculate buffer size */
  43. //bufferSize = pLibConfig->mixBufferSize * pLibConfig->numChannels * (EAS_I32)sizeof(EAS_PCM) * NUM_BUFFERS;
  44. if ( (result = EAS_Init(&pEASData)) != EAS_SUCCESS ) {
  45. printf( "Error initializing EAS: %li\n", result );
  46. }
  47. }
  48. void EASGlueShutdown(void) {
  49. EAS_RESULT result;
  50. EASGlueCloseFile();
  51. if ( (result = EAS_Shutdown(pEASData)) != EAS_SUCCESS ) {
  52. printf( "Error shutting down EAS: %li\n", result );
  53. }
  54. }
  55. void EASGlueOpenFile( const char * filename ) {
  56. EAS_RESULT result;
  57. /* open the file */
  58. file.path = filename;
  59. file.fd = 0;
  60. if ((result = EAS_OpenFile(pEASData, &file, &handle)) != EAS_SUCCESS) {
  61. printf( "Error opening EAS file: %li\n", result );
  62. return;
  63. }
  64. EAS_SetRepeat( pEASData, handle, -1 );
  65. /* prepare for playback */
  66. if ((result = EAS_Prepare(pEASData, handle)) != EAS_SUCCESS) {
  67. printf( "Error preparing EAS file: %li\n", result );
  68. return;
  69. }
  70. }
  71. void EASGluePause(void) {
  72. EAS_RESULT result;
  73. if ( handle == 0 ) {
  74. return;
  75. }
  76. result = EAS_Pause( pEASData, handle );
  77. if ( result != EAS_SUCCESS ) {
  78. printf( "Error pausing EAS file: %li\n", result );
  79. }
  80. }
  81. void EASGlueResume(void) {
  82. EAS_RESULT result;
  83. result = EAS_Resume( pEASData, handle );
  84. if ( result != EAS_SUCCESS ) {
  85. printf( "Error pausing EAS file: %li\n", result );
  86. }
  87. }
  88. void EASGlueCloseFile(void) {
  89. if ( handle == 0 ) {
  90. return;
  91. }
  92. // File must be paused or stopped before closing it.
  93. EASGluePause();
  94. EAS_RESULT result;
  95. result = EAS_CloseFile( pEASData, handle );
  96. if ( result != EAS_SUCCESS ) {
  97. printf( "Error closing EAS file: %li\n", result );
  98. }
  99. handle = 0;
  100. }
  101. void EASGlueRender( EAS_PCM * outputBuffer, EAS_I32 * generatedSamples ) {
  102. EAS_RESULT result;
  103. if ( ( result = EAS_Render( pEASData, outputBuffer, pLibConfig->mixBufferSize, generatedSamples ) ) != EAS_SUCCESS ) {
  104. printf( "Error rendering EAS: %li\n.", result );
  105. return;
  106. }
  107. }
  108. #ifndef NDEBUG
  109. /*----------------------------------------------------------------------------
  110. * EASLibraryCheck()
  111. *----------------------------------------------------------------------------
  112. * Purpose:
  113. * Displays the library version and checks it against the header
  114. * file used to build this code.
  115. *
  116. * Inputs:
  117. * pLibConfig - library configuration retrieved from the library
  118. *
  119. * Outputs:
  120. * returns EAS_TRUE if matched
  121. *
  122. * Side Effects:
  123. *
  124. *----------------------------------------------------------------------------
  125. */
  126. static EAS_BOOL EASLibraryCheck (const S_EAS_LIB_CONFIG *libConfig)
  127. {
  128. /* display the library version */
  129. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "EAS Library Version %d.%d.%d.%d\n",
  130. libConfig->libVersion >> 24,
  131. (libConfig->libVersion >> 16) & 0x0f,
  132. (libConfig->libVersion >> 8) & 0x0f,
  133. libConfig->libVersion & 0x0f); */ }
  134. /* display some info about the library build */
  135. if (libConfig->checkedVersion)
  136. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tChecked library\n"); */ }
  137. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tMaximum polyphony: %d\n", libConfig->maxVoices); */ }
  138. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tNumber of channels: %d\n", libConfig->numChannels); */ }
  139. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tSample rate: %d\n", libConfig->sampleRate); */ }
  140. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tMix buffer size: %d\n", libConfig->mixBufferSize); */ }
  141. if (libConfig->filterEnabled)
  142. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tFilter enabled\n"); */ }
  143. #ifndef _WIN32_WCE
  144. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tLibrary Build Timestamp: %s", ctime((time_t*)&libConfig->buildTimeStamp)); */ }
  145. #endif
  146. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tLibrary Build ID: %s\n", libConfig->buildGUID); */ }
  147. /* check it against the header file used to build this code */
  148. /*lint -e{778} constant expression used for display purposes may evaluate to zero */
  149. if (LIB_VERSION != libConfig->libVersion)
  150. {
  151. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "Library version does not match header files. EAS Header Version %d.%d.%d.%d\n",
  152. LIB_VERSION >> 24,
  153. (LIB_VERSION >> 16) & 0x0f,
  154. (LIB_VERSION >> 8) & 0x0f,
  155. LIB_VERSION & 0x0f); */ }
  156. return EAS_FALSE;
  157. }
  158. return EAS_TRUE;
  159. } /* end EASLibraryCheck */
  160. #endif