Commit 6fb683f4 authored by Leander Schulten's avatar Leander Schulten

New Feature: You now can control the system volume (currently only on windows)

parent 61c99267
Pipeline #198788 passed with stage
in 5 minutes and 40 seconds
......@@ -22,6 +22,7 @@ SOURCES += \
audio/aubio/onsetanalysis.cpp \
audio/aubio/tempoanalysis.cpp \
audio/audioeventdata.cpp \
audio/systemvolume.cpp \
dmx/channel.cpp \
gui/audioeventdataview.cpp \
gui/channelprogrammeditor.cpp \
......@@ -106,6 +107,7 @@ HEADERS += \
audio/aubio/onsetanalysis.h \
audio/aubio/tempoanalysis.h \
audio/audioeventdata.h \
audio/systemvolume.h \
dmx/deviceprototype.h \
dmx/channel.h \
gui/linegeometry.h \
......
#include "systemvolume.h"
SystemVolume::SystemVolume() {
#ifdef Q_OS_WIN
HRESULT hr;
hr = CoInitialize(nullptr);
if (hr < 0) {
return;
}
// Get enumerator for audio endpoint devices.
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), reinterpret_cast<void **>(&pEnumerator));
if (hr < 0) {
return;
}
// Get peak meter for default audio-rendering device.
hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
if (hr < 0) {
return;
}
hr = pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, nullptr, reinterpret_cast<void **>(&endpointVolume));
if (hr < 0) {
return;
}
#endif
startTimer(SystemVolumeUpdateRateInMs);
timerEvent(nullptr);
}
void SystemVolume::timerEvent(QTimerEvent * /*event*/) {
#ifdef Q_OS_WIN
if (endpointVolume) {
float vol;
auto hr = endpointVolume->GetMasterVolumeLevelScalar(&vol);
if (hr >= 0 /* success */) {
auto v = static_cast<double>(vol);
if (volume != v) {
volume = v;
emit volumeChanged();
}
}
}
#endif
}
SystemVolume::~SystemVolume() {
if (pEnumerator) {
pEnumerator->Release();
}
if (pDevice) {
pDevice->Release();
}
if (endpointVolume) {
endpointVolume->Release();
}
if (client) {
client->Release();
}
CoUninitialize();
}
void SystemVolume::setVolume(double volume) {
volume = std::clamp(volume, 0., 1.);
if (volume != this->volume) {
this->volume = volume;
emit volumeChanged();
#ifdef Q_OS_WIN
if (endpointVolume) {
endpointVolume->SetMasterVolumeLevelScalar(static_cast<float>(volume), nullptr);
}
#endif
}
}
#ifndef SYSTEMVOLUME_H
#define SYSTEMVOLUME_H
#include <QObject>
#ifdef Q_OS_WIN
// windows code from here: http://www.rohitab.com/discuss/topic/43988-increasedecreasemute-the-master-volume-in-windows/
#include <audioclient.h>
#include <conio.h>
#include <endpointvolume.h>
#include <gdiplus.h>
#include <iostream>
#include <mmdeviceapi.h>
#include <windows.h>
#endif
class SystemVolume : public QObject {
Q_OBJECT
#ifdef Q_OS_WIN
IMMDeviceEnumerator *pEnumerator = nullptr;
IMMDevice *pDevice = nullptr;
IAudioEndpointVolume *endpointVolume = nullptr;
WAVEFORMATEX *wformat = nullptr;
IAudioClient *client = nullptr;
#endif
double volume = -1;
Q_PROPERTY(double volume READ getVolume WRITE setVolume NOTIFY volumeChanged)
SystemVolume();
static constexpr int SystemVolumeUpdateRateInMs = 10'000;
protected:
void timerEvent(QTimerEvent *event) override;
public:
static SystemVolume &get() {
static SystemVolume sv;
return sv;
}
~SystemVolume() override;
[[nodiscard]] double getVolume() const { return volume; }
void setVolume(double volume);
signals:
void volumeChanged();
};
#endif // SYSTEMVOLUME_H
......@@ -12,7 +12,7 @@ Pane{
GridLayout{
anchors.left: parent.left
anchors.right: parent.right
rowSpacing: 10
rowSpacing: 6
columns: 2
Label{
text: "Settings file path:"
......@@ -178,6 +178,7 @@ Pane{
text: "SlideShow:"
}
RowLayout{
Layout.bottomMargin: -10
Button{
enabled: SlideShow.hasImages
text: SlideShow.windowVisibility !== Window.Hidden ? "Hide" : "Show"
......@@ -233,6 +234,30 @@ Pane{
}
}
Label {
text: "System Volume:"
}
RowLayout {
Label {
text: System.volume < 0 ? "Not availible" : ((volumeSlider.value * 100).toFixed(0) + "%")
Layout.preferredWidth: 30
}
Slider {
id: volumeSlider
from: 0
to: 1
Layout.preferredWidth: 180
value: System.volume
onValueChanged: System.volume = value;
enabled: UserManagment.currentUser.havePermission(Permission.CHANGE_SYSTEM_VOLUME) && System.volume >= 0
}
Label {
visible: !UserManagment.currentUser.havePermission(Permission.CHANGE_SYSTEM_VOLUME)
text: "You don't have the permission to change the volume"
}
}
}
Loader {
id: fileDialogLoader
......
......@@ -80,6 +80,7 @@ public:
CHANGE_MODULE_SETTINGS = 26,
MODIFY_THEME = 27,
SAVE_SLIDE_SHOW_SETTINGS = 28,
CHANGE_SYSTEM_VOLUME = 29,
};
Q_ENUM(Permission)
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment