Initial revision
[blender.git] / intern / SoundSystem / intern / SND_DeviceManager.cpp
1 /*
2  * SND_DeviceManager.h
3  *
4  * singleton for creating, switching and deleting audiodevices
5  *
6  * $Id$
7  *
8  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version. The Blender
14  * Foundation also sells licenses for use in proprietary software under
15  * the Blender License.  See http://www.blender.org/BL/ for information
16  * about this.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  *
27  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
28  * All rights reserved.
29  *
30  * The Original Code is: all of this file.
31  *
32  * Contributor(s): none yet.
33  *
34  * ***** END GPL/BL DUAL LICENSE BLOCK *****
35  */
36
37 #include "SND_DeviceManager.h"
38 #include "SND_DependKludge.h"
39 #include "SND_DummyDevice.h"
40 #ifdef USE_FMOD
41 #include "SND_FmodDevice.h"
42 #endif
43 #ifdef USE_OPENAL
44 #include "SND_OpenALDevice.h"
45 #endif
46
47 SND_IAudioDevice* SND_DeviceManager::m_instance = NULL;
48 int SND_DeviceManager::m_subscriptions = 0;
49
50 #ifdef USE_OPENAL
51 int SND_DeviceManager::m_device_type = snd_e_openaldevice;
52 #else
53 #       ifdef USE_FMOD
54 int SND_DeviceManager::m_device_type = snd_e_fmoddevice;
55 #       else
56 int SND_DeviceManager::m_device_type = snd_e_dummydevice;
57 #       endif
58 #endif
59
60 void SND_DeviceManager::Subscribe()
61 {
62         ++m_subscriptions;
63 }
64
65
66
67 void SND_DeviceManager::Unsubscribe()
68 {
69         --m_subscriptions;
70
71         // only release memory if there is a m_instance but no subscriptions left
72         if (m_subscriptions == 0 && m_instance)
73         {
74                 delete m_instance;
75                 m_instance = NULL;
76         }
77 }
78
79
80
81 SND_IAudioDevice* SND_DeviceManager::Instance()
82 {
83         // only give away an instance if there are subscriptions
84         if (m_subscriptions)
85         {
86                 // if there's no instance yet, set and create a new one
87                 if (m_instance == NULL)
88                 {
89                         SetDeviceType(m_device_type);
90                 }
91
92                 return m_instance;
93         }
94         else
95         {
96                 return NULL;
97         }
98 }
99                 
100
101
102 void SND_DeviceManager::SetDeviceType(int device_type)
103 {
104         // if we want to change devicetype, first delete the old one
105         if (m_instance)
106         {
107                 delete m_instance;
108                 m_instance = NULL;
109         }
110         
111         // let's create the chosen device
112         switch (device_type)
113         {
114 #ifdef USE_FMOD
115         case snd_e_fmoddevice:
116                 {
117                         m_instance = new SND_FmodDevice();
118                         m_device_type = device_type;
119                         break;
120                 }
121 #endif
122 #ifdef USE_OPENAL
123         case snd_e_openaldevice:
124                 {
125                         m_instance = new SND_OpenALDevice();
126                         m_device_type = device_type;
127                         break;
128                 }
129 #endif
130         default:
131                 {
132                         m_instance = new SND_DummyDevice();
133                         m_device_type = device_type;
134                         break;
135                 }
136         }
137 }