From fb500717f874a8ef2d541d6aefa0c645498a7157 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Wed, 4 Feb 2015 21:08:06 +0100 Subject: [PATCH] [DEV] basic volume group work corectly --- airtio/Interface.cpp | 12 +++++-- airtio/Interface.h | 2 +- airtio/Manager.cpp | 42 ++++++++++------------ airtio/Manager.h | 41 +++++++++------------- airtio/io/Manager.cpp | 81 ++++++++++++------------------------------- airtio/io/Manager.h | 43 +++++++++-------------- airtio/io/Node.cpp | 20 +++++++++++ airtio/io/Node.h | 4 +++ test/main.cpp | 8 ++--- 9 files changed, 111 insertions(+), 142 deletions(-) diff --git a/airtio/Interface.cpp b/airtio/Interface.cpp index f1bcf7f..e64c4ab 100644 --- a/airtio/Interface.cpp +++ b/airtio/Interface.cpp @@ -38,7 +38,8 @@ bool airtio::Interface::init(const std::string& _name, m_format = _format; m_process = std::make_shared(); m_volume = 0.0f; - + // register interface to be notify from the volume change. + m_node->registerAsRemote(shared_from_this()); // Create convertion interface if (m_node->isInput() == true) { // add all time the volume stage : @@ -310,6 +311,11 @@ void airtio::Interface::systemNeedOutputData(std::chrono::system_clock::time_poi m_process->pull(_time, _data, _nbChunk, _chunkSize); } -bool airtio::Interface::systemSetVolume(const std::string& _parameter, const std::string& _value) { - return false; +void airtio::Interface::systemVolumeChange() { + std::unique_lock lockProcess(m_mutex); + std::shared_ptr algo = m_process->get("volume"); + if (algo == nullptr) { + return; + } + algo->volumeChange(); } \ No newline at end of file diff --git a/airtio/Interface.h b/airtio/Interface.h index cfc278e..e449f31 100644 --- a/airtio/Interface.h +++ b/airtio/Interface.h @@ -172,7 +172,7 @@ namespace airtio { private: virtual void systemNewInputData(std::chrono::system_clock::time_point _time, void* _data, size_t _nbChunk); virtual void systemNeedOutputData(std::chrono::system_clock::time_point _time, void* _data, size_t _nbChunk, size_t _chunkSize); - virtual bool systemSetVolume(const std::string& _parameter, const std::string& _value); + virtual void systemVolumeChange(); float m_volume; //!< Local channel Volume }; }; diff --git a/airtio/Manager.cpp b/airtio/Manager.cpp index 010738b..5206581 100644 --- a/airtio/Manager.cpp +++ b/airtio/Manager.cpp @@ -33,35 +33,34 @@ airtio::Manager::~Manager() { std::vector > airtio::Manager::getListStreamInput() { std::vector > output; - output.push_back(std::make_pair("default", "48000 Hz, 16 bits, 2 channels: Default input ")); + //output.push_back(std::make_pair("default", "48000 Hz, 16 bits, 2 channels: Default input ")); return output; } std::vector > airtio::Manager::getListStreamOutput() { std::vector > output; - output.push_back(std::make_pair("default", "48000 Hz, 16 bits, 2 channels: Default output ")); + //output.push_back(std::make_pair("default", "48000 Hz, 16 bits, 2 channels: Default output ")); return output; } -bool airtio::Manager::setParameter(const std::string& _flow, const std::string& _filter, const std::string& _parameter, const std::string& _value) { - return airtio::io::Manager::getInstance()->setParameter(_flow, _filter, _parameter, _value); +bool airtio::Manager::setVolume(const std::string& _volumeName, float _valuedB) { + return airtio::io::Manager::getInstance()->setVolume(_volumeName, _valuedB); } -std::string airtio::Manager::getParameter(const std::string& _flow, const std::string& _filter, const std::string& _parameter) const { - return airtio::io::Manager::getInstance()->getParameter(_flow, _filter, _parameter); +float airtio::Manager::getVolume(const std::string& _volumeName) const { + return airtio::io::Manager::getInstance()->getVolume(_volumeName); } -std::string airtio::Manager::getParameterProperty(const std::string& _flow, const std::string& _filter, const std::string& _parameter) const { - return airtio::io::Manager::getInstance()->getParameterProperty(_flow, _filter, _parameter); +std::pair airtio::Manager::getVolumeRange(const std::string& _volumeName) const { + return airtio::io::Manager::getInstance()->getVolumeRange(_volumeName); } -std::shared_ptr -airtio::Manager::createOutput(float _freq, - const std::vector& _map, - airtalgo::format _format, - const std::string& _streamName, - const std::string& _name) { +std::shared_ptr airtio::Manager::createOutput(float _freq, + const std::vector& _map, + airtalgo::format _format, + const std::string& _streamName, + const std::string& _name) { // get global hardware interface: std::shared_ptr manager = airtio::io::Manager::getInstance(); // get the output or input channel : @@ -71,17 +70,14 @@ airtio::Manager::createOutput(float _freq, interface = airtio::Interface::create(_name, _freq, _map, _format, node); // store it in a list (needed to apply some parameters). m_listOpenInterface.push_back(interface); - // TODO : DEPRECATED ... - //interface->setMasterVolume(m_masterVolume); return interface; } -std::shared_ptr -airtio::Manager::createInput(float _freq, - const std::vector& _map, - airtalgo::format _format, - const std::string& _streamName, - const std::string& _name) { +std::shared_ptr airtio::Manager::createInput(float _freq, + const std::vector& _map, + airtalgo::format _format, + const std::string& _streamName, + const std::string& _name) { // get global hardware interface: std::shared_ptr manager = airtio::io::Manager::getInstance(); // get the output or input channel : @@ -91,7 +87,5 @@ airtio::Manager::createInput(float _freq, interface = airtio::Interface::create(_name, _freq, _map, _format, node); // store it in a list (needed to apply some parameters). m_listOpenInterface.push_back(interface); - // TODO : DEPRECATED ... - //interface->setMasterVolume(m_masterVolume); return interface; } diff --git a/airtio/Manager.h b/airtio/Manager.h index 5269a16..88c7f15 100644 --- a/airtio/Manager.h +++ b/airtio/Manager.h @@ -46,37 +46,28 @@ namespace airtio { virtual std::vector > getListStreamOutput(); /** - * @brief Set a parameter in the stream flow - * @param[in] _flow Low level Flow name (see json config file) - * @param[in] _filter name of the filter (if you added some personels) - * @param[in] _parameter Parameter name. - * @param[in] _value Value to set. + * @brief Set a volume for a specific group + * @param[in] _volumeName Name of the volume (MASTER, MATER_BT ...) + * @param[in] _value Volume in dB to set. * @return true set done * @return false An error occured - * @example : setParameter("", "volume", "MASTER", "-3dB"); - * @example : setParameter("microphone", "LowPassFilter", "cutFrequency", "1000Hz"); + * @example : setVolume("MASTER", -3.0f); */ - virtual bool setParameter(const std::string& _flow, const std::string& _filter, const std::string& _parameter, const std::string& _value); + virtual bool setVolume(const std::string& _volumeName, float _valuedB); + /** + * @brief Get a volume value + * @param[in] _volumeName Name of the volume (MASTER, MATER_BT ...) + * @return The Volume value in dB. + * @example ret = getVolume("MASTER"); can return something like ret = -3.0f + */ + virtual float getVolume(const std::string& _volumeName) const; /** * @brief Get a parameter value - * @param[in] _flow Low level Flow name (see json config file) - * @param[in] _filter name of the filter (if you added some personels) - * @param[in] _parameter Parameter name. - * @return The requested value. - * @example : getParameter("", "volume", "MASTER"); can return something like "-3dB" - * @example : getParameter("microphone", "LowPassFilter", "cutFrequency"); can return something like "[-120..0]dB" + * @param[in] _volumeName Name of the volume (MASTER, MATER_BT ...) + * @return The requested value Range. + * @example ret = getVolumeRange("MASTER"); can return something like ret=(-120.0f,0.0f) */ - virtual std::string getParameter(const std::string& _flow, const std::string& _filter, const std::string& _parameter) const; - /** - * @brief Get a parameter value - * @param[in] _flow Low level Flow name (see json config file) - * @param[in] _filter name of the filter (if you added some personels) - * @param[in] _parameter Parameter name. - * @return The requested value. - * @example : getParameter("", "volume", "MASTER"); can return something like "[-120..0]dB" - * @example : getParameter("microphone", "LowPassFilter", "cutFreqiency"); can return something like "]100..10000]Hz" - */ - virtual std::string getParameterProperty(const std::string& _flow, const std::string& _filter, const std::string& _parameter) const; + virtual std::pair getVolumeRange(const std::string& _volumeName) const; /** * @brief Create output Interface diff --git a/airtio/io/Manager.cpp b/airtio/io/Manager.cpp index f4a8cba..0a61420 100644 --- a/airtio/io/Manager.cpp +++ b/airtio/io/Manager.cpp @@ -53,7 +53,7 @@ std::shared_ptr airtio::io::Manager::getVolumeGroup(con continue; } if (it->getName() == _name) { - return nullptr; + return it; } } AIRTIO_DEBUG("Add a new volume group : '" << _name << "'"); @@ -62,69 +62,32 @@ std::shared_ptr airtio::io::Manager::getVolumeGroup(con return tmpVolume; } - -bool airtio::io::Manager::setParameter(const std::string& _flow, const std::string& _filter, const std::string& _parameter, const std::string& _value) { - AIRTIO_DEBUG("setParameter [BEGIN] : '" << _flow << "':'" << _filter << "':'" << _parameter << "':'" << _value << "'"); - bool out = false; - if ( _flow == "" - && _filter == "volume") { - // set IO volume - float value = 0; - if (sscanf(_value.c_str(), "%fdB", &value) != 1) { - AIRTIO_ERROR("Can not parse the value of audio volume : '" << _value << "'"); - return false; - } - for (auto &it : m_volumeGroup) { - if (it == nullptr) { - continue; - } - if (it->getName() == _parameter) { - it->setVolume(value); - for (auto &it2 : m_list) { - std::shared_ptr val = it2.lock(); - if (val != nullptr) { - // TODO : notify nodes ... - // val-> - } - } - return true; - } +bool airtio::io::Manager::setVolume(const std::string& _volumeName, float _valuedB) { + std::shared_ptr volume = getVolumeGroup(_volumeName); + if (volume == nullptr) { + AIRTIO_ERROR("Can not get volume ... : '" << _volumeName << "'"); + return false; + } + // TODO : Check range ... + volume->setVolume(_valuedB); + for (auto &it2 : m_list) { + std::shared_ptr val = it2.lock(); + if (val != nullptr) { + val->volumeChange(); } } - AIRTIO_TODO(" IMPLEMENT"); - AIRTIO_DEBUG("setParameter [ END ] : '" << out << "'"); - return out; + return true; } -std::string airtio::io::Manager::getParameter(const std::string& _flow, const std::string& _filter, const std::string& _parameter) const { - AIRTIO_DEBUG("getParameter [BEGIN] : '" << _flow << "':'" << _filter << "':'" << _parameter << "'"); - std::string out; - if ( _flow == "" - && _filter == "volume") { - // get IO volume - for (auto &it : m_volumeGroup) { - if (it == nullptr) { - continue; - } - if (it->getName() == _parameter) { - return std::to_string(it->getVolume()) + "dB";; - } - } +float airtio::io::Manager::getVolume(const std::string& _volumeName) { + std::shared_ptr volume = getVolumeGroup(_volumeName); + if (volume == nullptr) { + AIRTIO_ERROR("Can not get volume ... : '" << _volumeName << "'"); + return 0.0f; } - AIRTIO_TODO(" IMPLEMENT"); - AIRTIO_DEBUG("getParameter [ END ] : '" << out << "'"); - return out; + return volume->getVolume(); } -std::string airtio::io::Manager::getParameterProperty(const std::string& _flow, const std::string& _filter, const std::string& _parameter) const { - AIRTIO_DEBUG("getParameterProperty [BEGIN] : '" << _flow << "':'" << _filter << "':'" << _parameter << "'"); - std::string out; - if ( _flow == "" - && _filter == "volume") { - // return generic volumes - return "[-300..300]dB"; - } - AIRTIO_TODO(" IMPLEMENT"); - AIRTIO_DEBUG("getParameterProperty [ END ] : '" << out << "'"); - return out; +std::pair airtio::io::Manager::getVolumeRange(const std::string& _volumeName) const { + return std::make_pair(-300, 300); } diff --git a/airtio/io/Manager.h b/airtio/io/Manager.h index abff2f3..a3d1e94 100644 --- a/airtio/io/Manager.h +++ b/airtio/io/Manager.h @@ -44,39 +44,30 @@ namespace airtio { std::vector> m_volumeGroup; public: std::shared_ptr getVolumeGroup(const std::string& _name); - + /** - * @brief Set a parameter in the stream flow - * @param[in] _flow Low level Flow name (see json config file) - * @param[in] _filter name of the filter (if you added some personels) - * @param[in] _parameter Parameter name. - * @param[in] _value Value to set. + * @brief Set a volume for a specific group + * @param[in] _volumeName Name of the volume (MASTER, MATER_BT ...) + * @param[in] _value Volume in dB to set. * @return true set done * @return false An error occured - * @example : setParameter("", "volume", "MASTER", "-3dB"); - * @example : setParameter("microphone", "LowPassFilter", "cutFrequency", "1000Hz"); + * @example : setVolume("MASTER", -3.0f); */ - virtual bool setParameter(const std::string& _flow, const std::string& _filter, const std::string& _parameter, const std::string& _value); + virtual bool setVolume(const std::string& _volumeName, float _valuedB); + /** + * @brief Get a volume value + * @param[in] _volumeName Name of the volume (MASTER, MATER_BT ...) + * @return The Volume value in dB. + * @example ret = getVolume("MASTER"); can return something like ret = -3.0f + */ + virtual float getVolume(const std::string& _volumeName); /** * @brief Get a parameter value - * @param[in] _flow Low level Flow name (see json config file) - * @param[in] _filter name of the filter (if you added some personels) - * @param[in] _parameter Parameter name. - * @return The requested value. - * @example : getParameter("", "volume", "MASTER"); can return something like "-3dB" - * @example : getParameter("microphone", "LowPassFilter", "cutFrequency"); can return something like "[-120..0]dB" + * @param[in] _volumeName Name of the volume (MASTER, MATER_BT ...) + * @return The requested value Range. + * @example ret = getVolumeRange("MASTER"); can return something like ret=(-120.0f,0.0f) */ - virtual std::string getParameter(const std::string& _flow, const std::string& _filter, const std::string& _parameter) const; - /** - * @brief Get a parameter value - * @param[in] _flow Low level Flow name (see json config file) - * @param[in] _filter name of the filter (if you added some personels) - * @param[in] _parameter Parameter name. - * @return The requested value. - * @example : getParameter("", "volume", "MASTER"); can return something like "[-120..0]dB" - * @example : getParameter("microphone", "LowPassFilter", "cutFreqiency"); can return something like "]100..10000]Hz" - */ - virtual std::string getParameterProperty(const std::string& _flow, const std::string& _filter, const std::string& _parameter) const; + virtual std::pair getVolumeRange(const std::string& _volumeName) const; }; } } diff --git a/airtio/io/Node.cpp b/airtio/io/Node.cpp index 789430f..322f86a 100644 --- a/airtio/io/Node.cpp +++ b/airtio/io/Node.cpp @@ -311,6 +311,17 @@ void airtio::io::Node::stop() { } } +void airtio::io::Node::registerAsRemote(const std::shared_ptr& _interface) { + auto it = m_listAvaillable.begin(); + while (it != m_listAvaillable.end()) { + if (it->expired() == true) { + it = m_listAvaillable.erase(it); + } + ++it; + } + m_listAvaillable.push_back(_interface); +} + void airtio::io::Node::interfaceAdd(const std::shared_ptr& _interface) { { std::unique_lock lock(m_mutex); @@ -344,3 +355,12 @@ void airtio::io::Node::interfaceRemove(const std::shared_ptr& return; } + +void airtio::io::Node::volumeChange() { + for (auto &it : m_listAvaillable) { + std::shared_ptr node = it.lock(); + if (node != nullptr) { + node->systemVolumeChange(); + } + } +} diff --git a/airtio/io/Node.h b/airtio/io/Node.h index 0c84867..7bf5b61 100644 --- a/airtio/io/Node.h +++ b/airtio/io/Node.h @@ -42,8 +42,10 @@ namespace airtio { */ virtual ~Node(); private: + std::vector > m_listAvaillable; //!< List of all interface that exist on this Node std::vector > m_list; public: + void registerAsRemote(const std::shared_ptr& _interface); void interfaceAdd(const std::shared_ptr& _interface); void interfaceRemove(const std::shared_ptr& _interface); private: @@ -86,6 +88,8 @@ namespace airtio { const std::shared_ptr& getVolume() { return m_volume; } + public: + void volumeChange(); }; } } diff --git a/test/main.cpp b/test/main.cpp index 3641c10..5a58a8a 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -557,11 +557,11 @@ class testCallbackVolume { APPL_INFO(" get volume : " << m_interface->getParameter("volume", "FLOW") ); usleep(500000); */ - m_manager->setParameter("", "volume", "MASTER", "-3dB"); - APPL_INFO("get volume MASTER: " << m_manager->getParameter("", "volume", "MASTER") ); + m_manager->setVolume("MASTER", -3.0f); + APPL_INFO("get volume MASTER: " << m_manager->getVolume("MASTER") ); usleep(500000); - m_manager->setParameter("", "volume", "MEDIA", "-3dB"); - APPL_INFO("get volume MEDIA: " << m_manager->getParameter("", "volume", "MEDIA") ); + m_manager->setVolume("MEDIA", -3.0f); + APPL_INFO("get volume MEDIA: " << m_manager->getVolume("MEDIA") ); usleep(500000); /* m_interface->setParameter("volume", "FLOW", "3dB");