/
Sound.cpp
187 lines (159 loc) · 4.84 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
/*
This file is part of the Sound library.
Copyright (C) 2008-2012 Benjamin Eikel <benjamin@eikel.org>
Copyright (C) 2008-2012 Claudius Jähn <claudius@uni-paderborn.de>
Copyright (C) 2008-2012 Ralf Petring <ralf@petring.net>
Copyright (C) 2019 Sascha Brandt <sascha@brandt.graphics>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "Sound.h"
#include "Buffer.h"
#include "Listener.h"
#include "Source.h"
#include "SoundInternals.h"
#include "Serialization/StreamerFLAC.h"
#include "Serialization/StreamerMP3.h"
#include "Serialization/StreamerWAV.h"
#include <Util/Macros.h>
#include <Util/References.h>
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <cstdint>
#include <vector>
namespace Sound {
static ALCdevice *myDevice=nullptr;
static ALCcontext * myContext=nullptr;
// if (alcIsExtensionPresent(nullptr, "ALC_ENUMERATION_EXT") != AL_TRUE) {
// std::cout << "Enumeration not available!";
// return;
// }
// // defaultDeviceName contains the name of the default
// // device
// const ALCchar *defaultDeviceName;
// defaultDeviceName = alcGetString(nullptr, ALC_DEFAULT_DEVICE_SPECIFIER);
// std::cout << "\nDevice:"<< defaultDeviceName ;
//! [static]
std::string getDeviceName(){
const ALCchar *actualDeviceName;
actualDeviceName = alcGetString(myDevice, ALC_DEVICE_SPECIFIER);
return std::string(actualDeviceName);
}
//! [static]
bool checkErrorStatus(const char * file, int line, const std::string & msg) {
const ALenum error = alGetError();
if (error == AL_NO_ERROR) {
return true;
}
std::cerr << "AL ERROR (";
const ALchar * errorString = alGetString(error);
if(errorString != nullptr) {
std::cerr << errorString;
} else {
std::cerr << error;
}
std::cerr << "): " << msg << " at " << file << ":" << line << std::endl;
return false;
}
static void checkALCErrorStatus(const char * file, int line, ALCdevice * device) {
const ALCenum error = alcGetError(device);
if(error == ALC_NO_ERROR) {
return;
}
std::cerr << "ALC ERROR (";
const ALchar * errorString = alcGetString(device, error);
if(errorString != nullptr) {
std::cerr << errorString;
} else {
std::cerr << error;
}
std::cerr << ") at " << file << ":" << line << std::endl;
}
//! [static]
bool initSoundSystem(){
if(myContext != nullptr) { // already initialized?
return true;
}
StreamerFLAC::init();
StreamerMP3::init();
StreamerWAV::init();
// Open the default device
myDevice = alcOpenDevice(nullptr);
checkALCErrorStatus(__FILE__, __LINE__, myDevice);
if(myDevice == nullptr) {
return false;
}
// Create Context
myContext = alcCreateContext(myDevice, nullptr);
checkALCErrorStatus(__FILE__, __LINE__, myDevice);
if(myContext == nullptr) {
return false;
}
alcMakeContextCurrent(myContext);
checkALCErrorStatus(__FILE__, __LINE__, myDevice);
if(!checkErrorStatus(__FILE__, __LINE__, "initSoundSystem()")){
shutDownSoundSystem();
return false;
}
return true;
}
//! [static]
void shutDownSoundSystem(){
alcMakeContextCurrent(nullptr);
alcDestroyContext(myContext);
alcCloseDevice(myDevice);
myContext = nullptr;
myDevice = nullptr;
}
//! [static]
Listener * getListener() {
return Listener::getInstance();
}
//! [static]
Source * createSource(){
return Source::create();
}
//! [static] factory
Buffer * createNoise(unsigned int freq, unsigned int size) {
Util::Reference<Buffer> b(Buffer::create());
if(b.isNull())
return nullptr;
std::vector<uint8_t> noise;
noise.reserve(size);
unsigned int j = 1;
for (unsigned int i = 0; i < size; ++i) {
j = (j * 1234393 + i) % 0xffffff;
noise.push_back(static_cast<uint8_t>(j) % 256);
}
b->setData(AL_FORMAT_MONO8, noise.data(), static_cast<uint32_t>(noise.size()),freq);
return checkErrorStatus(__FILE__, __LINE__) ? b.detachAndDecrease() : nullptr;
}
//! [static] factory
Buffer * createRectangleSound(unsigned int width, unsigned int freq, unsigned int size) {
Util::Reference<Buffer> b(Buffer::create());
if(b.isNull())
return nullptr;
std::vector<uint8_t> noise;
noise.reserve(size);
uint8_t value = 0;
for (unsigned int i = 0; i < size; ++i) {
if(i % width == 0) {
value = 255 - value;
}
noise.push_back(value);
}
b->setData(AL_FORMAT_MONO8, noise.data(), static_cast<uint32_t>(noise.size()), freq);
return checkErrorStatus(__FILE__, __LINE__) ? b.detachAndDecrease() : nullptr;
}
//! [static] factory
Buffer * createSilence(unsigned int freq,unsigned int size) {
Util::Reference<Buffer> b(Buffer::create());
if(b.isNull())
return nullptr;
const std::vector<uint8_t> data(size, 128);
b->setData(AL_FORMAT_MONO8, data.data(), static_cast<uint32_t>(data.size()), freq);
return checkErrorStatus(__FILE__, __LINE__) ? b.detachAndDecrease() : nullptr;
}
}