audio_ScriptBinding.cc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _ASSET_MANAGER_H_
  23. #include "assets/assetManager.h"
  24. #endif
  25. #include "platform/platform.h"
  26. #ifndef _PLATFORMAUDIO_H_
  27. #include "platform/platformAudio.h"
  28. #endif
  29. #ifndef _SIMBASE_H_
  30. #include "sim/simBase.h"
  31. #endif
  32. #ifndef _AUDIODATABLOCK_H_
  33. #include "audio/audioDataBlock.h"
  34. #endif
  35. #ifndef _AUDIO_ASSET_H_
  36. #include "audio/AudioAsset.h"
  37. #endif
  38. #ifdef TORQUE_OS_IOS
  39. #include "platformiOS/iOSStreamSource.h"
  40. #endif
  41. extern ALuint alxGetWaveLen(ALuint buffer);
  42. extern F32 mAudioChannelVolumes[Audio::AudioVolumeChannels];
  43. //--------------------------------------------------------------------------
  44. // Expose all al get/set methods...
  45. //--------------------------------------------------------------------------
  46. enum AL_GetSetBits{
  47. Source = BIT(0),
  48. Listener = BIT(1),
  49. Context = BIT(2),
  50. Environment = BIT(3),
  51. Get = BIT(4),
  52. Set = BIT(5),
  53. Int = BIT(6),
  54. Float = BIT(7),
  55. Float3 = BIT(8),
  56. Float6 = BIT(9)
  57. };
  58. static ALenum getEnum(const char * name, U32 flags)
  59. {
  60. AssertFatal(name, "Audio getEnum: bad param");
  61. static struct {
  62. const char * mName;
  63. ALenum mAlenum;
  64. U32 mFlags;
  65. } table[] = {
  66. //-----------------------------------------------------------------------------------------------------------------
  67. // "name" ENUM Flags
  68. //-----------------------------------------------------------------------------------------------------------------
  69. { "AL_GAIN", AL_GAIN, (Source|Listener|Get|Set|Float) },
  70. { "AL_GAIN_LINEAR", AL_GAIN_LINEAR, (Source|Listener|Get|Set|Float) },
  71. { "AL_PITCH", AL_PITCH, (Source|Get|Set|Float) },
  72. { "AL_REFERENCE_DISTANCE", AL_REFERENCE_DISTANCE, (Source|Get|Set|Float) },
  73. { "AL_MAX_DISTANCE", AL_MAX_DISTANCE, (Source|Get|Set|Float) },
  74. { "AL_CONE_OUTER_GAIN", AL_CONE_OUTER_GAIN, (Source|Get|Set|Float) },
  75. { "AL_POSITION", AL_POSITION, (Source|Listener|Get|Set|Float3) },
  76. { "AL_DIRECTION", AL_DIRECTION, (Source|Get|Set|Float3) },
  77. { "AL_VELOCITY", AL_VELOCITY, (Source|Listener|Get|Set|Float3) },
  78. { "AL_ORIENTATION", AL_ORIENTATION, (Listener|Set|Float6) },
  79. { "AL_CONE_INNER_ANGLE", AL_CONE_INNER_ANGLE, (Source|Get|Set|Int) },
  80. { "AL_CONE_OUTER_ANGLE", AL_CONE_OUTER_ANGLE, (Source|Get|Set|Int) },
  81. { "AL_LOOPING", AL_LOOPING, (Source|Get|Set|Int) },
  82. { "AL_SAMPLE_OFFSET", AL_SAMPLE_OFFSET, (Source|Get|Set|Int) },
  83. { "AL_SEC_OFFSET", AL_SEC_OFFSET, (Source|Get|Set|Int) },
  84. { "AL_BYTE_OFFSET", AL_BYTE_OFFSET, (Source|Get|Set|Int) },
  85. //{ "AL_STREAMING", AL_STREAMING, (Source|Get|Set|Int) },
  86. //{ "AL_BUFFER", AL_BUFFER, (Source|Get|Set|Int) },
  87. { "AL_VENDOR", AL_VENDOR, (Context|Get) },
  88. { "AL_VERSION", AL_VERSION, (Context|Get) },
  89. { "AL_RENDERER", AL_RENDERER, (Context|Get) },
  90. { "AL_EXTENSIONS", AL_EXTENSIONS, (Context|Get) },
  91. /*
  92. // environment
  93. { "AL_ENV_ROOM_IASIG", AL_ENV_ROOM_IASIG, (Environment|Get|Set|Int) },
  94. { "AL_ENV_ROOM_HIGH_FREQUENCY_IASIG", AL_ENV_ROOM_HIGH_FREQUENCY_IASIG, (Environment|Get|Set|Int) },
  95. { "AL_ENV_REFLECTIONS_IASIG", AL_ENV_REFLECTIONS_IASIG, (Environment|Get|Set|Int) },
  96. { "AL_ENV_REVERB_IASIG", AL_ENV_REVERB_IASIG, (Environment|Get|Set|Int) },
  97. { "AL_ENV_ROOM_ROLLOFF_FACTOR_IASIG", AL_ENV_ROOM_ROLLOFF_FACTOR_IASIG, (Environment|Get|Set|Float) },
  98. { "AL_ENV_DECAY_TIME_IASIG", AL_ENV_DECAY_TIME_IASIG, (Environment|Get|Set|Float) },
  99. { "AL_ENV_DECAY_HIGH_FREQUENCY_RATIO_IASIG", AL_ENV_DECAY_HIGH_FREQUENCY_RATIO_IASIG, (Environment|Get|Set|Float) },
  100. { "AL_ENV_REFLECTIONS_DELAY_IASIG", AL_ENV_REFLECTIONS_DELAY_IASIG, (Environment|Get|Set|Float) },
  101. { "AL_ENV_REVERB_DELAY_IASIG", AL_ENV_REVERB_DELAY_IASIG, (Environment|Get|Set|Float) },
  102. { "AL_ENV_DIFFUSION_IASIG", AL_ENV_DIFFUSION_IASIG, (Environment|Get|Set|Float) },
  103. { "AL_ENV_DENSITY_IASIG", AL_ENV_DENSITY_IASIG, (Environment|Get|Set|Float) },
  104. { "AL_ENV_HIGH_FREQUENCY_REFERENCE_IASIG", AL_ENV_HIGH_FREQUENCY_REFERENCE_IASIG, (Environment|Get|Set|Float) },
  105. { "AL_ENV_ROOM_VOLUME_EXT", AL_ENV_ROOM_VOLUME_EXT, (Environment|Get|Set|Int) },
  106. { "AL_ENV_FLAGS_EXT", AL_ENV_FLAGS_EXT, (Environment|Get|Set|Int) },
  107. { "AL_ENV_EFFECT_VOLUME_EXT", AL_ENV_EFFECT_VOLUME_EXT, (Environment|Get|Set|Float) },
  108. { "AL_ENV_DAMPING_EXT", AL_ENV_DAMPING_EXT, (Environment|Get|Set|Float) },
  109. { "AL_ENV_ENVIRONMENT_SIZE_EXT", AL_ENV_ENVIRONMENT_SIZE_EXT, (Environment|Get|Set|Float) },
  110. // sample environment
  111. { "AL_ENV_SAMPLE_DIRECT_EXT", AL_ENV_SAMPLE_DIRECT_EXT, (Source|Get|Set|Int) },
  112. { "AL_ENV_SAMPLE_DIRECT_HF_EXT", AL_ENV_SAMPLE_DIRECT_HF_EXT, (Source|Get|Set|Int) },
  113. { "AL_ENV_SAMPLE_ROOM_EXT", AL_ENV_SAMPLE_ROOM_EXT, (Source|Get|Set|Int) },
  114. { "AL_ENV_SAMPLE_ROOM_HF_EXT", AL_ENV_SAMPLE_ROOM_HF_EXT, (Source|Get|Set|Int) },
  115. { "AL_ENV_SAMPLE_OUTSIDE_VOLUME_HF_EXT", AL_ENV_SAMPLE_OUTSIDE_VOLUME_HF_EXT, (Source|Get|Set|Int) },
  116. { "AL_ENV_SAMPLE_FLAGS_EXT", AL_ENV_SAMPLE_FLAGS_EXT, (Source|Get|Set|Int) },
  117. { "AL_ENV_SAMPLE_REVERB_MIX_EXT", AL_ENV_SAMPLE_REVERB_MIX_EXT, (Source|Get|Set|Float) },
  118. { "AL_ENV_SAMPLE_OBSTRUCTION_EXT", AL_ENV_SAMPLE_OBSTRUCTION_EXT, (Source|Get|Set|Float) },
  119. { "AL_ENV_SAMPLE_OBSTRUCTION_LF_RATIO_EXT", AL_ENV_SAMPLE_OBSTRUCTION_LF_RATIO_EXT, (Source|Get|Set|Float) },
  120. { "AL_ENV_SAMPLE_OCCLUSION_EXT", AL_ENV_SAMPLE_OCCLUSION_EXT, (Source|Get|Set|Float) },
  121. { "AL_ENV_SAMPLE_OCCLUSION_LF_RATIO_EXT", AL_ENV_SAMPLE_OCCLUSION_LF_RATIO_EXT, (Source|Get|Set|Float) },
  122. { "AL_ENV_SAMPLE_OCCLUSION_ROOM_RATIO_EXT", AL_ENV_SAMPLE_OCCLUSION_ROOM_RATIO_EXT, (Source|Get|Set|Float) },
  123. { "AL_ENV_SAMPLE_ROOM_ROLLOFF_EXT", AL_ENV_SAMPLE_ROOM_ROLLOFF_EXT, (Source|Get|Set|Float) },
  124. { "AL_ENV_SAMPLE_AIR_ABSORPTION_EXT", AL_ENV_SAMPLE_AIR_ABSORPTION_EXT, (Source|Get|Set|Float) },
  125. */
  126. };
  127. for(U32 i = 0; i < (sizeof(table) / sizeof(table[0])); i++)
  128. {
  129. if((table[i].mFlags & flags) != flags)
  130. continue;
  131. if(dStricmp(table[i].mName, name) == 0)
  132. return(table[i].mAlenum);
  133. }
  134. return(AL_INVALID);
  135. }
  136. //-----------------------------------------------
  137. ConsoleFunctionGroupBegin(Audio, "Functions dealing with the OpenAL audio layer.\n\n"
  138. "@see www.OpenAL.org for what these functions do. Variances from posted"
  139. " behaviour is described below.");
  140. /*! @defgroup AudioFunctions Audio
  141. @ingroup TorqueScriptFunctions
  142. @{
  143. */
  144. /*! Use the OpenALInitDriver function to initialize the OpenAL driver.
  145. This must be done before all other OpenAL operations.
  146. @return Returns true on successful initialization, false otherwise.
  147. @sa OpenALShutdownDriver
  148. */
  149. ConsoleFunctionWithDocs(OpenALInitDriver, ConsoleBool, 1, 1, ())
  150. {
  151. if (Audio::OpenALInit())
  152. {
  153. static bool registered = false;
  154. if (!registered) {
  155. ResourceManager->registerExtension(".wav", AudioBuffer::construct);
  156. }
  157. registered = true;
  158. return true;
  159. }
  160. return false;
  161. }
  162. //-----------------------------------------------
  163. /*! Use the OpenALShutdownDriver function to stop/shut down the OpenAL driver.
  164. After this is called, you must restart the driver with OpenALInitDriver to execute any new sound operations.
  165. @return No return value.
  166. @sa OpenALInitDriver
  167. */
  168. ConsoleFunctionWithDocs(OpenALShutdownDriver, ConsoleVoid, 1, 1, ())
  169. {
  170. Audio::OpenALShutdown();
  171. }
  172. //-----------------------------------------------
  173. /*!
  174. @note Currently does nothing (possibly deprecated)
  175. */
  176. ConsoleFunctionWithDocs(OpenALRegisterExtensions, ConsoleVoid, 1, 1, ())
  177. {
  178. }
  179. //-----------------------------------------------
  180. /*! Use the alGetString function to get the string equivalent to the specified OpenAL enumerated value.
  181. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  182. @return Returns a string corresponding to the passed ALEnum
  183. */
  184. ConsoleFunctionWithDocs(alGetString, ConsoleString, 2, 2, ( ALEnum ))
  185. {
  186. ALenum e = getEnum(argv[1], (Context|Get));
  187. if(e == AL_INVALID)
  188. {
  189. Con::errorf(ConsoleLogEntry::General, "alGetString: invalid enum name '%s'", argv[1]);
  190. return "";
  191. }
  192. return (const char*)alGetString(e);
  193. }
  194. //--------------------------------------------------------------------------
  195. // Soundfile
  196. //--------------------------------------------------------------------------
  197. /*! Use the alxGetAudioLength function to get the play-length of a specified resource sound file in milliseconds.
  198. @param audio-assetId The asset Id that specifies the audio file to check.
  199. @return Returns play-length of the audio file specified by audio-assetId in milliseconds.
  200. @sa alxGetStreamDuration, alxGetStreamPosition
  201. */
  202. ConsoleFunctionWithDocs(alxGetAudioLength, ConsoleInt, 2, 2, ( audio-assetId ))
  203. {
  204. // Fetch asset Id.
  205. const char* pAssetId = argv[1];
  206. // Acquire audio asset.
  207. AudioAsset* pAudioAsset = AssetDatabase.acquireAsset<AudioAsset>( pAssetId );
  208. // Did we get the audio asset?
  209. if ( pAudioAsset == NULL )
  210. {
  211. // No, so warn.
  212. Con::warnf( "alxGetAudioLength() - Could not find audio asset '%s'.", pAssetId );
  213. return 0;
  214. }
  215. Resource<AudioBuffer> buffer = AudioBuffer::find( pAudioAsset->getAudioFile() );
  216. if ( !buffer.isNull() )
  217. {
  218. ALuint alBuffer = buffer->getALBuffer();
  219. return alxGetWaveLen( alBuffer );
  220. }
  221. // Warn.
  222. Con::warnf( "alxGetAudioLength() - Could not find audio file '%s' for asset '%s'.", pAudioAsset->getAudioFile(), pAssetId );
  223. return 0;
  224. }
  225. //--------------------------------------------------------------------------
  226. // Source
  227. //--------------------------------------------------------------------------
  228. /*! Create a source from the specified asset Id.
  229. @param audio-assetId The asset Id to create the source from.
  230. @return The handle of the created source or 0 on error.
  231. */
  232. ConsoleFunctionWithDocs(alxCreateSource, ConsoleInt, 2, 2, (audio-assetId))
  233. {
  234. // Fetch asset Id.
  235. const char* pAssetId = argv[1];
  236. // Acquire audio asset.
  237. AudioAsset* pAudioAsset = AssetDatabase.acquireAsset<AudioAsset>( pAssetId );
  238. // Did we get the audio asset?
  239. if ( pAudioAsset == NULL )
  240. {
  241. // No, so warn.
  242. Con::warnf( "alxCreateSource() - Could not find audio asset '%s'.", pAssetId );
  243. return NULL_AUDIOHANDLE;
  244. }
  245. // Fetch audio handle.
  246. AUDIOHANDLE handle = alxCreateSource( pAudioAsset );
  247. // Release asset.
  248. AssetDatabase.releaseAsset( pAssetId );
  249. return handle;
  250. }
  251. //-----------------------------------------------
  252. /*! Use the alxSource* function to set a source parameter(s) as specified by the OpenAL enumerated type ALEnum.
  253. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  254. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  255. @param value An ALEnum type specific value corresponding to the new value for this enumerated parameters.
  256. @return No return value.
  257. @sa alxGetSource*, al*Listener*
  258. */
  259. ConsoleFunctionWithDocs(alxSourcef, ConsoleVoid, 4, 4, ( handle , ALEnum , value ))
  260. {
  261. ALenum e = getEnum(argv[2], (Source|Set|Float));
  262. if(e == AL_INVALID)
  263. {
  264. Con::errorf(ConsoleLogEntry::General, "cAudio_alxSourcef: invalid enum name '%s'", argv[2]);
  265. return;
  266. }
  267. alxSourcef(dAtoi(argv[1]), e, dAtof(argv[3]));
  268. }
  269. //-----------------------------------------------
  270. /*! Use the alxSource* function to set a source parameter(s) as specified by the OpenAL enumerated type ALEnum.
  271. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  272. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  273. @param x,y,z XYZ floating-point coordinates.
  274. @return No return value.
  275. @sa alxGetSource*, al*Listener*
  276. */
  277. ConsoleFunctionWithDocs(alxSource3f, ConsoleVoid, 3, 6, ( handle , ALEnum , x , y , z ))
  278. {
  279. ALenum e = getEnum(argv[2], (Source|Set|Float3));
  280. if(e == AL_INVALID)
  281. {
  282. Con::errorf(ConsoleLogEntry::General, "cAudio_alxSource3f: invalid enum name '%s'", argv[2]);
  283. return;
  284. }
  285. if((argc != 4 && argc != 6))
  286. {
  287. Con::errorf(ConsoleLogEntry::General, "cAudio_alxSource3f: wrong number of args");
  288. return;
  289. }
  290. Point3F pos;
  291. if(argc == 4)
  292. dSscanf(argv[3], "%g %g %g", &pos.x, &pos.y, &pos.z);
  293. else
  294. {
  295. pos.x = dAtof(argv[3]);
  296. pos.y = dAtof(argv[4]);
  297. pos.z = dAtof(argv[5]);
  298. }
  299. alxSource3f(dAtoi(argv[1]), e, pos.x, pos.y, pos.z);
  300. }
  301. //-----------------------------------------------
  302. /*! Use the alxSource* function to set a source parameter(s) as specified by the OpenAL enumerated type ALEnum.
  303. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  304. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  305. @param value An ALEnum type specific value corresponding to the new value for this enumerated parameters.
  306. @return No return value.
  307. @sa alxGetSource*, al*Listener*
  308. */
  309. ConsoleFunctionWithDocs(alxSourcei, ConsoleVoid, 4, 4, ( handle , ALEnum , value ))
  310. {
  311. ALenum e = getEnum(argv[2], (Source|Set|Int));
  312. if(e == AL_INVALID)
  313. {
  314. Con::errorf(ConsoleLogEntry::General, "cAudio_alxSourcei: invalid enum name '%s'", argv[2]);
  315. return;
  316. }
  317. alxSourcei(dAtoi(argv[1]), e, static_cast<ALint>(dAtoi(argv[3])));
  318. }
  319. //-----------------------------------------------
  320. /*! Use the alxGetSource* function to get the current value of a source parameter, as specified by ALEnum.
  321. Depending on the ALEnum you need to acquire, be sure to use the correct version (i.e. correct return type) of alxGetSource*.
  322. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  323. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  324. @return Returns current value of parameter specified by ALEnum for source identified by handle.
  325. @sa alxSource*, al*GetListener*
  326. */
  327. ConsoleFunctionWithDocs(alxGetSourcef, ConsoleFloat, 3, 3, ( handle , ALEnum ))
  328. {
  329. ALenum e = getEnum(argv[2], (Source|Get|Float));
  330. if(e == AL_INVALID)
  331. {
  332. Con::errorf(ConsoleLogEntry::General, "cAudio_alxGetSourcef: invalid enum name '%s'", argv[2]);
  333. return(0.f);
  334. }
  335. F32 value;
  336. alxGetSourcef(dAtoi(argv[1]), e, &value);
  337. return(value);
  338. }
  339. //-----------------------------------------------
  340. /*! Use the alxGetSource* function to get the current value of a source parameter, as specified by ALEnum.
  341. Depending on the ALEnum you need to acquire, be sure to use the correct version (i.e. correct return type) of alxGetSource*.
  342. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  343. @param ALEnum A string containing an OpenAL enumerated type name.
  344. @return Returns current value of parameter specified by ALEnum for source identified by handle.
  345. @sa alxSource*, al*GetListener*
  346. */
  347. ConsoleFunctionWithDocs(alxGetSource3f, ConsoleString, 3, 3, ( handle , ALEnum ))
  348. {
  349. ALenum e = getEnum(argv[2], (Source|Get|Float));
  350. if(e == AL_INVALID)
  351. {
  352. Con::errorf(ConsoleLogEntry::General, "cAudio_alxGetSource3f: invalid enum name '%s'", argv[2]);
  353. return("0 0 0");
  354. }
  355. F32 value1, value2, value3;
  356. alxGetSource3f(dAtoi(argv[1]), e, &value1, &value2, &value3);
  357. char * ret = Con::getReturnBuffer(64);
  358. dSprintf(ret, 64, "%7.3f %7.3 %7.3", value1, value2, value3);
  359. return(ret);
  360. }
  361. //-----------------------------------------------
  362. /*! Use the alxGetSource* function to get the current value of a source parameter, as specified by ALEnum.
  363. Depending on the ALEnum you need to acquire, be sure to use the correct version (i.e. correct return type) of alxGetSource*.
  364. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  365. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  366. @return Returns current value of parameter specified by ALEnum for source identified by handle.
  367. @sa alxSource*, al*GetListener*
  368. */
  369. ConsoleFunctionWithDocs(alxGetSourcei, ConsoleInt, 3, 3, ( handle , ALEnum ))
  370. {
  371. ALenum e = getEnum(argv[2], (Source|Get|Int));
  372. if(e == AL_INVALID)
  373. {
  374. Con::errorf(ConsoleLogEntry::General, "cAudio_alxGetSourcei: invalid enum name '%s'", argv[2]);
  375. return(0);
  376. }
  377. ALint value;
  378. alxGetSourcei(dAtoi(argv[1]), e, &value);
  379. return(static_cast<S32>(value));
  380. }
  381. //-----------------------------------------------
  382. /*! Play the audio asset Id.
  383. @param audio-assetId The asset Id to play.
  384. @return The handle to the playing audio asset Id or 0 on error.
  385. */
  386. ConsoleFunctionWithDocs(alxPlay, ConsoleInt, 2, 2, (audio-assetId))
  387. {
  388. // Fetch asset Id.
  389. const char* pAssetId = argv[1];
  390. // Acquire audio asset.
  391. AudioAsset* pAudioAsset = AssetDatabase.acquireAsset<AudioAsset>( pAssetId );
  392. // Did we get the audio asset?
  393. if ( pAudioAsset == NULL )
  394. {
  395. // No, so warn.
  396. Con::warnf( "alxPlay() - Could not find audio asset '%s'.", pAssetId );
  397. return NULL_AUDIOHANDLE;
  398. }
  399. // Fetch audio handle.
  400. AUDIOHANDLE handle = alxPlay( pAudioAsset );
  401. // Release asset.
  402. AssetDatabase.releaseAsset( pAssetId );
  403. return handle;
  404. }
  405. /*! Use the alxPause function to pause a currently playing sound as specified by handle.
  406. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  407. @return No return value.
  408. @sa alxIsPlaying, alxPlay, alxStopAll
  409. */
  410. ConsoleFunctionWithDocs(alxPause, ConsoleBool, 2, 2, ( handle ))
  411. {
  412. AUDIOHANDLE handle = dAtoi(argv[1]);
  413. if(handle == NULL_AUDIOHANDLE)
  414. return false;
  415. return alxPause( handle );
  416. }
  417. /*! Use the alxUnpause function to resume playing a currently paused sound as specified by handle.
  418. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  419. @return No return value.
  420. @sa alxIsPlaying, alxPlay, alxStopAll
  421. */
  422. ConsoleFunctionWithDocs(alxUnpause, ConsoleVoid, 2, 2, ( handle ))
  423. {
  424. AUDIOHANDLE handle = dAtoi(argv[1]);
  425. if(handle == NULL_AUDIOHANDLE)
  426. return;
  427. alxUnPause( handle );
  428. }
  429. //-----------------------------------------------
  430. /*! Use the alxStop function to stop a currently playing sound as specified by handle.
  431. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  432. @return No return value.
  433. @sa alxIsPlaying, alxPlay, alxStopAll
  434. */
  435. ConsoleFunctionWithDocs(alxStop, ConsoleVoid, 2, 2, ( handle ))
  436. {
  437. AUDIOHANDLE handle = dAtoi(argv[1]);
  438. if(handle == NULL_AUDIOHANDLE)
  439. return;
  440. alxStop(handle);
  441. }
  442. //-----------------------------------------------
  443. /*! Use the alxStopAll function to stop all currently playing sounds associated with registered handles.
  444. @return No return.
  445. @sa alxIsPlaying, alxPlay, alxStop
  446. */
  447. ConsoleFunctionWithDocs(alxStopAll, ConsoleVoid, 1, 1, ())
  448. {
  449. alxStopAll();
  450. }
  451. //-----------------------------------------------
  452. /*! Use the alxIsPlaying function to determine if the sound associated with a previously set-up sound handle is playing or not.
  453. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  454. @return Returns 1 if specified handle is being played, 0 otherwise.
  455. @sa alxPlay, alxStop, alxStopAll
  456. */
  457. ConsoleFunctionWithDocs(alxIsPlaying, ConsoleBool, 2, 5, ( handle ))
  458. {
  459. AUDIOHANDLE handle = dAtoi(argv[1]);
  460. if(handle == NULL_AUDIOHANDLE)
  461. return false;
  462. return alxIsPlaying(handle);
  463. }
  464. //--------------------------------------------------------------------------
  465. // Listener
  466. //--------------------------------------------------------------------------
  467. /*! Use the al*Listener* function to set a listener parameter(s) as specified by the OpenAL enumerated type ALEnum.
  468. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  469. @param value An ALEnum type specific value corresponding to the new value for this enumerated parameters.
  470. @return No return value.
  471. @sa al*GetListener*, alxSource*
  472. */
  473. ConsoleFunctionWithDocs(alxListenerf, ConsoleVoid, 3, 3, ( AlEnum , value ))
  474. {
  475. ALenum e = getEnum(argv[1], (Listener|Set|Float));
  476. if(e == AL_INVALID)
  477. {
  478. Con::errorf(ConsoleLogEntry::General, "alxListenerf: invalid enum name '%s'", argv[1]);
  479. return;
  480. }
  481. alxListenerf(e, dAtof(argv[2]));
  482. }
  483. //-----------------------------------------------
  484. /*! Use the al*Listener* function to set a listener parameter(s) as specified by the OpenAL enumerated type ALEnum.
  485. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  486. @param x,y,z XYZ floating-point coordinates.
  487. @return No return value.
  488. @sa al*GetListener*, alxSource*
  489. */
  490. ConsoleFunctionWithDocs(alListener3f, ConsoleVoid, 3, 5, ( ALEnum , x , y , z))
  491. {
  492. ALenum e = getEnum(argv[1], (Listener|Set|Float3));
  493. if(e == AL_INVALID)
  494. {
  495. Con::errorf(ConsoleLogEntry::General, "alListener3f: invalid enum name '%s'", argv[1]);
  496. return;
  497. }
  498. if(argc != 3 && argc != 5)
  499. {
  500. Con::errorf(ConsoleLogEntry::General, "alListener3f: wrong number of arguments");
  501. return;
  502. }
  503. Point3F pos;
  504. if(argc == 3)
  505. dSscanf(argv[2], "%g %g %g", &pos.x, &pos.y, &pos.z);
  506. else
  507. {
  508. pos.x = dAtof(argv[2]);
  509. pos.y = dAtof(argv[3]);
  510. pos.z = dAtof(argv[4]);
  511. }
  512. alxListenerPoint3F(e, &pos);
  513. }
  514. //-----------------------------------------------
  515. /*! Use the al*GetListener* function to get the current value of a listener parameter, as specified by ALEnum.
  516. Depending on the ALEnum you need to acquire, be sure to use the correct version (i.e. correct return type) of al*GetListener*.
  517. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  518. @return Returns a float (alxGetListenerf), a vector of three floats (alGetListener3f), or an integer value respectively (alGetListeneri).
  519. @sa alxGetSource*
  520. */
  521. ConsoleFunctionWithDocs(alxGetListenerf, ConsoleFloat, 2, 2, ( ALEnum ))
  522. {
  523. ALenum e = getEnum(argv[1], (Source|Get|Float));
  524. if(e == AL_INVALID)
  525. {
  526. Con::errorf(ConsoleLogEntry::General, "alxGetListenerf: invalid enum name '%s'", argv[1]);
  527. return(0.f);
  528. }
  529. F32 value;
  530. alxGetListenerf(e, &value);
  531. return(value);
  532. }
  533. //-----------------------------------------------
  534. /*! Use the al*GetListener* function to get the current value of a listener parameter, as specified by ALEnum.
  535. Depending on the ALEnum you need to acquire, be sure to use the correct version (i.e. correct return type) of al*GetListener*.
  536. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  537. @return Returns a float (alxGetListenerf), a vector of three floats (alGetListener3f), or an integer value respectively (alGetListeneri).
  538. @sa alxGetSource*
  539. */
  540. ConsoleFunctionWithDocs(alGetListener3f, ConsoleString, 2, 2, ( ALEnum ))
  541. {
  542. ALenum e = getEnum(argv[2], (Source|Get|Float));
  543. if(e == AL_INVALID)
  544. {
  545. Con::errorf(ConsoleLogEntry::General, "alGetListener3f: invalid enum name '%s'", argv[1]);
  546. return("0 0 0");
  547. }
  548. Point3F v;
  549. alxGetListenerPoint3F(e, &v);
  550. char * ret = Con::getReturnBuffer(64);
  551. dSprintf(ret, 64, "%7.3f %7.3 %7.3", v.x, v.y, v.z);
  552. return(ret);
  553. }
  554. //-----------------------------------------------
  555. /*! Use the al*GetListener* function to get the current value of a listener parameter, as specified by ALEnum.
  556. Depending on the ALEnum you need to acquire, be sure to use the correct version (i.e. correct return type) of al*GetListener*.
  557. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  558. @return Returns a float (alxGetListenerf), a vector of three floats (alGetListener3f), or an integer value respectively (alGetListeneri).
  559. @sa alxGetSource*
  560. */
  561. ConsoleFunctionWithDocs(alGetListeneri, ConsoleInt, 2, 2, ( ALEnum ))
  562. {
  563. ALenum e = getEnum(argv[1], (Source|Get|Int));
  564. if(e == AL_INVALID)
  565. {
  566. Con::errorf(ConsoleLogEntry::General, "alGetListeneri: invalid enum name '%s'", argv[1]);
  567. return(0);
  568. }
  569. S32 value;
  570. alGetListeneri(e, &value);
  571. return(value);
  572. }
  573. //--------------------------------------------------------------------------
  574. // Channel Volumes
  575. //--------------------------------------------------------------------------
  576. /*! Use the alxGetChannelVolume function to get the volume setting for a specified channel.
  577. @param channelID An integer value, equal to or greater than 0, corresponding to a valid audio channel.
  578. @return Returns volume [ 0.0, 1.0 ] for channel specified by channelID.
  579. @sa alxSetChannelVolume
  580. */
  581. ConsoleFunctionWithDocs(alxGetChannelVolume, ConsoleFloat, 2, 2, ( channelID ))
  582. {
  583. U32 channel = dAtoi(argv[1]);
  584. if(channel >= Audio::AudioVolumeChannels)
  585. {
  586. Con::errorf(ConsoleLogEntry::General, "alxGetChannelVolume: invalid channel '%d'", dAtoi(argv[1]));
  587. return(0.f);
  588. }
  589. return(mAudioChannelVolumes[channel]);
  590. }
  591. //-----------------------------------------------
  592. /*! Use the alxSetChannelVolume function to set a volume [ 0.0, 1.0 ] for the channel specified by channelID.
  593. @param channelID An integer value, equal to or greater than 0, corresponding to a valid audio channel.
  594. @param volume A value between 0.0 and 1.0 specifying the new volume for the specified channel.
  595. @return Returns true on success and false on failure.
  596. @sa alxGetChannelVolume
  597. */
  598. ConsoleFunctionWithDocs(alxSetChannelVolume, ConsoleBool, 3, 3, ( channelD , volume ))
  599. {
  600. U32 channel = dAtoi(argv[1]);
  601. F32 volume = mClampF(dAtof(argv[2]), 0.f, 1.f);
  602. if(channel >= Audio::AudioVolumeChannels)
  603. {
  604. Con::errorf(ConsoleLogEntry::General, "alxSetChannelVolume: channel '%d' out of range [0, %d]", channel, Audio::AudioVolumeChannels);
  605. return false;
  606. }
  607. mAudioChannelVolumes[channel] = volume;
  608. alxUpdateTypeGain(channel);
  609. return true;
  610. }
  611. //-----------------------------------------------
  612. /*! Use the alxGetStreamPosition function to get the current play position for a playing sound. Note, this value is a percentage equivalent to the percent of the sound that as already played.
  613. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  614. @return Returns -1 for invalid handle, and 0.0 to 1.0 for valid handle indicating what percentage of the sound file has been played.
  615. @sa alxGetStreamDuration
  616. */
  617. ConsoleFunctionWithDocs(alxGetStreamPosition, ConsoleFloat, 2, 2, ( handle ))
  618. {
  619. AUDIOHANDLE handle = dAtoi(argv[1]);
  620. if(handle == NULL_AUDIOHANDLE)
  621. return -1;
  622. return alxGetStreamPosition( handle );
  623. }
  624. //-----------------------------------------------
  625. /*! Use the alxGetStreamDuration function to determine the length of a previously set up sound in seconds.
  626. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  627. @return Returns -1 for invalid handle, and 0.0 to N.M for valid handle indicating length of scheduled sound in seconds.
  628. @sa alxGetStreamPosition
  629. */
  630. ConsoleFunctionWithDocs(alxGetStreamDuration, ConsoleFloat, 2, 2, ( handle ))
  631. {
  632. AUDIOHANDLE handle = dAtoi(argv[1]);
  633. if(handle == NULL_AUDIOHANDLE)
  634. return -1;
  635. return alxGetStreamDuration( handle );
  636. }
  637. #ifdef TORQUE_OS_IOS
  638. /*! Play the audio asset Id.
  639. @param audio-assetId The asset Id to play. This *must* be an MP3 to work correctly.
  640. @return The playing stream Id.
  641. */
  642. ConsoleFunctionWithDocs(startiOSAudioStream, ConsoleInt, 2, 2, (audio-assetId))
  643. {
  644. // Fetch asset Id.
  645. const char* pAssetId = argv[1];
  646. // Acquire audio asset.
  647. AudioAsset* pAudioAsset = AssetDatabase.acquireAsset<AudioAsset>( pAssetId );
  648. // Did we get the audio asset?
  649. if ( pAudioAsset == NULL )
  650. {
  651. // No, so warn.
  652. Con::warnf( "startiOSAudioStream() - Could not find audio asset '%s'.", pAssetId );
  653. return 0;
  654. }
  655. // Fetch the audio filename,
  656. char fileName[1024];
  657. Con::expandPath( fileName, sizeof(fileName), pAudioAsset->getAudioFile() );
  658. iOSStreamSource* pStream = new iOSStreamSource( fileName );
  659. pStream->start( pAudioAsset->getLooping() );
  660. // Release asset.
  661. AssetDatabase.releaseAsset( pAssetId );
  662. return pStream->getId();
  663. }
  664. //-----------------------------------------------
  665. /*! Stops playing the audio stream Id.
  666. */
  667. ConsoleFunctionWithDocs(stopiOSAudioStream, ConsoleVoid, 2, 2, ( streamId ))
  668. {
  669. SimObjectId streamId = dAtoi( argv[1] );
  670. iOSStreamSource* pStream = Sim::findObject<iOSStreamSource>( streamId );
  671. if( pStream != NULL )
  672. {
  673. if( pStream->isPlaying() )
  674. {
  675. pStream->stop();
  676. }
  677. pStream->deleteObject();
  678. }
  679. }
  680. /*!
  681. */
  682. ConsoleFunctionWithDocs(setiOSAudioStreamVolume, ConsoleVoid, 3, 3, setiPhoneAudioVolume( Stream ID, float volume ))
  683. {
  684. SimObjectId streamId = dAtoi( argv[1] );
  685. iOSStreamSource* pStream = Sim::findObject<iOSStreamSource>( streamId );
  686. F32 volume = dAtof( argv[2] );
  687. if( pStream ) {
  688. if( pStream->isPlaying() ) {
  689. pStream->setVolume(volume);
  690. }
  691. }
  692. }
  693. #endif
  694. /*! @} */ // group AudioFunctions