made device enumeration lazy

made enumerator to first attempt aquire default device name by standard means,
then to fall back to read name from opened device
getRecordingDeviceCount removed for being redundant

--HG--
branch : minor-mic-input
This commit is contained in:
Raidho
2016-10-25 02:05:53 +03:00
parent 424d2704ec
commit 3de68707d1
7 changed files with 45 additions and 84 deletions
+32 -40
View File
@@ -110,29 +110,6 @@ Audio::Audio()
if (!alcMakeContextCurrent(context) || alcGetError(device) != ALC_NO_ERROR)
throw love::Exception("Could not make context current.");
//find and push default capture device
//AL may not return actual device name string when asked directly
std::string defaultname;
ALCdevice *defaultdevice = alcCaptureOpenDevice(NULL, 8000, 8, 1);
if (alGetError() == AL_NO_ERROR)
{
defaultname = alcGetString(defaultdevice, ALC_CAPTURE_DEVICE_SPECIFIER);
alcCaptureCloseDevice(defaultdevice);
capture.push_back(new RecordingDevice(defaultname.c_str(), 0));
}
const ALCchar *devstr = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
size_t offset = 0;
while (true)
{
if (devstr[offset] == '\0')
break;
std::string str((ALCchar*)&devstr[offset]);
if (str != defaultname)
capture.push_back(new RecordingDevice(str.c_str(), capture.size()));
offset += str.length() + 1;
}
// pool must be allocated after AL context.
try
{
@@ -336,26 +313,41 @@ void Audio::setDistanceModel(DistanceModel distanceModel)
}
}
int Audio::getRecordingDeviceCount() const
std::vector<love::audio::RecordingDevice*> *Audio::getRecordingDevices()
{
return capture.size();
}
if (capture.size() == 0)
{
std::string defaultname(alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER));
love::audio::RecordingDevice *Audio::getRecordingDevice(int index) const
{
if (index < 0 || (unsigned int)index >= capture.size())
return nullptr;
//no device name obtained from AL, fallback to reading from device
if (defaultname.length() == 0)
{
//use some safe basic parameters - 8 kHz, 8 bits, 1 channel
ALCdevice *defaultdevice = alcCaptureOpenDevice(NULL, 8000, 8, 1);
if (alGetError() == AL_NO_ERROR)
{
defaultname = alcGetString(defaultdevice, ALC_CAPTURE_DEVICE_SPECIFIER);
alcCaptureCloseDevice(defaultdevice);
}
else
//failed to open default recording device - bail, return empty list
return &capture;
}
capture.push_back(new RecordingDevice(defaultname.c_str(), 0));
return capture[index];
}
int Audio::getRecordingDeviceIndex(love::audio::RecordingDevice *device) const
{
for (unsigned int i = 0; i < capture.size(); i++)
if (device == capture[i])
return i;
return -1;
const ALCchar *devstr = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
size_t offset = 0;
while (true)
{
if (devstr[offset] == '\0')
break;
std::string str((ALCchar*)&devstr[offset]);
if (str != defaultname)
capture.push_back(new RecordingDevice(str.c_str(), capture.size()));
offset += str.length() + 1;
}
}
return &capture;
}
} // openal
+1 -3
View File
@@ -105,9 +105,7 @@ public:
void setDopplerScale(float scale);
float getDopplerScale() const;
int getRecordingDeviceCount() const;
love::audio::RecordingDevice *getRecordingDevice(int index) const;
int getRecordingDeviceIndex(love::audio::RecordingDevice *device) const;
std::vector<love::audio::RecordingDevice*> *getRecordingDevices();
DistanceModel getDistanceModel() const;
void setDistanceModel(DistanceModel distanceModel);
+1 -1
View File
@@ -155,7 +155,7 @@ int RecordingDevice::getID() const
bool RecordingDevice::isRecording() const
{
return device == nullptr ? false : true;
return device != nullptr;
}
} //openal