SoundEngine.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. File: SoundEngine.h
  3. Abstract: These functions play background music tracks, multiple sound effects,
  4. and support stereo panning with a low-latency response.
  5. Version: 1.7
  6. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
  7. ("Apple") in consideration of your agreement to the following terms, and your
  8. use, installation, modification or redistribution of this Apple software
  9. constitutes acceptance of these terms. If you do not agree with these terms,
  10. please do not use, install, modify or redistribute this Apple software.
  11. In consideration of your agreement to abide by the following terms, and subject
  12. to these terms, Apple grants you a personal, non-exclusive license, under
  13. Apple's copyrights in this original Apple software (the "Apple Software"), to
  14. use, reproduce, modify and redistribute the Apple Software, with or without
  15. modifications, in source and/or binary forms; provided that if you redistribute
  16. the Apple Software in its entirety and without modifications, you must retain
  17. this notice and the following text and disclaimers in all such redistributions
  18. of the Apple Software.
  19. Neither the name, trademarks, service marks or logos of Apple Inc. may be used
  20. to endorse or promote products derived from the Apple Software without specific
  21. prior written permission from Apple. Except as expressly stated in this notice,
  22. no other rights or licenses, express or implied, are granted by Apple herein,
  23. including but not limited to any patent rights that may be infringed by your
  24. derivative works or by other works in which the Apple Software may be
  25. incorporated.
  26. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
  27. WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  28. WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  30. COMBINATION WITH YOUR PRODUCTS.
  31. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  32. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
  35. DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
  36. CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  37. APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. Copyright (C) 2008 Apple Inc. All Rights Reserved.
  39. */
  40. /*==================================================================================================
  41. SoundEngine.h
  42. ==================================================================================================*/
  43. #if !defined(__SoundEngine_h__)
  44. #define __SoundEngine_h__
  45. //==================================================================================================
  46. // Includes
  47. //==================================================================================================
  48. // System Includes
  49. #ifndef WIN32
  50. #include <CoreAudio/CoreAudioTypes.h>
  51. #include <AudioToolbox/AudioToolbox.h>
  52. #else
  53. typedef int OSStatus;
  54. typedef int SInt32;
  55. typedef unsigned int UInt32;
  56. typedef float Float32;
  57. typedef unsigned char Boolean;
  58. #endif
  59. #if defined(__cplusplus)
  60. extern "C"
  61. {
  62. #endif
  63. //==================================================================================================
  64. // Sound Engine
  65. //==================================================================================================
  66. /*!
  67. @enum SoundEngine error codes
  68. @abstract These are the error codes returned from the SoundEngine API.
  69. @constant kSoundEngineErrUnitialized
  70. The SoundEngine has not been initialized. Use SoundEngine_Initialize().
  71. @constant kSoundEngineErrInvalidID
  72. The specified EffectID was not found. This can occur if the effect has not been loaded, or
  73. if an unloaded is trying to be accessed.
  74. @constant kSoundEngineErrFileNotFound
  75. The specified file was not found.
  76. @constant kSoundEngineErrInvalidFileFormat
  77. The format of the file is invalid. Effect data must be little-endian 8 or 16 bit LPCM.
  78. @constant kSoundEngineErrDeviceNotFound
  79. The output device was not found.
  80. */
  81. enum {
  82. kSoundEngineErrUnitialized = 1,
  83. kSoundEngineErrInvalidID = 2,
  84. kSoundEngineErrFileNotFound = 3,
  85. kSoundEngineErrInvalidFileFormat = 4,
  86. kSoundEngineErrDeviceNotFound = 5
  87. };
  88. /*!
  89. @function SoundEngine_Initialize
  90. @abstract Initializes and sets up the sound engine. Calling after a previous initialize will
  91. reset the state of the SoundEngine, with all previous effects and music tracks
  92. erased. Note: This is not required, loading an effect or background track will
  93. initialize as necessary.
  94. @param inMixerOutputRate
  95. A Float32 that represents the output sample rate of the mixer unit. Setting this to
  96. 0 will use the default rate (the sample rate of the device)
  97. @result A OSStatus indicating success or failure.
  98. */
  99. OSStatus SoundEngine_Initialize(Float32 inMixerOutputRate);
  100. OSStatus SoundEngine_Reactivate();
  101. /*!
  102. @function SoundEngine_Teardown
  103. @abstract Tearsdown the sound engine.
  104. @result A OSStatus indicating success or failure.
  105. */
  106. OSStatus SoundEngine_Teardown();
  107. /*!
  108. @function SoundEngine_SetMasterVolume
  109. @abstract Sets the overall volume of all sounds coming from the process
  110. @param inValue
  111. A Float32 that represents the level. The range is between 0.0 and 1.0 (inclusive).
  112. @result A OSStatus indicating success or failure.
  113. */
  114. OSStatus SoundEngine_SetMasterVolume(Float32 inValue);
  115. /*!
  116. @function SoundEngine_SetListenerPosition
  117. @abstract Sets the position of the listener in the 3D space
  118. @param inX
  119. A Float32 that represents the listener's position along the X axis.
  120. @param inY
  121. A Float32 that represents the listener's position along the Y axis.
  122. @param inZ
  123. A Float32 that represents the listener's position along the Z axis.
  124. @result A OSStatus indicating success or failure.
  125. */
  126. OSStatus SoundEngine_SetListenerPosition(Float32 inX, Float32 inY, Float32 inZ);
  127. OSStatus SoundEngine_SetListenerDirection(Float32 inX, Float32 inY, Float32 inZ);
  128. /*!
  129. @function SoundEngine_SetListenerGain
  130. @abstract Sets the gain of the listener. Must be >= 0.0
  131. @param inValue
  132. A Float32 that represents the listener's gain
  133. @result A OSStatus indicating success or failure.
  134. */
  135. OSStatus SoundEngine_SetListenerGain(Float32 inValue);
  136. /*!
  137. @function SoundEngine_LoadBackgroundMusicTrack
  138. @abstract Tells the background music player which file to play
  139. @param inPath
  140. The absolute path to the file to play.
  141. @param inAddToQueue
  142. If true, file will be added to the current background music queue. If
  143. false, queue will be cleared and only loop the specified file.
  144. @param inLoadAtOnce
  145. If true, file will be loaded completely into memory. If false, data will be
  146. streamed from the file as needed. For games without large memory pressure and/or
  147. small background music files, this can save memory access and improve power efficiency
  148. @result A OSStatus indicating success or failure.
  149. */
  150. OSStatus SoundEngine_LoadBackgroundMusicTrack(const char* inPath, Boolean inAddToQueue, Boolean inLoadAtOnce);
  151. /*!
  152. @function SoundEngine_UnloadBackgroundMusicTrack
  153. @abstract Tells the background music player to release all resources and stop playing.
  154. @result A OSStatus indicating success or failure.
  155. */
  156. OSStatus SoundEngine_UnloadBackgroundMusicTrack();
  157. /*!
  158. @function SoundEngine_StartBackgroundMusic
  159. @abstract Tells the background music player to start playing.
  160. @result A OSStatus indicating success or failure.
  161. */
  162. OSStatus SoundEngine_StartBackgroundMusic();
  163. /*!
  164. @function SoundEngine_StopBackgroundMusic
  165. @abstract Tells the background music player to stop playing.
  166. @param inAddToQueue
  167. If true, playback will stop when all tracks have completed. If false, playback
  168. will stop immediately.
  169. @result A OSStatus indicating success or failure.
  170. */
  171. OSStatus SoundEngine_StopBackgroundMusic(Boolean inStopAtEnd);
  172. /*!
  173. @function SoundEngine_SetBackgroundMusicVolume
  174. @abstract Sets the volume for the background music player
  175. @param inValue
  176. A Float32 that represents the level. The range is between 0.0 and 1.0 (inclusive).
  177. @result A OSStatus indicating success or failure.
  178. */
  179. OSStatus SoundEngine_SetBackgroundMusicVolume(Float32 inValue);
  180. /*!
  181. @function SoundEngine_GetBackgroundMusicVolume
  182. @abstract Gets the volume for the background music player
  183. @result A Float32 representing the background music player volume, or 0 if it's not enabled
  184. */
  185. Float32 SoundEngine_GetBackgroundMusicVolume();
  186. /*!
  187. @function SoundEngine_LoadLoopingEffect
  188. @abstract Loads a sound effect from a file and return an ID to refer to that effect. Note: The files
  189. MUST all be in the same data format and sample rate
  190. @param inLoopFilePath
  191. The absolute path to the file to load. This is the file that will loop continually.
  192. @param inAttackFilePath
  193. The absolute path to the file to load. This will play once at the start of the effect.
  194. Set to NULL if no attack is desired, looping file will play immediately.
  195. @param inDecayFilePath
  196. The absolute path to the file to load. This will play once after looping has been ended.
  197. Triggered using SoundEngine_StopEffect with the inDoDecay set to true. Set to NULL if no
  198. decay is desired, looping file will stop immediately.
  199. @param outEffectID
  200. A UInt32 ID that refers to the effect.
  201. @result A OSStatus indicating success or failure.
  202. */
  203. OSStatus SoundEngine_LoadLoopingEffect(const char* inLoopFilePath, const char* inAttackFilePath, const char* inDecayFilePath, UInt32* outEffectID);
  204. /*!
  205. @function SoundEngine_LoadEffect
  206. @abstract Loads a sound effect from a file and return an ID to refer to that effect.
  207. @param inPath
  208. The absolute path to the file to load.
  209. @param outEffectID
  210. A UInt32 ID that refers to the effect.
  211. @result A OSStatus indicating success or failure.
  212. */
  213. OSStatus SoundEngine_LoadEffect(const char* inPath, UInt32* outEffectID);
  214. /*!
  215. @function SoundEngine_UnloadEffect
  216. @abstract Releases all resources associated with the given effect ID
  217. @param inEffectID
  218. The ID of the effect to unload.
  219. @result A OSStatus indicating success or failure.
  220. */
  221. OSStatus SoundEngine_UnloadEffect(UInt32 inEffectID);
  222. /*!
  223. @function SoundEngine_StartEffect
  224. @abstract Starts playback of an effect
  225. @param inEffectID
  226. The ID of the effect to start.
  227. @result A OSStatus indicating success or failure.
  228. */
  229. OSStatus SoundEngine_StartEffect(UInt32 inEffectID);
  230. /*!
  231. @function SoundEngine_StopEffect
  232. @abstract Stops playback of an effect
  233. @param inEffectID
  234. The ID of the effect to stop.
  235. @param inDoDecay
  236. Whether to play the decay file or stop immmediately (this is ignored
  237. for non-looping effects)
  238. @result A OSStatus indicating success or failure.
  239. */
  240. OSStatus SoundEngine_StopEffect(UInt32 inEffectID, Boolean inDoDecay);
  241. /*!
  242. @function SoundEngine_Vibrate
  243. @abstract Tells the device to vibrate
  244. */
  245. #if TARGET_OS_IPHONE
  246. #define SoundEngine_Vibrate() AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
  247. #endif
  248. /*!
  249. @function SoundEngine_SetEffectPitch
  250. @abstract Applies pitch shifting to an effect
  251. @param inEffectID
  252. The ID of the effect to adjust
  253. @param inValue
  254. A Float32 that represents the pitch scalar, with 1.0 being unchanged. Must
  255. be greater than 0.
  256. @result A OSStatus indicating success or failure.
  257. */
  258. OSStatus SoundEngine_SetEffectPitch(UInt32 inEffectID, Float32 inValue);
  259. /*!
  260. @function SoundEngine_SetEffectVolume
  261. @abstract Sets the volume for an effect
  262. @param inEffectID
  263. The ID of the effect to adjust
  264. @param inValue
  265. A Float32 that represents the level. The range is between 0.0 and 1.0 (inclusive).
  266. @result A OSStatus indicating success or failure.
  267. */
  268. OSStatus SoundEngine_SetEffectLevel(UInt32 inEffectID, Float32 inValue);
  269. /*!
  270. @function SoundEngine_SetEffectPosition
  271. @abstract Tells the engine whether a given effect should loop when played or if it should
  272. play through just once and stop.
  273. @param inEffectID
  274. The ID of the effect to adjust
  275. @param inX
  276. A Float32 that represents the effect's position along the X axis. Maximum distance
  277. is 100000.0 (absolute, not per axis), reference distance (distance from which gain
  278. begins to attenuate) is 1.0
  279. @param inY
  280. A Float32 that represents the effect's position along the Y axis. Maximum distance
  281. is 100000.0 (absolute, not per axis), reference distance (distance from which gain
  282. begins to attenuate) is 1.0
  283. @param inZ
  284. A Float32 that represents the effect's position along the Z axis. Maximum distance
  285. is 100000.0 by default (absolute, not per axis), reference distance (distance from
  286. which gain begins to attenuate) is 1.0
  287. @result A OSStatus indicating success or failure.
  288. */
  289. OSStatus SoundEngine_SetEffectPosition(UInt32 inEffectID, Float32 inX, Float32 inY, Float32 inZ);
  290. /*!
  291. @function SoundEngine_SetEffectsVolume
  292. @abstract Sets the overall volume for the effects
  293. @param inValue
  294. A Float32 that represents the level. The range is between 0.0 and 1.0 (inclusive).
  295. @result A OSStatus indicating success or failure.
  296. */
  297. OSStatus SoundEngine_SetEffectsVolume(Float32 inValue);
  298. /*!
  299. @function SoundEngine_GetEffectsVolume
  300. @abstract Gets the overall volume for the effects
  301. @result A Float32 representing the effects volume, or 0 if it's disabled
  302. */
  303. Float32 SoundEngine_GetEffectsVolume();
  304. /*!
  305. @function SoundEngine_SetMaxDistance
  306. @abstract Sets the maximum distance for effect attenuation. Gain will attenuate between the
  307. reference distance and the maximum distance, after which gain will be 0.0
  308. @param inValue
  309. A Float32 that represents the level. Must be greater than 0.0.
  310. @result A OSStatus indicating success or failure.
  311. */
  312. OSStatus SoundEngine_SetMaxDistance(Float32 inValue);
  313. /*!
  314. @function SoundEngine_SetReferenceDistance
  315. @abstract Sets the distance at which effect gain attenuation begins. It will attenuate between
  316. the reference distance and the maximum distance. Sounds closer than the reference
  317. distance will have no attenuation applied
  318. @param inValue
  319. A Float32 that represents the level. Must be greater than 0.0.
  320. @result A OSStatus indicating success or failure.
  321. */
  322. OSStatus SoundEngine_SetReferenceDistance(Float32 inValue);
  323. /*!
  324. @function SoundEngine_SetEffectReferenceDistance
  325. @abstract Sets the distance at which effect gain attenuation begins. It will attenuate between
  326. the reference distance and the maximum distance. Sounds closer than the reference
  327. distance will have no attenuation applied
  328. @param inEffectId
  329. The sound engine effect Id
  330. @param inValue
  331. A Float32 that represents the level. Must be greater than 0.0.
  332. @result A OSStatus indicating success or failure.
  333. */
  334. OSStatus SoundEngine_SetEffectReferenceDistance(UInt32 inEffectID, Float32 inValue);
  335. #if defined(__cplusplus)
  336. }
  337. #endif
  338. #endif