forked from vpinball/vpinball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audioplayer.cpp
173 lines (148 loc) · 6.16 KB
/
audioplayer.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
#include "StdAfx.h"
/*static*/ bool bass_init = false; //!! meh
int bass_BG_idx = -1;
int bass_STD_idx = -1;
/*static*/ float convert2decibelvolume(const float volume) // 0..100 -> DSBVOLUME_MIN..DSBVOLUME_MAX (-10000..0) (db/log scale)
{
const float totalvolume = max(min(volume, 100.0f), 0.0f);
const float decibelvolume = (totalvolume == 0.0f) ? DSBVOLUME_MIN : max(logf(totalvolume)*(float)(1000.0 / log(10.0)) - 2000.0f, (float)DSBVOLUME_MIN); // VP legacy conversion
return decibelvolume;
}
AudioPlayer::AudioPlayer()
{
m_stream = NULL;
#ifdef DEBUG_NO_SOUND
return;
#endif
if (!bass_init)
{
BASS_INFO info;
if (BASS_GetInfo(&info)) { // BASS is already in memory and initialized? (However that would've happened is a mystery to me)
bass_init = true;
return;
}
//
const SoundConfigTypes SoundMode3D = (SoundConfigTypes)LoadValueIntWithDefault("Player", "Sound3D", (int)SNDCFG_SND3D2CH);
const int DS_STD_idx = LoadValueIntWithDefault("Player", "SoundDevice", -1);
const int DS_BG_idx = LoadValueIntWithDefault("Player", "SoundDeviceBG", -1);
bass_STD_idx = -1;
bass_BG_idx = -1;
for(unsigned int idx = 0; idx < 2; ++idx)
{
const int DSidx = (idx == 0) ? DS_STD_idx : DS_BG_idx;
// now match the Direct Sound device with the BASS device (by name)
if (DSidx != -1)
{
DSAudioDevices DSads;
if (!FAILED(DirectSoundEnumerate(DSEnumCallBack, &DSads)))
{
if ((size_t)DSidx >= DSads.size() || DSads[DSidx]->guid != nullptr) // primary device has guid nullptr, so use BASS_idx = -1 in that case
{
BASS_DEVICEINFO dinfo;
for (int i = 1; BASS_GetDeviceInfo(i, &dinfo); i++) // 0 = no sound/no device
if (dinfo.flags & BASS_DEVICE_ENABLED) // device must be enabled
if (strcmp(dinfo.name, DSads[DSidx]->description.c_str()) == 0)
{
if(idx == 0)
bass_STD_idx = (dinfo.flags & BASS_DEVICE_DEFAULT) ? -1 : i;
else
bass_BG_idx = (dinfo.flags & BASS_DEVICE_DEFAULT) ? -1 : i;
break;
}
}
for (size_t i = 0; i < DSads.size(); i++)
delete DSads[i];
}
}
}
//BASS_SetConfig(/*BASS_CONFIG_THREAD |*/ BASS_CONFIG_FLOATDSP, fTrue);
BASS_SetConfig(/*BASS_CONFIG_THREAD |*/ BASS_CONFIG_CURVE_PAN, fTrue); // logarithmic scale, similar to DSound (although BASS still takes a 0..1 range)
//!! BASS_CONFIG_THREAD so far only works on Net stuff, not these ones here.. :/
//BASS_SetConfig(/*BASS_CONFIG_THREAD |*/ BASS_CONFIG_CURVE_VOL, fTrue); // dto. // is now converted internally, as otherwise PinMAMEs altsound will also get affected! (note that pan is not used yet in PinMAME!)
BASS_SetConfig(/*BASS_CONFIG_THREAD |*/ BASS_CONFIG_VISTA_SPEAKERS, fTrue); // to make BASS_ChannelSetAttribute(.., BASS_ATTRIB_PAN, pan); work, needs Vista or later
for(unsigned int idx = 0; idx < 2; ++idx)
{
if (!BASS_Init((idx == 0) ? bass_STD_idx : bass_BG_idx, 44100, (SoundMode3D != SNDCFG_SND3D2CH) && (idx == 0) ? 0 /*| BASS_DEVICE_MONO*/ /*| BASS_DEVICE_DSOUND*/ : 0, g_pvp->GetHwnd(), nullptr)) // note that sample rate is usually ignored and set depending on the input/file automatically
{
const int code = BASS_ErrorGetCode();
string bla2;
BASS_ErrorMapCode(code, bla2);
const string bla = "BASS music/sound library initialization error " + std::to_string(code) + ": " + bla2;
g_pvp->MessageBox(bla.c_str(), "Error", MB_ICONERROR);
}
if (/*SoundMode3D == SNDCFG_SND3D2CH &&*/ bass_STD_idx == bass_BG_idx) // skip 2nd device if it's the same and 3D is disabled //!!! for now try to just use one even if 3D! and then adapt channel settings if sample is a backglass sample
break;
}
bass_init = true;
}
}
AudioPlayer::~AudioPlayer()
{
if (m_stream)
{
if(bass_BG_idx != -1 && bass_STD_idx != bass_BG_idx) BASS_SetDevice(bass_BG_idx);
BASS_ChannelStop(m_stream);
BASS_StreamFree(m_stream);
}
}
void AudioPlayer::MusicPause()
{
if (m_stream)
{
if(bass_BG_idx != -1 && bass_STD_idx != bass_BG_idx) BASS_SetDevice(bass_BG_idx);
BASS_ChannelPause(m_stream);
}
}
void AudioPlayer::MusicUnpause()
{
if (m_stream)
{
if (bass_BG_idx != -1 && bass_STD_idx != bass_BG_idx) BASS_SetDevice(bass_BG_idx);
BASS_ChannelPlay(m_stream, 0);
}
}
bool AudioPlayer::MusicActive()
{
if (m_stream)
{
if (bass_BG_idx != -1 && bass_STD_idx != bass_BG_idx) BASS_SetDevice(bass_BG_idx);
return (BASS_ChannelIsActive(m_stream) == BASS_ACTIVE_PLAYING);
}
else
return false;
}
/*void AudioPlayer::MusicEnd()
{
if (m_stream)
{
if(bass_BG_idx != -1 && bass_STD_idx != bass_BG_idx) BASS_SetDevice(bass_BG_idx);
BASS_ChannelStop(m_stream);
}
}*/
bool AudioPlayer::MusicInit(const string& szFileName, const string& alt_szFileName, const float volume)
{
if (bass_BG_idx != -1 && bass_STD_idx != bass_BG_idx) BASS_SetDevice(bass_BG_idx);
m_stream = BASS_StreamCreateFile(FALSE, szFileName.c_str(), 0, 0, /*BASS_SAMPLE_LOOP*/0); //!! ?
if (m_stream == NULL)
m_stream = BASS_StreamCreateFile(FALSE, alt_szFileName.c_str(), 0, 0, /*BASS_SAMPLE_LOOP*/0); //!! ?
if (m_stream == NULL)
{
const int code = BASS_ErrorGetCode();
string bla2;
BASS_ErrorMapCode(code, bla2);
const string bla = "BASS music/sound library cannot load \"" + szFileName + "\" (error " + std::to_string(code) + ": " + bla2 + ")";
g_pvp->MessageBox(bla.c_str(), "Error", MB_ICONERROR);
return false;
}
BASS_ChannelSetAttribute(m_stream, BASS_ATTRIB_VOL, volume);
BASS_ChannelPlay(m_stream, 0);
return true;
}
void AudioPlayer::MusicVolume(const float volume)
{
if (m_stream)
{
if(bass_BG_idx != -1 && bass_STD_idx != bass_BG_idx) BASS_SetDevice(bass_BG_idx);
BASS_ChannelSetAttribute(m_stream, BASS_ATTRIB_VOL, volume);
}
}