allow other channel for non-dsp sounds (2D oneshots)
[vg.git] / vg_depencies.c
1 #include "vg_vorbis.h"
2 #include "vg_image.h"
3
4 #undef STB_VORBIS_HEADER_ONLY
5 #include "submodules/stb/stb_vorbis.c"
6 #undef L
7 #undef R
8 #undef C
9
10 #define STB_IMAGE_WRITE_IMPLEMENTATION
11 #define STB_IMAGE_IMPLEMENTATION
12 #include "vg/submodules/stb/stb_image.h"
13 #include "vg/submodules/stb/stb_image_write.h"
14
15 /*
16 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
17 */
18 int
19 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis *f, float *buffer,
20 int len )
21 {
22 int n = 0, c = 1;
23 if( f->channels < 2 ) c = 0;
24
25 while( n < len ) {
26 int k = f->channel_buffer_end - f->channel_buffer_start;
27
28 if( n+k >= len )
29 k = len - n;
30
31 for( int j=0; j < k; ++j ) {
32 *buffer++ = f->channel_buffers[ 0 ][f->channel_buffer_start+j];
33 *buffer++ = f->channel_buffers[ c ][f->channel_buffer_start+j];
34 }
35
36 n += k;
37 f->channel_buffer_start += k;
38
39 if( n == len )
40 break;
41
42 if( !stb_vorbis_get_frame_float( f, NULL, NULL ))
43 break;
44 }
45
46 return n;
47 }
48
49 /*
50 * ........ more wrecked code sorry!
51 */
52 int
53 stb_vorbis_get_samples_i16_downmixed( stb_vorbis *f, i16 *buffer, int len )
54 {
55 int n = 0, c = 1;
56 if( f->channels < 2 ) c = 0;
57
58 while( n < len ) {
59 int k = f->channel_buffer_end - f->channel_buffer_start;
60
61 if( n+k >= len )
62 k = len - n;
63
64 for( int j=0; j < k; ++j ) {
65 float sl = f->channel_buffers[ 0 ][f->channel_buffer_start+j],
66 sr = f->channel_buffers[ c ][f->channel_buffer_start+j];
67
68 *buffer++ = vg_clampf( 0.5f*(sl+sr), -1.0f, 1.0f ) * 32767.0f;
69 //*buffer++ = vg_clampf( sr, -1.0f, 1.0f ) * 32767.0f;
70 }
71
72 n += k;
73 f->channel_buffer_start += k;
74
75 if( n == len )
76 break;
77
78 if( !stb_vorbis_get_frame_float( f, NULL, NULL ))
79 break;
80 }
81
82 return n;
83 }