1 #include "../../c-flod/backends/wave_format.h"
2 #include "../../c-flod/flashlib/ByteArray.h"
6 struct ByteArray wave_streams
[1];
7 struct ByteArray out_wave
;
8 WAVE_HEADER_COMPLETE wavhdr
;
11 static int handle_overflow(int *sample
) {
15 } else if (*sample
< -32768) {
23 struct ByteArray
* mine
= &playa
.wave_streams
[0];
24 struct ByteArray
* out
= &playa
.out_wave
;
26 if(!mine
->bytesAvailable(mine
)) return;
29 size_t upsample_factor
= 44100 / playa
.wavhdr
.wave_hdr
.samplerate
;
30 size_t readbytes
= playa
.wavhdr
.wave_hdr
.bitwidth
== 8 ? 1 : 2;
31 int chan
[2] = { 0, 0 };
34 while(mine
->bytesAvailable(mine
)) {
37 for(c
= 0; c
< 2; c
++) {
38 if(c
< playa
.wavhdr
.wave_hdr
.channels
) {
39 if(readbytes
== 1) next
[c
] = ((uint8_t) ByteArray_readByte(mine
) - 128) * 256;
40 else next
[c
] = ByteArray_readShort(mine
);
41 handle_overflow(&next
[c
]);
43 next
[c
] = next
[c
- 1];
45 for(u
= 0; u
< upsample_factor
; u
++) {
46 for(c
= 0; c
< 2; c
++) {
47 int interpolated
= u
== 0 ? chan
[c
] :
48 chan
[c
] + ((next
[c
]-chan
[c
]) * ((float)u
/(float)upsample_factor
));
49 ByteArray_writeShort(out
, interpolated
);
52 for (c
=0; c
<2; c
++) chan
[c
] = next
[c
];
58 static char buf
[1024* 1024];
60 int main(int argc
, char ** argv
) {
61 struct ByteArray
*ba
= &playa
.wave_streams
[0];
63 if(!ByteArray_open_file(ba
, argv
[1])) return 1;
64 ByteArray_set_endian(ba
, BAE_LITTLE
);
65 ByteArray_readMultiByte(ba
, (void*) &playa
.wavhdr
, sizeof(WAVE_HEADER_COMPLETE
));
66 struct ByteArray
*out
= &playa
.out_wave
;
68 ByteArray_set_endian(out
, BAE_LITTLE
);
69 ByteArray_open_mem(out
, buf
, sizeof buf
);
71 ByteArray_dump_to_file(out
, "test.raw");