Created
January 2, 2021 23:31
-
-
Save ffAudio/a51e5f660f362035566e09fb4e629446 to your computer and use it in GitHub Desktop.
JUCE Component to display a score using lenmus/lomse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| \file LomseComponent.h | |
| \author Daniel Walz - Foleys Finest Audio (ffAudio) | |
| This uses JUCE www.juce.com | |
| and lenmus https://github.com/lenmus/lomse | |
| */ | |
| #pragma once | |
| //lomse headers | |
| #include <lomse_doorway.h> | |
| #include <lomse_presenter.h> | |
| #include <lomse_interactor.h> | |
| #include <lomse_graphic_view.h> | |
| #include <lomse_document.h> | |
| #include <lomse_events.h> | |
| /** | |
| A Component to display a score using the lomse library. | |
| */ | |
| class LomseComponent : public juce::Component | |
| { | |
| public: | |
| /** | |
| A Component to display a score using the lomse library. | |
| To use it create a doorway and use it to create a presenter | |
| \code{.cpp} | |
| // add a member | |
| lomse::LomseDoorway lomse; | |
| //initialize the Lomse library | |
| lomse.init_library (lomse::k_pix_format_rgba32, 96, false); | |
| \endcode | |
| */ | |
| LomseComponent() = default; | |
| /** | |
| Set a presenter to be displayed | |
| \param[in] newPresenter a presenter object to be used | |
| */ | |
| void setPresenter (std::unique_ptr<lomse::Presenter> newPresenter) | |
| { | |
| presenter = std::move (newPresenter); | |
| if (presenter.get() == nullptr) | |
| return; | |
| if (auto interactor = presenter->get_interactor(0).lock()) | |
| { | |
| interactor->add_event_handler (lomse::k_update_window_event, this, wrapper_update_window); | |
| } | |
| } | |
| /** | |
| Grant access to the lomse::Presenter | |
| */ | |
| lomse::Presenter* getPresenter() | |
| { | |
| return presenter.get(); | |
| } | |
| /** | |
| Draw the score | |
| */ | |
| void paint (juce::Graphics& g) override | |
| { | |
| if (auto interactor = (presenter.get() != nullptr) ? presenter->get_interactor(0).lock() : nullptr) | |
| { | |
| //connect the View with the window buffer | |
| juce::Image::BitmapData data (image, 0, 0, image.getWidth(), image.getHeight(), juce::Image::BitmapData::writeOnly); | |
| buffer.attach (data.data, static_cast<unsigned int> (image.getWidth()), static_cast<unsigned int> (image.getHeight()), 4 * image.getWidth()); | |
| interactor->set_rendering_buffer (&buffer); | |
| interactor->redraw_bitmap(); | |
| g.drawImageAt (image, 0, 0); | |
| } | |
| } | |
| /** | |
| Called when the component is resized to recreate the backing image | |
| */ | |
| void resized() override | |
| { | |
| image = juce::Image (juce::Image::ARGB, getWidth(), getHeight(), false); | |
| } | |
| /** | |
| Callback from lomse when the score needs to be drawn | |
| */ | |
| static void wrapper_update_window (void* pThis, lomse::SpEventInfo pEvent) | |
| { | |
| juce::ignoreUnused (pEvent); | |
| static_cast<LomseComponent*>(pThis)->repaint(); | |
| } | |
| private: | |
| juce::Image image; | |
| lomse::RenderingBuffer buffer; | |
| std::unique_ptr<lomse::Presenter> presenter; | |
| JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LomseComponent) | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the sample code you provided, but I don't see the effect when I run it, can you guide me?
My use is as follows: