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

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

wav.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 #ifndef __INCLUDE_MIST_WAV__
34 #define __INCLUDE_MIST_WAV__
35
36
37 #ifndef __INCLUDE_MIST_H__
38 #include "../mist.h"
39 #endif
40
41 // ステレオ音声の設定を読み込む
42 #ifndef __INCLUDE_MIST_STEREO__
43 #include "../config/stereo.h"
44 #endif
45
46 // 各型の情報を読み込む
47 #ifndef __INCLUDE_MIST_LIMITS__
48 #include "../config/stereo.h"
49 #endif
50
51
52 #include <iostream>
53 #include <string>
54
55 #include <deque>
56 #include <map>
57 #include <algorithm>
58
59
60 // mist名前空間の始まり
61 _MIST_BEGIN
62
63
64 namespace __wav_controller__
65 {
66 template < class T >
67 union byte_array
68 {
69 typedef T value_type;
70 value_type value;
71 unsigned char byte[ sizeof( value_type ) ];
72 byte_array( const value_type &v = 0 ) : value( v ){ }
73 };
74
75 // 構造体内のアライメントを1バイトに設定し，パッディングを禁止する
76 #if defined(__MIST_MSVC__) || defined(__INTEL_COMPILER)
77 #pragma pack( push, wav_align, 1 )
78 #endif
79 struct _riffheader_
80 {
81 enum{ bytes = 8 };
82 unsigned char riff[4]; // WAVの場合は必ず 'RIFF'
83 unsigned int file_size; // RIFFヘッダの後のファイルサイズ　つまりファイルサイズ−8
84 } _MIST_PACKED;
85
86 struct _wavheader_
87 {
88 enum{ bytes = 4 };
89 unsigned char wav[4]; // WAVの場合は必ず 'WAVE'
90 } _MIST_PACKED;
91
92 struct _fmtheader_
93 {
94 enum{ bytes = 24 };
95 unsigned char fmt[4]; // WAVの場合は必ず 'fmt ' ←スペース重要
96 unsigned int fmt_size; // fmtヘッダの後のファイルサイズ
97 unsigned short format_id; // WAVファイルの形式
98 unsigned short channel_num; // チャンネル数（1:モノラル，2:ステレオ）
99 unsigned int sampling_rate; // サンプリング周波数（11025、22050、44100）
100 unsigned int bytes_per_second; // １秒あたりの音声データのバイト数
101 unsigned short bytes_per_sample; // １サンプルあたりのバイト数
102 unsigned short bits_per_sample; // 量子化ビット数（8、16）
103 } _MIST_PACKED;
104
105 struct _wavdata_
106 {
107 enum{ bytes = 8 };
108 unsigned char data[4]; // WAVの場合は必ず 'data'
109 unsigned int data_size; // 本ヘッダの後のファイルサイズ
110 } _MIST_PACKED;
111
112 #if defined(__MIST_MSVC__) || defined(__INTEL_COMPILER)
113 #pragma pack( pop, wav_align )
114 #endif
115 // 構造体内のアライメントを1バイトに設定し，パッディングを禁止する 〜 ここまで 〜
116
117 template < class T >
118 struct sampling_bits_typedef
119 {
120 typedef T value_type;
121 };
122
123 #define __SAMPLING_BITS_CONVERTER__( from, to, expression ) \
124 inline void sampling_bits_convert( const from &a, to &b ){ b = static_cast< to >( expression ); }\
125
126 #define __SAMPLING_BITS_TYPEDEF__( from, to ) \
127 template < >\
128 struct sampling_bits_typedef< from >\
129 {\
130 typedef to value_type;\
131 };\
132
133 __SAMPLING_BITS_CONVERTER__( unsigned char, unsigned char, a ) // 8ビット→8ビット変換
134 __SAMPLING_BITS_CONVERTER__( unsigned char, signed char, a - 128 ) // 8ビット→8ビット変換
135 __SAMPLING_BITS_CONVERTER__( unsigned char, char, a - 128 ) // 8ビット→8ビット変換
136 __SAMPLING_BITS_CONVERTER__( unsigned char, unsigned short, a * 257.0 ) // 8ビット→16ビット変換
137 __SAMPLING_BITS_CONVERTER__( unsigned char, signed short, a * 257.0 - 32768.0 ) // 8ビット→16ビット変換
138 __SAMPLING_BITS_CONVERTER__( unsigned char, unsigned int, a * 65793.0 ) // 8ビット→24ビット変換
139 __SAMPLING_BITS_CONVERTER__( unsigned char, signed int, a * 65793.0 - 8388607.0 ) // 8ビット→24ビット変換
140 __SAMPLING_BITS_CONVERTER__( unsigned char, unsigned long, a * 65793.0 ) // 8ビット→24ビット変換
141 __SAMPLING_BITS_CONVERTER__( unsigned char, signed long, a * 65793.0 - 8388607.0 ) // 8ビット→24ビット変換
142 __SAMPLING_BITS_CONVERTER__( unsigned char, float, a * 65793.0 ) // 8ビット→24ビット変換
143 __SAMPLING_BITS_CONVERTER__( unsigned char, double, a * 65793.0 - 8388607.0 ) // 8ビット→24ビット変換
144 __SAMPLING_BITS_CONVERTER__( unsigned char, long double, a * 65793.0 - 8388607.0 ) // 8ビット→24ビット変換
145
146 __SAMPLING_BITS_CONVERTER__( signed short, unsigned char, a / 257.0 + 128.0 ) // 16ビット→8ビット変換
147 __SAMPLING_BITS_CONVERTER__( signed short, signed char, a / 257.0 ) // 16ビット→8ビット変換
148 __SAMPLING_BITS_CONVERTER__( signed short, char, a / 257.0 ) // 16ビット→8ビット変換
149 __SAMPLING_BITS_CONVERTER__( signed short, unsigned short, a + 32768.0 ) // 16ビット→16ビット変換
150 __SAMPLING_BITS_CONVERTER__( signed short, signed short, a ) // 16ビット→16ビット変換
151 __SAMPLING_BITS_CONVERTER__( signed short, unsigned int, a * 256.0 + 8388607.0 ) // 16ビット→24ビット変換
152 __SAMPLING_BITS_CONVERTER__( signed short, signed int, a * 256.0 ) // 16ビット→24ビット変換
153 __SAMPLING_BITS_CONVERTER__( signed short, float, a * 256.0 ) // 16ビット→24ビット変換
154 __SAMPLING_BITS_CONVERTER__( signed short, double, a * 256.0 ) // 16ビット→24ビット変換
155 __SAMPLING_BITS_CONVERTER__( signed short, long double, a * 256.0 ) // 16ビット→24ビット変換
156
157 __SAMPLING_BITS_CONVERTER__( signed int, unsigned char, a / 65793.0 + 128.0 ) // 24ビット→8ビット変換
158 __SAMPLING_BITS_CONVERTER__( signed int, signed char, a / 65793.0 ) // 24ビット→8ビット変換
159 __SAMPLING_BITS_CONVERTER__( signed int, char, a / 65793.0 ) // 24ビット→8ビット変換
160 __SAMPLING_BITS_CONVERTER__( signed int, unsigned short, a / 256.0 + 32768.0 ) // 24ビット→16ビット変換
161 __SAMPLING_BITS_CONVERTER__( signed int, signed short, a / 256.0 ) // 24ビット→16ビット変換
162 __SAMPLING_BITS_CONVERTER__( signed int, unsigned int, a + 8388607.0 ) // 24ビット→24ビット変換
163 __SAMPLING_BITS_CONVERTER__( signed int, signed int, a ) // 24ビット→24ビット変換
164 __SAMPLING_BITS_CONVERTER__( signed int, float, a ) // 24ビット→24ビット変換
165 __SAMPLING_BITS_CONVERTER__( signed int, double, a ) // 24ビット→24ビット変換
166 __SAMPLING_BITS_CONVERTER__( signed int, long double, a ) // 24ビット→24ビット変換
167
168
169 __SAMPLING_BITS_TYPEDEF__( unsigned short, signed short );
170 __SAMPLING_BITS_TYPEDEF__( unsigned int, signed int );
171 __SAMPLING_BITS_TYPEDEF__( unsigned long, signed int );
172 __SAMPLING_BITS_TYPEDEF__( signed char, unsigned char );
173 __SAMPLING_BITS_TYPEDEF__( signed long, signed int );
174 __SAMPLING_BITS_TYPEDEF__( char, unsigned char );
175 __SAMPLING_BITS_TYPEDEF__( float, signed int );
176 __SAMPLING_BITS_TYPEDEF__( double, signed int );
177 __SAMPLING_BITS_TYPEDEF__( long double, signed int );
178
179 #undef __SAMPLING_BITS_CONVERTER__
180 #undef __SAMPLING_BITS_TYPEDEF__
181
182 template < class T, class Allocator >
183 struct wav_controller
184 {
185 typedef typename array< T, Allocator >::size_type size_type;
186 typedef typename array< T, Allocator >::value_type value_type;
187 typedef _stereo_converter_< T > stereo_converter;
188 typedef typename stereo_converter::stereo_type stereo_type;
189
190
191 static size_type get_wav_bytes( const array< T, Allocator > &sound, size_type bits_per_sample, size_type channel_num )
192 {
193 switch( bits_per_sample )
194 {
195 case 8:
196 return( _riffheader_::bytes + _wavheader_::bytes + _fmtheader_::bytes + _wavdata_::bytes + sound.size( ) * channel_num );
197 break;
198
199 case 16:
200 return( _riffheader_::bytes + _wavheader_::bytes + _fmtheader_::bytes + _wavdata_::bytes + sound.size( ) * 2 * channel_num );
201 break;
202
203 case 24:
204 return( _riffheader_::bytes + _wavheader_::bytes + _fmtheader_::bytes + _wavdata_::bytes + sound.size( ) * 3 * channel_num );
205 break;
206
207 case 32:
208 return( _riffheader_::bytes + _wavheader_::bytes + _fmtheader_::bytes + _wavdata_::bytes + sound.size( ) * 4 * channel_num );
209 break;
210
211 default:
212 return( 0 );
213 }
214 }
215
216 static bool extract_wav_data8( unsigned char *wav, array< T, Allocator > &sound, size_type num_bytes, size_type channel_num )
217 {
218 size_type data_num = num_bytes / channel_num;
219 sound.resize( data_num );
220
221 unsigned char lin, rin;
222 value_type lout, rout;
223 size_type i;
224
225 switch( channel_num )
226 {
227 case 1:
228 for( i = 0 ; i < sound.size( ) ; i++ )
229 {
230 lin = wav[ i ];
231 sampling_bits_convert( lin, lout );
232 sound[ i ] = stereo_converter::convert_to( lout, lout );
233 }
234 break;
235
236 case 2:
237 for( i = 0 ; i < sound.size( ) ; i++ )
238 {
239 lin = wav[ i * 2 + 0 ];
240 rin = wav[ i * 2 + 1 ];
241 sampling_bits_convert( lin, lout );
242 sampling_bits_convert( rin, rout );
243 sound[ i ] = stereo_converter::convert_to( lout, rout );
244 }
245 break;
246
247 default:
248 return( false );
249 }
250
251 return( true );
252 }
253
254 static bool extract_wav_data16( unsigned char *wav, array< T, Allocator > &sound, size_type num_bytes, size_type channel_num )
255 {
256 signed short *wave = reinterpret_cast< signed short * >( wav );
257 size_type data_num = num_bytes / channel_num / 2;
258 sound.resize( data_num );
259
260 signed short lin, rin;
261 value_type lout, rout;
262 size_type i;
263
264 switch( channel_num )
265 {
266 case 1:
267 for( i = 0 ; i < sound.size( ) ; i++ )
268 {
269 lin = wave[ i ];
270 sampling_bits_convert( lin, lout );
271 sound[ i ] = stereo_converter::convert_to( lout, lout );
272 }
273 break;
274
275 case 2:
276 for( i = 0 ; i < sound.size( ) ; i++ )
277 {
278 lin = wave[ i * 2 + 0 ];
279 rin = wave[ i * 2 + 1 ];
280 sampling_bits_convert( lin, lout );
281 sampling_bits_convert( rin, rout );
282 sound[ i ] = stereo_converter::convert_to( lout, rout );
283 }
284 break;
285
286 default:
287 return( false );
288 }
289
290 return( true );
291 }
292
293 static bool extract_wav_data24( unsigned char *wav, array< T, Allocator > &sound, size_type num_bytes, size_type channel_num )
294 {
295 size_type data_num = num_bytes / channel_num / 3;
296 sound.resize( data_num );
297
298 signed int lin, rin;
299 value_type lout, rout;
300 size_type i;
301
302 switch( channel_num )
303 {
304 case 1:
305 for( i = 0 ; i < sound.size( ) ; i++ )
306 {
307 lin = ( wav[ i * 3 + 0 ] << 16 ) | ( wav[ i * 3 + 1 ] << 8 ) | wav[ i * 3 + 2 ];
308 sampling_bits_convert( lin, lout );
309 sound[ i ] = stereo_converter::convert_to( lout, lout );
310 }
311 break;
312
313 case 2:
314 for( i = 0 ; i < sound.size( ) ; i++ )
315 {
316 lin = ( wav[ i * 3 * 2 + 0 ] << 16 ) | ( wav[ i * 3 * 2 + 1 ] << 8 ) | wav[ i * 3 * 2 + 2 ];
317 rin = ( wav[ i * 3 * 2 + 3 ] << 16 ) | ( wav[ i * 3 * 2 + 4 ] << 8 ) | wav[ i * 3 * 2 + 5 ];
318 sampling_bits_convert( lin, lout );
319 sampling_bits_convert( rin, rout );
320 sound[ i ] = stereo_converter::convert_to( lout, rout );
321 }
322 break;
323
324 default:
325 return( false );
326 }
327
328 return( true );
329 }
330
331 static bool extract_wav_data32( unsigned char *wav, array< T, Allocator > &sound, size_type num_bytes, size_type channel_num )
332 {
333 signed int *wave = reinterpret_cast< signed int * >( wav );
334 size_type data_num = num_bytes / channel_num / 4;
335 sound.resize( data_num );
336
337 signed short lin, rin;
338 value_type lout, rout;
339 size_type i;
340
341 switch( channel_num )
342 {
343 case 1:
344 for( i = 0 ; i < sound.size( ) ; i++ )
345 {
346 lin = wave[ i ];
347 sampling_bits_convert( lin, lout );
348 sound[ i ] = stereo_converter::convert_to( lout, lout );
349 }
350 break;
351
352 case 2:
353 for( i = 0 ; i < sound.size( ) ; i++ )
354 {
355 lin = wave[ i * 2 + 0 ];
356 rin = wave[ i * 2 + 1 ];
357 sampling_bits_convert( lin, lout );
358 sampling_bits_convert( rin, rout );
359 sound[ i ] = stereo_converter::convert_to( lout, rout );
360 }
361 break;
362
363 default:
364 return( false );
365 }
366
367 return( true );
368 }
369
370 static bool compose_wav_data8( const array< T, Allocator > &sound, unsigned char *wav, size_type channel_num )
371 {
372 typedef typename sampling_bits_typedef< value_type >::value_type compose_type;
373 compose_type lin, rin;
374 unsigned char lout, rout;
375 size_type i;
376
377 switch( channel_num )
378 {
379 case 1:
380 for( i = 0 ; i < sound.size( ) ; i++ )
381 {
382 sampling_bits_convert( stereo_converter::convert_from( sound[ i ] ).l, lin );
383 sampling_bits_convert( lin, lout );
384 wav[ i ] = lout;
385 }
386 break;
387
388 case 2:
389 for( i = 0 ; i < sound.size( ) ; i++ )
390 {
391 stereo_type s( sound[ i ] );
392 sampling_bits_convert( s.l, lin );
393 sampling_bits_convert( s.r, rin );
394 sampling_bits_convert( lin, lout );
395 sampling_bits_convert( rin, rout );
396 wav[ i * 2 + 0 ] = lout;
397 wav[ i * 2 + 1 ] = rout;
398 }
399 break;
400
401 default:
402 return( false );
403 }
404
405 return( true );
406 }
407
408 static bool compose_wav_data16( const array< T, Allocator > &sound, unsigned char *wav, size_type channel_num )
409 {
410 signed short *wave = reinterpret_cast< signed short * >( wav );
411 typedef typename sampling_bits_typedef< value_type >::value_type compose_type;
412 compose_type lin, rin;
413 signed short lout, rout;
414 size_type i;
415
416 switch( channel_num )
417 {
418 case 1:
419 for( i = 0 ; i < sound.size( ) ; i++ )
420 {
421 sampling_bits_convert( stereo_converter::convert_from( sound[ i ] ).l, lin );
422 sampling_bits_convert( lin, lout );
423 wave[ i ] = lout;
424 }
425 break;
426
427 case 2:
428 for( i = 0 ; i < sound.size( ) ; i++ )
429 {
430 stereo_type s( sound[ i ] );
431 sampling_bits_convert( s.l, lin );
432 sampling_bits_convert( s.r, rin );
433 sampling_bits_convert( lin, lout );
434 sampling_bits_convert( rin, rout );
435 wave[ i * 2 + 0 ] = lout;
436 wave[ i * 2 + 1 ] = rout;
437 }
438 break;
439
440 default:
441 return( false );
442 }
443
444 return( true );
445 }
446
447 static bool compose_wav_data24( const array< T, Allocator > &sound, unsigned char *wav, size_type channel_num )
448 {
449 typedef typename sampling_bits_typedef< value_type >::value_type compose_type;
450 compose_type lin, rin;
451 signed int lout, rout;
452 size_type i;
453
454 switch( channel_num )
455 {
456 case 1:
457 for( i = 0 ; i < sound.size( ) ; i++ )
458 {
459 sampling_bits_convert( stereo_converter::convert_from( sound[ i ] ).l, lin );
460 sampling_bits_convert( lin, lout );
461 wav[ i * 3 + 0 ] = static_cast< unsigned char >( ( lout >> 16 ) & 0x000000ff );
462 wav[ i * 3 + 1 ] = static_cast< unsigned char >( ( lout >> 8 ) & 0x000000ff );
463 wav[ i * 3 + 2 ] = static_cast< unsigned char >( lout & 0x000000ff );
464 }
465 break;
466
467 case 2:
468 for( i = 0 ; i < sound.size( ) ; i++ )
469 {
470 stereo_type s( sound[ i ] );
471 sampling_bits_convert( s.l, lin );
472 sampling_bits_convert( s.r, rin );
473 sampling_bits_convert( lin, lout );
474 sampling_bits_convert( rin, rout );
475 wav[ i * 3 * 2 + 0 ] = static_cast< unsigned char >( ( lout >> 16 ) & 0x000000ff );
476 wav[ i * 3 * 2 + 1 ] = static_cast< unsigned char >( ( lout >> 8 ) & 0x000000ff );
477 wav[ i * 3 * 2 + 2 ] = static_cast< unsigned char >( lout & 0x000000ff );
478 wav[ i * 3 * 2 + 3 ] = static_cast< unsigned char >( ( rout >> 16 ) & 0x000000ff );
479 wav[ i * 3 * 2 + 4 ] = static_cast< unsigned char >( ( rout >> 8 ) & 0x000000ff );
480 wav[ i * 3 * 2 + 5 ] = static_cast< unsigned char >( rout & 0x000000ff );
481 }
482 break;
483
484 default:
485 return( false );
486 }
487
488 return( true );
489 }
490
491 static bool compose_wav_data32( const array< T, Allocator > &sound, unsigned char *wav, size_type channel_num )
492 {
493 signed int *wave = reinterpret_cast< signed int * >( wav );
494 typedef typename sampling_bits_typedef< value_type >::value_type compose_type;
495 compose_type lin, rin;
496 signed int lout, rout;
497 size_type i;
498
499 switch( channel_num )
500 {
501 case 1:
502 for( i = 0 ; i < sound.size( ) ; i++ )
503 {
504 sampling_bits_convert( stereo_converter::convert_from( sound[ i ] ).l, lin );
505 sampling_bits_convert( lin, lout );
506 wave[ i ] = lout;
507 }
508 break;
509
510 case 2:
511 for( i = 0 ; i < sound.size( ) ; i++ )
512 {
513 stereo_type s( sound[ i ] );
514 sampling_bits_convert( s.l, lin );
515 sampling_bits_convert( s.r, rin );
516 sampling_bits_convert( lin, lout );
517 sampling_bits_convert( rin, rout );
518 wave[ i * 2 + 0 ] = lout;
519 wave[ i * 2 + 1 ] = rout;
520 }
521 break;
522
523 default:
524 return( false );
525 }
526
527 return( true );
528 }
529
530 static bool convert_from_wav_data( unsigned char *wav, array< T, Allocator > &sound, unsigned int &sampling_rate )
531 {
532 // WAV用のヘッダの位置を指定する
533 _riffheader_ *priffhead = reinterpret_cast < _riffheader_ * >( wav );
534 _wavheader_ *pwavhead = reinterpret_cast < _wavheader_ * > ( wav + _riffheader_::bytes );
535 _fmtheader_ *pfmthead = reinterpret_cast < _fmtheader_ * > ( wav + _riffheader_::bytes + _wavheader_::bytes );
536
537 _riffheader_ &riffhead = *priffhead;
538 _wavheader_ &wavhead = *pwavhead;
539 _fmtheader_ &fmthead = *pfmthead;
540
541 if( !( riffhead.riff[0] == 'R' && riffhead.riff[1] == 'I' && riffhead.riff[2] == 'F' && riffhead.riff[3] == 'F' ) )
542 {
543 // WAVではありません
544 std::cerr << "This is not a wav format!" << std::endl;
545 return( false );
546 }
547
548 if( !( wavhead.wav[0] == 'W' && wavhead.wav[1] == 'A' && wavhead.wav[2] == 'V' && wavhead.wav[3] == 'E' ) )
549 {
550 // WAVではありません
551 std::cerr << "This is not a wav format!" << std::endl;
552 return( false );
553 }
554
555 if( !( fmthead.fmt[0] == 'f' && fmthead.fmt[1] == 'm' && fmthead.fmt[2] == 't' && fmthead.fmt[3] == ' ' ) )
556 {
557 // WAVではありません
558 std::cerr << "This is not a wav format!" << std::endl;
559 return( false );
560 }
561
562 _wavdata_ *pwavdata = reinterpret_cast < _wavdata_ * > ( wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 );
563 _wavdata_ &wavdata = *pwavdata;
564
565 sampling_rate = fmthead.sampling_rate;
566
567 if( fmthead.format_id != 0x0001 )
568 {
569 // リニア PCM のみサポート
570 std::cerr << "This is not a Linear PCM format!" << std::endl;
571 return( false );
572 }
573
574 bool ret = false;
575 switch( fmthead.bits_per_sample )
576 {
577 case 8:
578 ret = extract_wav_data8( wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes,
579 sound, wavdata.data_size, fmthead.channel_num );
580 break;
581
582 case 16:
583 ret = extract_wav_data16( wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes,
584 sound, wavdata.data_size, fmthead.channel_num );
585 break;
586
587 case 24:
588 ret = extract_wav_data24( wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes,
589 sound, wavdata.data_size, fmthead.channel_num );
590 break;
591
592 case 32:
593 ret = extract_wav_data32( wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes,
594 sound, wavdata.data_size, fmthead.channel_num );
595 break;
596
597 default:
598 // 未サポートの量子化ビット数
599 std::cerr << "This sampling format is not supported!" << std::endl;
600 break;
601 }
602
603 return( ret );
604 }
605
606 static bool convert_to_wav_data( const array< T, Allocator > &sound, unsigned char *wav, size_type wav_bits, size_type num_bytes, size_type sampling_rate )
607 {
608 size_t channel_num = stereo_converter::channel_num;
609
610 // WAV用のヘッダの位置を指定する
611 _riffheader_ *priffhead = reinterpret_cast < _riffheader_ * >( wav );
612 _wavheader_ *pwavhead = reinterpret_cast < _wavheader_ * > ( wav + _riffheader_::bytes );
613 _fmtheader_ *pfmthead = reinterpret_cast < _fmtheader_ * > ( wav + _riffheader_::bytes + _wavheader_::bytes );
614 _wavdata_ *pwavdata = reinterpret_cast < _wavdata_ * > ( wav + _riffheader_::bytes + _wavheader_::bytes + _fmtheader_::bytes );
615
616 _riffheader_ &riffhead = *priffhead;
617 _wavheader_ &wavhead = *pwavhead;
618 _fmtheader_ &fmthead = *pfmthead;
619 _wavdata_ &wavdata = *pwavdata;
620
621 riffhead.riff[0] = 'R';
622 riffhead.riff[1] = 'I';
623 riffhead.riff[2] = 'F';
624 riffhead.riff[3] = 'F';
625 riffhead.file_size = static_cast< unsigned int >( num_bytes - 8 );
626
627 wavhead.wav[0] = 'W';
628 wavhead.wav[1] = 'A';
629 wavhead.wav[2] = 'V';
630 wavhead.wav[3] = 'E';
631
632 fmthead.fmt[0] = 'f';
633 fmthead.fmt[1] = 'm';
634 fmthead.fmt[2] = 't';
635 fmthead.fmt[3] = ' ';
636 fmthead.fmt_size = 16;
637 fmthead.format_id = 0x0001;
638 fmthead.channel_num = static_cast< unsigned short >( channel_num );
639 fmthead.sampling_rate = static_cast< unsigned int >( sampling_rate );
640 fmthead.bytes_per_sample = wav_bits == 8 ? 1 : 2;
641 fmthead.bytes_per_second = static_cast< unsigned int >( sampling_rate * fmthead.bytes_per_sample );
642 fmthead.bits_per_sample = static_cast< unsigned short >( wav_bits );
643
644 wavdata.data[0] = 'd';
645 wavdata.data[1] = 'a';
646 wavdata.data[2] = 't';
647 wavdata.data[3] = 'a';
648 wavdata.data_size = static_cast< unsigned int >( sound.size( ) * channel_num * fmthead.bytes_per_sample );
649
650 bool ret = false;
651 switch( fmthead.bits_per_sample )
652 {
653 case 8:
654 ret = compose_wav_data8( sound, wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes, fmthead.channel_num );
655 break;
656
657 case 16:
658 ret = compose_wav_data16( sound, wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes, fmthead.channel_num );
659 break;
660
661 case 24:
662 ret = compose_wav_data24( sound, wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes, fmthead.channel_num );
663 break;
664
665 case 32:
666 ret = compose_wav_data32( sound, wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes, fmthead.channel_num );
667 break;
668
669 default:
670 // 未サポートの量子化ビット数
671 std::cerr << "This sampling format is not supported!" << std::endl;
672 break;
673 }
674
675 return( true );
676 }
677
678 static bool read( array< T, Allocator > &sound, const std::string &filename, unsigned int &sampling_rate )
679 {
680 size_type filesize;
681 FILE *fp;
682 if( ( fp = fopen( filename.c_str( ), "rb" ) ) == NULL ) return( false );
683 // ファイルサイズを取得
684 fseek( fp, 0, SEEK_END );
685 filesize = ftell( fp );
686 fseek( fp, 0, SEEK_SET );
687
688 unsigned char *buff = new unsigned char[ filesize + 1 ];
689 unsigned char *pointer = buff;
690 size_type read_size = 0;
691 while( feof( fp ) == 0 )
692 {
693 read_size = fread( pointer, sizeof( unsigned char ), 1024, fp );
694 if( read_size < 1024 )
695 {
696 break;
697 }
698 pointer += read_size;
699 }
700 fclose( fp );
701
702 bool ret = convert_from_wav_data( buff, sound, sampling_rate );
703 delete [] buff;
704 return( ret );
705 }
706
707 static bool write( const array< T, Allocator > &sound, const std::string &filename, size_type wav_bits, size_type sampling_rate )
708 {
709 size_type size = get_wav_bytes( sound, wav_bits, 1 );
710
711 unsigned char *buff = new unsigned char[ size + 1 ];
712 bool ret = convert_to_wav_data( sound, buff, wav_bits, size, sampling_rate );
713
714 if( !ret )
715 {
716 delete [] buff;
717 return( false );
718 }
719
720 FILE *fp;
721 if( ( fp = fopen( filename.c_str( ), "wb" ) ) == NULL )
722 {
723 delete [] buff;
724 return( false );
725 }
726
727 // ファイルへ書き出し
728 unsigned char *pointer = buff;
729 size_type write_size = 0, writed_size = 0;
730 while( size > 0 )
731 {
732 write_size = size < 1024 ? size : 1024;
733
734 writed_size = fwrite( pointer, sizeof( unsigned char ), write_size, fp );
735 pointer += writed_size;
736 size -= writed_size;
737 if( write_size != writed_size )
738 {
739 fclose( fp );
740 delete [] buff;
741 return( false );
742 }
743 }
744 fclose( fp );
745
746 delete [] buff;
747 return( true );
748 }
749 };
750 }
751
752
755
763
764
774 template < class T, class Allocator >
775 bool read_wav( array< T, Allocator > &sound, const std::string &filename, unsigned int &sampling_rate )
776 {
777 return( __wav_controller__::wav_controller< T, Allocator >::read( sound, filename, sampling_rate ) );
778 }
779
780
790 template < class T, class Allocator >
791 bool read_wav( array< T, Allocator > &sound, const std::wstring &filename, unsigned int &sampling_rate )
792 {
793 return( read_wav( sound, wstr2str( filename ), sampling_rate ) );
794 }
795
796
805 template < class T, class Allocator >
806 bool read_wav( array< T, Allocator > &sound, const std::string &filename )
807 {
808 unsigned int sampling_rate = 44100;
809 return( read_wav( sound, filename, sampling_rate ) );
810 }
811
812
821 template < class T, class Allocator >
822 bool read_wav( array< T, Allocator > &sound, const std::wstring &filename )
823 {
824 unsigned int sampling_rate = 44100;
825 return( read_wav( sound, wstr2str( filename ), sampling_rate ) );
826 }
827
828
841 template < class T, class Allocator >
842 bool write_wav( const array< T, Allocator > &sound, const std::string &filename, typename array< T, Allocator >::size_type wav_bits,
843 typename array< T, Allocator >::size_type sampling_rate )
844 {
845 return( __wav_controller__::wav_controller< T, Allocator >::write( sound, filename, wav_bits, sampling_rate ) );
846 }
847
848
861 template < class T, class Allocator >
862 bool write_wav( const array< T, Allocator > &sound, const std::wstring &filename, typename array< T, Allocator >::size_type wav_bits,
863 typename array< T, Allocator >::size_type sampling_rate )
864 {
865 return( write_wav( sound, wstr2str( filename ), wav_bits, sampling_rate ) );
866 }
867
868
870 // WAV 音声入出力グループの終わり
871
873 // 音声入出力グループの終わり
874
875
876 // mist名前空間の終わり
877 _MIST_END
878
879
880 #endif // __INCLUDE_MIST_WAV__
881