2020-09-16 20:14:35 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-23 18:07:00 +00:00
|
|
|
//
|
2020-09-16 20:14:35 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-23 18:07:00 +00:00
|
|
|
// VolumeControl.cpp
|
|
|
|
//
|
|
|
|
// Controls system audio volume.
|
|
|
|
//
|
|
|
|
|
2014-06-25 16:29:58 +00:00
|
|
|
#include "VolumeControl.h"
|
|
|
|
|
2017-11-13 22:16:38 +00:00
|
|
|
#include "math/Misc.h"
|
2014-06-25 16:29:58 +00:00
|
|
|
#include "Log.h"
|
2020-07-09 17:24:20 +00:00
|
|
|
|
2020-08-17 17:15:05 +00:00
|
|
|
#if defined(_RPI_)
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "Settings.h"
|
2020-07-09 17:24:20 +00:00
|
|
|
#endif
|
2020-07-03 18:23:51 +00:00
|
|
|
|
2020-07-09 17:24:20 +00:00
|
|
|
// The ALSA Audio Card and Audio Device selection code is disabled at the moment.
|
|
|
|
// As PulseAudio controls the sound devices for the desktop environment, it doesn't
|
|
|
|
// make much sense to be able to select ALSA devices directly. Normally (always?)
|
|
|
|
// the selection doesn't make any difference at all. But maybe some PulseAudio
|
|
|
|
// settings could be added later on, if needed.
|
|
|
|
// The code is still active for Raspberry Pi though as I'm not sure if this is
|
|
|
|
// useful for that device.
|
|
|
|
// Keeping mixerName and mixerCard at their default values should make sure that
|
|
|
|
// the rest of the volume control code in here compiles and works fine.
|
2014-06-25 16:29:58 +00:00
|
|
|
#if defined(__linux__)
|
2020-06-23 18:07:00 +00:00
|
|
|
#if defined(_RPI_) || defined(_VERO4K_)
|
2020-12-16 23:09:26 +00:00
|
|
|
std::string VolumeControl::mixerName = "PCM";
|
2020-06-23 18:07:00 +00:00
|
|
|
#else
|
2020-12-16 23:09:26 +00:00
|
|
|
std::string VolumeControl::mixerName = "Master";
|
2020-06-23 18:07:00 +00:00
|
|
|
#endif
|
2020-12-16 23:09:26 +00:00
|
|
|
std::string VolumeControl::mixerCard = "default";
|
2014-06-25 16:29:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
std::weak_ptr<VolumeControl> VolumeControl::sInstance;
|
|
|
|
|
|
|
|
VolumeControl::VolumeControl()
|
2020-06-23 18:07:00 +00:00
|
|
|
: originalVolume(0),
|
|
|
|
internalVolume(0)
|
2020-08-17 17:15:05 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
// #error TODO: Not implemented for MacOS yet!!!
|
2020-06-23 18:07:00 +00:00
|
|
|
#elif defined(__linux__)
|
|
|
|
, mixerIndex(0),
|
|
|
|
mixerHandle(nullptr),
|
|
|
|
mixerElem(nullptr),
|
|
|
|
mixerSelemId(nullptr)
|
2020-07-03 18:23:51 +00:00
|
|
|
#elif defined(_WIN64)
|
2020-06-23 18:07:00 +00:00
|
|
|
, mixerHandle(nullptr),
|
|
|
|
endpointVolume(nullptr)
|
|
|
|
#endif
|
2014-06-25 16:29:58 +00:00
|
|
|
{
|
2020-06-23 18:07:00 +00:00
|
|
|
init();
|
2014-06-25 16:29:58 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
// Get original volume levels for system.
|
|
|
|
originalVolume = getVolume();
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
VolumeControl::VolumeControl(
|
2020-12-20 23:23:22 +00:00
|
|
|
const VolumeControl& right):
|
2020-06-23 18:07:00 +00:00
|
|
|
originalVolume(0),
|
|
|
|
internalVolume(0)
|
2020-08-17 17:15:05 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
// #error TODO: Not implemented for MacOS yet!!!
|
2020-06-23 18:07:00 +00:00
|
|
|
#elif defined(__linux__)
|
|
|
|
, mixerIndex(0),
|
|
|
|
mixerHandle(nullptr),
|
|
|
|
mixerElem(nullptr),
|
|
|
|
mixerSelemId(nullptr)
|
2020-07-03 18:23:51 +00:00
|
|
|
#elif defined(_WIN64)
|
2020-06-23 18:07:00 +00:00
|
|
|
, mixerHandle(nullptr),
|
|
|
|
endpointVolume(nullptr)
|
|
|
|
#endif
|
2014-06-25 16:29:58 +00:00
|
|
|
{
|
2020-12-20 23:23:22 +00:00
|
|
|
static_cast<void>(right);
|
2020-06-23 18:07:00 +00:00
|
|
|
sInstance = right.sInstance;
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
VolumeControl & VolumeControl::operator=(const VolumeControl& right)
|
2014-06-25 16:29:58 +00:00
|
|
|
{
|
2020-06-23 18:07:00 +00:00
|
|
|
if (this != &right)
|
|
|
|
sInstance = right.sInstance;
|
2014-06-25 16:29:58 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
return *this;
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VolumeControl::~VolumeControl()
|
|
|
|
{
|
2020-06-23 18:07:00 +00:00
|
|
|
// Set original volume levels for system.
|
|
|
|
//setVolume(originalVolume);
|
2014-06-25 16:29:58 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
deinit();
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 23:23:22 +00:00
|
|
|
std::shared_ptr<VolumeControl>& VolumeControl::getInstance()
|
2014-06-25 16:29:58 +00:00
|
|
|
{
|
2020-06-23 18:07:00 +00:00
|
|
|
// Check if an VolumeControl instance is already created, if not create one.
|
|
|
|
static std::shared_ptr<VolumeControl> sharedInstance = sInstance.lock();
|
|
|
|
if (sharedInstance == nullptr) {
|
|
|
|
sharedInstance.reset(new VolumeControl);
|
|
|
|
sInstance = sharedInstance;
|
|
|
|
}
|
|
|
|
return sharedInstance;
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeControl::init()
|
|
|
|
{
|
2020-06-23 18:07:00 +00:00
|
|
|
// Initialize audio mixer interface.
|
2020-08-17 17:15:05 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
// #error TODO: Not implemented for MacOS yet!!!
|
2020-06-23 18:07:00 +00:00
|
|
|
#elif defined(__linux__)
|
|
|
|
// Try to open mixer device.
|
|
|
|
if (mixerHandle == nullptr) {
|
|
|
|
// Allow user to override the AudioCard and AudioDevice in es_settings.cfg.
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_RPI_)
|
2020-12-16 22:59:00 +00:00
|
|
|
mixerCard = Settings::getInstance()->getString("AudioCard");
|
|
|
|
mixerName = Settings::getInstance()->getString("AudioDevice");
|
2020-07-09 17:24:20 +00:00
|
|
|
#endif
|
2017-06-02 15:58:44 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
snd_mixer_selem_id_alloca(&mixerSelemId);
|
|
|
|
// Sets simple-mixer index and name.
|
|
|
|
snd_mixer_selem_id_set_index(mixerSelemId, mixerIndex);
|
2020-12-16 22:59:00 +00:00
|
|
|
snd_mixer_selem_id_set_name(mixerSelemId, mixerName.c_str());
|
2020-06-23 18:07:00 +00:00
|
|
|
// Open mixer.
|
|
|
|
if (snd_mixer_open(&mixerHandle, 0) >= 0) {
|
|
|
|
LOG(LogDebug) << "VolumeControl::init() - Opened ALSA mixer";
|
|
|
|
// Ok, attach to defualt card.
|
2020-12-16 22:59:00 +00:00
|
|
|
if (snd_mixer_attach(mixerHandle, mixerCard.c_str()) >= 0) {
|
2020-06-23 18:07:00 +00:00
|
|
|
LOG(LogDebug) << "VolumeControl::init() - Attached to default card";
|
|
|
|
// Ok, register simple element class.
|
|
|
|
if (snd_mixer_selem_register(mixerHandle, nullptr, nullptr) >= 0) {
|
|
|
|
LOG(LogDebug) << "VolumeControl::init() - Registered simple element class";
|
|
|
|
// Ok, load registered elements.
|
|
|
|
if (snd_mixer_load(mixerHandle) >= 0) {
|
|
|
|
LOG(LogDebug) << "VolumeControl::init() - Loaded mixer elements";
|
|
|
|
// Ok, find elements now.
|
|
|
|
mixerElem = snd_mixer_find_selem(mixerHandle, mixerSelemId);
|
|
|
|
if (mixerElem != nullptr) {
|
|
|
|
// Wohoo. good to go...
|
|
|
|
LOG(LogDebug) << "VolumeControl::init() - Mixer initialized";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) <<
|
|
|
|
"VolumeControl::init() - Failed to find mixer elements!";
|
|
|
|
snd_mixer_close(mixerHandle);
|
|
|
|
mixerHandle = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "VolumeControl::init() - Failed to load mixer elements!";
|
|
|
|
snd_mixer_close(mixerHandle);
|
|
|
|
mixerHandle = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) <<
|
|
|
|
"VolumeControl::init() - Failed to register simple element class!";
|
|
|
|
snd_mixer_close(mixerHandle);
|
|
|
|
mixerHandle = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "VolumeControl::init() - Failed to attach to default card!";
|
|
|
|
snd_mixer_close(mixerHandle);
|
|
|
|
mixerHandle = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "VolumeControl::init() - Failed to open ALSA mixer!";
|
|
|
|
}
|
|
|
|
}
|
2020-07-03 18:23:51 +00:00
|
|
|
#elif defined(_WIN64)
|
2020-06-23 18:07:00 +00:00
|
|
|
// Get windows version information.
|
2020-09-16 20:14:35 +00:00
|
|
|
OSVERSIONINFOEXA osVer = { sizeof(OSVERSIONINFO) };
|
2020-06-23 18:07:00 +00:00
|
|
|
::GetVersionExA(reinterpret_cast<LPOSVERSIONINFOA>(&osVer));
|
|
|
|
// Check windows version.
|
|
|
|
if (osVer.dwMajorVersion < 6) {
|
|
|
|
// Windows older than Vista. use mixer API. open default mixer.
|
|
|
|
if (mixerHandle == nullptr) {
|
2020-07-03 18:23:51 +00:00
|
|
|
#if defined(_WIN64)
|
2020-09-16 20:14:35 +00:00
|
|
|
if (mixerOpen(&mixerHandle, 0, reinterpret_cast<DWORD_PTR>(nullptr), 0, 0) ==
|
|
|
|
MMSYSERR_NOERROR) {
|
2020-07-03 18:23:51 +00:00
|
|
|
#else
|
2020-06-23 18:07:00 +00:00
|
|
|
if (mixerOpen(&mixerHandle, 0, nullptr, 0, 0) == MMSYSERR_NOERROR) {
|
2020-07-03 18:23:51 +00:00
|
|
|
#endif
|
2020-06-23 18:07:00 +00:00
|
|
|
// Retrieve info on the volume slider control for the "Speaker Out" line.
|
|
|
|
MIXERLINECONTROLS mixerLineControls;
|
|
|
|
mixerLineControls.cbStruct = sizeof(MIXERLINECONTROLS);
|
|
|
|
mixerLineControls.dwLineID = 0xFFFF0000; // Id of "Speaker Out" line.
|
|
|
|
mixerLineControls.cControls = 1;
|
|
|
|
// Id of "Speaker Out" line's volume slider.
|
|
|
|
//mixerLineControls.dwControlID = 0x00000000;
|
2020-09-16 20:14:35 +00:00
|
|
|
// Get volume control.
|
2020-06-23 18:07:00 +00:00
|
|
|
mixerLineControls.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
|
|
|
|
mixerLineControls.pamxctrl = &mixerControl;
|
|
|
|
mixerLineControls.cbmxctrl = sizeof(MIXERCONTROL);
|
2020-09-16 20:14:35 +00:00
|
|
|
if (mixerGetLineControls(reinterpret_cast<HMIXEROBJ>(mixerHandle),
|
|
|
|
&mixerLineControls, MIXER_GETLINECONTROLSF_ONEBYTYPE) !=
|
|
|
|
MMSYSERR_NOERROR) {
|
2020-06-23 18:07:00 +00:00
|
|
|
LOG(LogError) <<
|
|
|
|
"VolumeControl::getVolume() - Failed to get mixer volume control!";
|
|
|
|
mixerClose(mixerHandle);
|
|
|
|
mixerHandle = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "VolumeControl::init() - Failed to open mixer!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Windows Vista or above. use EndpointVolume API. get device enumerator.
|
|
|
|
if (endpointVolume == nullptr) {
|
|
|
|
CoInitialize(nullptr);
|
|
|
|
IMMDeviceEnumerator * deviceEnumerator = nullptr;
|
|
|
|
CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER,
|
2020-09-16 20:14:35 +00:00
|
|
|
__uuidof(IMMDeviceEnumerator), reinterpret_cast<LPVOID *>(&deviceEnumerator));
|
2020-06-23 18:07:00 +00:00
|
|
|
if (deviceEnumerator != nullptr) {
|
|
|
|
// Get default endpoint.
|
|
|
|
IMMDevice * defaultDevice = nullptr;
|
|
|
|
deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
|
|
|
|
if (defaultDevice != nullptr) {
|
|
|
|
// Retrieve endpoint volume.
|
|
|
|
defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
|
2020-09-16 20:14:35 +00:00
|
|
|
CLSCTX_INPROC_SERVER, nullptr,
|
|
|
|
reinterpret_cast<LPVOID *>(&endpointVolume));
|
2020-06-23 18:07:00 +00:00
|
|
|
if (endpointVolume == nullptr)
|
|
|
|
LOG(LogError) << "VolumeControl::init() - "
|
|
|
|
"Failed to get default audio endpoint volume!";
|
|
|
|
// Release default device. we don't need it anymore.
|
|
|
|
defaultDevice->Release();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) <<
|
|
|
|
"VolumeControl::init() - Failed to get default audio endpoint!";
|
|
|
|
}
|
|
|
|
// Release device enumerator. we don't need it anymore.
|
|
|
|
deviceEnumerator->Release();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "VolumeControl::init() - Failed to get audio endpoint enumerator!";
|
|
|
|
CoUninitialize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeControl::deinit()
|
|
|
|
{
|
2020-06-23 18:07:00 +00:00
|
|
|
// Deinitialize audio mixer interface.
|
2020-08-17 17:15:05 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
// #error TODO: Not implemented for MacOS yet!!!
|
2020-06-23 18:07:00 +00:00
|
|
|
#elif defined(__linux__)
|
|
|
|
if (mixerHandle != nullptr) {
|
2020-12-16 22:59:00 +00:00
|
|
|
snd_mixer_detach(mixerHandle, mixerCard.c_str());
|
2020-06-23 18:07:00 +00:00
|
|
|
snd_mixer_free(mixerHandle);
|
|
|
|
snd_mixer_close(mixerHandle);
|
|
|
|
mixerHandle = nullptr;
|
|
|
|
mixerElem = nullptr;
|
|
|
|
}
|
2020-07-03 18:23:51 +00:00
|
|
|
#elif defined(_WIN64)
|
2020-06-23 18:07:00 +00:00
|
|
|
if (mixerHandle != nullptr) {
|
|
|
|
mixerClose(mixerHandle);
|
|
|
|
mixerHandle = nullptr;
|
|
|
|
}
|
|
|
|
else if (endpointVolume != nullptr) {
|
|
|
|
endpointVolume->Release();
|
|
|
|
endpointVolume = nullptr;
|
|
|
|
CoUninitialize();
|
|
|
|
}
|
|
|
|
#endif
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeControl::getVolume() const
|
|
|
|
{
|
2020-06-23 18:07:00 +00:00
|
|
|
int volume = 0;
|
2014-06-25 16:29:58 +00:00
|
|
|
|
2020-08-17 17:15:05 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
// #error TODO: Not implemented for MacOS yet!!!
|
2020-06-23 18:07:00 +00:00
|
|
|
#elif defined(__linux__)
|
|
|
|
if (mixerElem != nullptr) {
|
|
|
|
// Get volume range.
|
|
|
|
long minVolume;
|
|
|
|
long maxVolume;
|
|
|
|
if (snd_mixer_selem_get_playback_volume_range(mixerElem, &minVolume, &maxVolume) == 0) {
|
|
|
|
// Ok, now get volume.
|
|
|
|
long rawVolume;
|
|
|
|
if (snd_mixer_selem_get_playback_volume(mixerElem,
|
|
|
|
SND_MIXER_SCHN_MONO, &rawVolume) == 0) {
|
|
|
|
// Worked. bring into range 0-100.
|
|
|
|
rawVolume -= minVolume;
|
|
|
|
if (rawVolume > 0)
|
|
|
|
volume = (rawVolume * 100.0) / (maxVolume - minVolume) + 0.5;
|
|
|
|
//else
|
|
|
|
// volume = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "VolumeControl::getVolume() - Failed to get mixer volume!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "VolumeControl::getVolume() - Failed to get volume range!";
|
|
|
|
}
|
|
|
|
}
|
2020-07-03 18:23:51 +00:00
|
|
|
#elif defined(_WIN64)
|
2020-06-23 18:07:00 +00:00
|
|
|
if (mixerHandle != nullptr) {
|
|
|
|
// Windows older than Vista. use mixer API. get volume from line control.
|
|
|
|
MIXERCONTROLDETAILS_UNSIGNED value;
|
|
|
|
MIXERCONTROLDETAILS mixerControlDetails;
|
|
|
|
mixerControlDetails.cbStruct = sizeof(MIXERCONTROLDETAILS);
|
|
|
|
mixerControlDetails.dwControlID = mixerControl.dwControlID;
|
|
|
|
// Always 1 for a MIXERCONTROL_CONTROLF_UNIFORM control.
|
|
|
|
mixerControlDetails.cChannels = 1;
|
|
|
|
// Always 0 except for a MIXERCONTROL_CONTROLF_MULTIPLE control.
|
|
|
|
mixerControlDetails.cMultipleItems = 0;
|
|
|
|
mixerControlDetails.paDetails = &value;
|
|
|
|
mixerControlDetails.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
|
2020-09-16 20:14:35 +00:00
|
|
|
if (mixerGetControlDetails(reinterpret_cast<HMIXEROBJ>(mixerHandle), &mixerControlDetails,
|
2020-06-23 18:07:00 +00:00
|
|
|
MIXER_GETCONTROLDETAILSF_VALUE) == MMSYSERR_NOERROR)
|
2020-09-16 20:14:35 +00:00
|
|
|
volume = static_cast<int>(Math::round((value.dwValue * 100) / 65535.0f));
|
2020-06-23 18:07:00 +00:00
|
|
|
else
|
|
|
|
LOG(LogError) << "VolumeControl::getVolume() - Failed to get mixer volume!";
|
|
|
|
}
|
|
|
|
else if (endpointVolume != nullptr) {
|
|
|
|
// Windows Vista or above. use EndpointVolume API.
|
|
|
|
float floatVolume = 0.0f; // 0-1
|
|
|
|
if (endpointVolume->GetMasterVolumeLevelScalar(&floatVolume) == S_OK) {
|
2020-09-16 20:14:35 +00:00
|
|
|
volume = static_cast<int>(Math::round(floatVolume * 100.0f));
|
2020-07-09 17:24:20 +00:00
|
|
|
LOG(LogInfo) << "System audio volume is " << volume;
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "VolumeControl::getVolume() - Failed to get master volume!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
// Clamp to 0-100 range.
|
|
|
|
if (volume < 0)
|
|
|
|
volume = 0;
|
|
|
|
if (volume > 100)
|
|
|
|
volume = 100;
|
|
|
|
return volume;
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeControl::setVolume(int volume)
|
|
|
|
{
|
2020-06-23 18:07:00 +00:00
|
|
|
// Clamp to 0-100 range.
|
|
|
|
if (volume < 0)
|
|
|
|
volume = 0;
|
|
|
|
if (volume > 100)
|
|
|
|
volume = 100;
|
|
|
|
|
|
|
|
// Store values in internal variables.
|
|
|
|
internalVolume = volume;
|
2020-08-17 17:15:05 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
// #error TODO: Not implemented for MacOS yet!!!
|
2020-06-23 18:07:00 +00:00
|
|
|
#elif defined(__linux__)
|
|
|
|
if (mixerElem != nullptr) {
|
|
|
|
// Get volume range.
|
|
|
|
long minVolume;
|
|
|
|
long maxVolume;
|
|
|
|
if (snd_mixer_selem_get_playback_volume_range(mixerElem, &minVolume, &maxVolume) == 0) {
|
|
|
|
// Ok, bring into minVolume-maxVolume range and set.
|
|
|
|
long rawVolume = (volume * (maxVolume - minVolume) / 100) + minVolume;
|
|
|
|
if (snd_mixer_selem_set_playback_volume(mixerElem,
|
|
|
|
SND_MIXER_SCHN_FRONT_LEFT, rawVolume) < 0 ||
|
|
|
|
snd_mixer_selem_set_playback_volume(mixerElem,
|
|
|
|
SND_MIXER_SCHN_FRONT_RIGHT, rawVolume) < 0) {
|
|
|
|
LOG(LogError) << "VolumeControl::getVolume() - Failed to set mixer volume!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "VolumeControl::getVolume() - Failed to get volume range!";
|
|
|
|
}
|
|
|
|
}
|
2020-07-03 18:23:51 +00:00
|
|
|
#elif defined(_WIN64)
|
2020-06-23 18:07:00 +00:00
|
|
|
if (mixerHandle != nullptr) {
|
|
|
|
// Windows older than Vista. use mixer API. get volume from line control.
|
|
|
|
MIXERCONTROLDETAILS_UNSIGNED value;
|
|
|
|
value.dwValue = (volume * 65535) / 100;
|
|
|
|
MIXERCONTROLDETAILS mixerControlDetails;
|
|
|
|
mixerControlDetails.cbStruct = sizeof(MIXERCONTROLDETAILS);
|
|
|
|
mixerControlDetails.dwControlID = mixerControl.dwControlID;
|
|
|
|
// Always 1 for a MIXERCONTROL_CONTROLF_UNIFORM control.
|
|
|
|
mixerControlDetails.cChannels = 1;
|
|
|
|
// Always 0 except for a MIXERCONTROL_CONTROLF_MULTIPLE control.
|
|
|
|
mixerControlDetails.cMultipleItems = 0;
|
|
|
|
mixerControlDetails.paDetails = &value;
|
|
|
|
mixerControlDetails.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
|
2020-09-16 20:14:35 +00:00
|
|
|
if (mixerSetControlDetails(reinterpret_cast<HMIXEROBJ>(mixerHandle), &mixerControlDetails,
|
2020-06-23 18:07:00 +00:00
|
|
|
MIXER_SETCONTROLDETAILSF_VALUE) != MMSYSERR_NOERROR)
|
|
|
|
LOG(LogError) << "VolumeControl::setVolume() - Failed to set mixer volume!";
|
|
|
|
}
|
|
|
|
else if (endpointVolume != nullptr) {
|
|
|
|
// Windows Vista or above. use EndpointVolume API.
|
|
|
|
float floatVolume = 0.0f; // 0-1
|
|
|
|
if (volume > 0)
|
2020-09-16 20:14:35 +00:00
|
|
|
floatVolume = static_cast<float>(volume) / 100.0f;
|
2020-06-23 18:07:00 +00:00
|
|
|
if (endpointVolume->SetMasterVolumeLevelScalar(floatVolume, nullptr) != S_OK)
|
|
|
|
LOG(LogError) << "VolumeControl::setVolume() - Failed to set master volume!";
|
|
|
|
}
|
|
|
|
#endif
|
2014-11-23 17:10:38 +00:00
|
|
|
}
|