~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to intern/audaspace/intern/AUD_ChannelMapperReader.cpp

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: AUD_ChannelMapperReader.cpp 25646 2010-01-01 11:55:56Z nexyon $
3
 
 *
4
 
 * ***** BEGIN LGPL LICENSE BLOCK *****
5
 
 *
6
 
 * Copyright 2009 Jörg Hermann Müller
 
2
 * ***** BEGIN GPL LICENSE BLOCK *****
 
3
 *
 
4
 * Copyright 2009-2011 Jörg Hermann Müller
7
5
 *
8
6
 * This file is part of AudaSpace.
9
7
 *
10
 
 * AudaSpace is free software: you can redistribute it and/or modify
11
 
 * it under the terms of the GNU Lesser General Public License as published by
12
 
 * the Free Software Foundation, either version 3 of the License, or
 
8
 * Audaspace is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or
13
11
 * (at your option) any later version.
14
12
 *
15
13
 * AudaSpace is distributed in the hope that it will be useful,
16
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
 * GNU Lesser General Public License for more details.
19
 
 *
20
 
 * You should have received a copy of the GNU Lesser General Public License
21
 
 * along with AudaSpace.  If not, see <http://www.gnu.org/licenses/>.
22
 
 *
23
 
 * ***** END LGPL LICENSE BLOCK *****
24
 
 */
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with Audaspace; if not, write to the Free Software Foundation,
 
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
21
 *
 
22
 * ***** END GPL LICENSE BLOCK *****
 
23
 */
 
24
 
 
25
/** \file audaspace/intern/AUD_ChannelMapperReader.cpp
 
26
 *  \ingroup audaspaceintern
 
27
 */
 
28
 
 
29
#include <cmath>
 
30
 
 
31
#ifndef M_PI
 
32
#define M_PI 3.14159265358979323846
 
33
#endif
 
34
 
 
35
#ifndef M_PI_2
 
36
#define M_PI_2 1.57079632679489661923
 
37
#endif
25
38
 
26
39
#include "AUD_ChannelMapperReader.h"
27
 
#include "AUD_Buffer.h"
28
40
 
29
 
AUD_ChannelMapperReader::AUD_ChannelMapperReader(AUD_IReader* reader,
30
 
                                                                                                 float **mapping) :
31
 
                AUD_EffectReader(reader)
 
41
AUD_ChannelMapperReader::AUD_ChannelMapperReader(AUD_Reference<AUD_IReader> reader,
 
42
                                                                                                 AUD_Channels channels) :
 
43
                AUD_EffectReader(reader), m_target_channels(channels),
 
44
        m_source_channels(AUD_CHANNELS_INVALID), m_mapping(0), m_map_size(0), m_mono_angle(0)
32
45
{
33
 
        m_specs = reader->getSpecs();
34
 
 
35
 
        int channels = -1;
36
 
        m_rch = m_specs.channels;
37
 
        while(mapping[++channels] != 0);
38
 
 
39
 
        m_mapping = new float*[channels]; AUD_NEW("mapping")
40
 
        m_specs.channels = (AUD_Channels)channels;
41
 
 
42
 
        float sum;
43
 
        int i;
44
 
 
45
 
        while(channels--)
46
 
        {
47
 
                m_mapping[channels] = new float[m_rch]; AUD_NEW("mapping")
48
 
                sum = 0.0f;
49
 
                for(i=0; i < m_rch; i++)
50
 
                        sum += mapping[channels][i];
51
 
                for(i=0; i < m_rch; i++)
52
 
                        m_mapping[channels][i] = sum > 0.0f ?
53
 
                                                                         mapping[channels][i]/sum : 0.0f;
54
 
        }
55
 
 
56
 
        m_buffer = new AUD_Buffer(); AUD_NEW("buffer")
57
46
}
58
47
 
59
48
AUD_ChannelMapperReader::~AUD_ChannelMapperReader()
60
49
{
61
 
        int channels = m_specs.channels;
62
 
 
63
 
        while(channels--)
64
 
        {
65
 
                delete[] m_mapping[channels]; AUD_DELETE("mapping")
66
 
        }
67
 
 
68
 
        delete[] m_mapping; AUD_DELETE("mapping")
69
 
 
70
 
        delete m_buffer; AUD_DELETE("buffer")
71
 
}
72
 
 
73
 
AUD_Specs AUD_ChannelMapperReader::getSpecs()
74
 
{
75
 
        return m_specs;
76
 
}
77
 
 
78
 
void AUD_ChannelMapperReader::read(int & length, sample_t* & buffer)
79
 
{
80
 
        m_reader->read(length, buffer);
81
 
 
82
 
        int channels = m_specs.channels;
83
 
 
84
 
        if(m_buffer->getSize() < length * 4 * channels)
85
 
                m_buffer->resize(length * 4 * channels);
86
 
 
87
 
        sample_t* in = buffer;
88
 
        sample_t* out = m_buffer->getBuffer();
 
50
        delete[] m_mapping;
 
51
}
 
52
 
 
53
void AUD_ChannelMapperReader::setChannels(AUD_Channels channels)
 
54
{
 
55
        m_target_channels = channels;
 
56
        calculateMapping();
 
57
}
 
58
 
 
59
void AUD_ChannelMapperReader::setMonoAngle(float angle)
 
60
{
 
61
        if(angle != angle)
 
62
                angle = 0;
 
63
        m_mono_angle = angle;
 
64
        if(m_source_channels == AUD_CHANNELS_MONO)
 
65
                calculateMapping();
 
66
}
 
67
 
 
68
float AUD_ChannelMapperReader::angleDistance(float alpha, float beta)
 
69
{
 
70
        alpha = fabs(alpha - beta);
 
71
 
 
72
        if(alpha > M_PI)
 
73
                alpha = fabs(alpha - 2 * M_PI);
 
74
 
 
75
        return alpha;
 
76
}
 
77
 
 
78
void AUD_ChannelMapperReader::calculateMapping()
 
79
{
 
80
        if(m_map_size < m_source_channels * m_target_channels)
 
81
        {
 
82
                delete[] m_mapping;
 
83
                m_mapping = new float[m_source_channels * m_target_channels];
 
84
                m_map_size = m_source_channels * m_target_channels;
 
85
        }
 
86
 
 
87
        for(int i = 0; i < m_source_channels * m_target_channels; i++)
 
88
                m_mapping[i] = 0;
 
89
 
 
90
        const AUD_Channel* source_channels = CHANNEL_MAPS[m_source_channels - 1];
 
91
        const AUD_Channel* target_channels = CHANNEL_MAPS[m_target_channels - 1];
 
92
 
 
93
        int lfe = -1;
 
94
 
 
95
        for(int i = 0; i < m_target_channels; i++)
 
96
        {
 
97
                if(target_channels[i] == AUD_CHANNEL_LFE)
 
98
                {
 
99
                        lfe = i;
 
100
                        break;
 
101
                }
 
102
        }
 
103
 
 
104
        const float* source_angles = CHANNEL_ANGLES[m_source_channels - 1];
 
105
        const float* target_angles = CHANNEL_ANGLES[m_target_channels - 1];
 
106
 
 
107
        if(m_source_channels == AUD_CHANNELS_MONO)
 
108
                source_angles = &m_mono_angle;
 
109
 
 
110
        int channel_min1, channel_min2;
 
111
        float angle_min1, angle_min2, angle;
 
112
 
 
113
        for(int i = 0; i < m_source_channels; i++)
 
114
        {
 
115
                if(source_channels[i] == AUD_CHANNEL_LFE)
 
116
                {
 
117
                        if(lfe != -1)
 
118
                                m_mapping[lfe * m_source_channels + i] = 1;
 
119
 
 
120
                        continue;
 
121
                }
 
122
 
 
123
                channel_min1 = channel_min2 = -1;
 
124
                angle_min1 = angle_min2 = 2 * M_PI;
 
125
 
 
126
                for(int j = 0; j < m_target_channels; j++)
 
127
                {
 
128
                        if(j == lfe)
 
129
                                continue;
 
130
                        angle = angleDistance(source_angles[i], target_angles[j]);
 
131
                        if(angle < angle_min1)
 
132
                        {
 
133
                                channel_min2 = channel_min1;
 
134
                                angle_min2 = angle_min1;
 
135
 
 
136
                                channel_min1 = j;
 
137
                                angle_min1 = angle;
 
138
                        }
 
139
                        else if(angle < angle_min2)
 
140
                        {
 
141
                                channel_min2 = j;
 
142
                                angle_min2 = angle;
 
143
                        }
 
144
                }
 
145
 
 
146
                angle = angle_min1 + angle_min2;
 
147
                if(channel_min2 == -1 || angle == 0)
 
148
                {
 
149
                        m_mapping[channel_min1 * m_source_channels + i] = 1;
 
150
                }
 
151
                else
 
152
                {
 
153
                        m_mapping[channel_min1 * m_source_channels + i] = cos(M_PI_2 * angle_min1 / angle);
 
154
                        m_mapping[channel_min2 * m_source_channels + i] = cos(M_PI_2 * angle_min2 / angle);
 
155
                }
 
156
        }
 
157
 
 
158
        /* AUD_XXX for(int i = 0; i < m_source_channels; i++)
 
159
        {
 
160
                for(int j = 0; j < m_target_channels; j++)
 
161
                {
 
162
                        std::cout << m_mapping[i * m_source_channels + j] << " ";
 
163
                }
 
164
                std::cout << std::endl;
 
165
        }*/
 
166
}
 
167
 
 
168
AUD_Specs AUD_ChannelMapperReader::getSpecs() const
 
169
{
 
170
        AUD_Specs specs = m_reader->getSpecs();
 
171
        specs.channels = m_target_channels;
 
172
        return specs;
 
173
}
 
174
 
 
175
void AUD_ChannelMapperReader::read(int& length, bool& eos, sample_t* buffer)
 
176
{
 
177
        AUD_Channels channels = m_reader->getSpecs().channels;
 
178
        if(channels != m_source_channels)
 
179
        {
 
180
                m_source_channels = channels;
 
181
                calculateMapping();
 
182
        }
 
183
 
 
184
        if(m_source_channels == m_target_channels)
 
185
        {
 
186
                m_reader->read(length, eos, buffer);
 
187
                return;
 
188
        }
 
189
 
 
190
        m_buffer.assureSize(length * channels * sizeof(sample_t));
 
191
 
 
192
        sample_t* in = m_buffer.getBuffer();
 
193
 
 
194
        m_reader->read(length, eos, in);
 
195
 
89
196
        sample_t sum;
90
197
 
91
198
        for(int i = 0; i < length; i++)
92
199
        {
93
 
                for(int j = 0; j < channels; j++)
 
200
                for(int j = 0; j < m_target_channels; j++)
94
201
                {
95
202
                        sum = 0;
96
 
                        for(int k = 0; k < m_rch; k++)
97
 
                                sum += m_mapping[j][k] * in[i * m_rch + k];
98
 
                        out[i * channels + j] = sum;
 
203
                        for(int k = 0; k < m_source_channels; k++)
 
204
                                sum += m_mapping[j * m_source_channels + k] * in[i * m_source_channels + k];
 
205
                        buffer[i * m_target_channels + j] = sum;
99
206
                }
100
207
        }
101
 
 
102
 
        buffer = m_buffer->getBuffer();
103
208
}
 
209
 
 
210
const AUD_Channel AUD_ChannelMapperReader::MONO_MAP[] =
 
211
{
 
212
        AUD_CHANNEL_FRONT_CENTER
 
213
};
 
214
 
 
215
const AUD_Channel AUD_ChannelMapperReader::STEREO_MAP[] =
 
216
{
 
217
        AUD_CHANNEL_FRONT_LEFT,
 
218
        AUD_CHANNEL_FRONT_RIGHT
 
219
};
 
220
 
 
221
const AUD_Channel AUD_ChannelMapperReader::STEREO_LFE_MAP[] =
 
222
{
 
223
        AUD_CHANNEL_FRONT_LEFT,
 
224
        AUD_CHANNEL_FRONT_RIGHT,
 
225
        AUD_CHANNEL_LFE
 
226
};
 
227
 
 
228
const AUD_Channel AUD_ChannelMapperReader::SURROUND4_MAP[] =
 
229
{
 
230
        AUD_CHANNEL_FRONT_LEFT,
 
231
        AUD_CHANNEL_FRONT_RIGHT,
 
232
        AUD_CHANNEL_REAR_LEFT,
 
233
        AUD_CHANNEL_REAR_RIGHT
 
234
};
 
235
 
 
236
const AUD_Channel AUD_ChannelMapperReader::SURROUND5_MAP[] =
 
237
{
 
238
        AUD_CHANNEL_FRONT_LEFT,
 
239
        AUD_CHANNEL_FRONT_RIGHT,
 
240
        AUD_CHANNEL_FRONT_CENTER,
 
241
        AUD_CHANNEL_REAR_LEFT,
 
242
        AUD_CHANNEL_REAR_RIGHT
 
243
};
 
244
 
 
245
const AUD_Channel AUD_ChannelMapperReader::SURROUND51_MAP[] =
 
246
{
 
247
        AUD_CHANNEL_FRONT_LEFT,
 
248
        AUD_CHANNEL_FRONT_RIGHT,
 
249
        AUD_CHANNEL_FRONT_CENTER,
 
250
        AUD_CHANNEL_LFE,
 
251
        AUD_CHANNEL_REAR_LEFT,
 
252
        AUD_CHANNEL_REAR_RIGHT
 
253
};
 
254
 
 
255
const AUD_Channel AUD_ChannelMapperReader::SURROUND61_MAP[] =
 
256
{
 
257
        AUD_CHANNEL_FRONT_LEFT,
 
258
        AUD_CHANNEL_FRONT_RIGHT,
 
259
        AUD_CHANNEL_FRONT_CENTER,
 
260
        AUD_CHANNEL_LFE,
 
261
        AUD_CHANNEL_REAR_CENTER,
 
262
        AUD_CHANNEL_REAR_LEFT,
 
263
        AUD_CHANNEL_REAR_RIGHT
 
264
};
 
265
 
 
266
const AUD_Channel AUD_ChannelMapperReader::SURROUND71_MAP[] =
 
267
{
 
268
        AUD_CHANNEL_FRONT_LEFT,
 
269
        AUD_CHANNEL_FRONT_RIGHT,
 
270
        AUD_CHANNEL_FRONT_CENTER,
 
271
        AUD_CHANNEL_LFE,
 
272
        AUD_CHANNEL_REAR_LEFT,
 
273
        AUD_CHANNEL_REAR_RIGHT,
 
274
        AUD_CHANNEL_SIDE_LEFT,
 
275
        AUD_CHANNEL_SIDE_RIGHT
 
276
};
 
277
 
 
278
const AUD_Channel* AUD_ChannelMapperReader::CHANNEL_MAPS[] =
 
279
{
 
280
        AUD_ChannelMapperReader::MONO_MAP,
 
281
        AUD_ChannelMapperReader::STEREO_MAP,
 
282
        AUD_ChannelMapperReader::STEREO_LFE_MAP,
 
283
        AUD_ChannelMapperReader::SURROUND4_MAP,
 
284
        AUD_ChannelMapperReader::SURROUND5_MAP,
 
285
        AUD_ChannelMapperReader::SURROUND51_MAP,
 
286
        AUD_ChannelMapperReader::SURROUND61_MAP,
 
287
        AUD_ChannelMapperReader::SURROUND71_MAP
 
288
};
 
289
 
 
290
const float AUD_ChannelMapperReader::MONO_ANGLES[] =
 
291
{
 
292
        0.0f * M_PI / 180.0f
 
293
};
 
294
 
 
295
const float AUD_ChannelMapperReader::STEREO_ANGLES[] =
 
296
{
 
297
        -90.0f * M_PI / 180.0f,
 
298
         90.0f * M_PI / 180.0f
 
299
};
 
300
 
 
301
const float AUD_ChannelMapperReader::STEREO_LFE_ANGLES[] =
 
302
{
 
303
   -90.0f * M_PI / 180.0f,
 
304
        90.0f * M_PI / 180.0f,
 
305
         0.0f * M_PI / 180.0f
 
306
};
 
307
 
 
308
const float AUD_ChannelMapperReader::SURROUND4_ANGLES[] =
 
309
{
 
310
         -45.0f * M_PI / 180.0f,
 
311
          45.0f * M_PI / 180.0f,
 
312
        -135.0f * M_PI / 180.0f,
 
313
         135.0f * M_PI / 180.0f
 
314
};
 
315
 
 
316
const float AUD_ChannelMapperReader::SURROUND5_ANGLES[] =
 
317
{
 
318
         -30.0f * M_PI / 180.0f,
 
319
          30.0f * M_PI / 180.0f,
 
320
           0.0f * M_PI / 180.0f,
 
321
        -110.0f * M_PI / 180.0f,
 
322
         110.0f * M_PI / 180.0f
 
323
};
 
324
 
 
325
const float AUD_ChannelMapperReader::SURROUND51_ANGLES[] =
 
326
{
 
327
          -30.0f * M_PI / 180.0f,
 
328
           30.0f * M_PI / 180.0f,
 
329
           0.0f * M_PI / 180.0f,
 
330
           0.0f * M_PI / 180.0f,
 
331
        -110.0f * M_PI / 180.0f,
 
332
         110.0f * M_PI / 180.0f
 
333
};
 
334
 
 
335
const float AUD_ChannelMapperReader::SURROUND61_ANGLES[] =
 
336
{
 
337
          -30.0f * M_PI / 180.0f,
 
338
           30.0f * M_PI / 180.0f,
 
339
           0.0f * M_PI / 180.0f,
 
340
           0.0f * M_PI / 180.0f,
 
341
         180.0f * M_PI / 180.0f,
 
342
        -110.0f * M_PI / 180.0f,
 
343
         110.0f * M_PI / 180.0f
 
344
};
 
345
 
 
346
const float AUD_ChannelMapperReader::SURROUND71_ANGLES[] =
 
347
{
 
348
          -30.0f * M_PI / 180.0f,
 
349
           30.0f * M_PI / 180.0f,
 
350
           0.0f * M_PI / 180.0f,
 
351
           0.0f * M_PI / 180.0f,
 
352
        -110.0f * M_PI / 180.0f,
 
353
         110.0f * M_PI / 180.0f,
 
354
        -150.0f * M_PI / 180.0f,
 
355
         150.0f * M_PI / 180.0f
 
356
};
 
357
 
 
358
const float* AUD_ChannelMapperReader::CHANNEL_ANGLES[] =
 
359
{
 
360
        AUD_ChannelMapperReader::MONO_ANGLES,
 
361
        AUD_ChannelMapperReader::STEREO_ANGLES,
 
362
        AUD_ChannelMapperReader::STEREO_LFE_ANGLES,
 
363
        AUD_ChannelMapperReader::SURROUND4_ANGLES,
 
364
        AUD_ChannelMapperReader::SURROUND5_ANGLES,
 
365
        AUD_ChannelMapperReader::SURROUND51_ANGLES,
 
366
        AUD_ChannelMapperReader::SURROUND61_ANGLES,
 
367
        AUD_ChannelMapperReader::SURROUND71_ANGLES
 
368
};