1 /*
2  *******************************************************************************************
3  * Dgame (a D game framework) - Copyright (c) Randy Schütt
4  * 
5  * This software is provided 'as-is', without any express or implied warranty.
6  * In no event will the authors be held liable for any damages arising from
7  * the use of this software.
8  * 
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  * 
13  * 1. The origin of this software must not be misrepresented; you must not claim
14  *    that you wrote the original software. If you use this software in a product,
15  *    an acknowledgment in the product documentation would be appreciated but is
16  *    not required.
17  * 
18  * 2. Altered source versions must be plainly marked as such, and must not be
19  *    misrepresented as being the original software.
20  * 
21  * 3. This notice may not be removed or altered from any source distribution.
22  *******************************************************************************************
23  */
24 module Dgame.Audio.Internal.core;
25 
26 package {
27 	import derelict.openal.al;
28 	import derelict.ogg.ogg;
29 	import derelict.vorbis.vorbis;
30 	import derelict.vorbis.file;
31 	
32 	import Dgame.Internal.Log;
33 }
34 
35 private:
36 
37 struct AL {
38 static:
39 	ALCdevice* Device;
40 	ALCcontext* Context;
41 }
42 
43 void _alError(string msg) {
44 	debug switch (alcGetError(AL.Device)) {
45 		case ALC_INVALID_DEVICE:
46 			Log.info("Invalid device");
47 			break;
48 		case ALC_INVALID_CONTEXT:
49 			Log.info("Invalid context");
50 			break;
51 		case ALC_INVALID_ENUM:
52 			Log.info("Invalid enum");
53 			break;
54 		case ALC_INVALID_VALUE:
55 			Log.info("Invalid value");
56 			break;
57 		case ALC_OUT_OF_MEMORY:
58 			Log.info("Out of memory");
59 			break;
60 		case ALC_NO_ERROR:
61 			Log.info("No error");
62 			break;
63 		default: break;
64 	}
65 	
66 	Log.error(msg);
67 }
68 
69 static this() {
70 	// Init openAL
71 	debug Log.info("init openAL");
72 	
73 	DerelictAL.load();
74 	DerelictOgg.load();
75 	DerelictVorbis.load();
76 	DerelictVorbisFile.load();
77 	
78 	AL.Device = alcOpenDevice(null);
79 	if (AL.Device is null)
80 		_alError("Device is null");
81 	
82 	AL.Context = alcCreateContext(AL.Device, null);
83 	if (AL.Context is null)
84 		_alError("Context is null.");
85 	
86 	alcMakeContextCurrent(AL.Context);
87 }
88 
89 static ~this() {
90 	alcMakeContextCurrent(null);
91 	alcDestroyContext(AL.Context);
92 	alcCloseDevice(AL.Device);
93 	
94 	DerelictVorbis.unload();
95 	DerelictVorbisFile.unload();
96 	DerelictOgg.unload();
97 	DerelictAL.unload();
98 }
99