-
Notifications
You must be signed in to change notification settings - Fork 1
/
audioplayer.cpp
153 lines (132 loc) · 4.67 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
#include "StdAfx.h"
/*static*/ bool bass_init = false; //!! meh
int bass_BG_idx = -1;
int bass_STD_idx = -1;
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 != NULL) // primary device has guid NULL, so use BASS_idx = -1 in that case
{
BASS_DEVICEINFO info;
for (int i = 1; BASS_GetDeviceInfo(i, &info); i++) // 0 = no sound/no device
if (info.flags & BASS_DEVICE_ENABLED) // device must be enabled
if (strcmp(info.name, DSads[DSidx]->description.c_str()) == 0)
{
if(idx == 0)
bass_STD_idx = (info.flags & BASS_DEVICE_DEFAULT) ? -1 : i;
else
bass_BG_idx = (info.flags & BASS_DEVICE_DEFAULT) ? -1 : i;
break;
}
}
for (size_t i = 0; i < DSads.size(); i++)
delete DSads[i];
}
}
}
//BASS_SetConfig(BASS_CONFIG_FLOATDSP, fTrue);
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) ? BASS_DEVICE_3D : 0, g_pvp->GetHwnd(), NULL)) // note that sample rate is usually ignored and set depending on the input/file automatically
{
char bla[128];
sprintf_s(bla, "BASS music/sound library initialization error %d", BASS_ErrorGetCode());
g_pvp->MessageBox(bla, "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 char * const 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, 0, 0, /*BASS_SAMPLE_LOOP*/0); //!! ?
if (m_stream == NULL)
{
char bla[MAX_PATH];
sprintf_s(bla, "BASS music/sound library cannot load %s", szFileName);
g_pvp->MessageBox(bla, "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);
}
}