-
Notifications
You must be signed in to change notification settings - Fork 15
/
OSLPlayer.cpp
109 lines (99 loc) · 2.49 KB
/
OSLPlayer.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
#include "OSLPlayer.h"
#include <math.h>
bool OSLPlayer::isPlaying(){
Log();
return state() == SL_PLAYSTATE_PLAYING;
}
bool OSLPlayer::isPaused(){
return state() == SL_PLAYSTATE_PAUSED;
}
bool OSLPlayer::isStopped(){
return state() == SL_PLAYSTATE_STOPPED;
}
OSLPlayer::OSLPlayer(OSLContext * context){
this->context = context;
player = NULL;
fdPlayerVolume = NULL;
playerObj = NULL;
player = NULL;
}
void OSLPlayer::clear(){
// destroy audio player object, and invalidate all associated interfaces
if (playerObj != NULL) {
(*playerObj)->Destroy(playerObj);
playerObj = NULL;
player = NULL;
fdPlayerVolume = NULL;
}
}
OSLPlayer::~OSLPlayer(){
clear();
}
SLuint32 OSLPlayer::state(){
SLresult result;
// make sure the URI audio player was created
if (NULL != player) {
SLuint32 state;
// LOGD("state()");
result = (*player)->GetPlayState(player, &state);
assert(SL_RESULT_SUCCESS == result);
return state;
}
return SL_PLAYSTATE_STOPPED;
}
//void OSLPlayer::clearLastSoundIfNeeded(OSLSound * sound){
// if(this->sound != NULL){
//// if(this->sound != sound)
// this->sound->clearPlayer();
//// else
//// LOGD("same sound");
// }
// else
// LOGD("NULL sound");
//}
//float gain_to_attenuation( float gain ){
// return 1000.0f * 20.0f * log10((double)gain);
//}
SLmillibel OSLPlayer::gain_to_attenuation(float gain) {
// float v = 1000.0f * 20.0f * log10((double) gain);
SLmillibel volume_mb;
if (gain >= 3.0f)
(*fdPlayerVolume)->GetMaxVolumeLevel(fdPlayerVolume, &volume_mb);
else if (gain <= 0.02f)
volume_mb = SL_MILLIBEL_MIN;
else {
volume_mb = M_LN2 / log(3.0f / (3.0f - gain)) * -1000.0f;
if (volume_mb > 0)
volume_mb = SL_MILLIBEL_MIN;
}
// LOGD("vol old = %f, new = %i", gain, volume_mb);
return volume_mb;
}
float from_attenuation( float gain ){
return pow(10, gain/ (1000.0f * 20.0f ));
}
void OSLPlayer::setVolume(float volume){
// LOGD("hm %f", volume);
SLresult result;
// LOGD("defaq?");
// SLVolumeItf volumeItf = getVolume();
if ( fdPlayerVolume != NULL) {
// LOGD("why?");
SLmillibel newVolume = gain_to_attenuation(volume);
result = (*fdPlayerVolume)->SetVolumeLevel(fdPlayerVolume, newVolume);
// LOGD("hm?");
assert(SL_RESULT_SUCCESS == result);
}
// else{
// LOGD("not?");
// }
// alSourcef(source, AL_GAIN, volume);
//checkError();
}
float OSLPlayer::getVolume(){
// LOGD("getVolume");
SLresult result;
SLmillibel millibel;
result = (*fdPlayerVolume)->GetVolumeLevel(fdPlayerVolume, &millibel);
return from_attenuation(millibel);
}