Skip to content

Commit faee57c

Browse files
committed
better audio devices selection and filtering + check on dir existence before removing
1 parent 1ce5813 commit faee57c

File tree

1 file changed

+66
-27
lines changed

1 file changed

+66
-27
lines changed

src/ofxVisualProgramming.cpp

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,11 @@ void ofxVisualProgramming::drawSubpatchNavigation(){
447447
//--------------------------------------------------------------
448448
void ofxVisualProgramming::resetTempFolder(){
449449
ofDirectory dir;
450-
dir.removeDirectory(ofToDataPath("temp/",true),true,true);
450+
451+
if(dir.doesDirectoryExist(ofToDataPath("temp/",true),true)){
452+
dir.removeDirectory(ofToDataPath("temp/",true),true,true);
453+
}
454+
451455
}
452456

453457
//--------------------------------------------------------------
@@ -1290,13 +1294,19 @@ void ofxVisualProgramming::loadPatch(string patchFile){
12901294
audioDevicesID_OUT.clear();
12911295
ofLog(OF_LOG_NOTICE,"------------------- AUDIO DEVICES");
12921296
for(size_t i=0;i<audioDevices.size();i++){
1293-
if(audioDevices[i].inputChannels > 0){
1297+
bool haveMinSR = false;
1298+
for(size_t sr=0;sr<audioDevices[i].sampleRates.size();sr++){
1299+
if(audioDevices[i].sampleRates.at(sr) >= 44100){
1300+
haveMinSR = true;
1301+
break;
1302+
}
1303+
}
1304+
if(audioDevices[i].inputChannels > 0 && haveMinSR){
12941305
audioDevicesStringIN.push_back(" "+audioDevices[i].name);
12951306
audioDevicesID_IN.push_back(i);
12961307
//ofLog(OF_LOG_NOTICE,"INPUT Device[%zu]: %s (IN:%i - OUT:%i)",i,audioDevices[i].name.c_str(),audioDevices[i].inputChannels,audioDevices[i].outputChannels);
1297-
12981308
}
1299-
if(audioDevices[i].outputChannels > 0){
1309+
if(audioDevices[i].outputChannels > 0 && haveMinSR){
13001310
audioDevicesStringOUT.push_back(" "+audioDevices[i].name);
13011311
audioDevicesID_OUT.push_back(i);
13021312
//ofLog(OF_LOG_NOTICE,"OUTPUT Device[%zu]: %s (IN:%i - OUT:%i)",i,audioDevices[i].name.c_str(),audioDevices[i].inputChannels,audioDevices[i].outputChannels);
@@ -1320,34 +1330,38 @@ void ofxVisualProgramming::loadPatch(string patchFile){
13201330
audioGUIOUTChannels = 0;
13211331

13221332
// check input devices
1323-
for(size_t i=0;i<audioDevicesID_IN.size();i++){
1324-
if(audioDevicesID_IN.at(i) == audioINDev){
1325-
audioGUIINIndex = i;
1326-
break;
1333+
if(!audioDevicesID_IN.empty()){
1334+
for(size_t i=0;i<audioDevicesID_IN.size();i++){
1335+
if(audioDevicesID_IN.at(i) == audioINDev){
1336+
audioGUIINIndex = i;
1337+
break;
1338+
}
13271339
}
13281340
}
13291341
if(audioGUIINIndex == -1){ // no input devices available
13301342
isInputDeviceAvailable = false;
13311343
audioGUIINIndex = 0;
13321344
}else{
13331345
isInputDeviceAvailable = true;
1346+
audioINDev = audioDevicesID_IN.at(audioGUIINIndex);
13341347
}
1335-
audioINDev = audioDevicesID_IN.at(audioGUIINIndex);
13361348

13371349
// check output devices
1338-
for(size_t i=0;i<audioDevicesID_OUT.size();i++){
1339-
if(audioDevicesID_OUT.at(i) == audioOUTDev){
1340-
audioGUIOUTIndex = i;
1341-
break;
1350+
if(!audioDevicesID_OUT.empty()){
1351+
for(size_t i=0;i<audioDevicesID_OUT.size();i++){
1352+
if(audioDevicesID_OUT.at(i) == audioOUTDev){
1353+
audioGUIOUTIndex = i;
1354+
break;
1355+
}
13421356
}
13431357
}
13441358
if(audioGUIOUTIndex == -1){ // no output devices available
13451359
isOutputDeviceAvailable = false;
13461360
audioGUIOUTIndex = 0;
13471361
}else{
13481362
isOutputDeviceAvailable = true;
1363+
audioOUTDev = audioDevicesID_OUT.at(audioGUIOUTIndex);
13491364
}
1350-
audioOUTDev = audioDevicesID_OUT.at(audioGUIOUTIndex);
13511365

13521366
// select default devices
13531367
if(isInputDeviceAvailable){
@@ -1364,7 +1378,7 @@ void ofxVisualProgramming::loadPatch(string patchFile){
13641378
audioGUIOUTChannels = 0;
13651379
}
13661380

1367-
ofLog(OF_LOG_NOTICE,"%i IN CH - %i OUT CH",audioGUIINChannels, audioGUIOUTChannels);
1381+
//ofLog(OF_LOG_NOTICE,"%i IN CH - %i OUT CH",audioGUIINChannels, audioGUIOUTChannels);
13681382

13691383
// fix samplerate to 44100 as minimum
13701384
if(audioSampleRate < 44100){
@@ -1387,15 +1401,31 @@ void ofxVisualProgramming::loadPatch(string patchFile){
13871401
}
13881402
this->out_silent() >> engine->blackhole();
13891403

1390-
engine->setOutputDeviceID(audioDevices[audioOUTDev].deviceID);
1391-
engine->setInputDeviceID(audioDevices[audioINDev].deviceID);
1404+
if(isInputDeviceAvailable){
1405+
engine->setInputDeviceID(audioDevices[audioINDev].deviceID);
1406+
}
1407+
1408+
if(isOutputDeviceAvailable){
1409+
engine->setOutputDeviceID(audioDevices[audioOUTDev].deviceID);
1410+
}
1411+
13921412
engine->setup(audioSampleRate, audioBufferSize, audioNumBuffers);
13931413
engine->sequencer.setTempo(bpm);
13941414

1395-
ofLog(OF_LOG_NOTICE,"[verbose]------------------- Soundstream INPUT Started on");
1396-
ofLog(OF_LOG_NOTICE,"Audio device: %s",audioDevices[audioINDev].name.c_str());
1397-
ofLog(OF_LOG_NOTICE,"[verbose]------------------- Soundstream OUTPUT Started on");
1398-
ofLog(OF_LOG_NOTICE,"Audio device: %s",audioDevices[audioOUTDev].name.c_str());
1415+
if(isInputDeviceAvailable){
1416+
ofLog(OF_LOG_NOTICE,"[verbose]------------------- Soundstream INPUT Started on");
1417+
ofLog(OF_LOG_NOTICE,"Audio device: %s",audioDevices[audioINDev].name.c_str());
1418+
}else{
1419+
ofLog(OF_LOG_ERROR,"------------------------------ Soundstream INPUT OFF, no input audio device available");
1420+
}
1421+
1422+
if(isOutputDeviceAvailable){
1423+
ofLog(OF_LOG_NOTICE,"[verbose]------------------- Soundstream OUTPUT Started on");
1424+
ofLog(OF_LOG_NOTICE,"Audio device: %s",audioDevices[audioOUTDev].name.c_str());
1425+
1426+
}else{
1427+
ofLog(OF_LOG_ERROR,"------------------------------ Soundstream OUTPUT OFF, no output audio device available");
1428+
}
13991429

14001430
std::this_thread::sleep_for(std::chrono::milliseconds(200));
14011431
}
@@ -1613,13 +1643,22 @@ void ofxVisualProgramming::activateDSP(){
16131643
}
16141644
this->out_silent() >> engine->blackhole();
16151645

1616-
ofLog(OF_LOG_NOTICE,"[verbose]------------------- Soundstream INPUT Started on");
1617-
ofLog(OF_LOG_NOTICE,"Audio device: %s, with %i INPUT channels",audioDevices[audioINDev].name.c_str(),audioGUIINChannels);
1618-
ofLog(OF_LOG_NOTICE,"[verbose]------------------- Soundstream OUTPUT Started on");
1619-
ofLog(OF_LOG_NOTICE,"Audio device: %s, with %i OUTPUT channels",audioDevices[audioOUTDev].name.c_str(),audioGUIOUTChannels);
1646+
if(isInputDeviceAvailable){
1647+
engine->setInputDeviceID(audioDevices[audioINDev].deviceID);
1648+
ofLog(OF_LOG_NOTICE,"[verbose]------------------- Soundstream INPUT Started on");
1649+
ofLog(OF_LOG_NOTICE,"Audio device: %s, with %i INPUT channels",audioDevices[audioINDev].name.c_str(),audioGUIINChannels);
1650+
}else{
1651+
ofLog(OF_LOG_ERROR,"------------------------------ Soundstream INPUT OFF, no input audio device available");
1652+
}
1653+
1654+
if(isOutputDeviceAvailable){
1655+
engine->setOutputDeviceID(audioDevices[audioOUTDev].deviceID);
1656+
ofLog(OF_LOG_NOTICE,"[verbose]------------------- Soundstream OUTPUT Started on");
1657+
ofLog(OF_LOG_NOTICE,"Audio device: %s, with %i OUTPUT channels",audioDevices[audioOUTDev].name.c_str(),audioGUIOUTChannels);
1658+
}else{
1659+
ofLog(OF_LOG_ERROR,"------------------------------ Soundstream OUTPUT OFF, no output audio device available");
1660+
}
16201661

1621-
engine->setOutputDeviceID(audioDevices[audioOUTDev].deviceID);
1622-
engine->setInputDeviceID(audioDevices[audioINDev].deviceID);
16231662
engine->setup(audioSampleRate, audioBufferSize, audioNumBuffers);
16241663
engine->sequencer.setTempo(bpm);
16251664

0 commit comments

Comments
 (0)