WinmmMixerPlugin.cxx 2.43 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

Max Kellermann's avatar
Max Kellermann committed
20
#include "mixer/MixerInternal.hxx"
21 22
#include "output/OutputAPI.hxx"
#include "output/plugins/WinmmOutputPlugin.hxx"
23
#include "util/Math.hxx"
24

25 26
#include <mmsystem.h>

27
#include <cassert>
28 29
#include <stdexcept>

30 31
#include <windows.h>

32
class WinmmMixer final : public Mixer {
33
	WinmmOutput &output;
34

35
public:
36 37
	WinmmMixer(WinmmOutput &_output, MixerListener &_listener)
		:Mixer(winmm_mixer_plugin, _listener),
38
		output(_output) {
39
	}
40 41

	/* virtual methods from class Mixer */
42
	void Open() override {
43 44
	}

45
	void Close() noexcept override {
46 47
	}

48 49
	int GetVolume() override;
	void SetVolume(unsigned volume) override;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
};

static inline int
winmm_volume_decode(DWORD volume)
{
	return lround((volume & 0xFFFF) / 655.35);
}

static inline DWORD
winmm_volume_encode(int volume)
{
	int value = lround(volume * 655.35);
	return MAKELONG(value, value);
}

65
static Mixer *
66
winmm_mixer_init(gcc_unused EventLoop &event_loop, AudioOutput &ao,
67
		 MixerListener &listener,
68
		 gcc_unused const ConfigBlock &block)
69
{
70
	return new WinmmMixer((WinmmOutput &)ao, listener);
71 72
}

73
int
74
WinmmMixer::GetVolume()
75 76
{
	DWORD volume;
77
	HWAVEOUT handle = winmm_output_get_handle(output);
78
	MMRESULT result = waveOutGetVolume(handle, &volume);
79

80 81
	if (result != MMSYSERR_NOERROR)
		throw std::runtime_error("Failed to get winmm volume");
82

83 84 85
	return winmm_volume_decode(volume);
}

86 87
void
WinmmMixer::SetVolume(unsigned volume)
88 89
{
	DWORD value = winmm_volume_encode(volume);
90
	HWAVEOUT handle = winmm_output_get_handle(output);
91 92
	MMRESULT result = waveOutSetVolume(handle, value);

93 94
	if (result != MMSYSERR_NOERROR)
		throw std::runtime_error("Failed to set winmm volume");
95 96
}

97
const MixerPlugin winmm_mixer_plugin = {
98 99
	winmm_mixer_init,
	false,
100
};