-
Notifications
You must be signed in to change notification settings - Fork 4
/
sccisoundinterfacemanager.cpp
337 lines (298 loc) · 7.31 KB
/
sccisoundinterfacemanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* @file sccisoundinterfacemanager.cpp
* @brief Implementation of manager
*/
#include "compiler.h"
#include "sccisoundinterfacemanager.h"
#include <algorithm>
#include "sccisoundinterface.h"
#include "sccispfmlight.h"
#include "dosio.h"
#include "common/profile.h"
namespace scci
{
//! Singleton instance
CSoundInterfaceManager CSoundInterfaceManager::sm_instance;
/**
* Gets instance
* @return The instance of sound manager
*/
SoundInterfaceManager* GetSoundInterfaceManager()
{
return CSoundInterfaceManager::GetInstance();
}
/**
* Constructor
*/
CSoundInterfaceManager::CSoundInterfaceManager()
: m_nDelayTime(0)
{
}
/**
* Destructor
*/
CSoundInterfaceManager::~CSoundInterfaceManager()
{
}
/**
* Delete
* @param[in] pInterface The instance of the sound interface
*/
void CSoundInterfaceManager::Delete(CSoundInterface* pInterface)
{
std::vector<CSoundInterface*>::iterator it = std::find(m_interfaces.begin(), m_interfaces.end(), pInterface);
if (it != m_interfaces.end())
{
m_interfaces.erase(it);
}
}
/**
* Get the count of interfaces
* @return The count of interfaces
*/
size_t CSoundInterfaceManager::getInterfaceCount()
{
return m_interfaces.size();
}
/**
* Get the information of the interface
* @param[in] iInterfaceNo The index of interfaces
* @return The information
*/
const SCCI_INTERFACE_INFO* CSoundInterfaceManager::getInterfaceInfo(size_t iInterfaceNo)
{
if (iInterfaceNo < m_interfaces.size())
{
return m_interfaces[iInterfaceNo]->GetInfo();
}
else
{
return NULL;
}
}
/**
* Gets interface instance
* @param[in] iInterfaceNo The index of interfaces
* @return The instance
*/
SoundInterface* CSoundInterfaceManager::getInterface(size_t iInterfaceNo)
{
if (iInterfaceNo < m_interfaces.size())
{
CSoundInterface* pInterface = m_interfaces[iInterfaceNo];
m_attachedInterfaces.push_back(pInterface);
pInterface->AddRef();
return pInterface;
}
else
{
return NULL;
}
}
/**
* Releases the sound interface
* @param[in] pSoundInterface The instance of the sound interface
* @retval true If succeeded
* @retval false If failed
*/
bool CSoundInterfaceManager::releaseInterface(SoundInterface* pSoundInterface)
{
std::vector<CSoundInterface*>::iterator it = std::find(m_attachedInterfaces.begin(), m_attachedInterfaces.end(), pSoundInterface);
if (it != m_attachedInterfaces.end())
{
m_attachedInterfaces.erase(it);
static_cast<CSoundInterface*>(pSoundInterface)->Release();
return true;
}
else
{
return false;
}
}
/**
* Releases all interfaces
* @retval true If succeeded
* @retval false If failed
*/
bool CSoundInterfaceManager::releaseAllInterface()
{
while (!m_attachedInterfaces.empty())
{
releaseInterface(m_attachedInterfaces.back());
}
return true;
}
/**
* Gets sound chip instance
* @param[in] iSoundChipType The type of the chip
* @param[in] dClock The clock of the chip
* @return The interface
*/
SoundChip* CSoundInterfaceManager::getSoundChip(SC_CHIP_TYPE iSoundChipType, UINT dClock)
{
if (iSoundChipType == SC_TYPE_NONE)
{
return NULL;
}
for (std::vector<CSoundInterface*>::iterator it = m_interfaces.begin(); it != m_interfaces.end(); ++it)
{
SoundChip* pChip = (*it)->GetSoundChip(iSoundChipType, dClock);
if (pChip)
{
return pChip;
}
}
return NULL;
}
/**
* Releases the instance of the sound chip
* @param[in] pSoundChip The instance of the sound chip
* @retval true If succeeded
* @retval false If failed
*/
bool CSoundInterfaceManager::releaseSoundChip(SoundChip* pSoundChip)
{
delete static_cast<CSoundChip*>(pSoundChip);
return true;
}
/**
* Releases all instances of the sound chip
* @retval true If succeeded
* @retval false If failed
*/
bool CSoundInterfaceManager::releaseAllSoundChip()
{
return releaseAllInterface();
}
/**
* Sets delay time
* @param[in] dMSec delay time
* @retval true If succeeded
* @retval false If failed
*/
bool CSoundInterfaceManager::setDelay(UINT dMSec)
{
if (dMSec > 10000)
{
return false;
}
m_nDelayTime = dMSec;
for (std::vector<CSoundInterface*>::iterator it = m_interfaces.begin(); it != m_interfaces.end(); ++it)
{
(*it)->setDelay(dMSec);
}
return true;
}
/**
* Gets delay time
* @return delay time
*/
UINT CSoundInterfaceManager::getDelay()
{
return m_nDelayTime;
}
/**
* Resets all interfaces
* @retval true If succeeded
* @retval false If failed
*/
bool CSoundInterfaceManager::reset()
{
bool err = false;
for (std::vector<CSoundInterface*>::iterator it = m_interfaces.begin(); it != m_interfaces.end(); ++it)
{
if (!(*it)->reset())
{
err = true;
}
}
return !err;
}
/**
* Sound Interface instance initialize
* @retval true If succeeded
* @retval false If failed
*/
bool CSoundInterfaceManager::initializeInstance()
{
OEMCHAR szPath[MAX_PATH];
milstr_ncpy(szPath, file_getcd(OEMTEXT("SCCI.ini")), NELEMENTS(szPath));
PFILEH pfh = profile_open(szPath, PFILEH_READONLY);
m_nDelayTime = profile_readint(OEMTEXT("scci"), OEMTEXT("DelayTime"), 0, pfh);
OEMCHAR szSections[4096];
if (profile_getsectionnames(szSections, NELEMENTS(szSections), pfh))
{
OEMCHAR* lpSections = szSections;
while (*lpSections != '\0')
{
OEMCHAR* lpKeyName = lpSections;
const size_t cchKeyName = OEMSTRLEN(lpSections);
lpSections += cchKeyName + 1;
if (milstr_memcmp(lpKeyName, OEMTEXT("SPFM Light")) != 0)
{
continue;
}
if ((lpKeyName[10] != '(') || (lpKeyName[cchKeyName - 1] != ')'))
{
continue;
}
if (profile_readint(lpKeyName, OEMTEXT("ACTIVE"), 0, pfh) == 0)
{
continue;
}
std::string deviceName(lpKeyName + 11, lpKeyName + cchKeyName - 1);
CSoundInterface* pInterface = new CSpfmLight(this, deviceName, m_nDelayTime);
if (!pInterface->Initialize())
{
delete pInterface;
continue;
}
for (UINT i = 0; i < 4; i++)
{
SCCI_SOUND_CHIP_INFO info;
memset(&info, 0, sizeof(info));
info.dBusID = i;
OEMCHAR szAppName[32];
OEMSPRINTF(szAppName, OEMTEXT("SLOT_%02d_CHIP_NAME"), i);
profile_read(lpKeyName, szAppName, OEMTEXT(""), info.cSoundChipName, NELEMENTS(info.cSoundChipName), pfh);
OEMSPRINTF(szAppName, OEMTEXT("SLOT_%02d_CHIP_ID"), i);
info.iSoundChip = static_cast<SC_CHIP_TYPE>(profile_readint(lpKeyName, szAppName, 0, pfh));
OEMSPRINTF(szAppName, OEMTEXT("SLOT_%02d_CHIP_CLOCK"), i);
info.dClock = profile_readint(lpKeyName, szAppName, 0, pfh);
if (info.iSoundChip == 0)
{
continue;
}
for (UINT j = 0; j < 2; j++)
{
OEMSPRINTF(szAppName, OEMTEXT("SLOT_%02d_CHIP_ID_CMP%d"), i, j + 1);
info.iCompatibleSoundChip[j] = static_cast<SC_CHIP_TYPE>(profile_readint(lpKeyName, szAppName, 0, pfh));
OEMSPRINTF(szAppName, OEMTEXT("SLOT_%02d_CHIP_CLOCK_CMP%d"), i, j + 1);
info.dCompatibleClock[j] = profile_readint(lpKeyName, szAppName, 0, pfh);
}
OEMSPRINTF(szAppName, OEMTEXT("SLOT_%02d_CHIP_LOCATION"), i);
info.dSoundLocation = static_cast<SC_CHIP_LOCATION>(profile_readint(lpKeyName, szAppName, 0, pfh));
pInterface->Add(info);
}
m_interfaces.push_back(pInterface);
}
}
profile_close(pfh);
return true;
}
/**
* Sound Interface instance release
* @retval true If succeeded
* @retval false If failed
*/
bool CSoundInterfaceManager::releaseInstance()
{
while (!m_interfaces.empty())
{
CSoundInterface* pInterface = m_interfaces.back();
m_interfaces.pop_back();
delete pInterface;
}
return true;
}
} // namespace scci