diff options
Diffstat (limited to 'muse_qt4_evolution/synti/fluid')
-rw-r--r-- | muse_qt4_evolution/synti/fluid/CMakeLists.txt | 50 | ||||
-rw-r--r-- | muse_qt4_evolution/synti/fluid/fluid.cpp | 518 | ||||
-rw-r--r-- | muse_qt4_evolution/synti/fluid/fluid.h | 80 | ||||
-rw-r--r-- | muse_qt4_evolution/synti/fluid/fluidgui.cpp | 68 | ||||
-rw-r--r-- | muse_qt4_evolution/synti/fluid/fluidgui.h | 31 | ||||
-rw-r--r-- | muse_qt4_evolution/synti/fluid/fluidgui.ui | 115 |
6 files changed, 862 insertions, 0 deletions
diff --git a/muse_qt4_evolution/synti/fluid/CMakeLists.txt b/muse_qt4_evolution/synti/fluid/CMakeLists.txt new file mode 100644 index 00000000..e51ea9c6 --- /dev/null +++ b/muse_qt4_evolution/synti/fluid/CMakeLists.txt @@ -0,0 +1,50 @@ +#============================================================================= +# MusE +# Linux Music Editor +# $Id:$ +# +# Copyright (C) 2002-2006 by Werner Schweer and others +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +#============================================================================= + +QT4_WRAP_CPP ( fluid_mocs fluidgui.h ) +QT4_WRAP_UI ( fluid_uis fluidgui.ui ) + +add_library ( fluid SHARED + fluid.cpp + fluidgui.cpp + fluidgui.h + ${fluid_mocs} + ${fluid_uis} + ) + +target_link_libraries( fluid synti ) + +# - tell cmake to name target fluid.so instead of +# libfluid.so +# - use precompiled header files +# +set_target_properties ( fluid + PROPERTIES PREFIX "" + COMPILE_FLAGS "-include ${PROJECT_BINARY_DIR}/all-pic.h" + LINK_FLAGS "-L${FLUID_LIBDIR} ${FLUID_LIB}" + ) +target_link_libraries(fluid + synti + ${QT_LIBRARIES} + ${FLUID_LIB} + ) + +install_targets ( /${CMAKE_INSTALL_LIBDIR}/${MusE_INSTALL_NAME}/synthi/ fluid ) + diff --git a/muse_qt4_evolution/synti/fluid/fluid.cpp b/muse_qt4_evolution/synti/fluid/fluid.cpp new file mode 100644 index 00000000..601b1899 --- /dev/null +++ b/muse_qt4_evolution/synti/fluid/fluid.cpp @@ -0,0 +1,518 @@ +//========================================================= +// MusE +// Linux Music Editor +// $Id: fluid.cpp,v 1.21 2005/11/23 13:55:32 wschweer Exp $ +// +// This file is derived from fluid Synth and modified +// for MusE. +// Parts of fluid are derived from Smurf Sound Font Editor. +// Parts of Smurf Sound Font Editor are derived from +// awesfx utilities +// Smurf: Copyright (C) 1999-2000 Josh Green +// fluid: Copyright (C) 2001 Peter Hanappe +// MusE: Copyright (C) 2001 Werner Schweer +// awesfx: Copyright (C) 1996-1999 Takashi Iwai +//========================================================= + +#include "muse/midictrl.h" +#include "fluid.h" +#include "fluidgui.h" + +//--------------------------------------------------------- +// instantiate +//--------------------------------------------------------- + +static Mess* instantiate(int sr, const char* name) + { + ISynth* synth = new ISynth(); + synth->setSampleRate(sr); + if (synth->init(name)) { + delete synth; + synth = 0; + } + return synth; + } + +//--------------------------------------------------------- +// MESS +//--------------------------------------------------------- + +extern "C" { + static MESS descriptor = { + "fluid", + "Werner Schweer", + "0.1", // fluid version string + MESS_MAJOR_VERSION, MESS_MINOR_VERSION, + instantiate, + }; + const MESS* mess_descriptor() { return &descriptor; } + } + +//--------------------------------------------------------- +// ISynth +//--------------------------------------------------------- + +ISynth::ISynth() + : Mess(2) + { + _busy = false; + sfont = 0; + _gmMode = false; // General Midi Mode + _fluidsynth = 0; + initBuffer = 0; + initLen = 0; + fontId = -1; + } + +//--------------------------------------------------------- +// playNote +// return true if busy +//--------------------------------------------------------- + +bool ISynth::playNote(int channel, int pitch, int velo) + { + if (_busy) { +// printf("fluid: playNote(): busy!\n"); + return true; + } + if (velo) { + int err = fluid_synth_noteon(_fluidsynth, channel, pitch, velo); + if (err) { + printf("ISynth: noteon error, channel %d pitch %d<%s>\n", + channel, pitch, fluid_synth_error(_fluidsynth)); + } + } + else + fluid_synth_noteoff(_fluidsynth, channel, pitch); + return false; + } + +//--------------------------------------------------------- +// setController +// return true if busy +//--------------------------------------------------------- + +bool ISynth::setController(int ch, int ctrl, int val) + { + if (_busy) { +// printf("fluid: setController(%d,%d,%d): busy!\n", ch, ctrl, val); + return true; + } + switch(ctrl) { + case CTRL_PROGRAM: + { + int hbank = (val & 0xff0000) >> 16; + int lbank = (val & 0xff00) >> 8; + if (hbank > 127) // map "dont care" to 0 + hbank = 0; + if (lbank > 127) + lbank = 0; + if (lbank == 127 || ch == 9) // drum HACK + lbank = 128; + int prog = val & 0x7f; + fluid_synth_program_select(_fluidsynth, ch, + hbank, lbank, prog); + } + break; + + case CTRL_PITCH: + fluid_synth_pitch_bend (_fluidsynth, ch, val); + break; + + default: +// printf("controller %x val %x\n", ctrl & 0x3fff, val); + fluid_synth_cc(_fluidsynth, ch, ctrl & 0x3fff, val); + break; + } + return false; + } + +//--------------------------------------------------------- +// sysex +// 7e 7f 09 01 GM on +// 7e 7f 09 02 GM off +// 7f 7f 04 01 ll hh Master Volume (ll-low byte, hh-high byte) +// 7c 00 01 nn ... replace Soundfont (nn-ascii char of path +// 7c 00 02 nn ... add Soundfont +// 7c 00 03 nn ... remove Soundfont +// +// return true if busy +//--------------------------------------------------------- + +bool ISynth::sysex(int len, const unsigned char* data) + { + if (_busy) { +// printf("fluid: sysex(): busy!\n"); + return true; + } + if (len >= 4) { + //--------------------------------------------- + // Universal Non Realtime + //--------------------------------------------- + + if (data[0] == 0x7e) { + if (data[1] == 0x7f) { // device Id + if (data[2] == 0x9) { // GM + if (data[3] == 0x1) { + gmOn(true); + return false; + } + else if (data[3] == 0x2) { + gmOn(false); + return false; + } + } + } + } + + //--------------------------------------------- + // Universal Realtime + //--------------------------------------------- + + else if (data[0] == 0x7f) { + if (data[1] == 0x7f) { // device Id + if ((data[2] == 0x4) && (data[3] == 0x1)) { + float v = (data[5]*128 + data[4])/32767.0; + fluid_synth_set_gain(_fluidsynth, v); + return false; + } + } + } + + //--------------------------------------------- + // MusE Soft Synth + //--------------------------------------------- + + else if (data[0] == 0x7c) { + int n = len - 3; + if (n < 1) { + printf("fluid: bad sysEx:\n"); + return false; + } + char buffer[n+1]; + memcpy(buffer, (char*)data+3, n); + buffer[n] = 0; + if (data[1] == 0) { // fluid + if (data[2] == 1) { // load sound font + sysexSoundFont(SF_REPLACE, buffer); + return false; + } + else if (data[2] == 2) { // load sound font + sysexSoundFont(SF_ADD, buffer); + return false; + } + else if (data[2] == 3) { // load sound font + sysexSoundFont(SF_REMOVE, buffer); + return false; + } + } + } + else if (data[0] == 0x41) { // roland + if (data[1] == 0x10 && data[2] == 0x42 && data[3] == 0x12 + && data[4] == 0x40 && data[5] == 00 && data[6] == 0x7f + && data[7] == 0x41) { + // gs on + gmOn(true); + return false; + } + } + } + printf("fluid: unknown sysex received, len %d:\n", len); + for (int i = 0; i < len; ++i) + printf("%02x ", data[i]); + printf("\n"); + return false; + } + +//--------------------------------------------------------- +// gmOn +//--------------------------------------------------------- + +void ISynth::gmOn(bool flag) + { + _gmMode = flag; + allNotesOff(); + } + +//--------------------------------------------------------- +// allNotesOff +// stop all notes +//--------------------------------------------------------- + +void ISynth::allNotesOff() + { + for (int ch = 0; ch < 16; ++ch) { + fluid_synth_cc(_fluidsynth, ch, 0x7b, 0); // all notes off + } + } + +//--------------------------------------------------------- +// guiVisible +//--------------------------------------------------------- + +bool ISynth::guiVisible() const + { + return gui->isVisible(); + } + +//--------------------------------------------------------- +// showGui +//--------------------------------------------------------- + +void ISynth::showGui(bool flag) + { + gui->setShown(flag); + } + +//--------------------------------------------------------- +// ~ISynth +//--------------------------------------------------------- + +ISynth::~ISynth() + { + // TODO delete settings + if (_fluidsynth) + delete_fluid_synth(_fluidsynth); + if (initBuffer) + delete [] initBuffer; + } + +//--------------------------------------------------------- +// process +//--------------------------------------------------------- + +void ISynth::process(float** ports, int offset, int n) + { + if (!_busy) { + // + // get and process all pending events from the + // synthesizer GUI + // + while (gui->fifoSize()) + processEvent(gui->readEvent()); + fluid_synth_write_float(_fluidsynth, n, ports[0], + offset, 1, ports[1], offset, 1); + } + // printf("%f %f\n", *ports[0], *(ports[0]+1)); + } + +//--------------------------------------------------------- +// getPatchName +//--------------------------------------------------------- + +const char* ISynth::getPatchName(int /*ch*/, int val, int) const + { + int hbank = (val & 0xff0000) >> 16; + int lbank = (val & 0xff00) >> 8; + if (hbank > 127) + hbank = 0; + if (lbank > 127) + lbank = 0; + if (lbank == 127) // drum HACK + lbank = 128; + int prog = val & 0x7f; + char* name = "---"; + + if (_busy) { + printf("fluid: getPatchName(): busy!\n"); + return name; + } + fluid_font = fluid_synth_get_sfont_by_id(_fluidsynth, hbank); + if (fluid_font) { + fluid_preset_t* preset = (*fluid_font->get_preset)(fluid_font, lbank, prog); + if (preset) + name = (*preset->get_name)(preset); + else + fprintf(stderr, "no fluid preset for bank %d prog %d\n", + lbank, prog); + } + else + fprintf(stderr, "ISynth::getPatchName(): no fluid font id=%d found\n", hbank); + return name; + } + +//--------------------------------------------------------- +// getNextPatch +//--------------------------------------------------------- + +const MidiPatch* ISynth::getPatchInfo(int ch, const MidiPatch* p) const + { + if (_busy) { + printf("fluid: getPatchInfo(): busy!\n"); + return 0; + } + if (p == 0) { + // get font at font stack index 0 + fluid_font = fluid_synth_get_sfont(_fluidsynth, 0); + if (fluid_font == 0) + return 0; + (*fluid_font->iteration_start)(fluid_font); + } + fluid_preset_t preset; + + while ((*fluid_font->iteration_next)(fluid_font, &preset)) { + patch.hbank = fluid_sfont_get_id(fluid_font); + int bank = (*preset.get_banknum)(&preset); + if (ch == 9 && bank != 128) // show only drums for channel 10 + continue; + if (bank == 128) + bank = 127; + patch.typ = 0; + patch.name = (*preset.get_name)(&preset); + patch.lbank = bank; + patch.prog = (*preset.get_num)(&preset); + return &patch; + } + return 0; + } + +//--------------------------------------------------------- +// getInitData +// construct an initialization string which can be used +// as a sysex to restore current state +//--------------------------------------------------------- + +void ISynth::getInitData(int* len, const unsigned char** data) + { + if (sfont == 0) { + *len = 0; + return; + } + int n = 4 + strlen(sfont); + if (n > initLen) { + if (initBuffer) + delete [] initBuffer; + initBuffer = new unsigned char[n]; + } + initBuffer[0] = 0x7c; + initBuffer[1] = 0x00; + initBuffer[2] = SF_REPLACE; + strcpy((char*)(initBuffer+3), sfont); + *len = n; + *data = initBuffer; + } + +//--------------------------------------------------------- +// sysexSoftfont +//--------------------------------------------------------- + +void ISynth::sysexSoundFont(SfOp op, const char* data) + { + char c = 'x'; + allNotesOff(); + switch(op) { + case SF_REMOVE: + break; + case SF_REPLACE: + case SF_ADD: + if (sfont && (strcmp(sfont, data) == 0)) { + fprintf(stderr, "fluid: font already loaded\n"); + break; + } + if (_busy) { + fprintf(stderr, "fluid: busy!\n"); + break; + } + _busy = true; + if (sfont) + delete[] sfont; + sfont = new char[strlen(data)+1]; + strcpy(sfont, data); + _busy = true; + write(writeFd, &c, 1); + break; + } + } + +//--------------------------------------------------------- +// fontLoad +// helper thread to load soundfont in the +// background +//--------------------------------------------------------- + +static void* helper(void* t) + { + ISynth* is = (ISynth*) t; + is->noRTHelper(); + pthread_exit(0); + } + +//------------------------------------ +// noRTHelper +//--------------------------------------------------------- + +void ISynth::noRTHelper() + { + for (;;) { + char c; + int n = read(readFd, &c, 1); + if (n != 1) { + perror("ISynth::read ipc failed\n"); + continue; + } + int id = getFontId(); + if (id != -1) { + fprintf(stderr, "ISynth: unload old font\n"); + fluid_synth_sfunload(synth(), (unsigned)id, true); + } + int rv = fluid_synth_sfload(synth(), getFont(), true); + if (rv == -1) { + fprintf(stderr, "ISynth: sfload %s failed\n", + fluid_synth_error(synth())); + } + else { + setFontId(rv); + fprintf(stderr, "ISynth: sfont %s loaded as %d\n ", + getFont(), rv); + } + fluid_synth_set_gain(synth(), 1.0); //? + _busy = false; + } + } + +//--------------------------------------------------------- +// init +// return true on error +//--------------------------------------------------------- + +bool ISynth::init(const char* name) + { + fluid_settings_t* settings; + settings = new_fluid_settings(); + fluid_settings_setnum(settings, "synth.sample-rate", float(sampleRate())); + + _fluidsynth = new_fluid_synth(settings); + + //--------------------------------------- + // create non realtime helper thread + // create message channels + // + int filedes[2]; // 0 - reading 1 - writing + if (pipe(filedes) == -1) { + perror("ISynth::thread:creating pipe"); + return true; + } + readFd = filedes[0]; + writeFd = filedes[1]; + + pthread_attr_t* attributes = (pthread_attr_t*) malloc(sizeof(pthread_attr_t)); + pthread_attr_init(attributes); + if (pthread_create(&helperThread, attributes, ::helper, this)) + perror("creating thread failed:"); + pthread_attr_destroy(attributes); + + char* p = getenv("DEFAULT_SOUNDFONT"); + if (p) { + sfont = new char[strlen(p)+1]; + strcpy(sfont, p); + char c = 'x'; + _busy = true; + write(writeFd, &c, 1); + } + + gui = new FLUIDGui; + gui->hide(); // to avoid flicker during MusE startup + gui->setWindowTitle(QString(name)); + return false; + } + diff --git a/muse_qt4_evolution/synti/fluid/fluid.h b/muse_qt4_evolution/synti/fluid/fluid.h new file mode 100644 index 00000000..af8ff3a1 --- /dev/null +++ b/muse_qt4_evolution/synti/fluid/fluid.h @@ -0,0 +1,80 @@ +//========================================================= +// MusE +// Linux Music Editor +// $Id: fluid.h,v 1.8 2005/11/23 13:55:32 wschweer Exp $ +// +// This file is derived from fluid Synth and modified +// for MusE. +// Parts of fluid are derived from Smurf Sound Font Editor. +// Parts of Smurf Sound Font Editor are derived from +// awesfx utilities +// Smurf: Copyright (C) 1999-2000 Josh Green +// fluid: Copyright (C) 2001 Peter Hanappe +// MusE: Copyright (C) 2001 Werner Schweer +// awesfx: Copyright (C) 1996-1999 Takashi Iwai +//========================================================= + +#ifndef _SYNTH_H +#define _SYNTH_H + +#include <fluidsynth.h> +#include "libsynti/mess.h" + +enum SfOp { SF_REPLACE = 1, SF_ADD, SF_REMOVE }; +class FLUIDGui; + +//--------------------------------------------------------- +// ISynth +//--------------------------------------------------------- + +class ISynth : public Mess { + bool _busy; + bool _gmMode; + + unsigned char* initBuffer; + int initLen; + + fluid_synth_t* _fluidsynth; + char* sfont; + mutable fluid_sfont_t* fluid_font; + int fontId; + + int readFd, writeFd; + + mutable MidiPatch patch; + + pthread_t helperThread; + FLUIDGui* gui; + + void gmOn(bool); + void sysexSoundFont(SfOp op, const char* data); + + void allNotesOff(); + void resetAllController(int); + + virtual void process(float**, int, int); + virtual bool playNote(int channel, int pitch, int velo); + virtual bool setController(int, int, int); + virtual bool sysex(int len, const unsigned char* p); + virtual const char* getPatchName (int, int, int) const; + virtual const MidiPatch* getPatchInfo(int, const MidiPatch *) const; + virtual void getInitData(int*, const unsigned char**); + + virtual bool guiVisible() const; + virtual void showGui(bool); + virtual bool hasGui() const { return true; } + + public: + ISynth(); + ~ISynth(); + + fluid_synth_t* synth() { return _fluidsynth; } + const fluid_synth_t* synth() const { return _fluidsynth; } + char* getFont() const { return sfont; } + void setFontId(int id) { fontId = id; } + int getFontId() const { return fontId; } + bool init(const char* name); + void noRTHelper(); + }; + +#endif /* _SYNTH_H */ diff --git a/muse_qt4_evolution/synti/fluid/fluidgui.cpp b/muse_qt4_evolution/synti/fluid/fluidgui.cpp new file mode 100644 index 00000000..aea6e38a --- /dev/null +++ b/muse_qt4_evolution/synti/fluid/fluidgui.cpp @@ -0,0 +1,68 @@ +//========================================================= +// MusE +// Linux Music Editor +// $Id: fluidgui.cpp,v 1.10 2005/11/23 13:55:32 wschweer Exp $ +// +// This is a simple GUI implemented with QT for +// fluid software synthesizer. +// +// (C) Copyright 2001-2004 Werner Schweer (ws@seh.de) +//========================================================= + +#include "fluidgui.h" +#include "muse/midi.h" + +//--------------------------------------------------------- +// FLUIDGui +//--------------------------------------------------------- + +FLUIDGui::FLUIDGui() + : QDialog(0), MessGui() + { + setupUi(this); + connect(fdialogButton, SIGNAL(clicked()), SLOT(soundFontFileDialog())); + connect(loadButton, SIGNAL(clicked()), SLOT(loadFont())); + } + +//--------------------------------------------------------- +// loadFont +// sysex f0 lenH lenM lenL 7c 00 01 name +//--------------------------------------------------------- + +void FLUIDGui::loadFont() + { + if (pathEntry->text().isEmpty()) + return; + QFileInfo fi(pathEntry->text()); + if (!fi.exists()) { + QString s = QString("SoundFont ") + pathEntry->text() + QString(" does not exists"); + QMessageBox::critical(this, tr("FLUID: open Soundfile"), s); + return; + } + const char* path = pathEntry->text().toLatin1().data(); + int len = strlen(path) + 1 + 3; + unsigned char buffer[len]; + int k = 0; + buffer[k++] = 0x7c; + buffer[k++] = 0x00; // fluid + buffer[k++] = 0x01; // load sound font + strcpy((char*)(&buffer[k]), path); + sendSysex(buffer, len); + } + +//--------------------------------------------------------- +// soundFontFileDialog +//--------------------------------------------------------- + +void FLUIDGui::soundFontFileDialog() + { + QString s = QFileDialog::getOpenFileName( + this, + tr("Fluid: select Sound Font"), + ".", + QString("*.[Ss][Ff]2")); + if (!s.isEmpty()) { + pathEntry->setText(s); + } + } + diff --git a/muse_qt4_evolution/synti/fluid/fluidgui.h b/muse_qt4_evolution/synti/fluid/fluidgui.h new file mode 100644 index 00000000..3b42280f --- /dev/null +++ b/muse_qt4_evolution/synti/fluid/fluidgui.h @@ -0,0 +1,31 @@ +//========================================================= +// MusE +// Linux Music Editor +// $Id: fluidgui.h,v 1.3 2005/10/04 21:37:44 lunar_shuttle Exp $ +// +// (C) Copyright 2001 Werner Schweer (ws@seh.de) +//========================================================= + +#ifndef __GUI_H__ +#define __GUI_H + +#include "ui_fluidgui.h" +#include "libsynti/gui.h" + +//--------------------------------------------------------- +// FLUIDGui +//--------------------------------------------------------- + +class FLUIDGui : public QDialog, public Ui::FLUIDGuiBase, public MessGui { + + Q_OBJECT + + private slots: + void soundFontFileDialog(); + void loadFont(); + + public: + FLUIDGui(); + }; + +#endif diff --git a/muse_qt4_evolution/synti/fluid/fluidgui.ui b/muse_qt4_evolution/synti/fluid/fluidgui.ui new file mode 100644 index 00000000..dde416d2 --- /dev/null +++ b/muse_qt4_evolution/synti/fluid/fluidgui.ui @@ -0,0 +1,115 @@ +<ui version="4.0" > + <author></author> + <comment></comment> + <exportmacro></exportmacro> + <class>FLUIDGuiBase</class> + <widget class="QDialog" name="FLUIDGuiBase" > + <property name="geometry" > + <rect> + <x>0</x> + <y>0</y> + <width>318</width> + <height>228</height> + </rect> + </property> + <property name="windowTitle" > + <string>Form1</string> + </property> + <layout class="QVBoxLayout" > + <property name="margin" > + <number>8</number> + </property> + <property name="spacing" > + <number>6</number> + </property> + <item> + <widget class="QLabel" name="TextLabel1" > + <property name="text" > + <string>Soundfont</string> + </property> + </widget> + </item> + <item> + <layout class="QHBoxLayout" > + <property name="margin" > + <number>0</number> + </property> + <property name="spacing" > + <number>6</number> + </property> + <item> + <widget class="QLineEdit" name="pathEntry" /> + </item> + <item> + <widget class="QToolButton" name="fdialogButton" > + <property name="text" > + <string/> + </property> + <property name="icon" > + <iconset/> + </property> + </widget> + </item> + </layout> + </item> + <item> + <spacer> + <property name="orientation" > + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" > + <size> + <width>230</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <layout class="QHBoxLayout" > + <property name="margin" > + <number>0</number> + </property> + <property name="spacing" > + <number>6</number> + </property> + <item> + <widget class="QPushButton" name="loadButton" > + <property name="sizePolicy" > + <sizepolicy> + <hsizetype>5</hsizetype> + <vsizetype>0</vsizetype> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text" > + <string>Load</string> + </property> + </widget> + </item> + <item> + <spacer> + <property name="orientation" > + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeType" > + <enum>QSizePolicy::Expanding</enum> + </property> + <property name="sizeHint" > + <size> + <width>20</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + </layout> + <widget class="QWidget" name="" /> + </widget> + <pixmapfunction></pixmapfunction> + <resources/> + <connections/> +</ui> |