Skip to content

Commit

Permalink
init fluidsynth on app launch and play test note
Browse files Browse the repository at this point in the history
  • Loading branch information
pestophagous committed Apr 6, 2020
1 parent 9fd8c65 commit b99677c
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 4 deletions.
1 change: 1 addition & 0 deletions build/src/app/libfluidsynth.so
1 change: 1 addition & 0 deletions build/src/app/libfluidsynth.so.2
3 changes: 2 additions & 1 deletion build_app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ fi
mkdir -p build/fsynth
pushd build/fsynth >& /dev/null
cmake --enable-debug -DCMAKE_BUILD_TYPE=Debug ../../fluidsynth/
make VERBOSE=1
make VERBOSE=1 DESTDIR=./gcc_64
make VERBOSE=1 DESTDIR=./gcc_64 install
popd >& /dev/null

source $DIR/path_to_qmake.bash
Expand Down
1 change: 1 addition & 0 deletions run_all_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ then
env

./tools/ci/provision.sh
./tools/ci/prep-dummy-soundcard.sh
git submodule update --init # avoid '--recursive' (as long as we can) due to inner qmlfmt deps

XDISPLAY=":1"
Expand Down
1 change: 1 addition & 0 deletions src/app/app.pro
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ HEADERS += \
view_model_collection.h

# 'pri' usage based on http://archive.is/https://www.toptal.com/qt/vital-guide-qmake
!include(../lib/fluidsynth_linkonly.pri) { error() }
!include(../lib/lib.pri) { error() }
!include(../libstyles/libstyles.pri) { error() }
14 changes: 11 additions & 3 deletions src/app/view_model_collection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,37 @@
#include "gui_tests.h"
#include "qml_message_interceptor.h"
#include "src/lib/cli_options.h"
#include "src/lib/fsynth.h"
#include "src/lib/logging_tags.h"
#include "src/lib/music_notes.h"
#include "src/lib/resource_helper.h"
#include "src/lib/resources.h"

namespace heory
{
// clang-format off
ViewModelCollection::ViewModelCollection( const QGuiApplication& app )
: m_opts( std::make_unique<CliOptions>( app ) ), m_qmlLogger( std::make_unique<QmlMessageInterceptor>() ), m_logging( std::make_unique<LoggingTags>( *m_opts ) )
: m_opts( std::make_unique<CliOptions>( app ) ),
m_qmlLogger( std::make_unique<QmlMessageInterceptor>() ),
m_logging( std::make_unique<LoggingTags>( *m_opts ) ),
m_fsynth( std::make_unique<FsynthWrapper>( *m_opts ) )
// clang-format on
{
heory::initLibResources();

// Do after the 'init..resource' calls, in case any ctor wants rsrcs:
// m_navigation = std::make_unique<Navigation>();
m_musicNotes = std::make_unique<MusicNotes>( m_fsynth.get() );
}

ViewModelCollection::~ViewModelCollection() = default;

void ViewModelCollection::ExportContextPropertiesToQml( QQmlApplicationEngine* engine )
{
// m_navigation->ExportContextPropertiesToQml( engine );
m_logging->ExportContextPropertiesToQml( engine );
ResourceHelper::ExportContextPropertiesToQml( engine );

m_musicNotes->ExportContextPropertiesToQml( engine );

// Keep this at the END of the 'ExportContext...' method, so all view models are exported before any tests run
if( m_opts->RunningGuiTests() )
{
Expand Down
5 changes: 5 additions & 0 deletions src/app/view_model_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
namespace heory
{
class CliOptions;
class FsynthWrapper;
class GuiTests;
class LoggingTags;
class MusicNotes;
class QmlMessageInterceptor;

class ViewModelCollection
Expand All @@ -35,6 +37,9 @@ class ViewModelCollection
const std::unique_ptr<const CliOptions> m_opts;
std::unique_ptr<QmlMessageInterceptor> m_qmlLogger;
std::unique_ptr<LoggingTags> m_logging;
std::unique_ptr<FsynthWrapper> m_fsynth;

std::unique_ptr<MusicNotes> m_musicNotes;

std::unique_ptr<GuiTests> m_guiTests;
};
Expand Down
4 changes: 4 additions & 0 deletions src/lib/fluidsynth.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

QMAKE_CXXFLAGS += -isystem $${top_builddir}/fsynth/gcc_64/usr/local/include

!include(fluidsynth_linkonly.pri) { error() }
7 changes: 7 additions & 0 deletions src/lib/fluidsynth_linkonly.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

# This 'pri' file can be used by the executable, which needs to link against
# fluidsynth but NOT be able to 'see' the header files.
#
# In other words, here we put libfluidsynth on linker path but >NOT< include path!

LIBS += -L$${top_builddir}/fsynth/gcc_64/usr/local/lib64 -lfluidsynth
94 changes: 94 additions & 0 deletions src/lib/fsynth.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "fsynth.h"

#include <QDebug>

#include <fluidsynth.h>
#include <fluidsynth/version.h>

#include <stdlib.h>

#include "util-assert.h"

namespace heory
{
namespace
{
bool IsRunningOnGithubRuner()
{
if( getenv( "GITHUB_ACTIONS" ) )
{
return true;
}
return false;
}
} // namespace

struct FsynthWrapper::Impl
{
Impl()
: test_lacking_alsa( IsRunningOnGithubRuner() )
{
}

const bool test_lacking_alsa;
fluid_settings_t* settings = nullptr;
fluid_synth_t* synth = nullptr;
fluid_midi_router_t* router = nullptr;
fluid_midi_driver_t* mdriver = nullptr;
fluid_audio_driver_t* adriver = nullptr;
};

FsynthWrapper::FsynthWrapper( const CliOptions& /*options*/ )
: m_i( new Impl )
{
m_i->settings = new_fluid_settings();
FASSERT( m_i->settings, "must be non-null" );

fluid_settings_setstr( m_i->settings, "audio.driver", "pulseaudio" );
fluid_settings_setstr( m_i->settings, "midi.driver", "alsa_seq" );
fluid_settings_setint( m_i->settings, "midi.autoconnect", 1 );
fluid_settings_setnum( m_i->settings, "synth.gain", 1 );

m_i->synth = new_fluid_synth( m_i->settings );
FASSERT( m_i->synth, "must be non-null" );

// dpkg-query -L fluid-soundfont-gm # find where 'sf2' sound fonts were installed
const int sf = fluid_synth_sfload( m_i->synth, "/usr/share/sounds/sf2/FluidR3_GM.sf2", /*reset_presets*/ 1 );
FASSERT( sf != FLUID_FAILED, "failed call to fluid_synth_sfload" );
qDebug() << "fluid_synth_sfload returned sf id:" << sf;

if( !m_i->test_lacking_alsa )
{
// fluidsynth: debug: Using 'alsa_seq' midi driver
// ALSA lib seq_hw.c:466:(snd_seq_hw_open) open /dev/snd/seq failed: No such file or directory
// fluidsynth: error: Error opening ALSA sequencer

This comment has been minimized.

Copy link
@pestophagous

pestophagous Apr 6, 2020

Author Owner

TODO. Figure out how to get these lines to succeed in github ci.


m_i->router = new_fluid_midi_router(
m_i->settings,
fluid_midi_dump_postrouter,
static_cast<void*>( m_i->synth ) );
FASSERT( m_i->router, "must be non-null" );

// In dump mode, text output is generated for events going into and out of
// the router. The example dump functions are put into the chain before and
// after the router.
m_i->mdriver = new_fluid_midi_driver(
m_i->settings,
fluid_midi_dump_prerouter,
static_cast<void*>( m_i->router ) );
FASSERT( m_i->mdriver, "must be non-null" );

m_i->adriver = new_fluid_audio_driver( m_i->settings, m_i->synth );
FASSERT( m_i->adriver, "must be non-null" );

const int note = fluid_synth_noteon( m_i->synth, 0 /*chan*/, 60 /*key*/, 100 /*velocity*/ );
FASSERT( note == FLUID_OK, "failed in fluid_synth_noteon" );
}
}

FsynthWrapper::~FsynthWrapper()
{
delete m_i;
}

} // namespace heory
30 changes: 30 additions & 0 deletions src/lib/fsynth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Copyright (c) 2020, pestophagous (pestophagous@users.noreply.github.com)
// See LICENSE.txt
//
// https://github.com/pestophagous/
//
#ifndef PROJECT_LIB_FSYNTH_H
#define PROJECT_LIB_FSYNTH_H

namespace heory
{
class CliOptions;

class FsynthWrapper
{
public:
explicit FsynthWrapper( const CliOptions& options );
~FsynthWrapper();

FsynthWrapper( const FsynthWrapper& ) = delete;
FsynthWrapper& operator=( const FsynthWrapper& ) = delete;

private:
struct Impl;
Impl* const m_i;
};

} // namespace heory

#endif // PROJECT_LIB_FSYNTH_H
5 changes: 5 additions & 0 deletions src/lib/lib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@ RESOURCES = libresources.qrc

SOURCES += \
cli_options.cc \
fsynth.cc \
lib.cc \
logging_tags.cc \
music_notes.cc \
resource_helper.cc \
resources.cc

HEADERS += \
cli_options.h \
fsynth.h \
lib.h \
logging_tags.h \
music_notes.h \
resource_helper.h \
resources.h

# 'pri' usage based on http://archive.is/https://www.toptal.com/qt/vital-guide-qmake
!include(../libstyles/libstyles.pri) { error() }
!include(../util/util.pri) { error() }
!include(fluidsynth.pri) { error() }

target.path = $$top_exe_dir
INSTALLS += target
22 changes: 22 additions & 0 deletions src/lib/music_notes.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "music_notes.h"

#include <QDebug>

#include "src/lib/fsynth.h"
#include "util-assert.h"

namespace heory
{
MusicNotes::MusicNotes( FsynthWrapper* fsynth )
: m_fsynth( fsynth )
{
}

MusicNotes::~MusicNotes() = default;

void MusicNotes::ExportContextPropertiesToQml( QQmlEngine* engine )
{
(void) engine;
}

} // namespace heory
45 changes: 45 additions & 0 deletions src/lib/music_notes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Copyright (c) 2020, pestophagous (pestophagous@users.noreply.github.com)
// See LICENSE.txt
//
// https://github.com/pestophagous/
//
#ifndef PROJECT_LIB_MUSIC_NOTES_H
#define PROJECT_LIB_MUSIC_NOTES_H

#include <QtCore/QObject>
#include <QtQml/QQmlEngine>

#include <memory>

namespace heory
{
class FsynthWrapper;

class MusicNotes : public QObject
{
Q_OBJECT

// clang-format off

// Q_PROPERTY( QString
// READ
// NOTIFY )
// clang-format on

public:
explicit MusicNotes( FsynthWrapper* fsynth );
~MusicNotes() override;

MusicNotes( const MusicNotes& ) = delete;
MusicNotes& operator=( const MusicNotes& ) = delete;

void ExportContextPropertiesToQml( QQmlEngine* engine );

private:
FsynthWrapper* const m_fsynth;
};

} // namespace heory

#endif // PROJECT_LIB_MUSIC_NOTES_H
29 changes: 29 additions & 0 deletions tools/ci/prep-dummy-soundcard.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

set -Eeuxo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/

cat << EOF > $HOME/.asoundrc
pcm.dummy {
type hw
card 0
}
ctl.dummy {
type hw
card 0
}
EOF
chmod go+r $HOME/.asoundrc

sudo bash -c 'cat << EOF >> /etc/modules.conf
# OSS/Free portion - card #1
alias sound-slot-0 snd-card-0
alias sound-service-0-0 snd-mixer-oss
alias sound-service-0-1 snd-seq-oss
alias sound-service-0-3 snd-pcm-oss
alias sound-service-0-8 snd-seq-oss
alias sound-service-0-12 snd-pcm-oss
EOF'
sudo modprobe snd-dummy || true # not working (as of Apr 5, 2020)
sudo modprobe snd-seq || true # not working (as of Apr 5, 2020)
find /lib/modules/ # no snd modules present (as of Apr 5, 2020)
8 changes: 8 additions & 0 deletions tools/ci/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ fi

sudo apt-get update
sudo apt-get --assume-yes install \
alsa-base \
alsa-oss \
alsa-utils \
build-essential \
clang-format-6.0 \
cmake \
fluid-soundfont-gm \
git \
libasound2-dev \
libc-bin \
Expand Down Expand Up @@ -55,9 +59,13 @@ sudo apt-get --assume-yes install \
libxcb-xkb1 \
libxkbcommon-x11-0 \
libxkbcommon0 \
linux-modules-$(uname -r) \
linux-modules-extra-$(uname -r) \
mesa-common-dev \
pkgconf \
psmisc \
python3 \
wget \
xvfb

sudo usermod -a -G audio $USER

0 comments on commit b99677c

Please sign in to comment.