forked from Xrysnow/cocos2d-x-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioCommon.cpp
135 lines (112 loc) · 3.43 KB
/
AudioCommon.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
#include "AudioCommon.h"
#include <cstdarg>
#include <cassert>
using namespace audio;
namespace
{
std::function<void(const std::string&)> AudioLoggingFunction;
std::function<bool()> AudioRequestRecordPermissionFunction;
enum class RecordPermissionState { UNKNOWN, GRANTED, DENIED };
RecordPermissionState AudioRecordPermissionState = RecordPermissionState::UNKNOWN;
}
std::string audio::string_format(const char* format, ...)
{
constexpr auto BUFFER_LENGTH = 512;
va_list args;
std::string buffer(BUFFER_LENGTH, '\0');
va_start(args, format);
int nret = vsnprintf(&buffer.front(), buffer.length() + 1, format, args);
va_end(args);
if (nret >= 0) {
if ((unsigned int)nret < buffer.length()) {
buffer.resize(nret);
}
else if ((unsigned int)nret > buffer.length()) { // VS2015/2017 or later Visual Studio Version
buffer.resize(nret);
va_start(args, format);
nret = vsnprintf(&buffer.front(), buffer.length() + 1, format, args);
va_end(args);
assert(nret == buffer.length());
}
// else equals, do nothing.
}
else { // less or equal VS2013 and Unix System glibc implement.
do {
buffer.resize(buffer.length() * 3 / 2);
va_start(args, format);
nret = vsnprintf(&buffer.front(), buffer.length() + 1, format, args);
va_end(args);
} while (nret < 0);
buffer.resize(nret);
}
return buffer;
}
void audio::setLoggingFunction(const std::function<void(const std::string&)>& callback)
{
AudioLoggingFunction = callback;
}
void audio::logging(const char* format, ...)
{
if (!AudioLoggingFunction)
return;
constexpr auto BUFFER_LENGTH = 512;
va_list args;
std::string buffer(BUFFER_LENGTH, '\0');
va_start(args, format);
int nret = vsnprintf(&buffer.front(), buffer.length() + 1, format, args);
va_end(args);
if (nret >= 0) {
if ((unsigned int)nret < buffer.length()) {
buffer.resize(nret);
}
else if ((unsigned int)nret > buffer.length()) { // VS2015/2017 or later Visual Studio Version
buffer.resize(nret);
va_start(args, format);
nret = vsnprintf(&buffer.front(), buffer.length() + 1, format, args);
va_end(args);
assert(nret == buffer.length());
}
// else equals, do nothing.
}
else { // less or equal VS2013 and Unix System glibc implement.
do {
buffer.resize(buffer.length() * 3 / 2);
va_start(args, format);
nret = vsnprintf(&buffer.front(), buffer.length() + 1, format, args);
va_end(args);
} while (nret < 0);
buffer.resize(nret);
}
AudioLoggingFunction(buffer);
}
const char* audio::getErrorString(int errorCode)
{
switch (errorCode)
{
case AL_NO_ERROR: return "no error";
case AL_INVALID_NAME: return "invalid name";
case AL_INVALID_ENUM: return "invalid enum";
case AL_INVALID_VALUE: return "invalid value";
case AL_INVALID_OPERATION: return "invalid operation";
case AL_OUT_OF_MEMORY: return "out of memory";
default: return "unknown error";
}
}
void audio::setOnRequestRecordPermission(const std::function<bool()>& callback)
{
AudioRequestRecordPermissionFunction = callback;
}
bool audio::requestRecordAudioPermission()
{
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
if (AudioRecordPermissionState != RecordPermissionState::UNKNOWN)
return AudioRecordPermissionState == RecordPermissionState::GRANTED;
if (AudioRequestRecordPermissionFunction)
{
const auto ret = AudioRequestRecordPermissionFunction();
AudioRecordPermissionState = ret ? RecordPermissionState::GRANTED : RecordPermissionState::DENIED;
return ret;
}
#endif
return true;
}