systemvolume.h 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#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

17 18 19 20
#ifdef Q_OS_MAC
#include <CoreAudio/CoreAudio.h>
#endif

21 22 23 24
#ifdef Q_OS_LINUX
#include <alsa/asoundlib.h>
#endif

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#ifdef Q_OS_WIN
class Callback : public IAudioEndpointVolumeCallback {

    ULONG counter = 0;
    HRESULT OnNotify(AUDIO_VOLUME_NOTIFICATION_DATA *pNotify) override;

public:
    ~Callback();
    HRESULT QueryInterface(REFIID /*riid*/, void ** /*ppvObject*/) override;
    ULONG AddRef() override { return ++counter; }
    ULONG Release() override { return --counter; }
};

#endif

40 41 42 43 44 45 46 47
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;
48
    Callback callback;
49 50 51
#endif
#ifdef Q_OS_MAC
    AudioDeviceID defaultOutputDeviceID = kAudioDeviceUnknown;
52 53 54 55 56 57 58 59 60 61 62
#endif
#ifdef Q_OS_LINUX
    // code from here: https://stackoverflow.com/questions/7657624/get-master-sound-volume-in-c-in-linux
    snd_mixer_t *handle = nullptr;
    snd_mixer_elem_t *elem = nullptr;
    snd_mixer_selem_id_t *sid = nullptr;
    long minv, maxv;

    static constexpr const char *mix_name = "Master";
    static constexpr const char *card = "default";
    static constexpr int mix_index = 0;
63 64 65 66 67 68
#endif
    double volume = -1;
    Q_PROPERTY(double volume READ getVolume WRITE setVolume NOTIFY volumeChanged)

    SystemVolume();

69 70 71 72 73 74 75 76
#ifdef Q_OS_LINUX
protected:
    void timerEvent(QTimerEvent *event) override;

private:
    static int snd_mixer_elem_callback(snd_mixer_elem_t *elem, unsigned int mask);
#endif

77 78 79 80 81 82 83 84 85 86 87 88 89
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