From e40161675dc8e26cff1123ea6b680c2e76f17faf Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 19 Jun 2018 16:12:31 +0900 Subject: [PATCH] Fix bug involving channels of sink/source Previously, sink/source data was improperly handled (number of channels was not updated based on the number of channels that the sink/source had). --- CMakeLists.txt | 2 +- Changelog.md | 4 +++ src/MfPA/Meter.cpp | 80 ++++++++++++++++++++++++---------------------- src/MfPA/Meter.hpp | 1 + 4 files changed, 47 insertions(+), 40 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9575e54..17a096a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.0) -project(MeterForPulseAudio VERSION 1.3) +project(MeterForPulseAudio VERSION 1.4) set(MeterForPulseAudio_SOURCES GameDevTools/src/GDT/GameLoop.cpp diff --git a/Changelog.md b/Changelog.md index d99e78a..983de32 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,7 @@ +# Version 1.4 + +Fix bug involving channel size. + # Version 1.3 Added feature to print available sinks/sources (invoked via arguments to diff --git a/src/MfPA/Meter.cpp b/src/MfPA/Meter.cpp index 7933942..7f37f6a 100644 --- a/src/MfPA/Meter.cpp +++ b/src/MfPA/Meter.cpp @@ -24,6 +24,7 @@ context(nullptr), stream(nullptr), runFlag(true), channels(1), +channelsChanged(true), window(sf::VideoMode(100,400), "Meter for PulseAudio"), barColor(barColor) { @@ -242,6 +243,7 @@ void MfPA::Meter::get_source_info_callback( } meter->channels = i->sample_spec.channels; + meter->channelsChanged = true; pa_sample_spec sampleSpec; sampleSpec.format = PA_SAMPLE_FLOAT32LE; sampleSpec.rate = i->sample_spec.rate; @@ -331,19 +333,8 @@ void MfPA::Meter::get_stream_data_callback( void MfPA::Meter::startMainLoop() { - // initialize levels - levels.resize(channels); - prevLevels.resize(channels); - levelsChanged.resize(channels); - for(unsigned int i = 0; i < channels; ++i) - { - levels[i] = 0.0f; - prevLevels[i] = std::make_tuple(0.0f, 0.0f); - levelsChanged[i] = false; - } - #ifndef NDEBUG - levelsPrintTimer = 1.0f; + levelsPrintTimer = 0.0f; #endif GDT::IntervalBasedGameLoop( @@ -379,6 +370,14 @@ void MfPA::Meter::update(float dt) } } + if(channelsChanged) + { + levels.resize(channels, 0.0f); + prevLevels.resize(channels, std::make_tuple(0.0f, 0.0f)); + levelsChanged.resize(channels, false); + channelsChanged = false; + } + for(unsigned int i = 0; i < channels; ++i) { levelsChanged[i] = false; @@ -387,11 +386,11 @@ void MfPA::Meter::update(float dt) while(!sampleQueue.empty()) { { - unsigned char currentChannel = 0; const auto& front = sampleQueue.front(); - for(auto iter = front.begin(); iter != front.end(); ++iter) + for(unsigned int i = 0; i < front.size(); ++i) { - float fabs = std::abs(*iter); + float fabs = std::abs(front[i]); + unsigned char currentChannel = i % channels; if(levels[currentChannel] < fabs) { levels[currentChannel] = fabs; @@ -402,7 +401,6 @@ void MfPA::Meter::update(float dt) std::get<0>(prevLevels[currentChannel]) = fabs; std::get<1>(prevLevels[currentChannel]) = 1.0f; } - currentChannel = (currentChannel + 1) % channels; } } sampleQueue.pop(); @@ -435,7 +433,8 @@ void MfPA::Meter::update(float dt) { for(unsigned int i = 0; i < channels; ++i) { - std::cout << "[" << i << "] " << levels[i] << " "; + std::cout << "[" << i << "] " << levels[i] << "_" + << std::get<0>(prevLevels[i]) << " "; } std::cout << std::endl; levelsPrintTimer = 1.0f; @@ -447,28 +446,31 @@ void MfPA::Meter::draw() { window.clear(); - for(unsigned int i = 0; i < channels; ++i) - { - // prev levels - barColor.a = 255 * std::get<1>(prevLevels[i]); - bar.setFillColor(barColor); - bar.setSize(sf::Vector2f( - 1.0f / (float)channels, - std::get<0>(prevLevels[i]))); - bar.setPosition(sf::Vector2f( - (float)i * 1.0f / (float)channels, - 1.0f - std::get<0>(prevLevels[i]))); - window.draw(bar); - // levels - barColor.a = 255; - bar.setFillColor(barColor); - bar.setSize(sf::Vector2f( - 1.0f / (float)channels, - levels[i])); - bar.setPosition(sf::Vector2f( - (float)i * 1.0f / (float)channels, - 1.0f - levels[i])); - window.draw(bar); + if(!channelsChanged) + { // don't draw until containers have been resized to channel amount + for(unsigned int i = 0; i < channels; ++i) + { + // prev levels + barColor.a = 255 * std::get<1>(prevLevels[i]); + bar.setFillColor(barColor); + bar.setSize(sf::Vector2f( + 1.0f / (float)channels, + std::get<0>(prevLevels[i]))); + bar.setPosition(sf::Vector2f( + (float)i * 1.0f / (float)channels, + 1.0f - std::get<0>(prevLevels[i]))); + window.draw(bar); + // levels + barColor.a = 255; + bar.setFillColor(barColor); + bar.setSize(sf::Vector2f( + 1.0f / (float)channels, + levels[i])); + bar.setPosition(sf::Vector2f( + (float)i * 1.0f / (float)channels, + 1.0f - levels[i])); + window.draw(bar); + } } window.display(); diff --git a/src/MfPA/Meter.hpp b/src/MfPA/Meter.hpp index 4a07a7f..28b3067 100644 --- a/src/MfPA/Meter.hpp +++ b/src/MfPA/Meter.hpp @@ -82,6 +82,7 @@ private: bool runFlag; unsigned char channels; + bool channelsChanged; std::vector levels; std::vector> prevLevels; std::vector levelsChanged; -- 2.49.0