framework2

Форк
0
374 строки · 11.2 Кб
1
#include "ofSoundStream.h"
2
#include "ofAppRunner.h"
3
#include "ofLog.h"
4

5
#if defined(OF_SOUND_PLAYER_FMOD)
6
#include "ofSoundPlayer.h"
7
#endif
8

9
#ifdef OF_SOUNDSTREAM_RTAUDIO
10
#include "ofRtAudioSoundStream.h"
11
#define OF_SOUND_STREAM_TYPE ofRtAudioSoundStream
12
#elif defined(OF_SOUNDSTREAM_ANDROID)
13
#include "ofxAndroidSoundStream.h"
14
#define OF_SOUND_STREAM_TYPE ofxAndroidSoundStream
15
#elif defined(OF_SOUNDSTREAM_IOS)
16
#include "ofxiOSSoundStream.h"
17
#define OF_SOUND_STREAM_TYPE ofxiOSSoundStream
18
#elif defined(OF_SOUNDSTREAM_EMSCRIPTEN)
19
#include "ofxEmscriptenSoundStream.h"
20
#define OF_SOUND_STREAM_TYPE ofxEmscriptenSoundStream
21
#endif
22

23
void ofFmodSetBuffersize(unsigned int bs);
24
float * ofFmodSoundGetSpectrum(int nBands);
25

26
//FIXME: this is needed to make video work on emscripten
27
//See: https://github.com/openframeworks/openFrameworks/issues/7377 
28
#ifdef OF_SOUNDSTREAM_EMSCRIPTEN
29
    namespace{
30
        ofSoundStream systemSoundStream;
31
    }
32
    #define OF_SYSTEM_SS systemSoundStream
33
#else
34
    namespace{
35
        ofSoundStream &getSystemSoundStream() {
36
            static ofSoundStream _;
37
            return _;
38
        }
39
    }
40
    #define OF_SYSTEM_SS getSystemSoundStream()
41
#endif
42

43

44
using std::shared_ptr;
45
using std::vector;
46

47
//------------------------------------------------------------
48
bool ofSoundStreamSettings::setInDevice(const ofSoundDevice & device){
49
	if(api!=ofSoundDevice::UNSPECIFIED && device.api!=api){
50
		ofLogWarning("ofSoundStreamSettings") << "Setting IN device with api: " << toString(device.api) << " will override the previously set: " << toString(api);
51
	}
52
	api = device.api;
53
	inDevice = device;
54
	return true;
55
}
56

57
//------------------------------------------------------------
58
bool ofSoundStreamSettings::setOutDevice(const ofSoundDevice & device){
59
	if(api!=ofSoundDevice::UNSPECIFIED && device.api!=api){
60
		ofLogWarning("ofSoundStreamSettings") << "Setting OUT device with api: " << toString(device.api) << " will override the previously set: " << toString(api);
61
	}
62
	api = device.api;
63
	outDevice = device;
64
	return true;
65
}
66

67
//------------------------------------------------------------
68
bool ofSoundStreamSettings::setApi(ofSoundDevice::Api api){
69
	if(api!=ofSoundDevice::UNSPECIFIED && inDevice.deviceID!=-1 && inDevice.api != api){
70
		ofLogError("ofSoundStreamSettings") << "Setting API after setting IN device with api: " << toString(inDevice.api) << " won't do anything";
71
		return false;
72
	}
73
	if(api!=ofSoundDevice::UNSPECIFIED && outDevice.deviceID!=-1 && outDevice.api != api){
74
		ofLogError("ofSoundStreamSettings") << "Setting API after setting IN device with api: " << toString(outDevice.api) << " won't do anything";
75
		return false;
76
	}
77
	this->api = api;
78
	return true;
79
}
80

81
//------------------------------------------------------------
82
const ofSoundDevice * ofSoundStreamSettings::getInDevice() const{
83
	return inDevice.deviceID==-1 ? nullptr : &inDevice;
84
}
85

86
//------------------------------------------------------------
87
const ofSoundDevice * ofSoundStreamSettings::getOutDevice() const{
88
	return outDevice.deviceID==-1 ? nullptr : &outDevice;
89
}
90

91
//------------------------------------------------------------
92
ofSoundDevice::Api ofSoundStreamSettings::getApi() const{
93
	return api;
94
}
95

96

97
//------------------------------------------------------------
98
void ofSoundStreamSetup(int nOutputChannels, int nInputChannels, ofBaseApp * appPtr){
99
	if( appPtr == nullptr ){
100
		appPtr = ofGetAppPtr();
101
	}
102
	ofSoundStreamSettings settings;
103
	settings.numOutputChannels = nOutputChannels;
104
	settings.numInputChannels = nInputChannels;
105
	settings.setOutListener(appPtr);
106
	settings.setInListener(appPtr);
107
	ofSoundStreamSetup(settings);
108
}
109

110
//------------------------------------------------------------
111
void ofSoundStreamSetup(int nOutputChannels, int nInputChannels, int sampleRate, int bufferSize, int nBuffers){
112
	ofSoundStreamSettings settings;
113
	settings.numOutputChannels = nOutputChannels;
114
	settings.numInputChannels = nInputChannels;
115
	settings.setOutListener(ofGetAppPtr());
116
	settings.setInListener(ofGetAppPtr());
117
	settings.numBuffers = nBuffers;
118
	settings.sampleRate = sampleRate;
119
	settings.bufferSize = bufferSize;
120
	ofSoundStreamSetup(settings);
121
}
122

123
//------------------------------------------------------------
124
void ofSoundStreamSetup(int nOutputChannels, int nInputChannels, ofBaseApp * appPtr, int sampleRate, int bufferSize, int nBuffers){
125
	ofSoundStreamSettings settings;
126
	settings.numOutputChannels = nOutputChannels;
127
	settings.numInputChannels = nInputChannels;
128
	settings.setOutListener(appPtr);
129
	settings.setInListener(appPtr);
130
	settings.numBuffers = nBuffers;
131
	settings.sampleRate = sampleRate;
132
	settings.bufferSize = bufferSize;
133
	ofSoundStreamSetup(settings);
134
}
135

136
//------------------------------------------------------------
137
void ofSoundStreamSetup(ofSoundStreamSettings & settings) {
138
    OF_SYSTEM_SS.setup(settings);
139
}
140

141
//------------------------------------------------------------
142
void ofSoundStreamStop(){
143
    OF_SYSTEM_SS.stop();
144
}
145

146
//------------------------------------------------------------
147
void ofSoundStreamStart(){
148
    OF_SYSTEM_SS.start();
149
}
150

151
//------------------------------------------------------------
152
void ofSoundStreamClose(){
153
    OF_SYSTEM_SS.close();
154
}
155

156
//------------------------------------------------------------
157
vector<ofSoundDevice> ofSoundStreamListDevices(){
158
	vector<ofSoundDevice> deviceList = OF_SYSTEM_SS.getDeviceList();
159
	ofLogNotice("ofSoundStreamListDevices") << std::endl << deviceList;
160
	return deviceList;
161
}
162

163
//------------------------------------------------------------
164
ofSoundStream::ofSoundStream(){
165
	#ifdef OF_SOUND_STREAM_TYPE
166
		setSoundStream(std::make_shared<OF_SOUND_STREAM_TYPE>());
167
	#endif
168
}
169

170
//------------------------------------------------------------
171
void ofSoundStream::setSoundStream(shared_ptr<ofBaseSoundStream> soundStreamPtr){
172
	soundStream = soundStreamPtr;
173
}
174

175
//------------------------------------------------------------
176
shared_ptr<ofBaseSoundStream> ofSoundStream::getSoundStream(){
177
	return soundStream;
178
}
179

180
//------------------------------------------------------------
181
vector<ofSoundDevice> ofSoundStream::getDeviceList(ofSoundDevice::Api api) const{
182
	if( soundStream ){
183
		return soundStream->getDeviceList(api);
184
	} else {
185
		return vector<ofSoundDevice>();
186
	}
187
}
188

189
//------------------------------------------------------------
190
vector<ofSoundDevice> ofSoundStream::listDevices() const{
191
	vector<ofSoundDevice> deviceList = getDeviceList();
192
	ofLogNotice("ofSoundStream::listDevices") << std::endl << deviceList;
193
	return deviceList;
194
}
195

196
//------------------------------------------------------------
197
void ofSoundStream::printDeviceList()  const{
198
	if( soundStream ) {
199
		soundStream->printDeviceList();
200
	}
201
}
202

203
//------------------------------------------------------------
204
void ofSoundStream::setDeviceID(int deviceID){
205
	if( soundStream ){
206
		tmpDeviceId = deviceID;
207
	}
208
}
209

210
//------------------------------------------------------------
211
void ofSoundStream::setDevice(const ofSoundDevice &device) {
212
    if( soundStream ){
213
        tmpDeviceId = device.deviceID;
214
    }
215
}
216

217
//------------------------------------------------------------
218
bool ofSoundStream::setup(const ofSoundStreamSettings & settings)
219
{
220
	if (soundStream) {
221
#if defined(OF_SOUND_PLAYER_FMOD)
222
		ofFmodSetBuffersize(settings.bufferSize);
223
#endif
224
		return soundStream->setup(settings);
225
	}
226
	return false;
227
}
228

229
//------------------------------------------------------------
230
bool ofSoundStream::setup(ofBaseApp * app, int outChannels, int inChannels, int sampleRate, int bufferSize, int nBuffers){
231
	if( soundStream ){
232
		ofSoundStreamSettings settings;
233
		settings.setInListener(app);
234
		settings.setOutListener(app);
235
		settings.numOutputChannels = outChannels;
236
		settings.numInputChannels = inChannels;
237
		settings.sampleRate = sampleRate;
238
		settings.bufferSize = bufferSize;
239
		settings.numBuffers = nBuffers;
240
		if(tmpDeviceId!=-1){
241
			ofSoundDevice device;
242
			device.deviceID = tmpDeviceId;
243
			settings.setInDevice(device);
244
			settings.setOutDevice(device);
245
		}
246
		return soundStream->setup(settings);
247
	}
248
	return false;
249
}
250

251
//------------------------------------------------------------
252
bool ofSoundStream::setup(int outChannels, int inChannels, int sampleRate, int bufferSize, int nBuffers){
253
	if( soundStream ){
254
		ofSoundStreamSettings settings;
255
		settings.setInListener(ofGetAppPtr());
256
		settings.setOutListener(ofGetAppPtr());
257
		settings.numOutputChannels = outChannels;
258
		settings.numInputChannels = inChannels;
259
		settings.sampleRate = sampleRate;
260
		settings.bufferSize = bufferSize;
261
		settings.numBuffers = nBuffers;
262
		if(tmpDeviceId!=-1){
263
			ofSoundDevice device;
264
			device.deviceID = tmpDeviceId;
265
			settings.setInDevice(device);
266
			settings.setOutDevice(device);
267
		}
268
		return soundStream->setup(settings);
269
	}
270
	return false;
271
}
272

273
//------------------------------------------------------------
274
void ofSoundStream::setInput(ofBaseSoundInput * soundInput){
275
	if( soundStream ){
276
		soundStream->setInput(soundInput);
277
	}
278
}
279

280
//------------------------------------------------------------
281
void ofSoundStream::setInput(ofBaseSoundInput &soundInput){
282
	setInput(&soundInput);
283
}
284

285
//------------------------------------------------------------
286
void ofSoundStream::setOutput(ofBaseSoundOutput * soundOutput){
287
	if( soundStream ){
288
		soundStream->setOutput(soundOutput);
289
	}
290
}
291

292
//------------------------------------------------------------
293
void ofSoundStream::setOutput(ofBaseSoundOutput &soundOutput){
294
	setOutput(&soundOutput);
295
}
296

297
//------------------------------------------------------------
298
void ofSoundStream::start(){
299
	if( soundStream ){
300
		soundStream->start();
301
	}
302
}
303

304
//------------------------------------------------------------
305
void ofSoundStream::stop(){
306
	if( soundStream ){
307
		soundStream->stop();
308
	}
309
}
310

311
//------------------------------------------------------------
312
void ofSoundStream::close(){
313
	if( soundStream ){
314
		soundStream->close();
315
	}
316
}
317

318
//------------------------------------------------------------
319
uint64_t ofSoundStream::getTickCount() const{
320
	if( soundStream ){
321
		return soundStream->getTickCount();
322
	}
323
	return 0;
324
}
325

326
//------------------------------------------------------------
327
int ofSoundStream::getNumInputChannels() const{
328
	if( soundStream ){
329
		return soundStream->getNumInputChannels();
330
	}
331
	return 0;
332
}
333

334
//------------------------------------------------------------
335
int ofSoundStream::getNumOutputChannels() const{
336
	if( soundStream ){
337
		return soundStream->getNumOutputChannels();
338
	}
339
	return 0;
340
}
341

342
//------------------------------------------------------------
343
int ofSoundStream::getSampleRate() const{
344
	if( soundStream ){
345
		return soundStream->getSampleRate();
346
	}
347
	return 0;
348
}
349

350
//------------------------------------------------------------
351
int ofSoundStream::getBufferSize() const{
352
	if( soundStream ){
353
		return soundStream->getBufferSize();
354
	}
355
	return 0;
356
}
357

358
//------------------------------------------------------------
359
vector<ofSoundDevice> ofSoundStream::getMatchingDevices(const std::string& name, unsigned int inChannels, unsigned int outChannels, ofSoundDevice::Api api) const {
360
	vector<ofSoundDevice> devs = getDeviceList(api);
361
	vector<ofSoundDevice> hits;
362
	
363
	for(size_t i = 0; i < devs.size(); i++) {
364
		bool nameMatch = devs[i].name.find(name) != std::string::npos;
365
		bool inMatch = (inChannels == UINT_MAX) || (devs[i].inputChannels == inChannels);
366
		bool outMatch = (outChannels == UINT_MAX) || (devs[i].outputChannels == outChannels);
367
		
368
		if(nameMatch && inMatch && outMatch) {
369
			hits.push_back(devs[i]);
370
		}
371
	}
372
	
373
	return hits;
374
}
375

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.