33 #ifndef __INCLUDE_MIST_WAV__
34 #define __INCLUDE_MIST_WAV__
37 #ifndef __INCLUDE_MIST_H__
42 #ifndef __INCLUDE_MIST_STEREO__
43 #include "../config/stereo.h"
47 #ifndef __INCLUDE_MIST_LIMITS__
48 #include "../config/stereo.h"
64 namespace __wav_controller__
71 unsigned char byte[
sizeof( value_type ) ];
72 byte_array(
const value_type &v = 0 ) : value( v ){ }
76 #if defined(__MIST_MSVC__) || defined(__INTEL_COMPILER)
77 #pragma pack( push, wav_align, 1 )
82 unsigned char riff[4];
83 unsigned int file_size;
96 unsigned int fmt_size;
97 unsigned short format_id;
98 unsigned short channel_num;
99 unsigned int sampling_rate;
100 unsigned int bytes_per_second;
101 unsigned short bytes_per_sample;
102 unsigned short bits_per_sample;
108 unsigned char data[4];
109 unsigned int data_size;
112 #if defined(__MIST_MSVC__) || defined(__INTEL_COMPILER)
113 #pragma pack( pop, wav_align )
118 struct sampling_bits_typedef
120 typedef T value_type;
123 #define __SAMPLING_BITS_CONVERTER__( from, to, expression ) \
124 inline void sampling_bits_convert( const from &a, to &b ){ b = static_cast< to >( expression ); }\
126 #define __SAMPLING_BITS_TYPEDEF__( from, to ) \
128 struct sampling_bits_typedef< from >\
130 typedef to value_type;\
133 __SAMPLING_BITS_CONVERTER__(
unsigned char,
unsigned char, a )
134 __SAMPLING_BITS_CONVERTER__(
unsigned char,
signed char, a - 128 )
135 __SAMPLING_BITS_CONVERTER__(
unsigned char,
char, a - 128 )
136 __SAMPLING_BITS_CONVERTER__(
unsigned char,
unsigned short, a * 257.0 )
137 __SAMPLING_BITS_CONVERTER__(
unsigned char,
signed short, a * 257.0 - 32768.0 )
138 __SAMPLING_BITS_CONVERTER__(
unsigned char,
unsigned int, a * 65793.0 )
139 __SAMPLING_BITS_CONVERTER__(
unsigned char,
signed int, a * 65793.0 - 8388607.0 )
140 __SAMPLING_BITS_CONVERTER__(
unsigned char,
unsigned long, a * 65793.0 )
141 __SAMPLING_BITS_CONVERTER__(
unsigned char,
signed long, a * 65793.0 - 8388607.0 )
142 __SAMPLING_BITS_CONVERTER__(
unsigned char,
float, a * 65793.0 )
143 __SAMPLING_BITS_CONVERTER__(
unsigned char,
double, a * 65793.0 - 8388607.0 )
144 __SAMPLING_BITS_CONVERTER__(
unsigned char,
long double, a * 65793.0 - 8388607.0 )
146 __SAMPLING_BITS_CONVERTER__(
signed short,
unsigned char, a / 257.0 + 128.0 )
147 __SAMPLING_BITS_CONVERTER__(
signed short,
signed char, a / 257.0 )
148 __SAMPLING_BITS_CONVERTER__(
signed short,
char, a / 257.0 )
149 __SAMPLING_BITS_CONVERTER__(
signed short,
unsigned short, a + 32768.0 )
150 __SAMPLING_BITS_CONVERTER__(
signed short,
signed short, a )
151 __SAMPLING_BITS_CONVERTER__(
signed short,
unsigned int, a * 256.0 + 8388607.0 )
152 __SAMPLING_BITS_CONVERTER__(
signed short,
signed int, a * 256.0 )
153 __SAMPLING_BITS_CONVERTER__(
signed short,
float, a * 256.0 )
154 __SAMPLING_BITS_CONVERTER__(
signed short,
double, a * 256.0 )
155 __SAMPLING_BITS_CONVERTER__(
signed short,
long double, a * 256.0 )
157 __SAMPLING_BITS_CONVERTER__(
signed int,
unsigned char, a / 65793.0 + 128.0 )
158 __SAMPLING_BITS_CONVERTER__(
signed int,
signed char, a / 65793.0 )
159 __SAMPLING_BITS_CONVERTER__(
signed int,
char, a / 65793.0 )
160 __SAMPLING_BITS_CONVERTER__(
signed int,
unsigned short, a / 256.0 + 32768.0 )
161 __SAMPLING_BITS_CONVERTER__(
signed int,
signed short, a / 256.0 )
162 __SAMPLING_BITS_CONVERTER__(
signed int,
unsigned int, a + 8388607.0 )
163 __SAMPLING_BITS_CONVERTER__(
signed int,
signed int, a )
164 __SAMPLING_BITS_CONVERTER__(
signed int,
float, a )
165 __SAMPLING_BITS_CONVERTER__(
signed int,
double, a )
166 __SAMPLING_BITS_CONVERTER__(
signed int,
long double, a )
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 );
179 #undef __SAMPLING_BITS_CONVERTER__
180 #undef __SAMPLING_BITS_TYPEDEF__
182 template <
class T,
class Allocator >
183 struct wav_controller
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;
191 static size_type get_wav_bytes(
const array< T, Allocator > &sound, size_type bits_per_sample, size_type channel_num )
193 switch( bits_per_sample )
196 return( _riffheader_::bytes + _wavheader_::bytes + _fmtheader_::bytes + _wavdata_::bytes + sound.size( ) * channel_num );
200 return( _riffheader_::bytes + _wavheader_::bytes + _fmtheader_::bytes + _wavdata_::bytes + sound.size( ) * 2 * channel_num );
204 return( _riffheader_::bytes + _wavheader_::bytes + _fmtheader_::bytes + _wavdata_::bytes + sound.size( ) * 3 * channel_num );
208 return( _riffheader_::bytes + _wavheader_::bytes + _fmtheader_::bytes + _wavdata_::bytes + sound.size( ) * 4 * channel_num );
216 static bool extract_wav_data8(
unsigned char *wav, array< T, Allocator > &sound, size_type num_bytes, size_type channel_num )
218 size_type data_num = num_bytes / channel_num;
219 sound.resize( data_num );
221 unsigned char lin, rin;
222 value_type lout, rout;
225 switch( channel_num )
228 for( i = 0 ; i < sound.size( ) ; i++ )
231 sampling_bits_convert( lin, lout );
232 sound[ i ] = stereo_converter::convert_to( lout, lout );
237 for( i = 0 ; i < sound.size( ) ; i++ )
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 );
254 static bool extract_wav_data16(
unsigned char *wav, array< T, Allocator > &sound, size_type num_bytes, size_type channel_num )
256 signed short *wave =
reinterpret_cast< signed short *
>( wav );
257 size_type data_num = num_bytes / channel_num / 2;
258 sound.resize( data_num );
260 signed short lin, rin;
261 value_type lout, rout;
264 switch( channel_num )
267 for( i = 0 ; i < sound.size( ) ; i++ )
270 sampling_bits_convert( lin, lout );
271 sound[ i ] = stereo_converter::convert_to( lout, lout );
276 for( i = 0 ; i < sound.size( ) ; i++ )
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 );
293 static bool extract_wav_data24(
unsigned char *wav, array< T, Allocator > &sound, size_type num_bytes, size_type channel_num )
295 size_type data_num = num_bytes / channel_num / 3;
296 sound.resize( data_num );
299 value_type lout, rout;
302 switch( channel_num )
305 for( i = 0 ; i < sound.size( ) ; i++ )
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 );
314 for( i = 0 ; i < sound.size( ) ; i++ )
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 );
331 static bool extract_wav_data32(
unsigned char *wav, array< T, Allocator > &sound, size_type num_bytes, size_type channel_num )
333 signed int *wave =
reinterpret_cast< signed int *
>( wav );
334 size_type data_num = num_bytes / channel_num / 4;
335 sound.resize( data_num );
337 signed short lin, rin;
338 value_type lout, rout;
341 switch( channel_num )
344 for( i = 0 ; i < sound.size( ) ; i++ )
347 sampling_bits_convert( lin, lout );
348 sound[ i ] = stereo_converter::convert_to( lout, lout );
353 for( i = 0 ; i < sound.size( ) ; i++ )
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 );
370 static bool compose_wav_data8(
const array< T, Allocator > &sound,
unsigned char *wav, size_type channel_num )
372 typedef typename sampling_bits_typedef< value_type >::value_type compose_type;
373 compose_type lin, rin;
374 unsigned char lout, rout;
377 switch( channel_num )
380 for( i = 0 ; i < sound.size( ) ; i++ )
382 sampling_bits_convert( stereo_converter::convert_from( sound[ i ] ).l, lin );
383 sampling_bits_convert( lin, lout );
389 for( i = 0 ; i < sound.size( ) ; i++ )
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;
408 static bool compose_wav_data16(
const array< T, Allocator > &sound,
unsigned char *wav, size_type channel_num )
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;
416 switch( channel_num )
419 for( i = 0 ; i < sound.size( ) ; i++ )
421 sampling_bits_convert( stereo_converter::convert_from( sound[ i ] ).l, lin );
422 sampling_bits_convert( lin, lout );
428 for( i = 0 ; i < sound.size( ) ; i++ )
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;
447 static bool compose_wav_data24(
const array< T, Allocator > &sound,
unsigned char *wav, size_type channel_num )
449 typedef typename sampling_bits_typedef< value_type >::value_type compose_type;
450 compose_type lin, rin;
451 signed int lout, rout;
454 switch( channel_num )
457 for( i = 0 ; i < sound.size( ) ; i++ )
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 );
468 for( i = 0 ; i < sound.size( ) ; i++ )
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 );
491 static bool compose_wav_data32(
const array< T, Allocator > &sound,
unsigned char *wav, size_type channel_num )
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;
499 switch( channel_num )
502 for( i = 0 ; i < sound.size( ) ; i++ )
504 sampling_bits_convert( stereo_converter::convert_from( sound[ i ] ).l, lin );
505 sampling_bits_convert( lin, lout );
511 for( i = 0 ; i < sound.size( ) ; i++ )
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;
530 static bool convert_from_wav_data(
unsigned char *wav, array< T, Allocator > &sound,
unsigned int &sampling_rate )
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 );
537 _riffheader_ &riffhead = *priffhead;
538 _wavheader_ &wavhead = *pwavhead;
539 _fmtheader_ &fmthead = *pfmthead;
541 if( !( riffhead.riff[0] ==
'R' && riffhead.riff[1] ==
'I' && riffhead.riff[2] ==
'F' && riffhead.riff[3] ==
'F' ) )
544 std::cerr <<
"This is not a wav format!" << std::endl;
548 if( !( wavhead.wav[0] ==
'W' && wavhead.wav[1] ==
'A' && wavhead.wav[2] ==
'V' && wavhead.wav[3] ==
'E' ) )
551 std::cerr <<
"This is not a wav format!" << std::endl;
555 if( !( fmthead.fmt[0] ==
'f' && fmthead.fmt[1] ==
'm' && fmthead.fmt[2] ==
't' && fmthead.fmt[3] ==
' ' ) )
558 std::cerr <<
"This is not a wav format!" << std::endl;
562 _wavdata_ *pwavdata = reinterpret_cast < _wavdata_ * > ( wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 );
563 _wavdata_ &wavdata = *pwavdata;
565 sampling_rate = fmthead.sampling_rate;
567 if( fmthead.format_id != 0x0001 )
570 std::cerr <<
"This is not a Linear PCM format!" << std::endl;
575 switch( fmthead.bits_per_sample )
578 ret = extract_wav_data8( wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes,
579 sound, wavdata.data_size, fmthead.channel_num );
583 ret = extract_wav_data16( wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes,
584 sound, wavdata.data_size, fmthead.channel_num );
588 ret = extract_wav_data24( wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes,
589 sound, wavdata.data_size, fmthead.channel_num );
593 ret = extract_wav_data32( wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes,
594 sound, wavdata.data_size, fmthead.channel_num );
599 std::cerr <<
"This sampling format is not supported!" << std::endl;
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 )
608 size_t channel_num = stereo_converter::channel_num;
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 );
616 _riffheader_ &riffhead = *priffhead;
617 _wavheader_ &wavhead = *pwavhead;
618 _fmtheader_ &fmthead = *pfmthead;
619 _wavdata_ &wavdata = *pwavdata;
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 );
627 wavhead.wav[0] =
'W';
628 wavhead.wav[1] =
'A';
629 wavhead.wav[2] =
'V';
630 wavhead.wav[3] =
'E';
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 );
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 );
651 switch( fmthead.bits_per_sample )
654 ret = compose_wav_data8( sound, wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes, fmthead.channel_num );
658 ret = compose_wav_data16( sound, wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes, fmthead.channel_num );
662 ret = compose_wav_data24( sound, wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes, fmthead.channel_num );
666 ret = compose_wav_data32( sound, wav + _riffheader_::bytes + _wavheader_::bytes + fmthead.fmt_size + 8 + _wavdata_::bytes, fmthead.channel_num );
671 std::cerr <<
"This sampling format is not supported!" << std::endl;
678 static bool read( array< T, Allocator > &sound,
const std::string &filename,
unsigned int &sampling_rate )
682 if( ( fp = fopen( filename.c_str( ),
"rb" ) ) == NULL )
return(
false );
684 fseek( fp, 0, SEEK_END );
685 filesize = ftell( fp );
686 fseek( fp, 0, SEEK_SET );
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 )
693 read_size = fread( pointer,
sizeof(
unsigned char ), 1024, fp );
694 if( read_size < 1024 )
698 pointer += read_size;
702 bool ret = convert_from_wav_data( buff, sound, sampling_rate );
707 static bool write(
const array< T, Allocator > &sound,
const std::string &filename, size_type wav_bits, size_type sampling_rate )
709 size_type size = get_wav_bytes( sound, wav_bits, 1 );
711 unsigned char *buff =
new unsigned char[ size + 1 ];
712 bool ret = convert_to_wav_data( sound, buff, wav_bits, size, sampling_rate );
721 if( ( fp = fopen( filename.c_str( ),
"wb" ) ) == NULL )
728 unsigned char *pointer = buff;
729 size_type write_size = 0, writed_size = 0;
732 write_size = size < 1024 ? size : 1024;
734 writed_size = fwrite( pointer,
sizeof(
unsigned char ), write_size, fp );
735 pointer += writed_size;
737 if( write_size != writed_size )
774 template <
class T,
class Allocator >
777 return( __wav_controller__::wav_controller< T, Allocator >::read( sound, filename, sampling_rate ) );
790 template <
class T,
class Allocator >
805 template <
class T,
class Allocator >
808 unsigned int sampling_rate = 44100;
809 return(
read_wav( sound, filename, sampling_rate ) );
821 template <
class T,
class Allocator >
824 unsigned int sampling_rate = 44100;
841 template <
class T,
class Allocator >
845 return( __wav_controller__::wav_controller< T, Allocator >::write( sound, filename, wav_bits, sampling_rate ) );
861 template <
class T,
class Allocator >
880 #endif // __INCLUDE_MIST_WAV__