Implement text with approximated BPM value during calculation

This commit is contained in:
NaiJi ✨ 2021-09-01 22:04:18 +03:00
parent 155ec1fca0
commit 55b62e62bb
6 changed files with 58 additions and 8 deletions

View File

@ -9,6 +9,7 @@ class BPMCalculator
public:
explicit BPMCalculator(const std::shared_ptr<Music>& music);
void setMusic(const std::shared_ptr<Music>& music);
std::shared_ptr<Music> music() const;
void start();

View File

@ -33,11 +33,16 @@ void Editor::draw() const
void Editor::enter()
{
auto& group = _group;
auto& music = _music;
_music->openFromFile("40mp.ogg");
_music->setVolume(5);
_bpm_calculator = std::make_shared<BPMCalculator>(_music);
std::shared_ptr<BPMCalculatorWidget> bpm_widget = std::make_shared<BPMCalculatorWidget>(_bpm_calculator, _font);
const auto bpm_widget_callback = [&group, bpm_widget=bpm_widget]()
bpm_widget->init();
const auto bpm_widget_callback = [&group, bpm_widget=bpm_widget, &music]()
{
music->stop();
bpm_widget->setVisibility(false);
group->unblock();
};
@ -91,9 +96,6 @@ void Editor::enter()
test_menu_2->addCascadeButton(test_cascade_button_2);
test_cascade_button_2->resetRect();
_music->openFromFile("Uta-test.flac");
_music->setVolume(5);
menu_bar->setVisibility(true);
_group = std::make_shared<Group>();

View File

@ -6,6 +6,10 @@ BPMCalculatorWidget::BPMCalculatorWidget(const std::shared_ptr<BPMCalculator>& b
_bpm_calculator(bpm_calculator),
_slider(std::make_shared<BPMSlider>())
{
_bpm_value.setFont(*_font);
_bpm_value.setCharacterSize(40);
_bpm_value.setFillColor(sf::Color::Black);
_bpm_value.setString("--");
}
void BPMCalculatorWidget::input(const sf::Event& event)
@ -29,6 +33,12 @@ void BPMCalculatorWidget::input(const sf::Event& event)
void BPMCalculatorWidget::update(const sf::Time& dt)
{
Window::update(dt);
const auto approximation = _bpm_calculator->getCurrentApproximation();
if (approximation != 0)
{
_bpm_value.setString(std::to_string(approximation));
}
}
void BPMCalculatorWidget::draw(sf::RenderTarget& target, sf::RenderStates states) const
@ -36,7 +46,11 @@ void BPMCalculatorWidget::draw(sf::RenderTarget& target, sf::RenderStates states
Window::draw(target, states);
if (_is_visible)
{
_slider->draw(target, states);
_button_start->draw(target, states);
target.draw(_bpm_value, states);
}
}
void BPMCalculatorWidget::setRect(const sf::FloatRect& rect)
@ -45,16 +59,38 @@ void BPMCalculatorWidget::setRect(const sf::FloatRect& rect)
_slider->setRect(sf::FloatRect{0, 0, rect.width / 8 * 6, 100});
_slider->setPosition({_window_content.getGlobalBounds().left + rect.width / 8,
_window_content.getGlobalBounds().top + rect.height / 8 * 3});
_button_start->setRect(sf::FloatRect{0, 0, rect.width / 10 * 3, 30});
_button_start->setPosition({_window_content.getGlobalBounds().left + rect.width / 7,
_window_content.getGlobalBounds().top + _window_content.getGlobalBounds().height - 40});
_bpm_value.setPosition({_window_content.getGlobalBounds().left + rect.width / 8,
_window_content.getGlobalBounds().top + rect.height / 8 });
}
void BPMCalculatorWidget::move(const sf::Vector2f &delta)
{
Window::move(delta);
_slider->move(delta);
_bpm_value.move(delta);
}
void BPMCalculatorWidget::setPosition(const sf::Vector2f &position)
{
Window::setPosition(position);
_slider->setPosition(position);
}
void BPMCalculatorWidget::init()
{
auto& bpm_calculator = _bpm_calculator;
_button_start = std::make_shared<PushButton>("Start", _font);
_button_start->setCallback([bpm_calculator, button_start=_button_start]()
{
bpm_calculator->music()->play();
bpm_calculator->start();
button_start->setVisibility(false);
});
addChild(_button_start);
}

View File

@ -21,8 +21,13 @@ public:
virtual void setPosition(const sf::Vector2f& position) override;
virtual void move(const sf::Vector2f& delta) override;
void init();
private:
std::shared_ptr<PushButton> _button_start;
std::shared_ptr<BPMCalculator> _bpm_calculator;
std::shared_ptr<BPMSlider> _slider;
sf::Text _bpm_value;
};

View File

@ -35,6 +35,7 @@ void Button::setRect(const sf::FloatRect& rect)
void Button::setPosition(const sf::Vector2f &position)
{
_button_content.setPosition(position);
_button_text.move(position.x + 5, position.y + 5);
}
void Button::move(const sf::Vector2f &delta)

View File

@ -17,10 +17,15 @@ void BPMCalculator::setMusic(const std::shared_ptr<Music>& music)
_music = music;
}
std::shared_ptr<Music> BPMCalculator::music() const
{
return _music;
}
void BPMCalculator::start()
{
_deltas.clear();
_previous_click_offset = _music->fetchOffset();
_previous_click_offset = 0;
}
void BPMCalculator::click()
@ -29,7 +34,7 @@ void BPMCalculator::click()
std::cout << click_offset << "\n\n\n\n";
if (_deltas.empty())
if (_previous_click_offset == 0)
{
_previous_click_offset = click_offset;
return;
@ -48,7 +53,7 @@ int BPMCalculator::getCurrentApproximation() const
std::cout << "S: " << sum << " _deltas.size(): " << _deltas.size();
std::cout << "\n " << (static_cast<float>(sum) / static_cast<float>(_deltas.size())) << '\n';
return (sum == 0)
return (sum == 0 || _deltas.size() < 8)
? 0.
: static_cast<int>(static_cast<float>(MICROSECONDS_IN_MINUTE) / (static_cast<float>(sum) / static_cast<float>(_deltas.size())));
}