Browse Source

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
Raidho 8 years ago
parent
commit
3de68707d1

+ 2 - 13
src/modules/audio/Audio.h

@@ -190,20 +190,9 @@ public:
 	virtual float getDopplerScale() const = 0;
 
 	/**
-	 * @return Number of recording devices.
+	 * @return Reference to a vector of pointers to recording devices. May be empty.
 	 **/
-	virtual int getRecordingDeviceCount() const = 0;
-
-	/**
-	 * @param index Index number of recording device. 0 is default device.
-	 * @return Selected recording device.
-	 **/
-	virtual RecordingDevice *getRecordingDevice(int index) const = 0;
-
-	/**
-	 * @return Index number of a recording device, -1 if not present.
-	 **/
-	virtual int getRecordingDeviceIndex(RecordingDevice *device) const = 0;
+	virtual std::vector<RecordingDevice*> *getRecordingDevices() = 0;
 
 	/**
 	 * Gets the distance model used for attenuation.

+ 2 - 12
src/modules/audio/null/Audio.cpp

@@ -144,19 +144,9 @@ float Audio::getDopplerScale() const
 	return 1.0f;
 }
 
-int Audio::getRecordingDeviceCount() const
+std::vector<love::audio::RecordingDevice*> *Audio::getRecordingDevices()
 {
-	return 0;
-}
-
-love::audio::RecordingDevice *Audio::getRecordingDevice(int index) const
-{
-	return nullptr;
-}
-
-int Audio::getRecordingDeviceIndex(love::audio::RecordingDevice *device) const
-{
-	return -1;
+	return &capture;
 }
 
 Audio::DistanceModel Audio::getDistanceModel() const

+ 2 - 3
src/modules/audio/null/Audio.h

@@ -71,9 +71,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);
@@ -81,6 +79,7 @@ public:
 private:
 	float volume;
 	DistanceModel distanceModel;
+	std::vector<love::audio::RecordingDevice*> capture;
 
 }; // Audio
 

+ 32 - 40
src/modules/audio/openal/Audio.cpp

@@ -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
-{
-	return capture.size();
-}
-
-love::audio::RecordingDevice *Audio::getRecordingDevice(int index) const
+std::vector<love::audio::RecordingDevice*> *Audio::getRecordingDevices()
 {
-	if (index < 0 || (unsigned int)index >= capture.size())
-		return nullptr;
-
-	return capture[index];
-}
+	if (capture.size() == 0)
+	{
+		std::string defaultname(alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER));
 
-int Audio::getRecordingDeviceIndex(love::audio::RecordingDevice *device) const
-{
-	for (unsigned int i = 0; i < capture.size(); i++)
-		if (device == capture[i])
-			return i;
+		//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 -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
src/modules/audio/openal/Audio.h

@@ -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
src/modules/audio/openal/RecordingDevice.cpp

@@ -155,7 +155,7 @@ int RecordingDevice::getID() const
 
 bool RecordingDevice::isRecording() const
 {
-	return device == nullptr ? false : true;
+	return device != nullptr;
 }
 
 } //openal

+ 5 - 12
src/modules/audio/wrap_Audio.cpp

@@ -277,21 +277,15 @@ int w_getDistanceModel(lua_State *L)
 	return 1;
 }
 
-int w_getRecordingDeviceCount(lua_State *L)
-{
-	lua_pushnumber(L, instance()->getRecordingDeviceCount());
-	return 1;
-}
-
 int w_getRecordingDevices(lua_State *L)
 {
-	int count = instance()->getRecordingDeviceCount();
-	lua_createtable(L, count, 0);
+	std::vector<RecordingDevice*> *devices = instance()->getRecordingDevices();
+
+	lua_createtable(L, (*devices).size(), 0);
 
-	for (int i = 0; i < count; i++)
+	for (unsigned int i = 0; i < (*devices).size(); i++)
 	{
-		RecordingDevice *device = instance()->getRecordingDevice(i);
-		luax_pushtype(L, AUDIO_RECORDING_DEVICE_ID, device);
+		luax_pushtype(L, AUDIO_RECORDING_DEVICE_ID, (*devices)[i]);
 		lua_rawseti(L, -2, i + 1);
 	}
 
@@ -319,7 +313,6 @@ static const luaL_Reg functions[] =
 	{ "getDopplerScale", w_getDopplerScale },
 	{ "setDistanceModel", w_setDistanceModel },
 	{ "getDistanceModel", w_getDistanceModel },
-	{ "getRecordingDeviceCount", w_getRecordingDeviceCount },
 	{ "getRecordingDevices", w_getRecordingDevices },
 	{ 0, 0 }
 };