| 1 | /* |
| 2 | Copyright (C) 2008, 2009 Andres Cabrera |
| 3 | [email protected] |
| 4 | |
| 5 | This file is part of CsoundQt. |
| 6 | |
| 7 | CsoundQt is free software; you can redistribute it |
| 8 | and/or modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | CsoundQt is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with Csound; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <QProcess> |
| 24 | #include <QFile> |
| 25 | #include <QTemporaryFile> |
| 26 | #include <QTextStream> |
| 27 | #include <QDir> |
| 28 | #include <QApplication> |
| 29 | #include <QDebug> |
| 30 | |
| 31 | #include "csound.h" |
| 32 | #include "configlists.h" |
| 33 | |
| 34 | typedef QPair<QString, QString> QStringPair; |
| 35 | |
| 36 | |
| 37 | ConfigLists::ConfigLists() |
| 38 | { |
| 39 | fileTypeNames << "wav" << "aiff" << "au" << "avr" |
| 40 | << "caf" << "flac" << "htk" << "ircam" |
| 41 | << "mat4" << "mat5" << "nis" << "paf" |
| 42 | << "pvf" << "raw" << "sd2" << "sds" |
| 43 | << "svx" << "voc" << "w64" << "wavex"; |
| 44 | |
| 45 | fileTypeExtensions << "*.wav" << "*.aif;*.aiff" << "*.au" << "*.avr" |
| 46 | << "*.caf" << "*.flac" << "*.htk;*.*" << "*.ircam;*.*" |
| 47 | << "*.mat4;*.*" << "*.mat5;*.*" << "*.nis;*.*" << "*.paf;*.*" |
| 48 | << "*.pvf" << "*.raw;*.*" << "*.sd2;*.*" << "*.sds;*.*" |
| 49 | << "*.svx;*.*" << "*.voc;*.*" << "*.w64;*.wav" << "*.wavex;*.wav"; |
| 50 | |
| 51 | fileTypeLongNames << "WAVE" << "AIFF" << "au" << "avr" |
| 52 | << "CAF" << "FLAC" << "htk" << "ircam" |
| 53 | << "mat4" << "mat5" << "nis" << "paf" |
| 54 | << "pvf" << "Raw (Headerless)" << "Sound Designer II" << "sds" |
| 55 | << "svx" << "voc" << "WAVE (w64)" << "WAVE (wavex)"; |
| 56 | |
| 57 | fileFormatFlags << "24bit" << "short"<< "uchar" |
| 58 | << "schar"<< "float"<< "long"; |
| 59 | fileFormatNames << "24 Bit" << "16 Bit (short)" << "unsigned 8-bit" |
| 60 | << "signed 8-bit" << "32 bit float"<< "long (32-bit)"; |
| 61 | |
| 62 | refreshModules(); |
| 63 | |
| 64 | languages << "English" << "Spanish" << "German" << "French" << "Portuguese" |
| 65 | << "Italian" << "Turkish" << "Finnish" << "Russian" << "Korean" |
| 66 | << "Persian"; |
| 67 | |
| 68 | languageCodes << "en" << "es" << "de" << "fr" << "pt" |
| 69 | << "it" << "tr" << "fi" << "ru" << "kr" |
| 70 | << "fa"; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | ConfigLists::~ConfigLists() |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | void ConfigLists::msgCallback(CSOUND *csound, int attr, const char *fmt, va_list args) |
| 79 | { |
| 80 | Q_UNUSED(attr); |
| 81 | QString *ud = (QString *) csoundGetHostData(csound); |
| 82 | QString msg; |
| 83 | msg = msg.vsprintf(fmt, args); |
| 84 | if (msg.isEmpty()) { |
| 85 | return; |
| 86 | } |
| 87 | ud->append(msg); |
| 88 | } |
| 89 | |
| 90 | void ConfigLists::refreshModules() |
| 91 | { |
| 92 | rtMidiNames.clear(); |
| 93 | rtAudioNames.clear(); |
| 94 | CSOUND *csound = csoundCreate(nullptr); |
| 95 | char *name, *type; |
| 96 | int n = 0; |
| 97 | while(!csoundGetModule(csound, n++, &name, &type)) { |
| 98 | if (strcmp(type, "audio") == 0) { |
| 99 | rtAudioNames << name; |
| 100 | } |
| 101 | } |
| 102 | rtAudioNames << "null"; // add also none (-+rtaudio=null) |
| 103 | n = 0; |
| 104 | while(!csoundGetModule(csound, n++, &name, &type)) { |
| 105 | if (strcmp(type, "midi") == 0) { |
| 106 | rtMidiNames << name; |
| 107 | } |
| 108 | } |
| 109 | if (rtAudioNames.contains("jack")) { |
| 110 | rtMidiNames << "jack"; |
| 111 | } |
| 112 | rtMidiNames << "virtual" << "none"; |
| 113 | csoundDestroy(csound); |
| 114 | } |
| 115 | |
| 116 | QHash<QString,QString> ConfigLists::getMidiInputDevices(QString module) |
| 117 | { |
| 118 | // based on code by Steven Yi |
| 119 | QHash<QString,QString> deviceList; |
| 120 | |
| 121 | CSOUND *cs = csoundCreate(nullptr); |
| 122 | if (module=="jack") { |
| 123 | csoundSetOption(cs, "-+rtaudio=jack"); |
| 124 | csoundSetOption(cs, "-+rtmidi=jack"); |
| 125 | } |
| 126 | csoundSetMIDIModule(cs, module.toLatin1().data()); |
| 127 | int i,newn, n = csoundGetMIDIDevList(cs, nullptr, 0); |
| 128 | CS_MIDIDEVICE *devs = (CS_MIDIDEVICE *) malloc(n*sizeof(CS_MIDIDEVICE)); |
| 129 | newn = csoundGetMIDIDevList(cs,devs,0); |
| 130 | if (newn != n) { |
| 131 | qDebug() << "Device number changed"; |
| 132 | return deviceList; |
| 133 | } |
| 134 | for (i = 0; i < n; i++) { |
| 135 | qDebug() << "Device "<<i << devs[i].device_name; |
| 136 | QString displayName, id; |
| 137 | if (module=="jack") { |
| 138 | displayName = devs[i].device_name; |
| 139 | id = devs[i].device_name; |
| 140 | } else { |
| 141 | displayName = QString("%1 (%2)") |
| 142 | .arg(devs[i].device_name) |
| 143 | .arg(devs[i].interface_name); |
| 144 | id = QString(devs[i].device_id); |
| 145 | } |
| 146 | deviceList.insert(displayName, id); |
| 147 | } |
| 148 | free(devs); |
| 149 | csoundDestroy(cs); |
| 150 | |
| 151 | qDebug()<<"Devices found: "<<deviceList; |
| 152 | return deviceList; |
| 153 | } |
| 154 | |
| 155 | QList<QPair<QString, QString> > ConfigLists::getMidiOutputDevices(QString module) |
| 156 | { |
| 157 | QList<QPair<QString, QString> > deviceList; |
| 158 | |
| 159 | CSOUND *cs = csoundCreate(nullptr); |
| 160 | if (module=="jack") { |
| 161 | csoundSetOption(cs, "-+rtaudio=jack"); |
| 162 | csoundSetOption(cs, "-+rtmidi=jack"); |
| 163 | } |
| 164 | csoundSetMIDIModule(cs, module.toLatin1().data()); |
| 165 | int i,newn, n = csoundGetMIDIDevList(cs, nullptr, 1); |
| 166 | CS_MIDIDEVICE *devs = (CS_MIDIDEVICE *) malloc(n*sizeof(CS_MIDIDEVICE)); |
| 167 | newn = csoundGetMIDIDevList(cs,devs,1); |
| 168 | if (newn != n) { |
| 169 | qDebug() << "Device number changed"; |
| 170 | return deviceList; |
| 171 | } |
| 172 | for (i = 0; i < n; i++) { |
| 173 | // qDebug() << devs[i].device_name; |
| 174 | QString displayName, id; |
| 175 | if (module=="jack") { |
| 176 | displayName = devs[i].device_name; |
| 177 | id = devs[i].device_name; |
| 178 | } else { |
| 179 | displayName = QString("%1 (%2)") |
| 180 | .arg(devs[i].device_name) |
| 181 | .arg(devs[i].interface_name); |
| 182 | id = QString(devs[i].device_id); |
| 183 | } |
| 184 | |
| 185 | deviceList.append(QStringPair(displayName, id)); |
| 186 | } |
| 187 | free(devs); |
| 188 | csoundDestroy(cs); |
| 189 | |
| 190 | return deviceList; |
| 191 | } |
| 192 | |
| 193 | QList<QPair<QString, QString> > ConfigLists::getAudioInputDevices(QString module) |
| 194 | { |
| 195 | QList<QStringPair> deviceList; |
| 196 | CSOUND *cs = csoundCreate(nullptr); |
| 197 | csoundSetRTAudioModule(cs, module.toLatin1().data()); |
| 198 | int i,newn, n = csoundGetAudioDevList(cs, nullptr, 0); |
| 199 | CS_AUDIODEVICE *devs = (CS_AUDIODEVICE *) malloc(n*sizeof(CS_AUDIODEVICE)); |
| 200 | newn = csoundGetAudioDevList(cs,devs,0); |
| 201 | if (newn != n) { |
| 202 | qDebug() << "Device number changed"; |
| 203 | return deviceList; |
| 204 | } |
| 205 | for (i = 0; i < n; i++) { |
| 206 | // qDebug() << "evs[i].device_name; |
| 207 | deviceList.append(QStringPair(devs[i].device_name, devs[i].device_id)); |
| 208 | } |
| 209 | free(devs); |
| 210 | csoundDestroy(cs); |
| 211 | return deviceList; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | QList<QPair<QString, QString> > ConfigLists::getAudioOutputDevices(QString module) |
| 216 | { |
| 217 | QList<QPair<QString, QString> > deviceList; |
| 218 | |
| 219 | CSOUND *cs = csoundCreate(nullptr); |
| 220 | csoundSetRTAudioModule(cs, module.toLatin1().data()); |
| 221 | int i,newn, n = csoundGetAudioDevList(cs, nullptr, 1); |
| 222 | CS_AUDIODEVICE *devs = (CS_AUDIODEVICE *) malloc(n*sizeof(CS_AUDIODEVICE)); |
| 223 | newn = csoundGetAudioDevList(cs,devs,1); |
| 224 | if (newn != n) { |
| 225 | qDebug() << "OutputDevices Device number changed"; |
| 226 | return deviceList; |
| 227 | } |
| 228 | for (i = 0; i < n; i++) { |
| 229 | // qDebug() << devs[i].device_name; |
| 230 | deviceList.append(QStringPair(devs[i].device_name, devs[i].device_id)); |
| 231 | } |
| 232 | free(devs); |
| 233 | csoundDestroy(cs); |
| 234 | |
| 235 | return deviceList; |
| 236 | } |
| 237 | |
| 238 | QStringList ConfigLists::runCsoundInternally(QStringList flags) |
| 239 | { |
| 240 | #if CS_APIVERSION>=4 |
| 241 | const char *argv[33]; |
| 242 | #else |
| 243 | char *argv[33]; |
| 244 | #endif |
| 245 | int index = 1; |
| 246 | Q_ASSERT(flags.size() < 32); |
| 247 | argv[0] = (char *) calloc(7, sizeof(char)); |
| 248 | strncpy((char *) argv[0], "csound", 6); |
| 249 | |
| 250 | foreach (QString flag, flags) { |
| 251 | argv[index] = (char *) calloc(flag.size()+1, sizeof(char)); |
| 252 | strncpy((char *) argv[index], flag.toLatin1(), flag.size()); |
| 253 | index++; |
| 254 | } |
| 255 | int argc = flags.size() + 1; |
| 256 | #ifdef MACOSX_PRE_SNOW |
| 257 | //Remember menu bar to set it after FLTK grabs it |
| 258 | menuBarHandle = GetMenuBar(); |
| 259 | #endif |
| 260 | m_messages.clear(); |
| 261 | CSOUND *csoundD = csoundCreate(&m_messages); |
| 262 | |
| 263 | csoundSetMessageCallback(csoundD, msgCallback); |
| 264 | int result = csoundCompile(csoundD, argc, argv); |
| 265 | |
| 266 | if(!result) { |
| 267 | csoundPerform(csoundD); |
| 268 | } else { |
| 269 | qDebug() << "Error compiling."; |
| 270 | qDebug() << m_messages; |
| 271 | } |
| 272 | |
| 273 | // FIXME This crashes on Linux for portmidi! And messes up devices on OS X |
| 274 | csoundDestroy(csoundD); |
| 275 | |
| 276 | #ifdef MACOSX_PRE_SNOW |
| 277 | // Put menu bar back |
| 278 | SetMenuBar(menuBarHandle); |
| 279 | #endif |
| 280 | return m_messages.split("\n"); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | bool ConfigLists::isJackRunning() { |
| 285 | CSOUND *cs = csoundCreate(nullptr); |
| 286 | csoundSetRTAudioModule(cs, "jack"); |
| 287 | int n = csoundGetAudioDevList(cs, nullptr, 1); |
| 288 | qDebug() << "isJackRunning" << n; |
| 289 | csoundDestroy(cs); |
| 290 | return n > 0; |
| 291 | } |