/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #pragma once #if defined(__APPLE__) && !__has_include() #include #include #else #include #include #include #endif namespace audio { ALuint const null_resource{ ~ALuint{0} }; // wrapper for audio sample struct openal_buffer { // members ALuint id { null_resource }; // associated AL resource unsigned int rate {}; // sample rate of the data std::string name; std::string caption; // constructors openal_buffer() = default; explicit openal_buffer( std::string const &Filename ); // methods // retrieves sound caption in currently set language void fetch_caption(); }; using buffer_handle = std::size_t; // class buffer_manager { public: // constructors buffer_manager(); // makes empty bindings for null buffer // destructor ~buffer_manager(); // methods // creates buffer object out of data stored in specified file. returns: handle to the buffer or null_handle if creation failed buffer_handle create( std::string const &Filename ); // provides direct access to a specified buffer. a buffer still decoded in the background is completed first audio::openal_buffer const & buffer( audio::buffer_handle const Buffer ) const; // creates AL buffers for files decoded in the background so far, or for all of them if requested void update( bool const Wait = false ); private: // types using buffer_sequence = std::vector; using index_map = std::unordered_map; struct decoder; // background decoding of sound files, used with sound.asyncload // methods // places in the bank a buffer containing data stored in specified file. returns: handle to the buffer buffer_handle emplace( std::string Filename ); // checks whether specified buffer is in the buffer bank. returns: buffer handle, or null_handle. buffer_handle find_buffer( std::string const &Buffername ) const; // checks whether specified file exists. returns: name of the located file, or empty string. std::string find_file( std::string const &Filename ) const; // creates AL buffer for a file decoded in the background, waiting for the decoder if needed void complete( audio::buffer_handle const Buffer ) const; // members mutable buffer_sequence m_buffers; // buffers decoded in the background are completed on first access index_map m_buffermappings; mutable std::unique_ptr m_decoder; }; } // audio //---------------------------------------------------------------------------