1 /*******************************************************************************
2  * Copyright 2009-2016 Jörg Müller
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 #pragma once
18 
19 /**
20  * @file ChannelMapperReader.h
21  * @ingroup respec
22  * The ChannelMapperReader class.
23  */
24 
25 #include "fx/EffectReader.h"
26 #include "util/Buffer.h"
27 
28 AUD_NAMESPACE_BEGIN
29 
30 /**
31  * This class maps a sound source's channels to a specific output channel count.
32  * \note The input sample format must be float.
33  */
34 class AUD_API ChannelMapperReader : public EffectReader
35 {
36 private:
37 	/**
38 	 * The sound reading buffer.
39 	 */
40 	Buffer m_buffer;
41 
42 	/**
43 	 * The output specification.
44 	 */
45 	Channels m_target_channels;
46 
47 	/**
48 	 * The channel count of the reader.
49 	 */
50 	Channels m_source_channels;
51 
52 	/**
53 	 * The mapping specification.
54 	 */
55 	float* m_mapping;
56 
57 	/**
58 	 * The size of the mapping.
59 	 */
60 	int m_map_size;
61 
62 	/**
63 	 * The mono source angle.
64 	 */
65 	float m_mono_angle;
66 
67 	static const Channel MONO_MAP[];
68 	static const Channel STEREO_MAP[];
69 	static const Channel STEREO_LFE_MAP[];
70 	static const Channel SURROUND4_MAP[];
71 	static const Channel SURROUND5_MAP[];
72 	static const Channel SURROUND51_MAP[];
73 	static const Channel SURROUND61_MAP[];
74 	static const Channel SURROUND71_MAP[];
75 	static const Channel* CHANNEL_MAPS[];
76 
77 	static const float MONO_ANGLES[];
78 	static const float STEREO_ANGLES[];
79 	static const float STEREO_LFE_ANGLES[];
80 	static const float SURROUND4_ANGLES[];
81 	static const float SURROUND5_ANGLES[];
82 	static const float SURROUND51_ANGLES[];
83 	static const float SURROUND61_ANGLES[];
84 	static const float SURROUND71_ANGLES[];
85 	static const float* CHANNEL_ANGLES[];
86 
87 	// delete copy constructor and operator=
88 	ChannelMapperReader(const ChannelMapperReader&) = delete;
89 	ChannelMapperReader& operator=(const ChannelMapperReader&) = delete;
90 
91 	/**
92 	 * Calculates the mapping matrix.
93 	 */
94 	void AUD_LOCAL calculateMapping();
95 
96 	/**
97 	 * Calculates the distance between two angles.
98 	 */
99 	float AUD_LOCAL angleDistance(float alpha, float beta);
100 
101 public:
102 	/**
103 	 * Creates a channel mapper reader.
104 	 * \param reader The reader to map.
105 	 * \param channels The target channel count this reader should map to.
106 	 */
107 	ChannelMapperReader(std::shared_ptr<IReader> reader, Channels channels);
108 
109 	/**
110 	 * Destroys the reader.
111 	 */
112 	~ChannelMapperReader();
113 
114 	/**
115 	 * Returns the channel configuration of the source reader.
116 	 * @return The channel configuration of the reader.
117 	 */
118 	Channels getSourceChannels() const;
119 
120 	/**
121 	 * Returns the target channel configuration.
122 	 * Equals getSpecs().channels.
123 	 * @return The target channel configuration.
124 	 */
125 	Channels getChannels() const;
126 
127 	/**
128 	 * Sets the requested channel output count.
129 	 * \param channels The channel output count.
130 	 */
131 	void setChannels(Channels channels);
132 
133 	/**
134 	 * Returns the mapping of the source channel to the target channel.
135 	 * @param source The number of the source channel. Should be in the range [0, source channels).
136 	 * @param target The number of the target channel. Should be in the range [0, target channels).
137 	 * @return The mapping value which should be between 0.0 and 1.0. If source or target are out of range, NaN is returned.
138 	 */
139 	float getMapping(int source, int target);
140 
141 	/**
142 	 * Sets the angle for mono sources.
143 	 * \param angle The angle for mono sources.
144 	 */
145 	void setMonoAngle(float angle);
146 
147 	virtual Specs getSpecs() const;
148 	virtual void read(int& length, bool& eos, sample_t* buffer);
149 };
150 
151 AUD_NAMESPACE_END
152