- メインページ
- モジュール
- ネームスペース
- データ構造
- ファイル

- ファイル一覧
- グローバル

stereo.h
説明を見る。
1 //
2 // Copyright (c) 2003-2011, MIST Project, Nagoya University
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the Nagoya University nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28
33
34 #ifndef __INCLUDE_MIST_STEREO__
35 #define __INCLUDE_MIST_STEREO__
36
37
38 #ifndef __INCLUDE_MIST_CONF_H__
39 #include "mist_conf.h"
40 #endif
41
42 #ifndef __INCLUDE_MIST_TYPE_TRAIT_H__
43 #include "type_trait.h"
44 #endif
45
46 #include <iostream>
47
48 // mist名前空間の始まり
49 _MIST_BEGIN
50
51
52 // MISTで利用する基底のデータ型
53
54
63 template< class T >
64 struct stereo
65 {
66 public:
67 typedef size_t size_type;
68 typedef ptrdiff_t difference_type;
69 typedef T& reference;
70 typedef const T& const_reference;
71 typedef T value_type;
72 typedef T* pointer;
73 typedef const T* const_pointer;
74
75 public:
76 value_type l;
77 value_type r;
78
80 stereo( ) : l( 0 ), r( 0 ){ }
81
83 stereo( const value_type &mono ) : l( mono ), r( mono ){ }
84
86 stereo( const value_type &left, const value_type &right ) : l( left ), r( right ){ }
87
89 template < class TT >
90 stereo( const stereo< TT > &s ) : l( static_cast< value_type >( s.l ) ), r( static_cast< value_type >( s.r ) ){ }
91
93 stereo( const stereo< T > &s ) : l( s.l ), r( s.r ){ }
94
95
96
98 template < class TT >
99 const stereo &operator =( const stereo< TT > &s )
100 {
101 l = static_cast< value_type >( s.l );
102 r = static_cast< value_type >( s.r );
103 return( *this );
104 }
105
107 const stereo &operator =( const stereo< T > &s )
108 {
109 if( &s != this )
110 {
111 l = s.l;
112 r = s.r;
113 }
114 return( *this );
115 }
116
117
119 const stereo &operator =( const value_type &mono )
120 {
121 l = mono;
122 r = mono;
123 return( *this );
124 }
125
126
128 template < class TT >
129 const stereo &operator +=( const stereo< TT > &s ){ l = static_cast< value_type >( l + s.l ); r = static_cast< value_type >( r + s.r ); return( *this ); }
130
132 template < class TT >
133 const stereo &operator -=( const stereo< TT > &s ){ l = static_cast< value_type >( l - s.l ); r = static_cast< value_type >( r - s.r ); return( *this ); }
134
136 template < class TT >
137 const stereo &operator *=( const stereo< TT > &s ){ l = static_cast< value_type >( l * s.l ); r = static_cast< value_type >( r * s.r ); return( *this ); }
138
140 template < class TT >
141 const stereo &operator /=( const stereo< TT > &s ){ l = static_cast< value_type >( l / s.l ); r = static_cast< value_type >( r / s.r ); return( *this ); }
142
144 const stereo &operator %=( const stereo &s ){ l %= s.l; r %= s.r; return( *this ); }
145
147 const stereo &operator |=( const stereo &s ){ l |= s.l; r |= s.r; return( *this ); }
148
150 const stereo &operator &=( const stereo &s ){ l &= s.l; r &= s.r; return( *this ); }
151
153 const stereo &operator ^=( const stereo &s ){ l ^= s.l; r ^= s.r; return( *this ); }
154
155
156
158 #if defined( __MIST_MSVC__ ) && __MIST_MSVC__ < 7
159 const stereo &operator +=( const double &mono )
160 #else
161 template < class TT >
162 const stereo &operator +=( const TT &mono )
163 #endif
164 {
165 l = static_cast< value_type >( l + mono );
166 r = static_cast< value_type >( r + mono );
167 return( *this );
168 }
169
171 #if defined( __MIST_MSVC__ ) && __MIST_MSVC__ < 7
172 const stereo &operator -=( const double &mono )
173 #else
174 template < class TT >
175 const stereo &operator -=( const TT &mono )
176 #endif
177 {
178 l = static_cast< value_type >( l - mono );
179 r = static_cast< value_type >( r - mono );
180 return( *this );
181 }
182
184 #if defined( __MIST_MSVC__ ) && __MIST_MSVC__ < 7
185 const stereo &operator *=( const double &mono )
186 #else
187 template < class TT >
188 const stereo &operator *=( const TT &mono )
189 #endif
190 {
191 l = static_cast< value_type >( l * mono );
192 r = static_cast< value_type >( r * mono );
193 return( *this );
194 }
195
197 #if defined( __MIST_MSVC__ ) && __MIST_MSVC__ < 7
198 const stereo &operator /=( const double &mono )
199 #else
200 template < class TT >
201 const stereo &operator /=( const TT &mono )
202 #endif
203 {
204 l = static_cast< value_type >( l / mono );
205 r = static_cast< value_type >( r / mono );
206 return( *this );
207 }
208
209
219 bool operator ==( const stereo &s ) const { return( l == s.l && r == s.r ); }
220
230 bool operator !=( const stereo &s ) const { return( !( *this == s ) ); }
231
241 bool operator < ( const stereo &s ) const
242 {
243 if( l == s.l )
244 {
245 return( r < s.r );
246 }
247 else
248 {
249 return( l < s.l );
250 }
251 }
252
262 bool operator <=( const stereo &s ) const { return( s >= *this ); }
263
273 bool operator > ( const stereo &s ) const { return( s < *this ); }
274
284 bool operator >=( const stereo &s ) const { return( !( *this < s ) ); }
285
286
288 value_type get_value( ) const
289 {
290 return( static_cast< value_type >( ( l + r ) / 2.0 ) );
291 }
292
293 // ステレオからモノラルへの自動キャスト演算子（危険のため一時停止）
294 //operator value_type( ) const { return( get_value( ) ); }
295
296 };
297
299 DEFINE_PROMOTE_BIND_OPERATOR1( stereo, + )
300
301
302 DEFINE_PROMOTE_BIND_OPERATOR2( stereo, + )
303
305 DEFINE_PROMOTE_BIND_OPERATOR3( stereo, + )
306
308 DEFINE_PROMOTE_BIND_OPERATOR1( stereo, - )
309
311 DEFINE_PROMOTE_BIND_OPERATOR2( stereo, - )
312
314 DEFINE_PROMOTE_BIND_OPERATOR4( stereo, - )
315
317 DEFINE_PROMOTE_BIND_OPERATOR1( stereo, * )
318
320 DEFINE_PROMOTE_BIND_OPERATOR2( stereo, * )
321
323 DEFINE_PROMOTE_BIND_OPERATOR3( stereo, * )
324
326 DEFINE_PROMOTE_BIND_OPERATOR1( stereo, / )
327
329 DEFINE_PROMOTE_BIND_OPERATOR2( stereo, / )
330
332 DEFINE_PROMOTE_BIND_OPERATOR1( stereo, % )
333
335 DEFINE_PROMOTE_BIND_OPERATOR1( stereo, | )
336
338 DEFINE_PROMOTE_BIND_OPERATOR1( stereo, & )
339
341 DEFINE_PROMOTE_BIND_OPERATOR1( stereo, ^ )
342
343
344
356 template < class T > inline std::ostream &operator <<( std::ostream &out, const stereo< T > &s )
357 {
358 out << "( ";
359 out << s.l << ", ";
360 out << s.r << " )";
361 return( out );
362 }
363
364
366 template < class T >
367 struct is_stereo
368 {
369 _MIST_CONST( bool, value, false );
370 };
371
372
373 // 画素の変換をサポートするためのコンバータ
374 template < class T >
375 struct _stereo_converter_
376 {
377 typedef T value_type;
378 typedef stereo< T > stereo_type;
379 enum{ channel_num = 1 };
380
381 static value_type convert_to( value_type l, value_type r )
382 {
383 return( stereo_type( l, r ).get_value( ) );
384 }
385
386 static stereo_type convert_from( const value_type &mono )
387 {
388 return( stereo_type( mono, mono ) );
389 }
390 };
391
392 #if defined(__MIST_MSVC__) && __MIST_MSVC__ < 7
393
394 #define IS_STEREO( type ) \
395 template < >\
396 struct is_stereo< stereo< type > >\
397 {\
398 enum{ value = true };\
399 };\
400
401
402 #define __STEREO_CONVERTER__( type ) \
403 template < >\
404 struct _stereo_converter_< stereo< type > >\
405 {\
406 typedef type value_type;\
407 typedef stereo< type > stereo_type;\
408 enum{ channel_num = 2 };\
409 \
410 static stereo_type convert_to( value_type l, value_type r )\
411 {\
412 return( stereo_type( l, r ) );\
413 }\
414 \
415 static stereo_type convert_from( const stereo_type &sound )\
416 {\
417 return( sound );\
418 }\
419 };\
420
421 // 各型に対する特殊化
422 IS_STEREO(unsigned char)
423 IS_STEREO(unsigned short)
424 IS_STEREO(unsigned int)
425 IS_STEREO(unsigned long)
426 IS_STEREO(signed char)
427 IS_STEREO(signed short)
428 IS_STEREO(signed int)
429 IS_STEREO(signed long)
430 IS_STEREO(bool)
431 IS_STEREO(char)
432 IS_STEREO(float)
433 IS_STEREO(double)
434 IS_STEREO(long double)
435 __STEREO_CONVERTER__(unsigned char)
436 __STEREO_CONVERTER__(unsigned short)
437 __STEREO_CONVERTER__(unsigned int)
438 __STEREO_CONVERTER__(unsigned long)
439 __STEREO_CONVERTER__(signed char)
440 __STEREO_CONVERTER__(signed short)
441 __STEREO_CONVERTER__(signed int)
442 __STEREO_CONVERTER__(signed long)
443 __STEREO_CONVERTER__(bool)
444 __STEREO_CONVERTER__(char)
445 __STEREO_CONVERTER__(float)
446 __STEREO_CONVERTER__(double)
447 __STEREO_CONVERTER__(long double)
448
449 #undef IS_STEREO
450 #undef __STEREO_CONVERTER__
451
452 #else
453
454 template < class T >
455 struct is_stereo< stereo< T > >
456 {
457 _MIST_CONST( bool, value, true );
458 };
459
460 template < class T >
461 struct _stereo_converter_< stereo< T > >
462 {
463 typedef T value_type;
464 typedef stereo< T > stereo_type;
465 enum{ channel_num = 2 };
466
467 static stereo_type convert_to( value_type l, value_type r )
468 {
469 return( stereo_type( l, r ) );
470 }
471
472 static stereo_type convert_from( const stereo_type &sound )
473 {
474 return( sound );
475 }
476 };
477
478 #endif
479
480 // mist名前空間の終わり
481 _MIST_END
482
483
484 #endif // __INCLUDE_MIST_STEREO__
485