zynaddsubfx

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

Nio.cpp (3922B)


      1 /*
      2   ZynAddSubFX - a software synthesizer
      3 
      4   Nio.cpp - IO Wrapper Layer
      5   Copyright (C) 2016 Mark McCurry
      6 
      7   This program is free software; you can redistribute it and/or
      8   modify it under the terms of the GNU General Public License
      9   as published by the Free Software Foundation; either version 2
     10   of the License, or (at your option) any later version.
     11 */
     12 #include "Nio.h"
     13 #include "OutMgr.h"
     14 #include "InMgr.h"
     15 #include "EngineMgr.h"
     16 #include "MidiIn.h"
     17 #include "AudioOut.h"
     18 #include "WavEngine.h"
     19 #include "../Misc/Config.h"
     20 #include <cstring>
     21 #include <iostream>
     22 #include <algorithm>
     23 
     24 namespace zyn {
     25 
     26 using std::string;
     27 using std::set;
     28 using std::cerr;
     29 using std::endl;
     30 
     31 #ifndef IN_DEFAULT
     32 #define IN_DEFAULT "NULL"
     33 #endif
     34 #ifndef OUT_DEFAULT
     35 #define OUT_DEFAULT "NULL"
     36 #endif
     37 
     38 InMgr     *in  = NULL;
     39 OutMgr    *out = NULL;
     40 EngineMgr *eng = NULL;
     41 string     postfix;
     42 
     43 bool   Nio::autoConnect     = false;
     44 bool   Nio::pidInClientName = false;
     45 string Nio::defaultSource   = IN_DEFAULT;
     46 string Nio::defaultSink     = OUT_DEFAULT;
     47 
     48 void Nio::init(const SYNTH_T &synth, const oss_devs_t& oss_devs,
     49                class Master *master)
     50 {
     51     in  = &InMgr::getInstance(); //Enable input wrapper
     52     out = &OutMgr::getInstance(&synth); //Initialize the Output Systems
     53     eng = &EngineMgr::getInstance(&synth, &oss_devs); //Initialize the Engines
     54 
     55     in->setMaster(master);
     56     out->setMaster(master);
     57 }
     58 
     59 bool Nio::start()
     60 {
     61     if(eng)
     62         return eng->start();
     63     else
     64         return false;
     65 }
     66 
     67 void Nio::stop()
     68 {
     69     if(eng)
     70         eng->stop();
     71 }
     72 
     73 void Nio::setDefaultSource(string name)
     74 {
     75     std::transform(name.begin(), name.end(), name.begin(), ::toupper);
     76     defaultSource = name;
     77 }
     78 
     79 void Nio::setDefaultSink(string name)
     80 {
     81     std::transform(name.begin(), name.end(), name.begin(), ::toupper);
     82     defaultSink = name;
     83 }
     84 
     85 bool Nio::setSource(string name)
     86 {
     87     return in->setSource(name);
     88 }
     89 
     90 bool Nio::setSink(string name)
     91 {
     92     return out->setSink(name);
     93 }
     94 
     95 void Nio::setPostfix(std::string post)
     96 {
     97     postfix = post;
     98 }
     99 
    100 std::string Nio::getPostfix(void)
    101 {
    102     return postfix;
    103 }
    104 
    105 set<string> Nio::getSources(void)
    106 {
    107     set<string> sources;
    108     for(std::list<Engine *>::iterator itr = eng->engines.begin();
    109         itr != eng->engines.end(); ++itr)
    110         if(dynamic_cast<MidiIn *>(*itr))
    111             sources.insert((*itr)->name);
    112     return sources;
    113 }
    114 
    115 set<string> Nio::getSinks(void)
    116 {
    117     set<string> sinks;
    118     for(std::list<Engine *>::iterator itr = eng->engines.begin();
    119         itr != eng->engines.end(); ++itr)
    120         if(dynamic_cast<AudioOut *>(*itr))
    121             sinks.insert((*itr)->name);
    122     return sinks;
    123 }
    124 
    125 string Nio::getSource()
    126 {
    127     return in->getSource();
    128 }
    129 
    130 string Nio::getSink()
    131 {
    132     return out->getSink();
    133 }
    134 
    135 #if JACK
    136 #include <jack/jack.h>
    137 void Nio::preferredSampleRate(unsigned &rate)
    138 {
    139 #if __linux__
    140     //avoid checking in with jack if it's off
    141     FILE *ps = popen("ps aux", "r");
    142     char buffer[4096];
    143     while(fgets(buffer, sizeof(buffer), ps))
    144         if(strstr(buffer, "jack"))
    145             break;
    146     pclose(ps);
    147 
    148     if(!strstr(buffer, "jack"))
    149         return;
    150 #endif
    151 
    152     jack_client_t *client = jack_client_open("temp-client",
    153                                              JackNoStartServer, 0);
    154     if(client) {
    155         rate = jack_get_sample_rate(client);
    156         jack_client_close(client);
    157     }
    158 }
    159 #else
    160 void Nio::preferredSampleRate(unsigned &)
    161 {}
    162 #endif
    163 
    164 void Nio::masterSwap(Master *master)
    165 {
    166     in->setMaster(master);
    167     out->setMaster(master);
    168 }
    169 
    170 void Nio::waveNew(class WavFile *wave)
    171 {
    172     out->wave->newFile(wave);
    173 }
    174 
    175 void Nio::waveStart(void)
    176 {
    177     out->wave->Start();
    178 }
    179 
    180 void Nio::waveStop(void)
    181 {
    182     out->wave->Stop();
    183 }
    184 
    185 void Nio::waveEnd(void)
    186 {
    187     out->wave->destroyFile();
    188 }
    189 
    190 void Nio::setAudioCompressor(bool isEnabled)
    191 {
    192     out->setAudioCompressor(isEnabled);
    193 }
    194 
    195 bool Nio::getAudioCompressor(void)
    196 {
    197     return out->getAudioCompressor();
    198 }
    199 
    200 }