-
Notifications
You must be signed in to change notification settings - Fork 5
/
Sound.cpp
207 lines (192 loc) · 5.2 KB
/
Sound.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
#include "common.h"
#include "client.h"
#include "Sound.h"
#include "SoundSystem.h"
#include "Remote.h"
#ifdef USE_OPENAL
#include "ALSound.h" // Thin wrapper of OpenAL
#endif
#ifdef USE_MOYAIAL
#include "MoyaiALSound.h" // Original simple sound mixing over OpenAL
#endif
bool Sound::g_no_real_sound = false;
Sound::Sound( SoundSystem *s) : parent(s), sound(0), ch(0), default_volume(1), temp_volume(1), external_id(0), last_samples(0), last_samples_num(0), last_play_volume(0) {
last_load_file_path[0] = '\0';
}
void Sound::play(){
play(default_volume);
}
void Sound::play(float vol){
if(vol==0)return;
if(!g_no_real_sound) {
#ifdef USE_FMOD
FMOD_RESULT r;
if( !this->ch ){
r = FMOD_System_PlaySound( parent->sys, FMOD_CHANNEL_FREE, sound, 0, & this->ch );
} else {
r = FMOD_System_PlaySound( parent->sys, FMOD_CHANNEL_REUSE, sound, 0, & this->ch );
}
FMOD_ERRCHECK(r);
FMOD_Channel_SetVolume(ch, default_volume * vol * temp_volume );
#endif
#ifdef USE_UNTZ
sound->play();
sound->setVolume(default_volume*vol*temp_volume);
#endif
#ifdef USE_OPENAL
sound->play();
sound->setVolume(default_volume*vol*temp_volume);
#endif
#ifdef USE_MOYAIAL
sound->play();
sound->setVolume(default_volume*vol*temp_volume);
#endif
}
last_play_volume = default_volume * vol * temp_volume;
if(parent->remote_head) parent->remote_head->notifySoundPlay( this, last_play_volume );
if(this->parent->soundPlayCallback) this->parent->soundPlayCallback(this, default_volume, vol*temp_volume );
}
void Sound::playDistance(float mindist, float maxdist, float dist, float relvol) {
if(dist<mindist) {
play(1 * relvol);
} else if( dist > maxdist ) {
return;
} else {
float width = maxdist - mindist;
float r = 1 - (dist - mindist) / width;
play(r * relvol);
}
}
void Sound::stop() {
// print("Sound::stop! %p debugid:%d ch:%p",this, this->debug_id ,this->ch);
#ifdef USE_FMOD
FMOD_Channel_Stop( this->ch );
this->ch = NULL;
#endif
#ifdef USE_UNTZ
sound->stop();
#endif
#ifdef USE_OPENAL
sound->stop();
#endif
#ifdef USE_MOYAIAL
sound->stop();
#endif
if(this->parent->remote_head) this->parent->remote_head->notifySoundStop(this);
if(this->parent->soundStopCallback) this->parent->soundStopCallback(this);
}
bool Sound::isPlaying() {
#ifdef USE_FMOD
if(!this->ch)return false;
FMOD_BOOL val;
FMOD_RESULT r;
r = FMOD_Channel_IsPlaying( this->ch, &val );
if( r != FMOD_OK ) return false;
return val;
#elif defined(USE_UNTZ)
if(!sound)return false;
return sound->isPlaying();
#elif defined(USE_OPENAL)
if(!sound)return false;
return sound->isPlaying();
#elif defined(USE_MOYAIAL)
if(!sound)return false;
return sound->isPlaying();
#else
return false; // TODO: implement linux virtual sound
#endif
}
void Sound::setVolume( float v ) {
#ifdef USE_FMOD
FMOD_Channel_SetVolume(this->ch, v );
#endif
#ifdef USE_UNTZ
sound->setVolume(v);
#endif
#ifdef USE_OPENAL
sound->setVolume(v);
#endif
#ifdef USE_MOYAIAL
sound->setVolume(v);
#endif
}
float Sound::getVolume() {
#ifdef USE_FMOD
float v;
FMOD_Channel_GetVolume(this->ch,&v);
return v;
#elif defined(USE_UNTZ)
return sound->getVolume();
#elif defined(USE_OPENAL)
return sound->getVolume();
#elif defined(USE_MOYAIAL)
return sound->getVolume();
#else
return 1; // TODO: implement linux virtual sound
#endif
}
void Sound::setLoop( bool flag ) {
#ifdef USE_FMOD
if( flag ) {
FMOD_Sound_SetMode( sound, FMOD_LOOP_NORMAL );
} else {
FMOD_Sound_SetMode( sound, FMOD_LOOP_OFF );
}
#endif
#ifdef USE_UNTZ
sound->setLooping(flag);
#endif
#ifdef USE_OPENAL
sound->setLooping(flag);
#endif
#ifdef USE_MOYAIAL
sound->setLooping(flag);
#endif
}
void Sound::updateLastSamples( float *samples, int samples_num ) {
if( last_samples ) FREE(last_samples);
size_t sz = samples_num*sizeof(float);
last_samples = (float*)MALLOC(sz);
memcpy( last_samples, samples, sz );
last_samples_num = samples_num;
}
// returns -1 if not playing
float Sound::getTimePositionSec() {
#ifdef USE_FMOD
if(!this->ch) return -1;
unsigned int pos_ms;
FMOD_RESULT r = FMOD_Channel_GetPosition( this->ch, &pos_ms, FMOD_TIMEUNIT_MS );
if( r != FMOD_OK ) {
print("FMOD_Channel_GetPosition: failed. ret:%d", r );
return -1;
}
return (float)(pos_ms) / 1000.0f;
#elif defined(USE_UNTZ)
return (float) sound->getPosition();
#elif defined(USE_OPENAL)
return (float) sound->getPosition();
#elif defined(USE_MOYAIAL)
return (float) sound->getPosition();
#else
return 0;
#endif
}
void Sound::setTimePositionSec( float sec ) {
#ifdef USE_FMOD
if(!this->ch) return;
unsigned int pos_ms = (unsigned int)(sec * 1000);
FMOD_RESULT r = FMOD_Channel_SetPosition( this->ch, pos_ms, FMOD_TIMEUNIT_MS );
if( r != FMOD_OK ) {
print("FMOD_Channel_SetPosition: failed. ret:%d", r );
}
#endif
#ifdef USE_UNTZ
sound->setPosition(sec);
#endif
#ifdef USE_OPENAL
sound->setPosition(sec);
#endif
#ifdef USE_MOYAIAL
sound->setPosition(sec);
#endif
}