Skip to content

Commit

Permalink
ofOpenALSoundPlayer Filesystem (#7441)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitre authored Oct 18, 2024
1 parent 95306dd commit 19c41e9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 68 deletions.
1 change: 1 addition & 0 deletions libs/openFrameworks/sound/ofAVEngineSoundPlayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifdef OF_SOUND_PLAYER_AV_ENGINE

#include "ofUtils.h"
#include "ofMath.h"
#include "ofLog.h"
#include "ofEvents.h"

Expand Down
132 changes: 75 additions & 57 deletions libs/openFrameworks/sound/ofOpenALSoundPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#define GLM_FORCE_CTOR_INIT
#include <glm/gtc/constants.hpp>
#include <glm/ext/scalar_common.hpp>

#include "ofLog.h"
#include "ofEvents.h"
#include "ofUtils.h"

#if defined (TARGET_OF_IOS) || defined (TARGET_OSX)
#include <OpenAL/al.h>
Expand All @@ -20,6 +22,8 @@
#include <mpg123.h>
#endif

namespace fs = of::filesystem;

static ALCdevice * alDevice = nullptr;
static ALCcontext * alContext = nullptr;
std::vector<float> ofOpenALSoundPlayer::window;
Expand All @@ -44,37 +48,37 @@ void ofOpenALSoundUpdate(){
// from http://devmaster.net/posts/2893/openal-lesson-6-advanced-loading-and-error-handles
static std::string getALErrorString(ALenum error) {
switch(error) {
case AL_NO_ERROR:
return "AL_NO_ERROR";
case AL_INVALID_NAME:
return "AL_INVALID_NAME";
case AL_INVALID_ENUM:
return "AL_INVALID_ENUM";
case AL_INVALID_VALUE:
return "AL_INVALID_VALUE";
case AL_INVALID_OPERATION:
return "AL_INVALID_OPERATION";
case AL_OUT_OF_MEMORY:
return "AL_OUT_OF_MEMORY";
};
case AL_NO_ERROR:
return "AL_NO_ERROR";
case AL_INVALID_NAME:
return "AL_INVALID_NAME";
case AL_INVALID_ENUM:
return "AL_INVALID_ENUM";
case AL_INVALID_VALUE:
return "AL_INVALID_VALUE";
case AL_INVALID_OPERATION:
return "AL_INVALID_OPERATION";
case AL_OUT_OF_MEMORY:
return "AL_OUT_OF_MEMORY";
};
return "UNKWOWN_ERROR";
}

static std::string getALCErrorString(ALCenum error) {
switch(error) {
case ALC_NO_ERROR:
return "ALC_NO_ERROR";
case ALC_INVALID_DEVICE:
return "ALC_INVALID_DEVICE";
case ALC_INVALID_CONTEXT:
return "ALC_INVALID_CONTEXT";
case ALC_INVALID_ENUM:
return "ALC_INVALID_ENUM";
case ALC_INVALID_VALUE:
return "ALC_INVALID_VALUE";
case ALC_OUT_OF_MEMORY:
return "ALC_OUT_OF_MEMORY";
};
case ALC_NO_ERROR:
return "ALC_NO_ERROR";
case ALC_INVALID_DEVICE:
return "ALC_INVALID_DEVICE";
case ALC_INVALID_CONTEXT:
return "ALC_INVALID_CONTEXT";
case ALC_INVALID_ENUM:
return "ALC_INVALID_ENUM";
case ALC_INVALID_VALUE:
return "ALC_INVALID_VALUE";
case ALC_OUT_OF_MEMORY:
return "ALC_OUT_OF_MEMORY";
};
return "UNKWOWN_ERROR";
}

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

if( alcMakeContextCurrent( alContext )==ALC_FALSE ){
ALCenum err = alcGetError( alDevice );
ALCenum err = alcGetError( alDevice );
ofLogError("ofOpenALSoundPlayer") << "initialize(): couldn't not make current the create OpenAL context: "<< getALCErrorString( err );
close();
return;
Expand Down Expand Up @@ -230,9 +234,13 @@ void ofOpenALSoundPlayer::close(){
}

// ----------------------------------------------------------------------------
bool ofOpenALSoundPlayer::sfReadFile(const of::filesystem::path& path, std::vector<short> & buffer, std::vector<float> & fftAuxBuffer){
bool ofOpenALSoundPlayer::sfReadFile(const fs::path & path, std::vector<short> & buffer, std::vector<float> & fftAuxBuffer){
SF_INFO sfInfo;
SNDFILE* f = sf_open(path.string().c_str(),SFM_READ,&sfInfo);
#ifdef OF_OS_WINDOWS
SNDFILE* f = sf_wchar_open(path.c_str(), SFM_READ,&sfInfo);
#else
SNDFILE* f = sf_open(path.c_str(), SFM_READ,&sfInfo);
#endif
if(!f){
ofLogError("ofOpenALSoundPlayer") << "sfReadFile(): couldn't read " << path;
return false;
Expand Down Expand Up @@ -284,11 +292,13 @@ bool ofOpenALSoundPlayer::sfReadFile(const of::filesystem::path& path, std::vect

#ifdef OF_USING_MPG123
//------------------------------------------------------------
bool ofOpenALSoundPlayer::mpg123ReadFile(const of::filesystem::path& path,std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
bool ofOpenALSoundPlayer::mpg123ReadFile(const fs::path & path, std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
int err = MPG123_OK;
duration = 0.0f;
mpg123_handle * f = mpg123_new(nullptr,&err);
if(mpg123_open(f,path.string().c_str())!=MPG123_OK){

// FIXME: Alternative, open the file separately use mpg123_open_fd() instead
if(mpg123_open(f, ofPathToString(path).c_str() )!=MPG123_OK){
ofLogError("ofOpenALSoundPlayer") << "mpg123ReadFile(): couldn't read " << path ;
return false;
}
Expand Down Expand Up @@ -323,10 +333,15 @@ bool ofOpenALSoundPlayer::mpg123ReadFile(const of::filesystem::path& path,std::v
#endif

//------------------------------------------------------------
bool ofOpenALSoundPlayer::sfStream(const of::filesystem::path& path,std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
bool ofOpenALSoundPlayer::sfStream(const fs::path & path, std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
if(!streamf){
SF_INFO sfInfo;
streamf = sf_open(path.string().c_str(),SFM_READ,&sfInfo);
#ifdef OF_OS_WINDOWS
streamf = sf_wchar_open(path.c_str(), SFM_READ,&sfInfo);
#else
streamf = sf_open(path.c_str(), SFM_READ,&sfInfo);
#endif

if(!streamf){
ofLogError("ofOpenALSoundPlayer") << "sfStream(): couldn't read " << path ;
return false;
Expand Down Expand Up @@ -386,11 +401,12 @@ bool ofOpenALSoundPlayer::sfStream(const of::filesystem::path& path,std::vector<

#ifdef OF_USING_MPG123
//------------------------------------------------------------
bool ofOpenALSoundPlayer::mpg123Stream(const of::filesystem::path& path,std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
bool ofOpenALSoundPlayer::mpg123Stream(const fs::path & path, std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
if(!mp3streamf){
int err = MPG123_OK;
mp3streamf = mpg123_new(nullptr,&err);
if(mpg123_open(mp3streamf,path.string().c_str())!=MPG123_OK){
// FIXME: Alternative, open the file separately use mpg123_open_fd() instead
if(mpg123_open(mp3streamf, ofPathToString(path).c_str())!=MPG123_OK){
mpg123_close(mp3streamf);
mpg123_delete(mp3streamf);
ofLogError("ofOpenALSoundPlayer") << "mpg123Stream(): couldn't read " << path ;
Expand Down Expand Up @@ -437,13 +453,14 @@ bool ofOpenALSoundPlayer::mpg123Stream(const of::filesystem::path& path,std::vec
#endif

//------------------------------------------------------------
bool ofOpenALSoundPlayer::stream(const of::filesystem::path& fileName, std::vector<short> & buffer){
bool ofOpenALSoundPlayer::stream(const fs::path & fileName, std::vector<short> & buffer){
#ifdef OF_USING_MPG123
if(ofFilePath::getFileExt(fileName)=="mp3" || ofFilePath::getFileExt(fileName)=="MP3" || mp3streamf){
auto ext { ofGetExtensionLower(fileName) };
if (ext == ".mp3" || mp3streamf) {
if(!mpg123Stream(fileName,buffer,fftAuxBuffer)) return false;
}else
} else
#endif
if(!sfStream(fileName,buffer,fftAuxBuffer)) return false;
if(!sfStream(fileName,buffer,fftAuxBuffer)) return false;

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

bool ofOpenALSoundPlayer::readFile(const of::filesystem::path& fileName, std::vector<short> & buffer){
bool ofOpenALSoundPlayer::readFile(const fs::path & fileName, std::vector<short> & buffer){
#ifdef OF_USING_MPG123
if(ofFilePath::getFileExt(fileName)!="mp3" && ofFilePath::getFileExt(fileName)!="MP3"){
if(!sfReadFile(fileName,buffer,fftAuxBuffer)) return false;
}else{
auto ext { ofGetExtensionLower(fileName) };
if (ext == ".mp3") {
if(!mpg123ReadFile(fileName,buffer,fftAuxBuffer)) return false;
}else{
if(!sfReadFile(fileName,buffer,fftAuxBuffer)) return false;
}
#else
if(!sfReadFile(fileName,buffer,fftAuxBuffer)) return false;
Expand All @@ -480,7 +498,7 @@ bool ofOpenALSoundPlayer::readFile(const of::filesystem::path& fileName, std::ve
}

//------------------------------------------------------------
bool ofOpenALSoundPlayer::load(const of::filesystem::path & _fileName, bool is_stream){
bool ofOpenALSoundPlayer::load(const fs::path& _fileName, bool is_stream){

auto fileName = ofToDataPath(_fileName);

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

alSourcef (sources[0], AL_PITCH, 1.0f);
alSourcef (sources[0], AL_GAIN, 1.0f);
alSourcef (sources[0], AL_ROLLOFF_FACTOR, 0.0);
alSourcei (sources[0], AL_SOURCE_RELATIVE, AL_TRUE);
alSourcef (sources[0], AL_ROLLOFF_FACTOR, 0.0);
alSourcei (sources[0], AL_SOURCE_RELATIVE, AL_TRUE);
}else{
std::vector<std::vector<short> > multibuffer;
multibuffer.resize(channels);
Expand Down Expand Up @@ -706,7 +724,7 @@ void ofOpenALSoundPlayer::update(ofEventArgs & args){

//------------------------------------------------------------
void ofOpenALSoundPlayer::unload(){

stop();
ofRemoveListener(ofEvents().update,this,&ofOpenALSoundPlayer::update);

Expand Down Expand Up @@ -804,7 +822,7 @@ void ofOpenALSoundPlayer::setPositionMS(int ms){
}else
#endif
if(streamf){
stream_samples_read = sf_seek(streamf,float(ms)/1000.f*samplerate,SEEK_SET) * channels;
stream_samples_read = sf_seek(streamf,float(ms)/1000.f*samplerate,SEEK_SET) * channels;
}else{
for(int i=0;i<(int)channels;i++){
alSourcef(sources[sources.size()-channels+i],AL_SEC_OFFSET,float(ms)/1000.f);
Expand Down Expand Up @@ -846,13 +864,13 @@ void ofOpenALSoundPlayer::setPan(float p){
float pos[3] = {p,0,0};
alSourcefv(sources[sources.size()-1],AL_POSITION,pos);
}else{
// calculates left/right volumes from pan-value (constant panning law)
// see: Curtis Roads: Computer Music Tutorial p 460
// calculates left/right volumes from pan-value (constant panning law)
// see: Curtis Roads: Computer Music Tutorial p 460
// thanks to jasch
float angle = p * glm::quarter_pi<float>(); // in radians from -45. to +45.
float cosAngle = std::cos(angle);
float sinAngle = std::sin(angle);

float angle = p * glm::quarter_pi<float>(); // in radians from -45. to +45.
float cosAngle = std::cos(angle);
float sinAngle = std::sin(angle);

float leftVol = (cosAngle - sinAngle) * glm::one_over_root_two<float>(); //// multiplied by 1/sqrt(2)
float rightVol = (cosAngle + sinAngle) * glm::one_over_root_two<float>(); // multiplied by 1/sqrt(2)
Expand Down Expand Up @@ -948,8 +966,8 @@ void ofOpenALSoundPlayer::play(){
float pos[3] = {1,0,0};
alSourcefv(sources[sources.size()-channels+i],AL_POSITION,pos);
}
alSourcef (sources[sources.size()-channels+i], AL_ROLLOFF_FACTOR, 0.0);
alSourcei (sources[sources.size()-channels+i], AL_SOURCE_RELATIVE, AL_TRUE);
alSourcef (sources[sources.size()-channels+i], AL_ROLLOFF_FACTOR, 0.0);
alSourcei (sources[sources.size()-channels+i], AL_SOURCE_RELATIVE, AL_TRUE);
}

err = alGetError();
Expand Down
14 changes: 3 additions & 11 deletions libs/openFrameworks/utils/ofConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ typedef TESSindex ofIndexType;
#if defined(OF_USE_EXPERIMENTAL_FS)
// C++17 experimental fs support
#include <experimental/filesystem>

namespace std {
namespace experimental{
namespace filesystem {
Expand All @@ -445,16 +444,9 @@ typedef TESSindex ofIndexType;
}
#else
#include <filesystem>
#if defined(OF_HAS_CPP17)
// Regular C++17 fs support
namespace of {
namespace filesystem = std::filesystem;
}
#else
namespace of {
namespace filesystem = std::filesystem;
}
#endif
namespace of {
namespace filesystem = std::filesystem;
}
#endif
#else //not OF_USING_STD_FS
// No experimental or c++17 filesytem support use boost
Expand Down

0 comments on commit 19c41e9

Please sign in to comment.