audio_ScriptBinding.cc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. ResourceManager->registerExtension(".ogg", AudioBuffer::construct);
  157. }
  158. registered = true;
  159. return true;
  160. }
  161. return false;
  162. }
  163. //-----------------------------------------------
  164. /*! Use the OpenALShutdownDriver function to stop/shut down the OpenAL driver.
  165. After this is called, you must restart the driver with OpenALInitDriver to execute any new sound operations.
  166. @return No return value.
  167. @sa OpenALInitDriver
  168. */
  169. ConsoleFunctionWithDocs(OpenALShutdownDriver, ConsoleVoid, 1, 1, ())
  170. {
  171. Audio::OpenALShutdown();
  172. }
  173. //-----------------------------------------------
  174. /*!
  175. @note Currently does nothing (possibly deprecated)
  176. */
  177. ConsoleFunctionWithDocs(OpenALRegisterExtensions, ConsoleVoid, 1, 1, ())
  178. {
  179. }
  180. //-----------------------------------------------
  181. /*! Use the alGetString function to get the string equivalent to the specified OpenAL enumerated value.
  182. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  183. @return Returns a string corresponding to the passed ALEnum
  184. */
  185. ConsoleFunctionWithDocs(alGetString, ConsoleString, 2, 2, ( ALEnum ))
  186. {
  187. ALenum e = getEnum(argv[1], (Context|Get));
  188. if(e == AL_INVALID)
  189. {
  190. Con::errorf(ConsoleLogEntry::General, "alGetString: invalid enum name '%s'", argv[1]);
  191. return "";
  192. }
  193. return (const char*)alGetString(e);
  194. }
  195. //--------------------------------------------------------------------------
  196. // Soundfile
  197. //--------------------------------------------------------------------------
  198. /*! Use the alxGetAudioLength function to get the play-length of a specified resource sound file in milliseconds.
  199. @param audio-assetId The asset Id that specifies the audio file to check.
  200. @return Returns play-length of the audio file specified by audio-assetId in milliseconds.
  201. @sa alxGetStreamDuration, alxGetStreamPosition
  202. */
  203. ConsoleFunctionWithDocs(alxGetAudioLength, ConsoleInt, 2, 2, ( audio-assetId ))
  204. {
  205. // Fetch asset Id.
  206. const char* pAssetId = argv[1];
  207. // Acquire audio asset.
  208. AudioAsset* pAudioAsset = AssetDatabase.acquireAsset<AudioAsset>( pAssetId );
  209. // Did we get the audio asset?
  210. if ( pAudioAsset == NULL )
  211. {
  212. // No, so warn.
  213. Con::warnf( "alxGetAudioLength() - Could not find audio asset '%s'.", pAssetId );
  214. return 0;
  215. }
  216. Resource<AudioBuffer> buffer = AudioBuffer::find( pAudioAsset->getAudioFile() );
  217. if ( !buffer.isNull() )
  218. {
  219. ALuint alBuffer = buffer->getALBuffer();
  220. return alxGetWaveLen( alBuffer );
  221. }
  222. // Warn.
  223. Con::warnf( "alxGetAudioLength() - Could not find audio file '%s' for asset '%s'.", pAudioAsset->getAudioFile(), pAssetId );
  224. return 0;
  225. }
  226. //--------------------------------------------------------------------------
  227. // Source
  228. //--------------------------------------------------------------------------
  229. /*! Create a source from the specified asset Id.
  230. @param audio-assetId The asset Id to create the source from.
  231. @return The handle of the created source or 0 on error.
  232. */
  233. ConsoleFunctionWithDocs(alxCreateSource, ConsoleInt, 2, 2, (audio-assetId))
  234. {
  235. // Fetch asset Id.
  236. const char* pAssetId = argv[1];
  237. // Acquire audio asset.
  238. AudioAsset* pAudioAsset = AssetDatabase.acquireAsset<AudioAsset>( pAssetId );
  239. // Did we get the audio asset?
  240. if ( pAudioAsset == NULL )
  241. {
  242. // No, so warn.
  243. Con::warnf( "alxCreateSource() - Could not find audio asset '%s'.", pAssetId );
  244. return NULL_AUDIOHANDLE;
  245. }
  246. // Fetch audio handle.
  247. AUDIOHANDLE handle = alxCreateSource( pAudioAsset );
  248. // Release asset.
  249. AssetDatabase.releaseAsset( pAssetId );
  250. return handle;
  251. }
  252. //-----------------------------------------------
  253. /*! Use the alxSource* function to set a source parameter(s) as specified by the OpenAL enumerated type ALEnum.
  254. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  255. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  256. @param value An ALEnum type specific value corresponding to the new value for this enumerated parameters.
  257. @return No return value.
  258. @sa alxGetSource*, al*Listener*
  259. */
  260. ConsoleFunctionWithDocs(alxSourcef, ConsoleVoid, 4, 4, ( handle , ALEnum , value ))
  261. {
  262. ALenum e = getEnum(argv[2], (Source|Set|Float));
  263. if(e == AL_INVALID)
  264. {
  265. Con::errorf(ConsoleLogEntry::General, "cAudio_alxSourcef: invalid enum name '%s'", argv[2]);
  266. return;
  267. }
  268. alxSourcef(dAtoi(argv[1]), e, dAtof(argv[3]));
  269. }
  270. //-----------------------------------------------
  271. /*! Use the alxSource* function to set a source parameter(s) as specified by the OpenAL enumerated type ALEnum.
  272. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  273. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  274. @param x,y,z XYZ floating-point coordinates.
  275. @return No return value.
  276. @sa alxGetSource*, al*Listener*
  277. */
  278. ConsoleFunctionWithDocs(alxSource3f, ConsoleVoid, 3, 6, ( handle , ALEnum , x , y , z ))
  279. {
  280. ALenum e = getEnum(argv[2], (Source|Set|Float3));
  281. if(e == AL_INVALID)
  282. {
  283. Con::errorf(ConsoleLogEntry::General, "cAudio_alxSource3f: invalid enum name '%s'", argv[2]);
  284. return;
  285. }
  286. if((argc != 4 && argc != 6))
  287. {
  288. Con::errorf(ConsoleLogEntry::General, "cAudio_alxSource3f: wrong number of args");
  289. return;
  290. }
  291. Point3F pos;
  292. if(argc == 4)
  293. dSscanf(argv[3], "%g %g %g", &pos.x, &pos.y, &pos.z);
  294. else
  295. {
  296. pos.x = dAtof(argv[3]);
  297. pos.y = dAtof(argv[4]);
  298. pos.z = dAtof(argv[5]);
  299. }
  300. alxSource3f(dAtoi(argv[1]), e, pos.x, pos.y, pos.z);
  301. }
  302. //-----------------------------------------------
  303. /*! Use the alxSource* function to set a source parameter(s) as specified by the OpenAL enumerated type ALEnum.
  304. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  305. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  306. @param value An ALEnum type specific value corresponding to the new value for this enumerated parameters.
  307. @return No return value.
  308. @sa alxGetSource*, al*Listener*
  309. */
  310. ConsoleFunctionWithDocs(alxSourcei, ConsoleVoid, 4, 4, ( handle , ALEnum , value ))
  311. {
  312. ALenum e = getEnum(argv[2], (Source|Set|Int));
  313. if(e == AL_INVALID)
  314. {
  315. Con::errorf(ConsoleLogEntry::General, "cAudio_alxSourcei: invalid enum name '%s'", argv[2]);
  316. return;
  317. }
  318. alxSourcei(dAtoi(argv[1]), e, static_cast<ALint>(dAtoi(argv[3])));
  319. }
  320. //-----------------------------------------------
  321. /*! Use the alxGetSource* function to get the current value of a source parameter, as specified by ALEnum.
  322. Depending on the ALEnum you need to acquire, be sure to use the correct version (i.e. correct return type) of alxGetSource*.
  323. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  324. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  325. @return Returns current value of parameter specified by ALEnum for source identified by handle.
  326. @sa alxSource*, al*GetListener*
  327. */
  328. ConsoleFunctionWithDocs(alxGetSourcef, ConsoleFloat, 3, 3, ( handle , ALEnum ))
  329. {
  330. ALenum e = getEnum(argv[2], (Source|Get|Float));
  331. if(e == AL_INVALID)
  332. {
  333. Con::errorf(ConsoleLogEntry::General, "cAudio_alxGetSourcef: invalid enum name '%s'", argv[2]);
  334. return(0.f);
  335. }
  336. F32 value;
  337. alxGetSourcef(dAtoi(argv[1]), e, &value);
  338. return(value);
  339. }
  340. //-----------------------------------------------
  341. /*! Use the alxGetSource* function to get the current value of a source parameter, as specified by ALEnum.
  342. Depending on the ALEnum you need to acquire, be sure to use the correct version (i.e. correct return type) of alxGetSource*.
  343. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  344. @param ALEnum A string containing an OpenAL enumerated type name.
  345. @return Returns current value of parameter specified by ALEnum for source identified by handle.
  346. @sa alxSource*, al*GetListener*
  347. */
  348. ConsoleFunctionWithDocs(alxGetSource3f, ConsoleString, 3, 3, ( handle , ALEnum ))
  349. {
  350. ALenum e = getEnum(argv[2], (Source|Get|Float));
  351. if(e == AL_INVALID)
  352. {
  353. Con::errorf(ConsoleLogEntry::General, "cAudio_alxGetSource3f: invalid enum name '%s'", argv[2]);
  354. return("0 0 0");
  355. }
  356. F32 value1, value2, value3;
  357. alxGetSource3f(dAtoi(argv[1]), e, &value1, &value2, &value3);
  358. char * ret = Con::getReturnBuffer(64);
  359. dSprintf(ret, 64, "%7.3f %7.3 %7.3", value1, value2, value3);
  360. return(ret);
  361. }
  362. //-----------------------------------------------
  363. /*! Use the alxGetSource* function to get the current value of a source parameter, as specified by ALEnum.
  364. Depending on the ALEnum you need to acquire, be sure to use the correct version (i.e. correct return type) of alxGetSource*.
  365. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  366. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  367. @return Returns current value of parameter specified by ALEnum for source identified by handle.
  368. @sa alxSource*, al*GetListener*
  369. */
  370. ConsoleFunctionWithDocs(alxGetSourcei, ConsoleInt, 3, 3, ( handle , ALEnum ))
  371. {
  372. ALenum e = getEnum(argv[2], (Source|Get|Int));
  373. if(e == AL_INVALID)
  374. {
  375. Con::errorf(ConsoleLogEntry::General, "cAudio_alxGetSourcei: invalid enum name '%s'", argv[2]);
  376. return(0);
  377. }
  378. ALint value;
  379. alxGetSourcei(dAtoi(argv[1]), e, &value);
  380. return(static_cast<S32>(value));
  381. }
  382. //-----------------------------------------------
  383. /*! Play the audio asset Id.
  384. @param audio-assetId The asset Id to play.
  385. @return The handle to the playing audio asset Id or 0 on error.
  386. */
  387. ConsoleFunctionWithDocs(alxPlay, ConsoleInt, 2, 2, (audio-assetId))
  388. {
  389. // Fetch asset Id.
  390. const char* pAssetId = argv[1];
  391. // Acquire audio asset.
  392. AudioAsset* pAudioAsset = AssetDatabase.acquireAsset<AudioAsset>( pAssetId );
  393. // Did we get the audio asset?
  394. if ( pAudioAsset == NULL )
  395. {
  396. // No, so warn.
  397. Con::warnf( "alxPlay() - Could not find audio asset '%s'.", pAssetId );
  398. return NULL_AUDIOHANDLE;
  399. }
  400. // Fetch audio handle.
  401. AUDIOHANDLE handle = alxPlay( pAudioAsset );
  402. // Release asset.
  403. AssetDatabase.releaseAsset( pAssetId );
  404. return handle;
  405. }
  406. /*! Use the alxPause function to pause a currently playing sound as specified by handle.
  407. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  408. @return No return value.
  409. @sa alxIsPlaying, alxPlay, alxStopAll
  410. */
  411. ConsoleFunctionWithDocs(alxPause, ConsoleBool, 2, 2, ( handle ))
  412. {
  413. AUDIOHANDLE handle = dAtoi(argv[1]);
  414. if(handle == NULL_AUDIOHANDLE)
  415. return false;
  416. return alxPause( handle );
  417. }
  418. /*! Use the alxUnpause function to resume playing a currently paused sound as specified by handle.
  419. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  420. @return No return value.
  421. @sa alxIsPlaying, alxPlay, alxStopAll
  422. */
  423. ConsoleFunctionWithDocs(alxUnpause, ConsoleVoid, 2, 2, ( handle ))
  424. {
  425. AUDIOHANDLE handle = dAtoi(argv[1]);
  426. if(handle == NULL_AUDIOHANDLE)
  427. return;
  428. alxUnPause( handle );
  429. }
  430. //-----------------------------------------------
  431. /*! Use the alxStop function to stop a currently playing sound as specified by handle.
  432. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  433. @return No return value.
  434. @sa alxIsPlaying, alxPlay, alxStopAll
  435. */
  436. ConsoleFunctionWithDocs(alxStop, ConsoleVoid, 2, 2, ( handle ))
  437. {
  438. AUDIOHANDLE handle = dAtoi(argv[1]);
  439. if(handle == NULL_AUDIOHANDLE)
  440. return;
  441. alxStop(handle);
  442. }
  443. //-----------------------------------------------
  444. /*! Use the alxStopAll function to stop all currently playing sounds associated with registered handles.
  445. @return No return.
  446. @sa alxIsPlaying, alxPlay, alxStop
  447. */
  448. ConsoleFunctionWithDocs(alxStopAll, ConsoleVoid, 1, 1, ())
  449. {
  450. alxStopAll();
  451. }
  452. //-----------------------------------------------
  453. /*! Use the alxIsPlaying function to determine if the sound associated with a previously set-up sound handle is playing or not.
  454. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  455. @return Returns 1 if specified handle is being played, 0 otherwise.
  456. @sa alxPlay, alxStop, alxStopAll
  457. */
  458. ConsoleFunctionWithDocs(alxIsPlaying, ConsoleBool, 2, 5, ( handle ))
  459. {
  460. AUDIOHANDLE handle = dAtoi(argv[1]);
  461. if(handle == NULL_AUDIOHANDLE)
  462. return false;
  463. return alxIsPlaying(handle);
  464. }
  465. //--------------------------------------------------------------------------
  466. // Listener
  467. //--------------------------------------------------------------------------
  468. /*! Use the al*Listener* function to set a listener parameter(s) as specified by the OpenAL enumerated type ALEnum.
  469. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  470. @param value An ALEnum type specific value corresponding to the new value for this enumerated parameters.
  471. @return No return value.
  472. @sa al*GetListener*, alxSource*
  473. */
  474. ConsoleFunctionWithDocs(alxListenerf, ConsoleVoid, 3, 3, ( AlEnum , value ))
  475. {
  476. ALenum e = getEnum(argv[1], (Listener|Set|Float));
  477. if(e == AL_INVALID)
  478. {
  479. Con::errorf(ConsoleLogEntry::General, "alxListenerf: invalid enum name '%s'", argv[1]);
  480. return;
  481. }
  482. alxListenerf(e, dAtof(argv[2]));
  483. }
  484. //-----------------------------------------------
  485. /*! Use the al*Listener* function to set a listener parameter(s) as specified by the OpenAL enumerated type ALEnum.
  486. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  487. @param x,y,z XYZ floating-point coordinates.
  488. @return No return value.
  489. @sa al*GetListener*, alxSource*
  490. */
  491. ConsoleFunctionWithDocs(alListener3f, ConsoleVoid, 3, 5, ( ALEnum , x , y , z))
  492. {
  493. ALenum e = getEnum(argv[1], (Listener|Set|Float3));
  494. if(e == AL_INVALID)
  495. {
  496. Con::errorf(ConsoleLogEntry::General, "alListener3f: invalid enum name '%s'", argv[1]);
  497. return;
  498. }
  499. if(argc != 3 && argc != 5)
  500. {
  501. Con::errorf(ConsoleLogEntry::General, "alListener3f: wrong number of arguments");
  502. return;
  503. }
  504. Point3F pos;
  505. if(argc == 3)
  506. dSscanf(argv[2], "%g %g %g", &pos.x, &pos.y, &pos.z);
  507. else
  508. {
  509. pos.x = dAtof(argv[2]);
  510. pos.y = dAtof(argv[3]);
  511. pos.z = dAtof(argv[4]);
  512. }
  513. alxListenerPoint3F(e, &pos);
  514. }
  515. //-----------------------------------------------
  516. /*! Use the al*GetListener* function to get the current value of a listener parameter, as specified by ALEnum.
  517. Depending on the ALEnum you need to acquire, be sure to use the correct version (i.e. correct return type) of al*GetListener*.
  518. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  519. @return Returns a float (alxGetListenerf), a vector of three floats (alGetListener3f), or an integer value respectively (alGetListeneri).
  520. @sa alxGetSource*
  521. */
  522. ConsoleFunctionWithDocs(alxGetListenerf, ConsoleFloat, 2, 2, ( ALEnum ))
  523. {
  524. ALenum e = getEnum(argv[1], (Source|Get|Float));
  525. if(e == AL_INVALID)
  526. {
  527. Con::errorf(ConsoleLogEntry::General, "alxGetListenerf: invalid enum name '%s'", argv[1]);
  528. return(0.f);
  529. }
  530. F32 value;
  531. alxGetListenerf(e, &value);
  532. return(value);
  533. }
  534. //-----------------------------------------------
  535. /*! Use the al*GetListener* function to get the current value of a listener parameter, as specified by ALEnum.
  536. Depending on the ALEnum you need to acquire, be sure to use the correct version (i.e. correct return type) of al*GetListener*.
  537. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  538. @return Returns a float (alxGetListenerf), a vector of three floats (alGetListener3f), or an integer value respectively (alGetListeneri).
  539. @sa alxGetSource*
  540. */
  541. ConsoleFunctionWithDocs(alGetListener3f, ConsoleString, 2, 2, ( ALEnum ))
  542. {
  543. ALenum e = getEnum(argv[2], (Source|Get|Float));
  544. if(e == AL_INVALID)
  545. {
  546. Con::errorf(ConsoleLogEntry::General, "alGetListener3f: invalid enum name '%s'", argv[1]);
  547. return("0 0 0");
  548. }
  549. Point3F v;
  550. alxGetListenerPoint3F(e, &v);
  551. char * ret = Con::getReturnBuffer(64);
  552. dSprintf(ret, 64, "%7.3f %7.3 %7.3", v.x, v.y, v.z);
  553. return(ret);
  554. }
  555. //-----------------------------------------------
  556. /*! Use the al*GetListener* function to get the current value of a listener parameter, as specified by ALEnum.
  557. Depending on the ALEnum you need to acquire, be sure to use the correct version (i.e. correct return type) of al*GetListener*.
  558. @param ALEnum A string containing an OpenAL enumerated type name. See (above) table of ALEnum values for legal values.
  559. @return Returns a float (alxGetListenerf), a vector of three floats (alGetListener3f), or an integer value respectively (alGetListeneri).
  560. @sa alxGetSource*
  561. */
  562. ConsoleFunctionWithDocs(alGetListeneri, ConsoleInt, 2, 2, ( ALEnum ))
  563. {
  564. ALenum e = getEnum(argv[1], (Source|Get|Int));
  565. if(e == AL_INVALID)
  566. {
  567. Con::errorf(ConsoleLogEntry::General, "alGetListeneri: invalid enum name '%s'", argv[1]);
  568. return(0);
  569. }
  570. S32 value;
  571. alGetListeneri(e, &value);
  572. return(value);
  573. }
  574. //--------------------------------------------------------------------------
  575. // Channel Volumes
  576. //--------------------------------------------------------------------------
  577. /*! Use the alxGetChannelVolume function to get the volume setting for a specified channel.
  578. @param channelID An integer value, equal to or greater than 0, corresponding to a valid audio channel.
  579. @return Returns volume [ 0.0, 1.0 ] for channel specified by channelID.
  580. @sa alxSetChannelVolume
  581. */
  582. ConsoleFunctionWithDocs(alxGetChannelVolume, ConsoleFloat, 2, 2, ( channelID ))
  583. {
  584. U32 channel = dAtoi(argv[1]);
  585. if(channel >= Audio::AudioVolumeChannels)
  586. {
  587. Con::errorf(ConsoleLogEntry::General, "alxGetChannelVolume: invalid channel '%d'", dAtoi(argv[1]));
  588. return(0.f);
  589. }
  590. return(mAudioChannelVolumes[channel]);
  591. }
  592. //-----------------------------------------------
  593. /*! Use the alxSetChannelVolume function to set a volume [ 0.0, 1.0 ] for the channel specified by channelID.
  594. @param channelID An integer value, equal to or greater than 0, corresponding to a valid audio channel.
  595. @param volume A value between 0.0 and 1.0 specifying the new volume for the specified channel.
  596. @return Returns true on success and false on failure.
  597. @sa alxGetChannelVolume
  598. */
  599. ConsoleFunctionWithDocs(alxSetChannelVolume, ConsoleBool, 3, 3, ( channelD , volume ))
  600. {
  601. U32 channel = dAtoi(argv[1]);
  602. F32 volume = mClampF(dAtof(argv[2]), 0.f, 1.f);
  603. if(channel >= Audio::AudioVolumeChannels)
  604. {
  605. Con::errorf(ConsoleLogEntry::General, "alxSetChannelVolume: channel '%d' out of range [0, %d]", channel, Audio::AudioVolumeChannels);
  606. return false;
  607. }
  608. mAudioChannelVolumes[channel] = volume;
  609. alxUpdateTypeGain(channel);
  610. return true;
  611. }
  612. //-----------------------------------------------
  613. /*! 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.
  614. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  615. @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.
  616. @sa alxGetStreamDuration
  617. */
  618. ConsoleFunctionWithDocs(alxGetStreamPosition, ConsoleFloat, 2, 2, ( handle ))
  619. {
  620. AUDIOHANDLE handle = dAtoi(argv[1]);
  621. if(handle == NULL_AUDIOHANDLE)
  622. return -1;
  623. return alxGetStreamPosition( handle );
  624. }
  625. //-----------------------------------------------
  626. /*! Use the alxGetStreamDuration function to determine the length of a previously set up sound in seconds.
  627. @param handle The ID (a non-negative integer) corresponding to a previously set up sound source.
  628. @return Returns -1 for invalid handle, and 0.0 to N.M for valid handle indicating length of scheduled sound in seconds.
  629. @sa alxGetStreamPosition
  630. */
  631. ConsoleFunctionWithDocs(alxGetStreamDuration, ConsoleFloat, 2, 2, ( handle ))
  632. {
  633. AUDIOHANDLE handle = dAtoi(argv[1]);
  634. if(handle == NULL_AUDIOHANDLE)
  635. return -1;
  636. return alxGetStreamDuration( handle );
  637. }
  638. #ifdef TORQUE_OS_IOS
  639. /*! Play the audio asset Id.
  640. @param audio-assetId The asset Id to play. This *must* be an MP3 to work correctly.
  641. @return The playing stream Id.
  642. */
  643. ConsoleFunctionWithDocs(startiOSAudioStream, ConsoleInt, 2, 2, (audio-assetId))
  644. {
  645. // Fetch asset Id.
  646. const char* pAssetId = argv[1];
  647. // Acquire audio asset.
  648. AudioAsset* pAudioAsset = AssetDatabase.acquireAsset<AudioAsset>( pAssetId );
  649. // Did we get the audio asset?
  650. if ( pAudioAsset == NULL )
  651. {
  652. // No, so warn.
  653. Con::warnf( "startiOSAudioStream() - Could not find audio asset '%s'.", pAssetId );
  654. return 0;
  655. }
  656. // Fetch the audio filename,
  657. char fileName[1024];
  658. Con::expandPath( fileName, sizeof(fileName), pAudioAsset->getAudioFile() );
  659. iOSStreamSource* pStream = new iOSStreamSource( fileName );
  660. pStream->start( pAudioAsset->getLooping() );
  661. // Release asset.
  662. AssetDatabase.releaseAsset( pAssetId );
  663. return pStream->getId();
  664. }
  665. //-----------------------------------------------
  666. /*! Stops playing the audio stream Id.
  667. */
  668. ConsoleFunctionWithDocs(stopiOSAudioStream, ConsoleVoid, 2, 2, ( streamId ))
  669. {
  670. SimObjectId streamId = dAtoi( argv[1] );
  671. iOSStreamSource* pStream = Sim::findObject<iOSStreamSource>( streamId );
  672. if( pStream != NULL )
  673. {
  674. if( pStream->isPlaying() )
  675. {
  676. pStream->stop();
  677. }
  678. pStream->deleteObject();
  679. }
  680. }
  681. /*!
  682. */
  683. ConsoleFunctionWithDocs(setiOSAudioStreamVolume, ConsoleVoid, 3, 3, setiPhoneAudioVolume( Stream ID, float volume ))
  684. {
  685. SimObjectId streamId = dAtoi( argv[1] );
  686. iOSStreamSource* pStream = Sim::findObject<iOSStreamSource>( streamId );
  687. F32 volume = dAtof( argv[2] );
  688. if( pStream ) {
  689. if( pStream->isPlaying() ) {
  690. pStream->setVolume(volume);
  691. }
  692. }
  693. }
  694. #endif
  695. /*! @} */ // group AudioFunctions