zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

commit 13f7a55881503c16bfaa756ff7f332934eef96e9
parent 93791ff182ee154c7b492a207c61de5f42c894cd
Author: fundamental <[email protected]>
Date:   Tue, 17 Jan 2012 15:48:06 -0500

Nio: Reorganized to fix buffer resizing

- Sound buffer should now resize at startup properly

Diffstat:
Msrc/Misc/Master.cpp | 7+++++--
Msrc/Nio/EngineMgr.cpp | 17+++++++++++++++++
Msrc/Nio/JackEngine.cpp | 6++----
Msrc/Nio/Nio.cpp | 87+++++++++++++++++++++++++++++++++----------------------------------------------
Msrc/Nio/Nio.h | 41++++++++++++++++-------------------------
Msrc/Nio/OutMgr.cpp | 4+++-
Msrc/UI/NioUI.cpp | 80++++++++++++++++++++++++++++++-------------------------------------------------
Msrc/main.cpp | 25+++++++++++++------------
8 files changed, 122 insertions(+), 145 deletions(-)

diff --git a/src/Misc/Master.cpp b/src/Misc/Master.cpp @@ -121,8 +121,11 @@ bool Master::mutexLock(lockset request) Master &Master::getInstance() { - static Master instance; - return instance; + static Master *instance = NULL; + if(!instance) + instance = new Master; + + return *instance; } /* diff --git a/src/Nio/EngineMgr.cpp b/src/Nio/EngineMgr.cpp @@ -1,6 +1,7 @@ #include "EngineMgr.h" #include <algorithm> #include <iostream> +#include "Nio.h" #include "InMgr.h" #include "OutMgr.h" #include "AudioOut.h" @@ -65,6 +66,13 @@ EngineMgr::EngineMgr() defaultOut = dynamic_cast<AudioOut *>(defaultEng); defaultIn = dynamic_cast<MidiIn *>(defaultEng); + + //Accept command line options + if(!Nio::defaultSink.empty()) + setOutDefault(Nio::defaultSink); + + if(!Nio::defaultSource.empty()) + setInDefault(Nio::defaultSource); } EngineMgr::~EngineMgr() @@ -140,6 +148,11 @@ bool EngineMgr::setInDefault(string name) defaultIn = chosen; return true; } + + //Warn user + cerr << "Error: " << name << " is not a recognized MIDI input source" << endl; + cerr << " Defaulting to the NULL input source" << endl; + return false; } @@ -150,5 +163,9 @@ bool EngineMgr::setOutDefault(string name) defaultOut = chosen; return true; } + + //Warn user + cerr << "Error: " << name << " is not a recognized audio backend" << endl; + cerr << " Defaulting to the NULL audio backend" << endl; return false; } diff --git a/src/Nio/JackEngine.cpp b/src/Nio/JackEngine.cpp @@ -53,7 +53,7 @@ bool JackEngine::connectServer(string server) string clientname = "zynaddsubfx"; - string postfix = Nio::getInstance().getPostfix(); + string postfix = Nio::getPostfix(); if(!postfix.empty()) clientname += "_" + postfix; jack_status_t jackstatus; @@ -74,8 +74,6 @@ bool JackEngine::connectServer(string server) cerr << "Error, failed to open jack client on server: " << server << " status " << jackstatus << endl; return false; - - return true; } bool JackEngine::connectJack() @@ -175,7 +173,7 @@ bool JackEngine::openAudio() audio.jackNframes = jack_get_buffer_size(jackClient); //Attempt to autoConnect when specified - if(Nio::getInstance().autoConnect) { + if(Nio::autoConnect) { const char **outPorts = jack_get_ports( jackClient, NULL, diff --git a/src/Nio/Nio.cpp b/src/Nio/Nio.cpp @@ -5,68 +5,54 @@ #include "MidiIn.h" #include "AudioOut.h" #include <iostream> -using namespace std; +#include <algorithm> +using std::string; +using std::set; +using std::cerr; +using std::endl; -Nio &Nio::getInstance() -{ - static Nio instance; - return instance; -} - -Nio::Nio() - :autoConnect(false), - in(InMgr::getInstance()), //Enable input wrapper - out(OutMgr::getInstance()), //Initialize the Output Systems - eng(EngineMgr::getInstance()), //Initialize The Engines - postfix("") //no default postfix -{} +InMgr *in = NULL; +OutMgr *out = NULL; +EngineMgr *eng = NULL; +string postfix; -Nio::~Nio() -{ - stop(); -} +bool Nio::autoConnect = false; +string Nio::defaultSource; +string Nio::defaultSink; bool Nio::start() { - return eng.start(); //Drivers start your engines! + in = &InMgr::getInstance(); //Enable input wrapper + out = &OutMgr::getInstance(); //Initialize the Output Systems + eng = &EngineMgr::getInstance(); //Initialize The Engines + return eng->start(); } void Nio::stop() { - eng.stop(); + eng->stop(); } -int Nio::setDefaultSource(string name) +void Nio::setDefaultSource(string name) { - if(name.empty()) - return 0; - - if(!eng.setInDefault(name)) { - cerr << "There is no input for " << name << endl; - return false; - } - return 0; + std::transform(name.begin(), name.end(), name.begin(), ::toupper); + defaultSource = name; } - -int Nio::setDefaultSink(string name) +void Nio::setDefaultSink(string name) { - if(name.empty()) - return 0; - - if(!eng.setOutDefault(name)) - cerr << "There is no output for " << name << endl; - return 0; + std::transform(name.begin(), name.end(), name.begin(), ::toupper); + defaultSink = name; } bool Nio::setSource(string name) { - return in.setSource(name); + return in->setSource(name); } bool Nio::setSink(string name) { - return out.setSink(name); + return out->setSink(name); } void Nio::setPostfix(std::string post) @@ -74,38 +60,37 @@ void Nio::setPostfix(std::string post) postfix = post; } -std::string Nio::getPostfix(void) const +std::string Nio::getPostfix(void) { return postfix; } - -set<string> Nio::getSources() const +set<string> Nio::getSources(void) { set<string> sources; - for(list<Engine *>::iterator itr = eng.engines.begin(); - itr != eng.engines.end(); ++itr) + for(std::list<Engine *>::iterator itr = eng->engines.begin(); + itr != eng->engines.end(); ++itr) if(dynamic_cast<MidiIn *>(*itr)) sources.insert((*itr)->name); return sources; } -set<string> Nio::getSinks() const +set<string> Nio::getSinks(void) { set<string> sinks; - for(list<Engine *>::iterator itr = eng.engines.begin(); - itr != eng.engines.end(); ++itr) + for(std::list<Engine *>::iterator itr = eng->engines.begin(); + itr != eng->engines.end(); ++itr) if(dynamic_cast<AudioOut *>(*itr)) sinks.insert((*itr)->name); return sinks; } -string Nio::getSource() const +string Nio::getSource() { - return in.getSource(); + return in->getSource(); } -string Nio::getSink() const +string Nio::getSink() { - return out.getSink(); + return out->getSink(); } diff --git a/src/Nio/Nio.h b/src/Nio/Nio.h @@ -6,38 +6,29 @@ /**Interface to Nio Subsystem * * Should be only externally included header */ -class Nio +namespace Nio { - public: - static Nio &getInstance(); - ~Nio(); + bool start(void); + void stop(void); - bool start(); - void stop(); + void setDefaultSource(std::string name); + void setDefaultSink(std::string name); - int setDefaultSource(std::string name); - int setDefaultSink(std::string name); + bool setSource(std::string name); + bool setSink(std::string name); - bool setSource(std::string name); - bool setSink(std::string name); + void setPostfix(std::string post); + std::string getPostfix(void); - void setPostfix(std::string post); - std::string getPostfix(void) const; + std::set<std::string> getSources(void); + std::set<std::string> getSinks(void); - std::set<std::string> getSources() const; - std::set<std::string> getSinks() const; + std::string getSource(void); + std::string getSink(void); - std::string getSource() const; - std::string getSink() const; - - bool autoConnect; - private: - Nio(); - - class InMgr & in; - class OutMgr & out; - class EngineMgr & eng; - std::string postfix; + extern bool autoConnect; + extern std::string defaultSource; + extern std::string defaultSink; }; #endif diff --git a/src/Nio/OutMgr.cpp b/src/Nio/OutMgr.cpp @@ -31,6 +31,8 @@ OutMgr::OutMgr() //init samples outr = new float[synth->buffersize]; outl = new float[synth->buffersize]; + memset(outl, 0, synth->bufferbytes); + memset(outr, 0, synth->bufferbytes); } OutMgr::~OutMgr() @@ -121,7 +123,7 @@ void OutMgr::addSmps(float *l, float *r) //allow wave file to syphon off stream wave->push(Stereo<float *>(l, r), synth->buffersize); - if(currentOut->getSampleRate() != synth->samplerate) { //we need to resample + if(currentOut->getSampleRate() != (size_t)synth->samplerate) { //we need to resample //cout << "BAD RESAMPLING" << endl; Stereo<Sample> smps(Sample(synth->buffersize, l), Sample( synth->buffersize, diff --git a/src/UI/NioUI.cpp b/src/UI/NioUI.cpp @@ -17,62 +17,42 @@ NioUI::NioUI() :Fl_Window(200, 100, 400, 400, "New IO Controls") { //hm, I appear to be leaking memory - Fl_Tabs *wintabs = new Fl_Tabs(0, 0, 400, 400 - 15); + Fl_Group *settings = new Fl_Group(0, 20, 400, 400 - 35, "Settings"); { - Fl_Group *gen = new Fl_Group(0, 20, 400, 400 - 35, "General"); - { - Fl_Text_Buffer *buff = new Fl_Text_Buffer(); - Fl_Text_Display *intro = new Fl_Text_Display(20, 40, 350, 300); - intro->buffer(buff); - buff->text("Thanks For Testing Out the New" - " Input/Output system. " - "Beware of bugs that may exist and" - " enjoy the new system."); - intro->wrap_mode(4, 40); - } - gen->end(); - - Fl_Group *settings = new Fl_Group(0, 20, 400, 400 - 35, "Settings"); - { - audio = new Fl_Choice(60, 80, 100, 25, "Audio"); - audio->callback(audioCallback); - midi = new Fl_Choice(60, 100, 100, 25, "Midi"); - midi->callback(midiCallback); - } - settings->end(); - - Nio &nio = Nio::getInstance(); + audio = new Fl_Choice(60, 80, 100, 25, "Audio"); + audio->callback(audioCallback); + midi = new Fl_Choice(60, 100, 100, 25, "Midi"); + midi->callback(midiCallback); + } + settings->end(); - //initialize midi list - { - set<string> midiList = nio.getSources(); - string source = nio.getSource(); - int midival = 0; - for(set<string>::iterator itr = midiList.begin(); + //initialize midi list + { + set<string> midiList = Nio::getSources(); + string source = Nio::getSource(); + int midival = 0; + for(set<string>::iterator itr = midiList.begin(); itr != midiList.end(); ++itr) { - midi->add(itr->c_str()); - if(*itr == source) - midival = midi->size() - 2; - } - midi->value(midival); + midi->add(itr->c_str()); + if(*itr == source) + midival = midi->size() - 2; } + midi->value(midival); + } - //initialize audio list - { - set<string> audioList = nio.getSinks(); - string sink = nio.getInstance().getSink(); - int audioval = 0; - for(set<string>::iterator itr = audioList.begin(); + //initialize audio list + { + set<string> audioList = Nio::getSinks(); + string sink = Nio::getSink(); + int audioval = 0; + for(set<string>::iterator itr = audioList.begin(); itr != audioList.end(); ++itr) { - audio->add(itr->c_str()); - if(*itr == sink) - audioval = audio->size() - 2; - } - audio->value(audioval); + audio->add(itr->c_str()); + if(*itr == sink) + audioval = audio->size() - 2; } + audio->value(audioval); } - wintabs->end(); - resizable(this); size_range(400, 300); } @@ -85,12 +65,12 @@ void NioUI::refresh() void NioUI::midiCallback(Fl_Widget *c) { - bool good = Nio::getInstance().setSource(static_cast<Fl_Choice *>(c)->text()); + bool good = Nio::setSource(static_cast<Fl_Choice *>(c)->text()); static_cast<Fl_Choice *>(c)->textcolor(fl_rgb_color(255 * !good, 0, 0)); } void NioUI::audioCallback(Fl_Widget *c) { - bool good = Nio::getInstance().setSink(static_cast<Fl_Choice *>(c)->text()); + bool good = Nio::setSink(static_cast<Fl_Choice *>(c)->text()); static_cast<Fl_Choice *>(c)->textcolor(fl_rgb_color(255 * !good, 0, 0)); } diff --git a/src/main.cpp b/src/main.cpp @@ -162,7 +162,7 @@ void exitprogram() pthread_mutex_lock(&master->mutex); pthread_mutex_unlock(&master->mutex); - Nio::getInstance().stop(); + Nio::stop(); #ifndef DISABLE_GUI delete ui; @@ -199,10 +199,6 @@ int main(int argc, char *argv[]) synth->alias(); //build aliases sprng(time(NULL)); - //produce denormal buf - denormalkillbuf = new float [synth->buffersize]; - for(int i = 0; i < synth->buffersize; ++i) - denormalkillbuf[i] = (RND - 0.5f) * 1e-16; /* Parse command-line options */ struct option opts[] = { @@ -339,20 +335,18 @@ int main(int argc, char *argv[]) dump.startnow(); break; case 'N': - Nio::getInstance().setPostfix(optarguments); + Nio::setPostfix(optarguments); break; case 'I': if(optarguments) - if(Nio::getInstance().setDefaultSource(optarguments)) - exit(1); + Nio::setDefaultSource(optarguments); break; case 'O': if(optarguments) - if(Nio::getInstance().setDefaultSink(optarguments)) - exit(1); + Nio::setDefaultSink(optarguments); break; case 'a': - Nio::getInstance().autoConnect = true; + Nio::autoConnect = true; break; case '?': cerr << "ERROR:Bad option or parameter.\n" << endl; @@ -361,6 +355,8 @@ int main(int argc, char *argv[]) } } + synth->alias(); + if(exitwithversion) { cout << "Version: " << VERSION << endl; return 0; @@ -387,6 +383,11 @@ int main(int argc, char *argv[]) return 0; } + //produce denormal buf + denormalkillbuf = new float [synth->buffersize]; + for(int i = 0; i < synth->buffersize; ++i) + denormalkillbuf[i] = (RND - 0.5f) * 1e-16; + initprogram(argc, argv); #if 0 //TODO update this code @@ -432,7 +433,7 @@ int main(int argc, char *argv[]) } //Run the Nio system - bool ioGood = Nio::getInstance().start(); + bool ioGood = Nio::start(); #ifndef DISABLE_GUI if(noui == 0)