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

20
#include "config.h"
21
#include "Volume.hxx"
22
#include "Silence.hxx"
23
#include "Traits.hxx"
24
#include "util/ConstBuffer.hxx"
25
#include "util/WritableBuffer.hxx"
26
#include "util/RuntimeError.hxx"
27

28 29
#include "PcmDither.cxx" // including the .cxx file to get inlined templates

30
#include <assert.h>
31 32 33
#include <stdint.h>
#include <string.h>

34 35
template<SampleFormat F, class Traits=SampleTraits<F>>
static inline typename Traits::value_type
36 37
pcm_volume_sample(PcmDither &dither,
		  typename Traits::value_type _sample,
Max Kellermann's avatar
Max Kellermann committed
38
		  int volume) noexcept
39 40 41
{
	typename Traits::long_type sample(_sample);

42 43 44
	return dither.DitherShift<typename Traits::long_type,
				  Traits::BITS + PCM_VOLUME_BITS,
				  Traits::BITS>(sample * volume);
45 46
}

47
template<SampleFormat F, class Traits=SampleTraits<F>>
48
static void
49 50
pcm_volume_change(PcmDither &dither,
		  typename Traits::pointer_type dest,
51
		  typename Traits::const_pointer_type src,
52
		  size_t n,
Max Kellermann's avatar
Max Kellermann committed
53
		  int volume) noexcept
54
{
55 56
	for (size_t i = 0; i != n; ++i)
		dest[i] = pcm_volume_sample<F, Traits>(dither, src[i], volume);
57 58 59
}

static void
60
pcm_volume_change_8(PcmDither &dither,
61
		    int8_t *dest, const int8_t *src, size_t n,
Max Kellermann's avatar
Max Kellermann committed
62
		    int volume) noexcept
63
{
64
	pcm_volume_change<SampleFormat::S8>(dither, dest, src, n, volume);
65 66 67
}

static void
68
pcm_volume_change_16(PcmDither &dither,
69
		     int16_t *dest, const int16_t *src, size_t n,
Max Kellermann's avatar
Max Kellermann committed
70
		     int volume) noexcept
71
{
72
	pcm_volume_change<SampleFormat::S16>(dither, dest, src, n, volume);
73 74 75
}

static void
76
pcm_volume_change_24(PcmDither &dither,
77
		     int32_t *dest, const int32_t *src, size_t n,
Max Kellermann's avatar
Max Kellermann committed
78
		     int volume) noexcept
79
{
80
	pcm_volume_change<SampleFormat::S24_P32>(dither, dest, src, n,
81
						 volume);
82 83
}

84
static void
85
pcm_volume_change_32(PcmDither &dither,
86
		     int32_t *dest, const int32_t *src, size_t n,
Max Kellermann's avatar
Max Kellermann committed
87
		     int volume) noexcept
88
{
89
	pcm_volume_change<SampleFormat::S32>(dither, dest, src, n, volume);
90 91
}

92
static void
93
pcm_volume_change_float(float *dest, const float *src, size_t n,
Max Kellermann's avatar
Max Kellermann committed
94
			float volume) noexcept
95
{
96 97
	for (size_t i = 0; i != n; ++i)
		dest[i] = src[i] * volume;
98 99
}

100 101
void
PcmVolume::Open(SampleFormat _format)
102
{
103
	assert(format == SampleFormat::UNDEFINED);
104

105 106
	switch (_format) {
	case SampleFormat::UNDEFINED:
107 108
		throw FormatRuntimeError("Software volume for %s is not implemented",
					 sample_format_to_string(_format));
109 110 111 112 113 114 115

	case SampleFormat::S8:
	case SampleFormat::S16:
	case SampleFormat::S24_P32:
	case SampleFormat::S32:
	case SampleFormat::FLOAT:
		break;
116 117 118 119

	case SampleFormat::DSD:
		// TODO: implement this; currently, it's a no-op
		break;
120 121
	}

122 123 124 125
	format = _format;
}

ConstBuffer<void>
126
PcmVolume::Apply(ConstBuffer<void> src) noexcept
127 128 129 130 131 132 133 134
{
	if (volume == PCM_VOLUME_1)
		return src;

	void *data = buffer.Get(src.size);

	if (volume == 0) {
		/* optimized special case: 0% volume = memset(0) */
135
		PcmSilence({data, src.size}, format);
136 137 138
		return { data, src.size };
	}

139
	switch (format) {
140
	case SampleFormat::UNDEFINED:
141 142
		assert(false);
		gcc_unreachable();
143

144
	case SampleFormat::S8:
145
		pcm_volume_change_8(dither, (int8_t *)data,
146
				    (const int8_t *)src.data,
147
				    src.size / sizeof(int8_t),
Max Kellermann's avatar
Max Kellermann committed
148
				    volume);
149
		break;
150

151
	case SampleFormat::S16:
152
		pcm_volume_change_16(dither, (int16_t *)data,
153
				     (const int16_t *)src.data,
154
				     src.size / sizeof(int16_t),
Max Kellermann's avatar
Max Kellermann committed
155
				     volume);
156
		break;
157

158
	case SampleFormat::S24_P32:
159
		pcm_volume_change_24(dither, (int32_t *)data,
160
				     (const int32_t *)src.data,
161
				     src.size / sizeof(int32_t),
Max Kellermann's avatar
Max Kellermann committed
162
				     volume);
163
		break;
164

165
	case SampleFormat::S32:
166
		pcm_volume_change_32(dither, (int32_t *)data,
167
				     (const int32_t *)src.data,
168
				     src.size / sizeof(int32_t),
Max Kellermann's avatar
Max Kellermann committed
169
				     volume);
170
		break;
171

172
	case SampleFormat::FLOAT:
173 174
		pcm_volume_change_float((float *)data,
					(const float *)src.data,
175
					src.size / sizeof(float),
176
					pcm_volume_to_float(volume));
177
		break;
178 179 180 181

	case SampleFormat::DSD:
		// TODO: implement this; currently, it's a no-op
		return src;
182
	}
183

184
	return { data, src.size };
185
}