d6f23ff5de4c7c018e6d6f6a429db9ff148e3ff1
[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 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include "SND_DeviceManager.h"
42 #include "SND_DependKludge.h"
43 #include "SND_DummyDevice.h"
44 #ifdef USE_FMOD
45 #include "SND_FmodDevice.h"
46 #endif
47 #ifdef USE_OPENAL
48 #include "SND_OpenALDevice.h"
49 #endif
50
51 SND_IAudioDevice* SND_DeviceManager::m_instance = NULL;
52 int SND_DeviceManager::m_subscriptions = 0;
53
54 #ifdef USE_OPENAL
55 int SND_DeviceManager::m_device_type = snd_e_openaldevice;
56 #else
57 #       ifdef USE_FMOD
58 int SND_DeviceManager::m_device_type = snd_e_fmoddevice;
59 #       else
60 int SND_DeviceManager::m_device_type = snd_e_dummydevice;
61 #       endif
62 #endif
63
64 void SND_DeviceManager::Subscribe()
65 {
66         ++m_subscriptions;
67 }
68
69
70
71 void SND_DeviceManager::Unsubscribe()
72 {
73         --m_subscriptions;
74
75         // only release memory if there is a m_instance but no subscriptions left
76         if (m_subscriptions == 0 && m_instance)
77         {
78                 delete m_instance;
79                 m_instance = NULL;
80         }
81 }
82
83
84
85 SND_IAudioDevice* SND_DeviceManager::Instance()
86 {
87         // only give away an instance if there are subscriptions
88         if (m_subscriptions)
89         {
90                 // if there's no instance yet, set and create a new one
91                 if (m_instance == NULL)
92                 {
93                         SetDeviceType(m_device_type);
94                 }
95
96                 return m_instance;
97         }
98         else
99         {
100                 return NULL;
101         }
102 }
103                 
104
105
106 void SND_DeviceManager::SetDeviceType(int device_type)
107 {
108         // if we want to change devicetype, first delete the old one
109         if (m_instance)
110         {
111                 delete m_instance;
112                 m_instance = NULL;
113         }
114         
115         // let's create the chosen device
116         switch (device_type)
117         {
118 #ifdef USE_FMOD
119         case snd_e_fmoddevice:
120                 {
121                         m_instance = new SND_FmodDevice();
122                         m_device_type = device_type;
123                         break;
124                 }
125 #endif
126 #ifdef USE_OPENAL
127         case snd_e_openaldevice:
128                 {
129                         m_instance = new SND_OpenALDevice();
130                         m_device_type = device_type;
131                         break;
132                 }
133 #endif
134         default:
135                 {
136                         m_instance = new SND_DummyDevice();
137                         m_device_type = device_type;
138                         break;
139                 }
140         }
141 }