diff --git a/.gitmodules b/.gitmodules index b186571..aba81be 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "lib/kit"] path = lib/kit url = https://github.com/flipcoder/kit.git +[submodule "lib/coal"] + path = lib/coal + url = http://github.com/flipcoder/coal diff --git a/Qor/Audio.cpp b/Qor/Audio.cpp index 3d14fb6..4df2f1b 100644 --- a/Qor/Audio.cpp +++ b/Qor/Audio.cpp @@ -8,50 +8,52 @@ float Audio :: s_Rolloff = 1.0f; float Audio :: s_MaxDist = 2048.0f; float Audio :: s_ReferenceDist = 256.0f; -Audio::Buffer :: Buffer(){ - if(Headless::enabled()) - return; - auto l = Audio::lock(); - alGenBuffers(1, &id); -} +std::unique_ptr Audio :: s_pCoal; +std::unique_ptr Audio :: s_pSpace; + +//Audio::Buffer :: Buffer(){ +// if(Headless::enabled()) +// return; +// auto l = Audio::lock(); + //alGenBuffers(1, &id); +//} Audio::Buffer :: Buffer(const std::string& fn, ICache* c) { if(Headless::enabled()) return; auto l = Audio::lock(); - Audio::check_errors(); - id = alutCreateBufferFromFile(fn.c_str()); - Audio::check_errors(); + buf = std::make_shared(fn); } Audio::Buffer :: Buffer(const std::tuple& args): Buffer(std::get<0>(args), std::get<1>(args)) {} Audio::Buffer :: ~Buffer() { - if(id){ - auto l = Audio::lock(); - Audio::check_errors(); - alDeleteBuffers(1, &id); - Audio::check_errors(); - } + //if(id){ + // auto l = Audio::lock(); + // //Audio::check_errors(); + // //alDeleteBuffers(1, &id); + // //Audio::check_errors(); + //} } float Audio::Buffer :: length() const { if(Headless::enabled()) return 0.0f; - assert(id > 0); + //assert(id > 0); - ALint sz; - ALint channels; - ALint bits; - ALint freq; + //ALint sz; + //ALint channels; + //ALint bits; + //ALint freq; - alGetBufferi(id, AL_SIZE, &sz); - alGetBufferi(id, AL_CHANNELS, &channels); - alGetBufferi(id, AL_BITS, &bits); - alGetBufferi(id, AL_FREQUENCY, &freq); + //alGetBufferi(id, AL_SIZE, &sz); + //alGetBufferi(id, AL_CHANNELS, &channels); + //alGetBufferi(id, AL_BITS, &bits); + //alGetBufferi(id, AL_FREQUENCY, &freq); - unsigned samples = sz * 8 / (channels * bits); - return (float)samples / (float)freq; + //unsigned samples = sz * 8 / (channels * bits); + //return (float)samples / (float)freq; + return 0.0f; } Audio::Source :: Source( @@ -60,18 +62,20 @@ Audio::Source :: Source( flags(_flags) { auto l = Audio::lock(); - alGenSources(1, &id); - if(flags & F_AUTOPLAY){ - play(); - } + source = std::make_shared(); + s_pSpace->add(source); + //alGenSources(1, &id); + //if(flags & F_AUTOPLAY){ + // play(); + //} } Audio::Source :: ~Source() { auto l = Audio::lock(); //stop(); //alSourcei(buffer_id, AL_BUFFER, 0); - Audio::check_errors(); - alDeleteSources(1, &id); - Audio::check_errors(); + //Audio::check_errors(); + //alDeleteSources(1, &id); + //Audio::check_errors(); } bool Audio::Source :: update() { return false; @@ -79,45 +83,49 @@ bool Audio::Source :: update() { } void Audio::Source :: bind(Buffer* buf) { auto l = Audio::lock(); - buffer_id = buf ? buf->id : 0; - alSourcei(id, AL_BUFFER, buf ? buf->id : 0); - check_errors(); -} -void Audio::Source :: refresh() { - if(!buffer_id) - return; - auto l = Audio::lock(); - check_errors(); - //alSourcei(id, AL_BUFFER, buffer_id); - alSourcef(id, AL_PITCH, pitch); - alSourcef(id, AL_GAIN, kit::clamp(gain, 0.0f, 1.0f - K_EPSILON)); - alSourcei(id, AL_SOURCE_RELATIVE, (flags & F_AMBIENT) ? AL_TRUE : AL_FALSE); - alSourcei(id, AL_ROLLOFF_FACTOR, (flags & F_AMBIENT) ? 0.0f : s_Rolloff); - alSourcefv(id, AL_POSITION, glm::value_ptr(pos)); - alSourcefv(id, AL_VELOCITY, glm::value_ptr(vel)); - alSourcef(id, AL_MAX_DISTANCE, s_MaxDist); - alSourcef(id, AL_REFERENCE_DISTANCE, s_ReferenceDist); - alSourcei(id, AL_LOOPING, (flags & F_LOOP) ? AL_TRUE : AL_FALSE); - check_errors(); -} + //buffer_id = buf ? buf->id : 0; + //alSourcei(id, AL_BUFFER, buf ? buf->id : 0); + //check_errors(); + source->add(buf->buf); +} +//void Audio::Source :: refresh() { +// if(!buffer_id) +// return; +// auto l = Audio::lock(); +// //check_errors(); +// //alSourcei(id, AL_BUFFER, buffer_id); +// alSourcef(id, AL_PITCH, pitch); +// alSourcef(id, AL_GAIN, kit::clamp(gain, 0.0f, 1.0f - K_EPSILON)); +// alSourcei(id, AL_SOURCE_RELATIVE, (flags & F_AMBIENT) ? AL_TRUE : AL_FALSE); +// alSourcei(id, AL_ROLLOFF_FACTOR, (flags & F_AMBIENT) ? 0.0f : s_Rolloff); +// alSourcefv(id, AL_POSITION, glm::value_ptr(pos)); +// alSourcefv(id, AL_VELOCITY, glm::value_ptr(vel)); +// alSourcef(id, AL_MAX_DISTANCE, s_MaxDist); +// alSourcef(id, AL_REFERENCE_DISTANCE, s_ReferenceDist); +// alSourcei(id, AL_LOOPING, (flags & F_LOOP) ? AL_TRUE : AL_FALSE); +// //check_errors(); +//} void Audio::Source :: play() { auto l = Audio::lock(); - refresh(); - alSourcePlay(id); + source->play(); + //refresh(); + //alSourcePlay(id); } bool Audio::Source :: playing() const { auto l = Audio::lock(); - ALint state; - alGetSourcei(id, AL_SOURCE_STATE, &state); - //LOGf("state: %s", state) - return state == AL_PLAYING; + return source->playing; + //ALint state; + //alGetSourcei(id, AL_SOURCE_STATE, &state); + ////LOGf("state: %s", state) + //return state == AL_PLAYING; } bool Audio::Source :: stopped() const { auto l = Audio::lock(); - ALint state; - alGetSourcei(id, AL_SOURCE_STATE, &state); - //LOGf("state: %s", state) - return state == AL_STOPPED; + return not source->playing; + //ALint state; + //alGetSourcei(id, AL_SOURCE_STATE, &state); + ////LOGf("state: %s", state) + //return state == AL_STOPPED; } //bool initial() const { // auto l = Audio::lock(); @@ -127,210 +135,213 @@ bool Audio::Source :: stopped() const { //} void Audio::Source :: pause() { auto l = Audio::lock(); - alSourcePause(id); + source->pause(); } void Audio::Source :: stop() { auto l = Audio::lock(); - if(playing()) - alSourceStop(id); + source->stop(); + //if(playing()) + // alSourceStop(id); } -void Audio::Stream :: deinit() -{ - auto l = Audio::lock(); - if(m_bOpen) - { - auto l = Audio::lock(); - stop(); - clear(); - alDeleteBuffers(NUM_BUFFERS, m_Buffers); - Audio::clear_errors(); - m_bOpen = false; - } -} +//void Audio::Stream :: deinit() +//{ +// auto l = Audio::lock(); +// if(m_bOpen) +// { +// auto l = Audio::lock(); +// stop(); +// clear(); +// alDeleteBuffers(NUM_BUFFERS, m_Buffers); +// Audio::clear_errors(); +// m_bOpen = false; +// } +//} -void Audio::OggStream :: deinit() -{ - auto l = Audio::lock(); - if(m_bOpen) - { - stop(); - clear(); - ov_clear(&m_Ogg); - Stream::deinit(); - m_bOpen = false; - } -} +//void Audio::OggStream :: deinit() +//{ +// auto l = Audio::lock(); +// if(m_bOpen) +// { +// stop(); +// clear(); +// ov_clear(&m_Ogg); +// Stream::deinit(); +// m_bOpen = false; +// } +//} Audio::Stream :: ~Stream() { - deinit(); + //deinit(); } -Audio::OggStream :: ~OggStream() -{ - deinit(); -} +//Audio::OggStream :: ~OggStream() +//{ +// //deinit(); +//} bool Audio::Stream :: update() { auto l = Audio::lock(); - clear_errors(); - int processed; - bool active = true; + m_pStream->update(); + //clear_errors(); + //int processed; + //bool active = true; - alGetSourcei(id, AL_BUFFERS_PROCESSED, &processed); + //alGetSourcei(id, AL_BUFFERS_PROCESSED, &processed); - while(processed--) - { - ALuint buffer; + //while(processed--) + //{ + // ALuint buffer; - alSourceUnqueueBuffers(id, 1, &buffer); - Audio::clear_errors(); + // alSourceUnqueueBuffers(id, 1, &buffer); + // Audio::clear_errors(); - active = stream(buffer); + // active = stream(buffer); - if(active) { - alSourceQueueBuffers(id, 1, &buffer); - Audio::check_errors(); - } - } - return active; + // if(active) { + // alSourceQueueBuffers(id, 1, &buffer); + // //Audio::check_errors(); + // } + //} + //return active; } -void Audio::Stream :: clear() -{ - auto l = Audio::lock(); - Audio::check_errors(); - int queued; - alGetSourcei(id, AL_BUFFERS_QUEUED, &queued); - while(queued--) - { - ALuint buffer; - alSourceUnqueueBuffers(id, 1, &buffer); - Audio::clear_errors(); - //if(Audio::check_errors()) - // break; - } -} +//void Audio::Stream :: clear() +//{ +// auto l = Audio::lock(); +// //Audio::check_errors(); +// int queued; +// alGetSourcei(id, AL_BUFFERS_QUEUED, &queued); +// while(queued--) +// { +// ALuint buffer; +// alSourceUnqueueBuffers(id, 1, &buffer); +// //Audio::clear_errors(); +// //if(Audio::check_errors()) +// // break; +// } +//} -void Audio::Stream :: refresh() -{ +//void Audio::Stream :: refresh() +//{ - //if(playing()) - //{ - auto l = Audio::lock(); +// //if(playing()) +// //{ +// auto l = Audio::lock(); - update(); - - //alSourcei(id, AL_BUFFER, buffer_id); - alSourcef(id, AL_PITCH, pitch); - alSourcef(id, AL_GAIN, kit::clamp(gain, 0.0f, 1.0f - K_EPSILON)); - alSourcefv(id, AL_POSITION, glm::value_ptr(pos)); - alSourcefv(id, AL_VELOCITY, glm::value_ptr(vel)); - alSourcei(id, AL_SOURCE_RELATIVE, (flags & F_AMBIENT) ? AL_TRUE : AL_FALSE); - alSourcei(id, AL_ROLLOFF_FACTOR, (flags & F_AMBIENT) ? 0.0f : s_Rolloff); - alSourcef(id, AL_MAX_DISTANCE, s_MaxDist); - alSourcef(id, AL_REFERENCE_DISTANCE, s_ReferenceDist); - //alSourcei(id, AL_LOOPING, (flags & F_LOOP) ? AL_TRUE : AL_FALSE); - //} -} +// update(); + +// //alSourcei(id, AL_BUFFER, buffer_id); +// alSourcef(id, AL_PITCH, pitch); +// alSourcef(id, AL_GAIN, kit::clamp(gain, 0.0f, 1.0f - K_EPSILON)); +// alSourcefv(id, AL_POSITION, glm::value_ptr(pos)); +// alSourcefv(id, AL_VELOCITY, glm::value_ptr(vel)); +// alSourcei(id, AL_SOURCE_RELATIVE, (flags & F_AMBIENT) ? AL_TRUE : AL_FALSE); +// alSourcei(id, AL_ROLLOFF_FACTOR, (flags & F_AMBIENT) ? 0.0f : s_Rolloff); +// alSourcef(id, AL_MAX_DISTANCE, s_MaxDist); +// alSourcef(id, AL_REFERENCE_DISTANCE, s_ReferenceDist); +// //alSourcei(id, AL_LOOPING, (flags & F_LOOP) ? AL_TRUE : AL_FALSE); +// //} +//} void Audio::Stream :: play() { auto l = Audio::lock(); - if(playing()) - return; - clear(); - for(int i=0;iplay(); + //if(playing()) + // return; + //clear(); + //for(int i=0;i 0) - size += result; - else - { - if(result < 0) - return false; - else - break; - } - } - - if(size == 0) - return false; - - alBufferData(buffer, m_Format, data, size, m_VorbisInfo->rate); - Audio::check_errors(); - return true; -} +// if(result > 0) +// size += result; +// else +// { +// if(result < 0) +// return false; +// else +// break; +// } +// } + +// if(size == 0) +// return false; + +// alBufferData(buffer, m_Format, data, size, m_VorbisInfo->rate); +// //Audio::check_errors(); +// return true; +//} -bool Audio::RawStream :: stream(unsigned int buffer) -{ - auto l = Audio::lock(); +//bool Audio::RawStream :: stream(unsigned int buffer) +//{ +// auto l = Audio::lock(); - char data[BUFFER_SIZE]; - int size = 0; - int endian = 0; - int section; - int result; - - while(size < BUFFER_SIZE) - { - result = m_onRead(data + size, BUFFER_SIZE - size); +// char data[BUFFER_SIZE]; +// int size = 0; +// int endian = 0; +// int section; +// int result; + +// while(size < BUFFER_SIZE) +// { +// result = m_onRead(data + size, BUFFER_SIZE - size); - if(result > 0) - size += result; - else - { - if(result < 0) - return false; - else - break; - } - } - - if(size == 0) - return false; - - alBufferData(buffer, m_Format, data, size, m_Rate); - Audio::check_errors(); - return true; -} +// if(result > 0) +// size += result; +// else +// { +// if(result < 0) +// return false; +// else +// break; +// } +// } + +// if(size == 0) +// return false; + +// alBufferData(buffer, m_Format, data, size, m_Rate); +// //Audio::check_errors(); +// return true; +//} Audio::Listener :: Listener() { - gain = 1.0f; - pos = glm::vec3(0.0f, 0.0f, 0.0f); - vel = glm::vec3(0.0f, 0.0f, 0.0f); - at = glm::vec3(0.0f, 0.0f, -1.0f); - up = glm::vec3(0.0f, -1.0f, 0.0f); + //gain = 1.0f; + //pos = glm::vec3(0.0f, 0.0f, 0.0f); + //vel = glm::vec3(0.0f, 0.0f, 0.0f); + //at = glm::vec3(0.0f, 0.0f, -1.0f); + //up = glm::vec3(0.0f, -1.0f, 0.0f); } Audio::Listener :: ~Listener() {} @@ -340,134 +351,147 @@ void Audio::Listener :: listen() if(Headless::enabled()) return; - auto l = Audio::lock(); - alListenerf(AL_GAIN, kit::clamp(gain, 0.0f, 1.0f - K_EPSILON)); - alListenerfv(AL_POSITION, glm::value_ptr(pos)); - alListenerfv(AL_VELOCITY, glm::value_ptr(vel)); - float ori[6]; - ori[0] = at.x; ori[1] = at.y; ori[2] = at.z; - ori[3] = up.x; ori[4] = up.y; ori[5] = up.z; - alListenerfv(AL_ORIENTATION, ori); + //auto l = Audio::lock(); + //alListenerf(AL_GAIN, kit::clamp(gain, 0.0f, 1.0f - K_EPSILON)); + //alListenerfv(AL_POSITION, glm::value_ptr(pos)); + //alListenerfv(AL_VELOCITY, glm::value_ptr(vel)); + //float ori[6]; + //ori[0] = at.x; ori[1] = at.y; ori[2] = at.z; + //ori[3] = up.x; ori[4] = up.y; ori[5] = up.z; + //alListenerfv(AL_ORIENTATION, ori); } Audio :: Audio() { if(Headless::enabled()) return; - - auto l = lock(); - alutInit(0, NULL); - alutInitWithoutContext(0, NULL); - m_pDevice = alcOpenDevice(NULL); - if(not m_pDevice) - throw std::runtime_error("failed to open OpenAL audio device"); - m_pContext = alcCreateContext(m_pDevice, NULL); - if(not m_pContext) - alcCloseDevice(m_pDevice); - try{ - set_context(); - }catch(...){ - alcDestroyContext(m_pContext); - alcCloseDevice(m_pDevice); - throw; + + if(not s_pCoal){ + s_pCoal = kit::make_unique(); + s_pSpace = kit::make_unique(); } + + //auto l = lock(); + //alutInit(0, NULL); + //alutInitWithoutContext(0, NULL); + //m_pDevice = alcOpenDevice(NULL); + //if(not m_pDevice) + // throw std::runtime_error("failed to open OpenAL audio device"); + //m_pContext = alcCreateContext(m_pDevice, NULL); + //if(not m_pContext) + // alcCloseDevice(m_pDevice); + //try{ + // set_context(); + //}catch(...){ + // alcDestroyContext(m_pContext); + // alcCloseDevice(m_pDevice); + // throw; + //} } Audio::Stream :: Stream() { - init(); + //init(); } Audio::Stream :: Stream(std::string fn): Resource(fn) { - init(fn); + source = std::make_shared(); + m_pStream = std::make_shared(fn); + source->add(m_pStream); + s_pSpace->add(source); } -void Audio::Stream :: init(std::string fn) -{ - if(Headless::enabled()) - return; +//void Audio::Stream :: init(std::string fn) +//{ +// if(Headless::enabled()) +// return; - auto l = Audio::lock(); +// auto l = Audio::lock(); - deinit(); +// deinit(); - clear_errors(); +// clear_errors(); - alGenBuffers(NUM_BUFFERS, m_Buffers); +// alGenBuffers(NUM_BUFFERS, m_Buffers); - if(check_errors()) - K_ERROR(READ, Filesystem::getFileName(fn)); +// //if(check_errors()) +// // K_ERROR(READ, Filesystem::getFileName(fn)); - m_Format = AL_FORMAT_MONO16; // default - m_bOpen = true; -} +// m_Format = AL_FORMAT_MONO16; // default +// m_bOpen = true; +//} -Audio::OggStream :: OggStream(std::string fn): - Stream(fn) -{ - auto l = Audio::lock(); +//Audio::OggStream :: OggStream(std::string fn): +// Stream(fn) +//{ +// auto l = Audio::lock(); - if(Headless::enabled()) - return; +// if(Headless::enabled()) +// return; - clear_errors(); +// //clear_errors(); - int r; - if((r = ov_fopen((char*)&fn[0], &m_Ogg)) < 0) - K_ERROR(READ, Filesystem::getFileName(fn)); +// //int r; +// //if((r = ov_fopen((char*)&fn[0], &m_Ogg)) < 0) +// // K_ERROR(READ, Filesystem::getFileName(fn)); - if(check_errors()) - K_ERROR(READ, Filesystem::getFileName(fn)); +// ////if(check_errors()) +// //// K_ERROR(READ, Filesystem::getFileName(fn)); - m_VorbisInfo = ov_info(&m_Ogg, -1); - m_VorbisComment = ov_comment(&m_Ogg, -1); +// //m_VorbisInfo = ov_info(&m_Ogg, -1); +// //m_VorbisComment = ov_comment(&m_Ogg, -1); - if(check_errors()) - K_ERROR(READ, Filesystem::getFileName(fn)); +// ////if(check_errors()) +// //// K_ERROR(READ, Filesystem::getFileName(fn)); - if(m_VorbisInfo->channels == 1) - m_Format = AL_FORMAT_MONO16; - else - m_Format = AL_FORMAT_STEREO16; +// //if(m_VorbisInfo->channels == 1) +// // m_Format = AL_FORMAT_MONO16; +// //else +// // m_Format = AL_FORMAT_STEREO16; - if(check_errors()) - K_ERROR(READ, Filesystem::getFileName(fn)); - - flags |= Source::F_LOOP; - - //std::cout - // << "version " << m_VorbisInfo->version << "\n" - // << "channels " << m_VorbisInfo->channels << "\n" - // << "rate (hz) " << m_VorbisInfo->rate << "\n" - // << "bitrate upper " << m_VorbisInfo->bitrate_upper << "\n" - // << "bitrate nominal " << m_VorbisInfo->bitrate_nominal << "\n" - // << "bitrate lower " << m_VorbisInfo->bitrate_lower << "\n" - // << "bitrate window " << m_VorbisInfo->bitrate_window << "\n" - // << "\n" - // << "vendor " << m_VorbisComment->vendor << "\n"; +// ////if(check_errors()) +// //// K_ERROR(READ, Filesystem::getFileName(fn)); + +// //flags |= Source::F_LOOP; + +// //std::cout +// // << "version " << m_VorbisInfo->version << "\n" +// // << "channels " << m_VorbisInfo->channels << "\n" +// // << "rate (hz) " << m_VorbisInfo->rate << "\n" +// // << "bitrate upper " << m_VorbisInfo->bitrate_upper << "\n" +// // << "bitrate nominal " << m_VorbisInfo->bitrate_nominal << "\n" +// // << "bitrate lower " << m_VorbisInfo->bitrate_lower << "\n" +// // << "bitrate window " << m_VorbisInfo->bitrate_window << "\n" +// // << "\n" +// // << "vendor " << m_VorbisComment->vendor << "\n"; - //for(int i = 0; i < m_VorbisComment->comments; i++) - // std::cout << " " << m_VorbisComment->user_comments[i] << "\n"; +// //for(int i = 0; i < m_VorbisComment->comments; i++) +// // std::cout << " " << m_VorbisComment->user_comments[i] << "\n"; - //std::cout << std::endl; -} +// //std::cout << std::endl; +//} Audio :: ~Audio() { - auto l = lock(); - alcDestroyContext(m_pContext); - alcCloseDevice(m_pDevice); - alutExit(); + //auto l = lock(); + //alcDestroyContext(m_pContext); + //alcCloseDevice(m_pDevice); + //alutExit(); +} + +void Audio :: update() +{ + s_pSpace->update(); } void Audio :: set_context() { - auto l = Audio::lock(); - if(not alcMakeContextCurrent(m_pContext)) - throw std::runtime_error("failed to set OpenAL Context"); - clear_errors(); + //auto l = Audio::lock(); + //if(not alcMakeContextCurrent(m_pContext)) + // throw std::runtime_error("failed to set OpenAL Context"); + //clear_errors(); } void Audio :: listen(Listener* listener) const @@ -478,74 +502,75 @@ void Audio :: listen(Listener* listener) const bool Audio :: error() const { - auto l = Audio::lock(); - return alGetError(); -} - -void Audio :: clear_errors() -{ - auto l = Audio::lock(); - alGetError(); -} -bool Audio :: check_errors() -{ - int error = alGetError(); - if(error != AL_NO_ERROR) { - std::tuple errpair = error_string_al(error); - WARNINGf("OpenAL Error (%s): %s", - std::get<0>(errpair) % std::get<1>(errpair) - ); - return true; - } + //auto l = Audio::lock(); + //return alGetError(); return false; } -std::tuple Audio :: error_string_al(int code) -{ - switch(code) - { - case AL_INVALID_NAME: - return std::make_tuple("AL_INVALID_NAME", "Invalid name."); - case AL_INVALID_ENUM: - return std::make_tuple("AL_INVALID_ENUM", "Invalid enum."); - case AL_INVALID_VALUE: - return std::make_tuple("AL_INVALID_VALUE", "Invalid value."); - case AL_INVALID_OPERATION: - return std::make_tuple("AL_INVALID_OPERATION", "Invalid operation."); - case AL_OUT_OF_MEMORY: - return std::make_tuple("AL_OUT_OF_MEMORY", "Out of memory."); - } - return (code!=AL_NO_ERROR) ? - std::tuple(std::string(), std::string("No Error.")): - std::tuple( - boost::to_string(code), - std::string("Unknown Error Code") - ); -} +//void Audio :: clear_errors() +//{ +// //auto l = Audio::lock(); +// //alGetError(); +//} +//bool Audio :: check_errors() +//{ +// int error = alGetError(); +// if(error != AL_NO_ERROR) { +// std::tuple errpair = error_string_al(error); +// WARNINGf("OpenAL Error (%s): %s", +// std::get<0>(errpair) % std::get<1>(errpair) +// ); +// return true; +// } +// return false; +//} -std::tuple Audio :: error_string_ov(int code) -{ - switch(code) - { - // libvorbis return codes http://www.xiph.org/vorbis/doc/libvorbis/return.html - case OV_EREAD: - return std::make_tuple("OC_EREAD","Read from media."); - case OV_ENOTVORBIS: - return std::make_tuple("OC_ENOTVORBIS","Not Vorbis data."); - case OV_EVERSION: - return std::make_tuple("OV_EVERSION", "Vorbis version mismatch."); - case OV_EBADHEADER: - return std::make_tuple("OV_EBADHEADER", "Invalid Vorbis header."); - case OV_EFAULT: - return std::make_tuple("OV_EFAULT", "Internal logic fault (bug or heap/stack corruption."); - } - return code ? - std::tuple( - boost::to_string(code), - std::string("Unknown Error Code ") + boost::to_string(code) - ): - std::tuple("","No Error."); -} +//std::tuple Audio :: error_string_al(int code) +//{ +// switch(code) +// { +// case AL_INVALID_NAME: +// return std::make_tuple("AL_INVALID_NAME", "Invalid name."); +// case AL_INVALID_ENUM: +// return std::make_tuple("AL_INVALID_ENUM", "Invalid enum."); +// case AL_INVALID_VALUE: +// return std::make_tuple("AL_INVALID_VALUE", "Invalid value."); +// case AL_INVALID_OPERATION: +// return std::make_tuple("AL_INVALID_OPERATION", "Invalid operation."); +// case AL_OUT_OF_MEMORY: +// return std::make_tuple("AL_OUT_OF_MEMORY", "Out of memory."); +// } +// return (code!=AL_NO_ERROR) ? +// std::tuple(std::string(), std::string("No Error.")): +// std::tuple( +// boost::to_string(code), +// std::string("Unknown Error Code") +// ); +//} + +//std::tuple Audio :: error_string_ov(int code) +//{ +// switch(code) +// { +// // libvorbis return codes http://www.xiph.org/vorbis/doc/libvorbis/return.html +// case OV_EREAD: +// return std::make_tuple("OC_EREAD","Read from media."); +// case OV_ENOTVORBIS: +// return std::make_tuple("OC_ENOTVORBIS","Not Vorbis data."); +// case OV_EVERSION: +// return std::make_tuple("OV_EVERSION", "Vorbis version mismatch."); +// case OV_EBADHEADER: +// return std::make_tuple("OV_EBADHEADER", "Invalid Vorbis header."); +// case OV_EFAULT: +// return std::make_tuple("OV_EFAULT", "Internal logic fault (bug or heap/stack corruption."); +// } +// return code ? +// std::tuple( +// boost::to_string(code), +// std::string("Unknown Error Code ") + boost::to_string(code) +// ): +// std::tuple("","No Error."); +//} #endif diff --git a/Qor/Audio.h b/Qor/Audio.h index c172f4d..d9f0188 100644 --- a/Qor/Audio.h +++ b/Qor/Audio.h @@ -2,17 +2,18 @@ #define _AUDIO_H #ifndef QOR_NO_AUDIO -#ifdef _WIN32 - #include - #include -#else - #include - #include -#endif -#include -#include -#include -#include +//#ifdef _WIN32 +// #include +// #include +//#else +// #include +// #include +//#endif +//#include +//#include +//#include +//#include +#include #include #include "kit/log/log.h" #include "Filesystem.h" @@ -40,24 +41,25 @@ class Audio public Resource { unsigned id = 0; - + std::shared_ptr buf; Buffer(); Buffer(const std::string& fn, ICache* c); Buffer(const std::tuple& args); virtual ~Buffer(); - bool good() const { return id!=0; } + //bool good() const { return id!=0; } float length() const; }; struct Source { - mutable unsigned id; - float pitch = 1.0f; + //mutable unsigned id; + std::shared_ptr source; + //float pitch = 1.0f; float gain = 1.0f; - float rolloff = 0.0f; + //float rolloff = 0.0f; glm::vec3 pos; - glm::vec3 vel; - unsigned int buffer_id = 0; + //glm::vec3 vel; + //unsigned int buffer_id = 0; enum eFlags { F_LOOP = kit::bit(0), F_AUTOPLAY = kit::bit(1), @@ -68,19 +70,19 @@ class Audio virtual ~Source(); virtual bool update(); void bind(Buffer* buf); - virtual void refresh(); + //virtual void refresh(); virtual void play(); bool playing() const; bool stopped() const; void pause(); void stop(); - virtual bool good() const { return id!=0; } + //virtual bool good() const { return id!=0; } }; struct Stream: public Source, public Resource - { + { public: Stream(); @@ -91,75 +93,75 @@ class Audio virtual ~Stream(); virtual bool update() override; - void clear(); - virtual void refresh() override; + //void clear(); + //virtual void refresh() override; virtual void play() override; - virtual bool good() const override { return m_bOpen; } + //virtual bool good() const override { return m_bOpen; } protected: - void init(std::string fn = ""); - virtual void deinit(); + //void init(std::string fn = ""; + //virtual void deinit(); - ALenum m_Format; - ALuint m_Buffers[NUM_BUFFERS]; - bool m_bOpen = false; - std::string m_Filename; + std::shared_ptr m_pStream; + //ALenum m_Format; + //ALuint m_Buffers[NUM_BUFFERS]; + //bool m_bOpen = false; + //std::string m_Filename; - virtual bool stream(unsigned int buffer); + //virtual bool stream(unsigned int buffer); }; - struct OggStream: - public Stream - { - public: + //struct OggStream: + // public Stream + //{ + // public: - OggStream(std::string fn); - OggStream(const std::tuple& args): - OggStream(std::get<0>(args)) - {} + // OggStream(std::string fn); + // OggStream(const std::tuple& args): + // OggStream(std::get<0>(args)) + // {} - virtual ~OggStream(); + // virtual ~OggStream(); - private: + // private: - virtual void deinit() override; - virtual bool stream(unsigned int buffer) override; + // virtual void deinit() override; + // virtual bool stream(unsigned int buffer) override; - //FILE* m_File; - OggVorbis_File m_Ogg; - vorbis_info* m_VorbisInfo; - vorbis_comment* m_VorbisComment; - }; + // //FILE* m_File; + // OggVorbis_File m_Ogg; + // vorbis_info* m_VorbisInfo; + // vorbis_comment* m_VorbisComment; + //}; - struct RawStream: - public Stream - { - public: - - RawStream() {} - RawStream(std::string fn): - Stream(fn) - {} - RawStream(const std::tuple& args): - RawStream(std::get<0>(args)) - {} + //struct RawStream: + // public Stream + //{ + // public: + + // RawStream() {} + // RawStream(std::string fn): + // Stream(fn) + // {} + // RawStream(const std::tuple& args): + // RawStream(std::get<0>(args)) + // {} - virtual ~RawStream() {} + // virtual ~RawStream() {} - void on_read(std::function func) { - m_onRead = func; - } + // void on_read(std::function func) { + // m_onRead = func; + // } - private: + // private: - virtual void deinit() override {} - virtual bool stream(unsigned int buffer) override; + // virtual void deinit() override {} + // virtual bool stream(unsigned int buffer) override; - long m_Rate = 44100L; + // long m_Rate = 44100L; - std::function m_onRead; - }; - + // std::function m_onRead; + //}; struct Listener { @@ -167,6 +169,7 @@ class Audio glm::vec3 at; glm::vec3 up; float gain; + std::shared_ptr listener; Listener(); virtual ~Listener(); @@ -175,13 +178,14 @@ class Audio Audio(); virtual ~Audio(); + void update(); void set_context(); void listen(Listener* listener) const; bool error() const; - static void clear_errors(); - static bool check_errors(); - static std::tuple error_string_al(int code); - static std::tuple error_string_ov(int code); + //static void clear_errors(); + //static bool check_errors(); + //static std::tuple error_string_al(int code); + //static std::tuple error_string_ov(int code); static float rolloff() { return s_Rolloff; } static float max_distance() { return s_MaxDist; } @@ -203,13 +207,18 @@ class Audio //K_S_GET_SET(float, max_distance, m_MaxDist); //K_S_GET_SET(float, reference_distance, m_ReferenceDist); + static coal::Space* space() { return s_pSpace.get(); } + private: - ALCdevice* m_pDevice = nullptr; - ALCcontext* m_pContext = nullptr; + //ALCdevice* m_pDevice = nullptr; + //ALCcontext* m_pContext = nullptr; static float s_Rolloff; static float s_MaxDist; static float s_ReferenceDist; + + static std::unique_ptr s_pCoal; + static std::unique_ptr s_pSpace; }; #endif diff --git a/Qor/BasicState.cpp b/Qor/BasicState.cpp index 6f9bb29..17d4172 100644 --- a/Qor/BasicState.cpp +++ b/Qor/BasicState.cpp @@ -18,7 +18,9 @@ BasicState :: BasicState(Qor* engine): m_pRoot(make_shared()), m_pRTTRoot(make_shared()), m_pPipeline(engine->pipeline()), - m_pResources(engine->resources()) + m_pResources(engine->resources()), + m_pMusic(engine->make("loading.ogg")), + m_pSound(engine->make("test.wav")) {} void BasicState :: preload() @@ -27,6 +29,8 @@ void BasicState :: preload() m_pRoot->add(m_pCamera->as_node()); m_pRTTCamera = make_shared(m_pQor->resources(), m_pQor->window()); m_pRTTRoot->add(m_pRTTCamera->as_node()); + m_pRoot->add(m_pMusic); + m_pRoot->add(m_pSound); } BasicState :: ~BasicState() @@ -61,6 +65,9 @@ void BasicState :: enter() }, std::make_shared(m_pRenderBuffer->texture()) ); m_pRoot->add(mesh); + + m_pMusic->play(); + m_pSound->play(); } void BasicState :: logic(Freq::Time t) diff --git a/Qor/BasicState.h b/Qor/BasicState.h index dc26767..fc471d4 100644 --- a/Qor/BasicState.h +++ b/Qor/BasicState.h @@ -13,6 +13,7 @@ #include "Text.h" #include "Menu.h" #include "RenderBuffer.h" +#include "Sound.h" class Qor; @@ -50,6 +51,9 @@ class BasicState: //unsigned m_DetailShader = ~0u; //unsigned m_Shader = ~0u; + + std::shared_ptr m_pMusic; + std::shared_ptr m_pSound; }; #endif diff --git a/Qor/Camera.cpp b/Qor/Camera.cpp index 7b7fe11..6cd8db5 100644 --- a/Qor/Camera.cpp +++ b/Qor/Camera.cpp @@ -81,16 +81,7 @@ void Camera :: logic_self(Freq::Time t) if(m_bListens) { - //auto pos = position(); - //auto wpos = position(Space::WORLD); - //LOGf("camera mtx: %s", Matrix::to_string(*matrix_c(Space::WORLD))); - //LOGf("camera local: (%s, %s, %s)", pos.x % pos.y % pos.z); - //LOGf("camera world: (%s, %s, %s)", wpos.x % wpos.y % wpos.z); - //auto fo = focal_offset(); - //LOGf("focal offset: (%s, %s, %s)", fo.x % fo.y % fo.z); - //auto opos = wpos - fo * Matrix::scale(*matrix_c(Space::WORLD)); m_Listener.pos = position(Space::WORLD); - //LOGf("camera pos w/ offset: (%s, %s, %s)", opos.x % opos.y % opos.z); m_Listener.at = Matrix::heading(*matrix_c(Space::WORLD)); m_Listener.up = Matrix::up(*matrix_c(Space::WORLD)); m_Listener.listen(); diff --git a/Qor/LoadingState.cpp b/Qor/LoadingState.cpp index 936cbd1..8cfd3bd 100644 --- a/Qor/LoadingState.cpp +++ b/Qor/LoadingState.cpp @@ -171,8 +171,8 @@ void LoadingState :: logic(Freq::Time t) #ifndef QOR_NO_AUDIO if(m_pMusic && m_pMusic->source()) { - m_pMusic->source()->gain = m_Fade.get().r(); - m_pMusic->source()->refresh(); + //m_pMusic->source()->gain = m_Fade.get().r(); + //m_pMusic->source()->refresh(); } #endif diff --git a/Qor/Qor.cpp b/Qor/Qor.cpp index e309a19..b0e67fd 100644 --- a/Qor/Qor.cpp +++ b/Qor/Qor.cpp @@ -34,12 +34,12 @@ namespace fs = boost::filesystem; Qor* Qor :: s_pQor = nullptr; Qor :: Qor(const Args& args, std::string appname="qor"): - m_Args(args), - m_App(appname) + m_Args(args) { m_Filename = args.filename(); s_pQor = this; + if(m_Args.has('d', "dedicated")|| m_Args.has('s', "server")) { @@ -69,7 +69,6 @@ Qor :: Qor(const Args& args, std::string appname="qor"): #ifndef QOR_NO_AUDIO m_Resources.register_class("audiobuffer"); m_Resources.register_class("audiostream"); - m_Resources.register_class("oggstream"); #endif m_Resources.register_class("meshdata"); //m_Resources.register_class("particledata"); @@ -200,6 +199,7 @@ void Qor :: logic() //t = m_pTimer->tick(); ++m_FramesLastSecond; + m_pAudio->update(); m_pInput->logic(t); if(m_pInput->quit_flag()) { @@ -381,7 +381,7 @@ unsigned Qor :: resolve_resource( return class_id; } if(ends_with(fn_cut, ".ogg")) { - static unsigned class_id = m_Resources.class_id("oggstream"); + static unsigned class_id = m_Resources.class_id("audiostream"); return class_id; } #endif diff --git a/Qor/Sound.cpp b/Qor/Sound.cpp index 11b4b28..f0463dd 100644 --- a/Qor/Sound.cpp +++ b/Qor/Sound.cpp @@ -4,18 +4,18 @@ #include "Headless.h" using namespace std; -shared_ptr Sound :: raw(std::function func, Cache* cache) -{ - auto snd = make_shared(cache); - if(not Headless::enabled()){ - snd->m_pSource = make_shared(); - ((Audio::RawStream*)(snd->m_pSource.get()))->on_read(func); - snd->m_pSource->flags |= Audio::Source::F_AMBIENT; - snd->m_pSource->refresh(); - snd->update_signals(); - } - return snd; -} +//shared_ptr Sound :: raw(std::function func, Cache* cache) +//{ +// auto snd = make_shared(cache); +// if(not Headless::enabled()){ +// snd->m_pSource = make_shared(); +// ((Audio::RawStream*)(snd->m_pSource.get()))->on_read(func); +// snd->m_pSource->flags |= Audio::Source::F_AMBIENT; +// snd->m_pSource->refresh(); +// snd->update_signals(); +// } +// return snd; +//} Sound :: Sound(Cache* cache): m_pResources(cache) @@ -56,7 +56,7 @@ Sound :: Sound(const std::string& fn, Cache* cache): if(m_bStream){ //m_pSource = cache->cache_cast(fn); - m_pSource = make_shared(cache->transform(fn)); + m_pSource = make_shared(cache->transform(fn)); //m_pSource->refresh(); }else{ m_pBuffer = cache->cache_cast(fn); @@ -70,8 +70,8 @@ Sound :: Sound(const std::string& fn, Cache* cache): m_pSource->flags |= Audio::Source::F_LOOP; //if(m_bAutoplay) // source()->play(); - if(m_pSource) - m_pSource->refresh(); + //if(m_pSource) + // m_pSource->refresh(); update_signals(); } @@ -108,7 +108,7 @@ void Sound :: logic_self(Freq::Time t) { if(not m_bAmbient) m_pSource->pos = position(Space::WORLD); - m_pSource->refresh(); + //m_pSource->refresh(); m_pSource->update(); } @@ -189,7 +189,7 @@ void Sound :: gain(float g) m_Gain = g; if(m_pSource){ m_pSource->gain = g; - m_pSource->refresh(); + //m_pSource->refresh(); } } diff --git a/Qor/Sound.h b/Qor/Sound.h index d497f84..ae096f1 100644 --- a/Qor/Sound.h +++ b/Qor/Sound.h @@ -22,10 +22,10 @@ class Sound: ) {} - static std::shared_ptr raw( - std::function func, - Cache* cache - ); + //static std::shared_ptr raw( + // std::function func, + // Cache* cache + //); virtual ~Sound(); diff --git a/lib/coal b/lib/coal new file mode 160000 index 0000000..7042bd3 --- /dev/null +++ b/lib/coal @@ -0,0 +1 @@ +Subproject commit 7042bd3cf4b14c419a1d37475462afe2a47e6217 diff --git a/premake4.lua b/premake4.lua index 78953c1..cd6f160 100644 --- a/premake4.lua +++ b/premake4.lua @@ -35,11 +35,8 @@ solution("qor") "GLEW", "assimp", "freeimage", - "openal", - "alut", - "ogg", - "vorbis", - "vorbisfile", + "portaudio", + "sndfile", "boost_system", "boost_filesystem", "boost_coroutine", @@ -103,10 +100,6 @@ solution("qor") "GLEW32", "assimp", "freeimage", - "alut", - "libogg", - "libvorbis", - "libvorbisfile", "boost_system-vc140-mt-1_61", "boost_thread-vc140-mt-1_61", "boost_python-vc140-mt-1_61", @@ -137,7 +130,7 @@ solution("qor") "c:/gtkmm/include/cairomm", "c:/gtkmm/include", "c:/local/boost_1_61_0", - "c:/Program Files (x86)/OpenAL 1.1 SDK/include", + --"c:/Program Files (x86)/OpenAL 1.1 SDK/include", "c:/msvc/include", } configuration { "windows", "Debug" } @@ -146,7 +139,7 @@ solution("qor") } configuration { "windows" } libdirs { - "c:/Program Files (x86)/OpenAL 1.1 SDK/libs/Win32", + --"c:/Program Files (x86)/OpenAL 1.1 SDK/libs/Win32", "c:/msvc/lib32", "c:/gtkmm/lib", "c:/local/boost_1_61_0/lib32-msvc-14.0", @@ -177,7 +170,9 @@ solution("qor") "Qor/**.h", "Qor/**.cpp", "lib/kit/**.h", - "lib/kit/**.cpp" + "lib/kit/**.cpp", + "lib/coal/**.h", + "lib/coal/**.cpp", } -- Exluding Files @@ -186,11 +181,13 @@ solution("qor") "Qor/scripts/**", "Qor/addons/**", "lib/kit/tests/**", + "lib/coal/tests/**", "lib/kit/toys/**" } includedirs { "lib/kit", + "lib/coal", "/usr/local/include/", "/usr/include/bullet/", "/usr/include/raknet/DependentExtensions"