Skip to content

Commit 19c41e9

Browse files
authored
ofOpenALSoundPlayer Filesystem (#7441)
1 parent 95306dd commit 19c41e9

3 files changed

Lines changed: 79 additions & 68 deletions

File tree

libs/openFrameworks/sound/ofAVEngineSoundPlayer.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifdef OF_SOUND_PLAYER_AV_ENGINE
1111

1212
#include "ofUtils.h"
13+
#include "ofMath.h"
1314
#include "ofLog.h"
1415
#include "ofEvents.h"
1516

libs/openFrameworks/sound/ofOpenALSoundPlayer.cpp

Lines changed: 75 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
#define GLM_FORCE_CTOR_INIT
66
#include <glm/gtc/constants.hpp>
77
#include <glm/ext/scalar_common.hpp>
8+
89
#include "ofLog.h"
910
#include "ofEvents.h"
11+
#include "ofUtils.h"
1012

1113
#if defined (TARGET_OF_IOS) || defined (TARGET_OSX)
1214
#include <OpenAL/al.h>
@@ -20,6 +22,8 @@
2022
#include <mpg123.h>
2123
#endif
2224

25+
namespace fs = of::filesystem;
26+
2327
static ALCdevice * alDevice = nullptr;
2428
static ALCcontext * alContext = nullptr;
2529
std::vector<float> ofOpenALSoundPlayer::window;
@@ -44,37 +48,37 @@ void ofOpenALSoundUpdate(){
4448
// from http://devmaster.net/posts/2893/openal-lesson-6-advanced-loading-and-error-handles
4549
static std::string getALErrorString(ALenum error) {
4650
switch(error) {
47-
case AL_NO_ERROR:
48-
return "AL_NO_ERROR";
49-
case AL_INVALID_NAME:
50-
return "AL_INVALID_NAME";
51-
case AL_INVALID_ENUM:
52-
return "AL_INVALID_ENUM";
53-
case AL_INVALID_VALUE:
54-
return "AL_INVALID_VALUE";
55-
case AL_INVALID_OPERATION:
56-
return "AL_INVALID_OPERATION";
57-
case AL_OUT_OF_MEMORY:
58-
return "AL_OUT_OF_MEMORY";
59-
};
51+
case AL_NO_ERROR:
52+
return "AL_NO_ERROR";
53+
case AL_INVALID_NAME:
54+
return "AL_INVALID_NAME";
55+
case AL_INVALID_ENUM:
56+
return "AL_INVALID_ENUM";
57+
case AL_INVALID_VALUE:
58+
return "AL_INVALID_VALUE";
59+
case AL_INVALID_OPERATION:
60+
return "AL_INVALID_OPERATION";
61+
case AL_OUT_OF_MEMORY:
62+
return "AL_OUT_OF_MEMORY";
63+
};
6064
return "UNKWOWN_ERROR";
6165
}
6266

6367
static std::string getALCErrorString(ALCenum error) {
6468
switch(error) {
65-
case ALC_NO_ERROR:
66-
return "ALC_NO_ERROR";
67-
case ALC_INVALID_DEVICE:
68-
return "ALC_INVALID_DEVICE";
69-
case ALC_INVALID_CONTEXT:
70-
return "ALC_INVALID_CONTEXT";
71-
case ALC_INVALID_ENUM:
72-
return "ALC_INVALID_ENUM";
73-
case ALC_INVALID_VALUE:
74-
return "ALC_INVALID_VALUE";
75-
case ALC_OUT_OF_MEMORY:
76-
return "ALC_OUT_OF_MEMORY";
77-
};
69+
case ALC_NO_ERROR:
70+
return "ALC_NO_ERROR";
71+
case ALC_INVALID_DEVICE:
72+
return "ALC_INVALID_DEVICE";
73+
case ALC_INVALID_CONTEXT:
74+
return "ALC_INVALID_CONTEXT";
75+
case ALC_INVALID_ENUM:
76+
return "ALC_INVALID_ENUM";
77+
case ALC_INVALID_VALUE:
78+
return "ALC_INVALID_VALUE";
79+
case ALC_OUT_OF_MEMORY:
80+
return "ALC_OUT_OF_MEMORY";
81+
};
7882
return "UNKWOWN_ERROR";
7983
}
8084

@@ -176,14 +180,14 @@ void ofOpenALSoundPlayer::initialize(){
176180
// Create OpenAL context and make it current. If fails, close the OpenAL device that was just opened.
177181
alContext = alcCreateContext( alDevice, nullptr );
178182
if( !alContext ){
179-
ALCenum err = alcGetError( alDevice );
183+
ALCenum err = alcGetError( alDevice );
180184
ofLogError("ofOpenALSoundPlayer") << "initialize(): couldn't not create OpenAL context: "<< getALCErrorString( err );
181185
close();
182186
return;
183187
}
184188

185189
if( alcMakeContextCurrent( alContext )==ALC_FALSE ){
186-
ALCenum err = alcGetError( alDevice );
190+
ALCenum err = alcGetError( alDevice );
187191
ofLogError("ofOpenALSoundPlayer") << "initialize(): couldn't not make current the create OpenAL context: "<< getALCErrorString( err );
188192
close();
189193
return;
@@ -230,9 +234,13 @@ void ofOpenALSoundPlayer::close(){
230234
}
231235

232236
// ----------------------------------------------------------------------------
233-
bool ofOpenALSoundPlayer::sfReadFile(const of::filesystem::path& path, std::vector<short> & buffer, std::vector<float> & fftAuxBuffer){
237+
bool ofOpenALSoundPlayer::sfReadFile(const fs::path & path, std::vector<short> & buffer, std::vector<float> & fftAuxBuffer){
234238
SF_INFO sfInfo;
235-
SNDFILE* f = sf_open(path.string().c_str(),SFM_READ,&sfInfo);
239+
#ifdef OF_OS_WINDOWS
240+
SNDFILE* f = sf_wchar_open(path.c_str(), SFM_READ,&sfInfo);
241+
#else
242+
SNDFILE* f = sf_open(path.c_str(), SFM_READ,&sfInfo);
243+
#endif
236244
if(!f){
237245
ofLogError("ofOpenALSoundPlayer") << "sfReadFile(): couldn't read " << path;
238246
return false;
@@ -284,11 +292,13 @@ bool ofOpenALSoundPlayer::sfReadFile(const of::filesystem::path& path, std::vect
284292

285293
#ifdef OF_USING_MPG123
286294
//------------------------------------------------------------
287-
bool ofOpenALSoundPlayer::mpg123ReadFile(const of::filesystem::path& path,std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
295+
bool ofOpenALSoundPlayer::mpg123ReadFile(const fs::path & path, std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
288296
int err = MPG123_OK;
289297
duration = 0.0f;
290298
mpg123_handle * f = mpg123_new(nullptr,&err);
291-
if(mpg123_open(f,path.string().c_str())!=MPG123_OK){
299+
300+
// FIXME: Alternative, open the file separately use mpg123_open_fd() instead
301+
if(mpg123_open(f, ofPathToString(path).c_str() )!=MPG123_OK){
292302
ofLogError("ofOpenALSoundPlayer") << "mpg123ReadFile(): couldn't read " << path ;
293303
return false;
294304
}
@@ -323,10 +333,15 @@ bool ofOpenALSoundPlayer::mpg123ReadFile(const of::filesystem::path& path,std::v
323333
#endif
324334

325335
//------------------------------------------------------------
326-
bool ofOpenALSoundPlayer::sfStream(const of::filesystem::path& path,std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
336+
bool ofOpenALSoundPlayer::sfStream(const fs::path & path, std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
327337
if(!streamf){
328338
SF_INFO sfInfo;
329-
streamf = sf_open(path.string().c_str(),SFM_READ,&sfInfo);
339+
#ifdef OF_OS_WINDOWS
340+
streamf = sf_wchar_open(path.c_str(), SFM_READ,&sfInfo);
341+
#else
342+
streamf = sf_open(path.c_str(), SFM_READ,&sfInfo);
343+
#endif
344+
330345
if(!streamf){
331346
ofLogError("ofOpenALSoundPlayer") << "sfStream(): couldn't read " << path ;
332347
return false;
@@ -386,11 +401,12 @@ bool ofOpenALSoundPlayer::sfStream(const of::filesystem::path& path,std::vector<
386401

387402
#ifdef OF_USING_MPG123
388403
//------------------------------------------------------------
389-
bool ofOpenALSoundPlayer::mpg123Stream(const of::filesystem::path& path,std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
404+
bool ofOpenALSoundPlayer::mpg123Stream(const fs::path & path, std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
390405
if(!mp3streamf){
391406
int err = MPG123_OK;
392407
mp3streamf = mpg123_new(nullptr,&err);
393-
if(mpg123_open(mp3streamf,path.string().c_str())!=MPG123_OK){
408+
// FIXME: Alternative, open the file separately use mpg123_open_fd() instead
409+
if(mpg123_open(mp3streamf, ofPathToString(path).c_str())!=MPG123_OK){
394410
mpg123_close(mp3streamf);
395411
mpg123_delete(mp3streamf);
396412
ofLogError("ofOpenALSoundPlayer") << "mpg123Stream(): couldn't read " << path ;
@@ -437,13 +453,14 @@ bool ofOpenALSoundPlayer::mpg123Stream(const of::filesystem::path& path,std::vec
437453
#endif
438454

439455
//------------------------------------------------------------
440-
bool ofOpenALSoundPlayer::stream(const of::filesystem::path& fileName, std::vector<short> & buffer){
456+
bool ofOpenALSoundPlayer::stream(const fs::path & fileName, std::vector<short> & buffer){
441457
#ifdef OF_USING_MPG123
442-
if(ofFilePath::getFileExt(fileName)=="mp3" || ofFilePath::getFileExt(fileName)=="MP3" || mp3streamf){
458+
auto ext { ofGetExtensionLower(fileName) };
459+
if (ext == ".mp3" || mp3streamf) {
443460
if(!mpg123Stream(fileName,buffer,fftAuxBuffer)) return false;
444-
}else
461+
} else
445462
#endif
446-
if(!sfStream(fileName,buffer,fftAuxBuffer)) return false;
463+
if(!sfStream(fileName,buffer,fftAuxBuffer)) return false;
447464

448465
fftBuffers.resize(channels);
449466
int numFrames = buffer.size()/channels;
@@ -457,12 +474,13 @@ bool ofOpenALSoundPlayer::stream(const of::filesystem::path& fileName, std::vect
457474
return true;
458475
}
459476

460-
bool ofOpenALSoundPlayer::readFile(const of::filesystem::path& fileName, std::vector<short> & buffer){
477+
bool ofOpenALSoundPlayer::readFile(const fs::path & fileName, std::vector<short> & buffer){
461478
#ifdef OF_USING_MPG123
462-
if(ofFilePath::getFileExt(fileName)!="mp3" && ofFilePath::getFileExt(fileName)!="MP3"){
463-
if(!sfReadFile(fileName,buffer,fftAuxBuffer)) return false;
464-
}else{
479+
auto ext { ofGetExtensionLower(fileName) };
480+
if (ext == ".mp3") {
465481
if(!mpg123ReadFile(fileName,buffer,fftAuxBuffer)) return false;
482+
}else{
483+
if(!sfReadFile(fileName,buffer,fftAuxBuffer)) return false;
466484
}
467485
#else
468486
if(!sfReadFile(fileName,buffer,fftAuxBuffer)) return false;
@@ -480,7 +498,7 @@ bool ofOpenALSoundPlayer::readFile(const of::filesystem::path& fileName, std::ve
480498
}
481499

482500
//------------------------------------------------------------
483-
bool ofOpenALSoundPlayer::load(const of::filesystem::path & _fileName, bool is_stream){
501+
bool ofOpenALSoundPlayer::load(const fs::path& _fileName, bool is_stream){
484502

485503
auto fileName = ofToDataPath(_fileName);
486504

@@ -549,8 +567,8 @@ bool ofOpenALSoundPlayer::load(const of::filesystem::path & _fileName, bool is_s
549567

550568
alSourcef (sources[0], AL_PITCH, 1.0f);
551569
alSourcef (sources[0], AL_GAIN, 1.0f);
552-
alSourcef (sources[0], AL_ROLLOFF_FACTOR, 0.0);
553-
alSourcei (sources[0], AL_SOURCE_RELATIVE, AL_TRUE);
570+
alSourcef (sources[0], AL_ROLLOFF_FACTOR, 0.0);
571+
alSourcei (sources[0], AL_SOURCE_RELATIVE, AL_TRUE);
554572
}else{
555573
std::vector<std::vector<short> > multibuffer;
556574
multibuffer.resize(channels);
@@ -706,7 +724,7 @@ void ofOpenALSoundPlayer::update(ofEventArgs & args){
706724

707725
//------------------------------------------------------------
708726
void ofOpenALSoundPlayer::unload(){
709-
727+
710728
stop();
711729
ofRemoveListener(ofEvents().update,this,&ofOpenALSoundPlayer::update);
712730

@@ -804,7 +822,7 @@ void ofOpenALSoundPlayer::setPositionMS(int ms){
804822
}else
805823
#endif
806824
if(streamf){
807-
stream_samples_read = sf_seek(streamf,float(ms)/1000.f*samplerate,SEEK_SET) * channels;
825+
stream_samples_read = sf_seek(streamf,float(ms)/1000.f*samplerate,SEEK_SET) * channels;
808826
}else{
809827
for(int i=0;i<(int)channels;i++){
810828
alSourcef(sources[sources.size()-channels+i],AL_SEC_OFFSET,float(ms)/1000.f);
@@ -846,13 +864,13 @@ void ofOpenALSoundPlayer::setPan(float p){
846864
float pos[3] = {p,0,0};
847865
alSourcefv(sources[sources.size()-1],AL_POSITION,pos);
848866
}else{
849-
// calculates left/right volumes from pan-value (constant panning law)
850-
// see: Curtis Roads: Computer Music Tutorial p 460
867+
// calculates left/right volumes from pan-value (constant panning law)
868+
// see: Curtis Roads: Computer Music Tutorial p 460
851869
// thanks to jasch
852-
853-
float angle = p * glm::quarter_pi<float>(); // in radians from -45. to +45.
854-
float cosAngle = std::cos(angle);
855-
float sinAngle = std::sin(angle);
870+
871+
float angle = p * glm::quarter_pi<float>(); // in radians from -45. to +45.
872+
float cosAngle = std::cos(angle);
873+
float sinAngle = std::sin(angle);
856874

857875
float leftVol = (cosAngle - sinAngle) * glm::one_over_root_two<float>(); //// multiplied by 1/sqrt(2)
858876
float rightVol = (cosAngle + sinAngle) * glm::one_over_root_two<float>(); // multiplied by 1/sqrt(2)
@@ -948,8 +966,8 @@ void ofOpenALSoundPlayer::play(){
948966
float pos[3] = {1,0,0};
949967
alSourcefv(sources[sources.size()-channels+i],AL_POSITION,pos);
950968
}
951-
alSourcef (sources[sources.size()-channels+i], AL_ROLLOFF_FACTOR, 0.0);
952-
alSourcei (sources[sources.size()-channels+i], AL_SOURCE_RELATIVE, AL_TRUE);
969+
alSourcef (sources[sources.size()-channels+i], AL_ROLLOFF_FACTOR, 0.0);
970+
alSourcei (sources[sources.size()-channels+i], AL_SOURCE_RELATIVE, AL_TRUE);
953971
}
954972

955973
err = alGetError();

libs/openFrameworks/utils/ofConstants.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,6 @@ typedef TESSindex ofIndexType;
431431
#if defined(OF_USE_EXPERIMENTAL_FS)
432432
// C++17 experimental fs support
433433
#include <experimental/filesystem>
434-
435434
namespace std {
436435
namespace experimental{
437436
namespace filesystem {
@@ -445,16 +444,9 @@ typedef TESSindex ofIndexType;
445444
}
446445
#else
447446
#include <filesystem>
448-
#if defined(OF_HAS_CPP17)
449-
// Regular C++17 fs support
450-
namespace of {
451-
namespace filesystem = std::filesystem;
452-
}
453-
#else
454-
namespace of {
455-
namespace filesystem = std::filesystem;
456-
}
457-
#endif
447+
namespace of {
448+
namespace filesystem = std::filesystem;
449+
}
458450
#endif
459451
#else //not OF_USING_STD_FS
460452
// No experimental or c++17 filesytem support use boost

0 commit comments

Comments
 (0)