1 /* 2 * zlib License 3 * 4 * (C) 2016 jython234 5 * 6 * This software is provided 'as-is', without any express or implied 7 * warranty. In no event will the authors be held liable for any damages 8 * arising from the use of this software. 9 * 10 * Permission is granted to anyone to use this software for any purpose, 11 * including commercial applications, and to alter it and redistribute it 12 * freely, subject to the following restrictions: 13 * 14 * 1. The origin of this software must not be misrepresented; you must not 15 * claim that you wrote the original software. If you use this software 16 * in a product, an acknowledgment in the product documentation would be 17 * appreciated but is not required. 18 * 2. Altered source versions must be plainly marked as such, and must not be 19 * misrepresented as being the original software. 20 * 3. This notice may not be removed or altered from any source distribution. 21 */ 22 module blocksound.audio; 23 24 import blocksound.core; 25 import blocksound.backend.types; 26 27 public import blocksound.backend.types : Source, Sound; 28 29 version(blocksound_ALBackend) { 30 import blocksound.backend.openal; 31 } 32 33 /++ 34 Loads a Sound from a file. 35 36 Params: 37 file = The file where the sound is stored. 38 39 Returns: A Sound instance loaded from the specified file. 40 +/ 41 Sound loadSoundFile(in string file) @system { 42 version(blocksound_ALBackend) { 43 import blocksound.backend.openal : ALSound; 44 45 return ALSound.loadSound(file); 46 } else { 47 throw new Exception("No backend avaliable! (Try compiling with version \"blocksound_ALBackend\" enabled)"); 48 } 49 } 50 51 /// Manages the Audio. 52 class AudioManager { 53 private Vec3 _listenerLocation; 54 private float _gain; 55 56 private AudioBackend backend; 57 private shared ArrayList!Source sources; 58 59 /// The location where the listener is. 60 @property Vec3 listenerLocation() @safe nothrow { return _listenerLocation; } 61 /// The location where the listener is. 62 @property void listenerLocation(Vec3 loc) @safe { 63 _listenerLocation = loc; 64 backend.setListenerLocation(loc); 65 } 66 67 /// The listener's gain or volume. 68 @property float gain() @safe nothrow { return _gain; } 69 /// The listener's gain or volume. 70 @property void gain(float gain) @safe { 71 _gain = gain; 72 backend.setListenerGain(gain); 73 } 74 75 /++ 76 Initializes the AudioManager and it's backend. 77 Backend is decided at compile-time. 78 +/ 79 this() @trusted { 80 import std.exception : enforce; 81 82 enforce(INIT, new Exception("BlockSound has not been initialized!")); 83 84 version(blocksound_ALBackend) { 85 backend = new ALAudioBackend(); 86 } else { 87 throw new Exception("No backend avaliable! (Try compiling with version \"blocksound_ALBackend\" enabled)"); 88 } 89 90 sources = new ArrayList!Source(); 91 } 92 93 /++ 94 Create a a new Source at the specified 95 location. The Source is also added to this AudioManager. 96 97 Params: 98 location = The location of the Source. 99 100 Returns: A new Source. 101 +/ 102 Source createSource(Vec3 location) @trusted { 103 Source source = backend_createSource(location); 104 sources.add(source); 105 return source; 106 } 107 108 /++ 109 Deletes a Source, frees it's resources, 110 and removes it from the AudioManager. 111 112 Params: 113 source = The source to be deleted. 114 +/ 115 void deleteSource(Source source) @trusted { 116 sources.remove(source); 117 source.cleanup(); 118 } 119 120 /// Cleanup any resources used by the backend. 121 void cleanup() @trusted { 122 backend.cleanup(); 123 } 124 }