framework2
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
23void ofFmodSetBuffersize(unsigned int bs);
24float * 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
29namespace{
30ofSoundStream systemSoundStream;
31}
32#define OF_SYSTEM_SS systemSoundStream
33#else
34namespace{
35ofSoundStream &getSystemSoundStream() {
36static ofSoundStream _;
37return _;
38}
39}
40#define OF_SYSTEM_SS getSystemSoundStream()
41#endif
42
43
44using std::shared_ptr;
45using std::vector;
46
47//------------------------------------------------------------
48bool ofSoundStreamSettings::setInDevice(const ofSoundDevice & device){
49if(api!=ofSoundDevice::UNSPECIFIED && device.api!=api){
50ofLogWarning("ofSoundStreamSettings") << "Setting IN device with api: " << toString(device.api) << " will override the previously set: " << toString(api);
51}
52api = device.api;
53inDevice = device;
54return true;
55}
56
57//------------------------------------------------------------
58bool ofSoundStreamSettings::setOutDevice(const ofSoundDevice & device){
59if(api!=ofSoundDevice::UNSPECIFIED && device.api!=api){
60ofLogWarning("ofSoundStreamSettings") << "Setting OUT device with api: " << toString(device.api) << " will override the previously set: " << toString(api);
61}
62api = device.api;
63outDevice = device;
64return true;
65}
66
67//------------------------------------------------------------
68bool ofSoundStreamSettings::setApi(ofSoundDevice::Api api){
69if(api!=ofSoundDevice::UNSPECIFIED && inDevice.deviceID!=-1 && inDevice.api != api){
70ofLogError("ofSoundStreamSettings") << "Setting API after setting IN device with api: " << toString(inDevice.api) << " won't do anything";
71return false;
72}
73if(api!=ofSoundDevice::UNSPECIFIED && outDevice.deviceID!=-1 && outDevice.api != api){
74ofLogError("ofSoundStreamSettings") << "Setting API after setting IN device with api: " << toString(outDevice.api) << " won't do anything";
75return false;
76}
77this->api = api;
78return true;
79}
80
81//------------------------------------------------------------
82const ofSoundDevice * ofSoundStreamSettings::getInDevice() const{
83return inDevice.deviceID==-1 ? nullptr : &inDevice;
84}
85
86//------------------------------------------------------------
87const ofSoundDevice * ofSoundStreamSettings::getOutDevice() const{
88return outDevice.deviceID==-1 ? nullptr : &outDevice;
89}
90
91//------------------------------------------------------------
92ofSoundDevice::Api ofSoundStreamSettings::getApi() const{
93return api;
94}
95
96
97//------------------------------------------------------------
98void ofSoundStreamSetup(int nOutputChannels, int nInputChannels, ofBaseApp * appPtr){
99if( appPtr == nullptr ){
100appPtr = ofGetAppPtr();
101}
102ofSoundStreamSettings settings;
103settings.numOutputChannels = nOutputChannels;
104settings.numInputChannels = nInputChannels;
105settings.setOutListener(appPtr);
106settings.setInListener(appPtr);
107ofSoundStreamSetup(settings);
108}
109
110//------------------------------------------------------------
111void ofSoundStreamSetup(int nOutputChannels, int nInputChannels, int sampleRate, int bufferSize, int nBuffers){
112ofSoundStreamSettings settings;
113settings.numOutputChannels = nOutputChannels;
114settings.numInputChannels = nInputChannels;
115settings.setOutListener(ofGetAppPtr());
116settings.setInListener(ofGetAppPtr());
117settings.numBuffers = nBuffers;
118settings.sampleRate = sampleRate;
119settings.bufferSize = bufferSize;
120ofSoundStreamSetup(settings);
121}
122
123//------------------------------------------------------------
124void ofSoundStreamSetup(int nOutputChannels, int nInputChannels, ofBaseApp * appPtr, int sampleRate, int bufferSize, int nBuffers){
125ofSoundStreamSettings settings;
126settings.numOutputChannels = nOutputChannels;
127settings.numInputChannels = nInputChannels;
128settings.setOutListener(appPtr);
129settings.setInListener(appPtr);
130settings.numBuffers = nBuffers;
131settings.sampleRate = sampleRate;
132settings.bufferSize = bufferSize;
133ofSoundStreamSetup(settings);
134}
135
136//------------------------------------------------------------
137void ofSoundStreamSetup(ofSoundStreamSettings & settings) {
138OF_SYSTEM_SS.setup(settings);
139}
140
141//------------------------------------------------------------
142void ofSoundStreamStop(){
143OF_SYSTEM_SS.stop();
144}
145
146//------------------------------------------------------------
147void ofSoundStreamStart(){
148OF_SYSTEM_SS.start();
149}
150
151//------------------------------------------------------------
152void ofSoundStreamClose(){
153OF_SYSTEM_SS.close();
154}
155
156//------------------------------------------------------------
157vector<ofSoundDevice> ofSoundStreamListDevices(){
158vector<ofSoundDevice> deviceList = OF_SYSTEM_SS.getDeviceList();
159ofLogNotice("ofSoundStreamListDevices") << std::endl << deviceList;
160return deviceList;
161}
162
163//------------------------------------------------------------
164ofSoundStream::ofSoundStream(){
165#ifdef OF_SOUND_STREAM_TYPE
166setSoundStream(std::make_shared<OF_SOUND_STREAM_TYPE>());
167#endif
168}
169
170//------------------------------------------------------------
171void ofSoundStream::setSoundStream(shared_ptr<ofBaseSoundStream> soundStreamPtr){
172soundStream = soundStreamPtr;
173}
174
175//------------------------------------------------------------
176shared_ptr<ofBaseSoundStream> ofSoundStream::getSoundStream(){
177return soundStream;
178}
179
180//------------------------------------------------------------
181vector<ofSoundDevice> ofSoundStream::getDeviceList(ofSoundDevice::Api api) const{
182if( soundStream ){
183return soundStream->getDeviceList(api);
184} else {
185return vector<ofSoundDevice>();
186}
187}
188
189//------------------------------------------------------------
190vector<ofSoundDevice> ofSoundStream::listDevices() const{
191vector<ofSoundDevice> deviceList = getDeviceList();
192ofLogNotice("ofSoundStream::listDevices") << std::endl << deviceList;
193return deviceList;
194}
195
196//------------------------------------------------------------
197void ofSoundStream::printDeviceList() const{
198if( soundStream ) {
199soundStream->printDeviceList();
200}
201}
202
203//------------------------------------------------------------
204void ofSoundStream::setDeviceID(int deviceID){
205if( soundStream ){
206tmpDeviceId = deviceID;
207}
208}
209
210//------------------------------------------------------------
211void ofSoundStream::setDevice(const ofSoundDevice &device) {
212if( soundStream ){
213tmpDeviceId = device.deviceID;
214}
215}
216
217//------------------------------------------------------------
218bool ofSoundStream::setup(const ofSoundStreamSettings & settings)
219{
220if (soundStream) {
221#if defined(OF_SOUND_PLAYER_FMOD)
222ofFmodSetBuffersize(settings.bufferSize);
223#endif
224return soundStream->setup(settings);
225}
226return false;
227}
228
229//------------------------------------------------------------
230bool ofSoundStream::setup(ofBaseApp * app, int outChannels, int inChannels, int sampleRate, int bufferSize, int nBuffers){
231if( soundStream ){
232ofSoundStreamSettings settings;
233settings.setInListener(app);
234settings.setOutListener(app);
235settings.numOutputChannels = outChannels;
236settings.numInputChannels = inChannels;
237settings.sampleRate = sampleRate;
238settings.bufferSize = bufferSize;
239settings.numBuffers = nBuffers;
240if(tmpDeviceId!=-1){
241ofSoundDevice device;
242device.deviceID = tmpDeviceId;
243settings.setInDevice(device);
244settings.setOutDevice(device);
245}
246return soundStream->setup(settings);
247}
248return false;
249}
250
251//------------------------------------------------------------
252bool ofSoundStream::setup(int outChannels, int inChannels, int sampleRate, int bufferSize, int nBuffers){
253if( soundStream ){
254ofSoundStreamSettings settings;
255settings.setInListener(ofGetAppPtr());
256settings.setOutListener(ofGetAppPtr());
257settings.numOutputChannels = outChannels;
258settings.numInputChannels = inChannels;
259settings.sampleRate = sampleRate;
260settings.bufferSize = bufferSize;
261settings.numBuffers = nBuffers;
262if(tmpDeviceId!=-1){
263ofSoundDevice device;
264device.deviceID = tmpDeviceId;
265settings.setInDevice(device);
266settings.setOutDevice(device);
267}
268return soundStream->setup(settings);
269}
270return false;
271}
272
273//------------------------------------------------------------
274void ofSoundStream::setInput(ofBaseSoundInput * soundInput){
275if( soundStream ){
276soundStream->setInput(soundInput);
277}
278}
279
280//------------------------------------------------------------
281void ofSoundStream::setInput(ofBaseSoundInput &soundInput){
282setInput(&soundInput);
283}
284
285//------------------------------------------------------------
286void ofSoundStream::setOutput(ofBaseSoundOutput * soundOutput){
287if( soundStream ){
288soundStream->setOutput(soundOutput);
289}
290}
291
292//------------------------------------------------------------
293void ofSoundStream::setOutput(ofBaseSoundOutput &soundOutput){
294setOutput(&soundOutput);
295}
296
297//------------------------------------------------------------
298void ofSoundStream::start(){
299if( soundStream ){
300soundStream->start();
301}
302}
303
304//------------------------------------------------------------
305void ofSoundStream::stop(){
306if( soundStream ){
307soundStream->stop();
308}
309}
310
311//------------------------------------------------------------
312void ofSoundStream::close(){
313if( soundStream ){
314soundStream->close();
315}
316}
317
318//------------------------------------------------------------
319uint64_t ofSoundStream::getTickCount() const{
320if( soundStream ){
321return soundStream->getTickCount();
322}
323return 0;
324}
325
326//------------------------------------------------------------
327int ofSoundStream::getNumInputChannels() const{
328if( soundStream ){
329return soundStream->getNumInputChannels();
330}
331return 0;
332}
333
334//------------------------------------------------------------
335int ofSoundStream::getNumOutputChannels() const{
336if( soundStream ){
337return soundStream->getNumOutputChannels();
338}
339return 0;
340}
341
342//------------------------------------------------------------
343int ofSoundStream::getSampleRate() const{
344if( soundStream ){
345return soundStream->getSampleRate();
346}
347return 0;
348}
349
350//------------------------------------------------------------
351int ofSoundStream::getBufferSize() const{
352if( soundStream ){
353return soundStream->getBufferSize();
354}
355return 0;
356}
357
358//------------------------------------------------------------
359vector<ofSoundDevice> ofSoundStream::getMatchingDevices(const std::string& name, unsigned int inChannels, unsigned int outChannels, ofSoundDevice::Api api) const {
360vector<ofSoundDevice> devs = getDeviceList(api);
361vector<ofSoundDevice> hits;
362
363for(size_t i = 0; i < devs.size(); i++) {
364bool nameMatch = devs[i].name.find(name) != std::string::npos;
365bool inMatch = (inChannels == UINT_MAX) || (devs[i].inputChannels == inChannels);
366bool outMatch = (outChannels == UINT_MAX) || (devs[i].outputChannels == outChannels);
367
368if(nameMatch && inMatch && outMatch) {
369hits.push_back(devs[i]);
370}
371}
372
373return hits;
374}
375