Skip to content

Instantly share code, notes, and snippets.

@ffAudio
Created January 2, 2021 23:31
Show Gist options
  • Select an option

  • Save ffAudio/a51e5f660f362035566e09fb4e629446 to your computer and use it in GitHub Desktop.

Select an option

Save ffAudio/a51e5f660f362035566e09fb4e629446 to your computer and use it in GitHub Desktop.
JUCE Component to display a score using lenmus/lomse
/**
\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)
};
@ayyb1988
Copy link

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:

 class MainWindow    : public juce::DocumentWindow
    {
    public:
        LomseComponent *pComponent;
        lomse::LomseDoorway lomse;
        lomse::Presenter *pPresenter;

        MainWindow (juce::String name)
            : DocumentWindow (name,
                              juce::Desktop::getInstance().getDefaultLookAndFeel()
                                                          .findColour (juce::ResizableWindow::backgroundColourId),
                              DocumentWindow::allButtons)
        {
            setUsingNativeTitleBar (true);
//            setContentOwned (new MainComponent(), true);
            pComponent = new LomseComponent();
            pComponent->setSize(360,720);
            setContentOwned (pComponent, true);
            //initialize the Lomse library
            lomse.init_library (lomse::k_pix_format_rgba32, 96, false);

            pPresenter = lomse.new_document(lomse::k_view_vertical_book,
                                                              "(lenmusdoc (vers 0.0)"
                                                              "(content "
                                                              "(para (txt \"Hello world!\"))"
                                                              "(score (vers 1.6) "
                                                              "(instrument (musicData (clef G)(key C)(time 2 4)(n c4 q) )))"
                                                              ")"
                                                              ")",
                                                              lomse::Document::k_format_ldp);
            pComponent->setPresenter(std::unique_ptr<lomse::Presenter>(pPresenter));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment