WinmmMixerPlugin.cxx 2.74 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */

#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "mixer/MixerInternal.hxx"
22 23
#include "output/OutputAPI.hxx"
#include "output/plugins/WinmmOutputPlugin.hxx"
24 25
#include "util/Error.hxx"
#include "util/Domain.hxx"
26

27 28
#include <mmsystem.h>

29 30 31 32
#include <assert.h>
#include <math.h>
#include <windows.h>

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

36
public:
37 38
	WinmmMixer(WinmmOutput &_output, MixerListener &_listener)
		:Mixer(winmm_mixer_plugin, _listener),
39
		output(_output) {
40
	}
41 42 43 44 45 46 47 48 49 50 51

	/* virtual methods from class Mixer */
	virtual bool Open(gcc_unused Error &error) override {
		return true;
	}

	virtual void Close() override {
	}

	virtual int GetVolume(Error &error) override;
	virtual bool SetVolume(unsigned volume, Error &error) override;
52 53
};

54
static constexpr Domain winmm_mixer_domain("winmm_mixer");
55 56 57 58 59 60 61 62 63 64 65 66 67 68

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);
}

69
static Mixer *
70
winmm_mixer_init(gcc_unused EventLoop &event_loop, AudioOutput &ao,
71
		 MixerListener &listener,
72
		 gcc_unused const config_param &param,
73
		 gcc_unused Error &error)
74
{
75
	return new WinmmMixer((WinmmOutput &)ao, listener);
76 77
}

78 79
int
WinmmMixer::GetVolume(Error &error)
80 81
{
	DWORD volume;
82
	HWAVEOUT handle = winmm_output_get_handle(output);
83
	MMRESULT result = waveOutGetVolume(handle, &volume);
84

85
	if (result != MMSYSERR_NOERROR) {
86
		error.Set(winmm_mixer_domain, "Failed to get winmm volume");
87 88
		return -1;
	}
89

90 91 92
	return winmm_volume_decode(volume);
}

93 94
bool
WinmmMixer::SetVolume(unsigned volume, Error &error)
95 96
{
	DWORD value = winmm_volume_encode(volume);
97
	HWAVEOUT handle = winmm_output_get_handle(output);
98 99 100
	MMRESULT result = waveOutSetVolume(handle, value);

	if (result != MMSYSERR_NOERROR) {
101
		error.Set(winmm_mixer_domain, "Failed to set winmm volume");
102 103
		return false;
	}
104

105 106 107
	return true;
}

108
const MixerPlugin winmm_mixer_plugin = {
109 110
	winmm_mixer_init,
	false,
111
};