more horizontal refactoring
[swftools.git] / lib / modules / swfsound.c
1 /* swfaction.c
2
3    SWF Sound handling routines
4    
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2001, 2002 Matthias Kramm <kramm@quiss.org>
9  
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
23
24 #ifndef NO_MP3
25
26 #include "../rfxswf.h"
27
28 #ifdef BLADEENC
29 #define HAVE_SOUND
30
31 CodecInitOut * init = 0;
32 void swf_SetSoundStreamHead(TAG*tag, U16 avgnumsamples)
33 {
34     U8 playbackrate = 3; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
35     U8 playbacksize = 1; // 0 = 8 bit, 1 = 16 bit
36     U8 playbacktype = 0; // 0 = mono, 1 = stereo
37     U8 compression = 2; // 0 = raw, 1 = ADPCM, 2 = mp3, 3 = raw le, 6 = nellymoser
38     U8 rate = 3; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
39     U8 size = 1; // 0 = 8 bit, 1 = 16 bit
40     U8 type = 0; // 0 = mono, 1 = stereo
41
42     CodecInitIn params;
43     memset(&params, 0, sizeof(params));
44     params.frequency = 44100;  //48000, 44100 or 32000
45     params.mode = 3;      //0 = Stereo, 2 = Dual Channel, 3 = Mono
46     params.emphasis = 0;  //0 = None, 1 = 50/15 microsec, 3 = CCITT J.17
47     params.bitrate = 128;         //default is 128 (64 for mono)
48     init = codecInit(&params);
49
50     swf_SetU8(tag,(playbackrate<<2)|(playbacksize<<1)|playbacktype);
51     swf_SetU8(tag,(compression<<4)|(rate<<2)|(size<<1)|type);
52     swf_SetU16(tag,avgnumsamples);
53
54     printf("numSamples:%d\n",init->nSamples);
55     printf("bufferSize:%d\n",init->bufferSize);
56 }
57
58 void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int numsamples, char first)
59 {
60     char*buf;
61     int len = 0;
62
63     buf = rfx_alloc(init->bufferSize);
64     if(!buf)
65         return;
66     
67     len = codecEncodeChunk (numsamples, samples, buf);
68     len += codecFlush (&buf[len]);
69     len += codecExit (&buf[len]);
70
71     if(first) {
72         swf_SetU16(tag, numsamples); // number of samples
73         swf_SetU16(tag, 0); // seek
74     }
75     swf_SetBlock(tag, buf, len);
76     rfx_free(buf);
77 }
78 #endif
79
80 void swf_SetSoundDefineRaw(TAG*tag, S16*samples, int numsamples)
81 {
82     swf_SetU8(tag,(/*compression*/0<<4)|(/*rate*/3<<2)|(/*size*/1<<1)|/*mono*/0);
83     swf_SetU32(tag, numsamples); // 44100 -> 11025
84     swf_SetBlock(tag, (U8*)samples, numsamples*2);
85 }
86
87 /* TODO: find a better way to set these from the outside */
88
89 int swf_mp3_in_samplerate = 44100;
90 int swf_mp3_out_samplerate = 11025;
91 int swf_mp3_channels = 1;
92 int swf_mp3_bitrate = 32;
93
94 #ifdef HAVE_LAME
95 #define HAVE_SOUND
96
97 #include <stdarg.h>
98 #include <lame.h>
99
100 static lame_global_flags*lame_flags;
101
102 void null_errorf(const char *format, va_list ap)
103 {
104 }
105
106 static void initlame()
107 {
108     unsigned char buf[4096];
109     int bufsize = 1152*2;
110
111     lame_flags = lame_init();
112
113     lame_set_in_samplerate(lame_flags, swf_mp3_in_samplerate);
114     lame_set_num_channels(lame_flags, swf_mp3_channels);
115     lame_set_scale(lame_flags, 0);
116
117     // MPEG1    32, 44.1,   48khz
118     // MPEG2    16, 22.05,  24
119     // MPEG2.5   8, 11.025, 12
120     lame_set_out_samplerate(lame_flags, swf_mp3_out_samplerate);
121
122     lame_set_quality(lame_flags, 0);
123     lame_set_mode(lame_flags, MONO/*3*/);
124     lame_set_brate(lame_flags, swf_mp3_bitrate);
125     //lame_set_compression_ratio(lame_flags, 11.025);
126     lame_set_bWriteVbrTag(lame_flags, 0);
127
128     lame_init_params(lame_flags);
129     lame_init_bitstream(lame_flags);
130
131     lame_set_errorf(lame_flags, null_errorf);
132     /* The first two flush calls to lame always fail, for
133        some reason. Do them here where they cause no damage. */
134     lame_encode_flush_nogap(lame_flags, buf, bufsize);
135     //printf("init:flush_nogap():%d\n", len);
136     lame_encode_flush(lame_flags, buf, bufsize);
137     //printf("init:flush():%d\n", len);
138     lame_set_errorf(lame_flags, 0);
139 }
140
141 void swf_SetSoundStreamHead(TAG*tag, int avgnumsamples)
142 {
143     int len;
144
145     U8 playbackrate = 1; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
146     U8 playbacksize = 1; // 0 = 8 bit, 1 = 16 bit
147     U8 playbacktype = 0; // 0 = mono, 1 = stereo
148     U8 compression = 2; // 0 = raw, 1 = ADPCM, 2 = mp3, 3 = raw le, 6 = nellymoser
149     U8 rate = 1; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
150     U8 size = 1; // 0 = 8 bit, 1 = 16 bit
151     U8 type = 0; // 0 = mono, 1 = stereo
152
153     if(swf_mp3_out_samplerate == 5512) playbackrate = rate = 0; // lame doesn't support this
154     else if(swf_mp3_out_samplerate == 11025) playbackrate = rate = 1;
155     else if(swf_mp3_out_samplerate == 22050) playbackrate = rate = 2;
156     else if(swf_mp3_out_samplerate == 44100) playbackrate = rate = 3;
157     else fprintf(stderr, "Invalid samplerate: %d\n", swf_mp3_out_samplerate);
158     
159     initlame();
160
161     swf_SetU8(tag,(playbackrate<<2)|(playbacksize<<1)|playbacktype);
162     swf_SetU8(tag,(compression<<4)|(rate<<2)|(size<<1)|type);
163     swf_SetU16(tag,avgnumsamples);
164 }
165
166 void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int seek, char first)
167 {
168     char*buf;
169     int len = 0;
170     int bufsize = 16384;
171     int numsamples = (int)(((swf_mp3_out_samplerate > 22050) ? 1152 : 576) * ((double)swf_mp3_in_samplerate/swf_mp3_out_samplerate));
172     int fs = 0;
173
174     buf = rfx_alloc(bufsize);
175     if(!buf)
176         return;
177
178     if(first) {
179         fs = lame_get_framesize(lame_flags);
180         swf_SetU16(tag, fs * first); // samples per mp3 frame
181         swf_SetU16(tag, seek); // seek
182     }
183
184     len += lame_encode_buffer(lame_flags, samples, samples, numsamples, &buf[len], bufsize-len);
185     len += lame_encode_flush_nogap(lame_flags, &buf[len], bufsize-len);
186     swf_SetBlock(tag, buf, len);
187     if(len == 0) {
188         fprintf(stderr, "error: mp3 empty block, %d samples, first:%d, framesize:%d\n",
189                 numsamples, first, fs);
190     }/* else {
191         fprintf(stderr, "ok: mp3 nonempty block, %d samples, first:%d, framesize:%d\n",
192                 numsamples, first, fs);
193     }*/
194     rfx_free(buf);
195 }
196
197 void swf_SetSoundStreamEnd(TAG*tag)
198 {
199     lame_close (lame_flags);
200 }
201
202 void swf_SetSoundDefine(TAG*tag, S16*samples, int num)
203 {
204     char*buf;
205     int oldlen=0,len = 0;
206     int bufsize = 16384;
207     int blocksize = (int)(((swf_mp3_out_samplerate > 22050) ? 1152 : 576) * ((double)swf_mp3_in_samplerate/swf_mp3_out_samplerate));
208     int t;
209     int blocks;
210
211     U8 compression = 2; // 0 = raw, 1 = ADPCM, 2 = mp3, 3 = raw le, 6 = nellymoser
212     U8 rate = 1; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
213     U8 size = 1; // 0 = 8 bit, 1 = 16 bit
214     U8 type = 0; // 0 = mono, 1 = stereo
215     
216     if(swf_mp3_out_samplerate == 5512) rate = 0;
217     else if(swf_mp3_out_samplerate == 11025) rate = 1;
218     else if(swf_mp3_out_samplerate == 22050) rate = 2;
219     else if(swf_mp3_out_samplerate == 44100) rate = 3;
220     else fprintf(stderr, "Invalid samplerate: %d\n", swf_mp3_out_samplerate);
221
222     blocks = num / (blocksize);
223
224     swf_SetU8(tag,(compression<<4)|(rate<<2)|(size<<1)|type);
225
226     swf_SetU32(tag, (int)(tag,blocks*blocksize / 
227             ((double)swf_mp3_in_samplerate/swf_mp3_out_samplerate)) // account for resampling
228             );
229
230     buf = rfx_alloc(bufsize);
231     if(!buf)
232         return;
233
234     initlame();
235
236     swf_SetU16(tag, 0); //delayseek
237     for(t=0;t<blocks;t++) {
238         int s;
239         U16*pos;
240         pos= &samples[t*blocksize];
241         len += lame_encode_buffer(lame_flags, pos, pos, blocksize, &buf[len], bufsize-len);
242         len += lame_encode_flush_nogap(lame_flags, &buf[len], bufsize-len);
243         swf_SetBlock(tag, buf, len);
244         len = 0;
245     }
246
247     rfx_free(buf);
248 }
249
250 #endif
251
252 #endif
253
254 #ifndef HAVE_SOUND
255
256 // supply stubs
257
258 void swf_SetSoundStreamHead(TAG*tag, int avgnumsamples)
259 {
260     fprintf(stderr, "Error: no mp3 soundstream support compiled in.\n");exit(1);
261 }
262 void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int seek, char first)
263 {
264     fprintf(stderr, "Error: no mp3 soundstream support compiled in.\n");exit(1);
265 }
266 void swf_SetSoundStreamEnd(TAG*tag)
267 {
268     fprintf(stderr, "Error: no mp3 soundstream support compiled in.\n");exit(1);
269 }
270 void swf_SetSoundDefine(TAG*tag, S16*samples, int num)
271 {
272     swf_SetSoundDefineRaw(tag, samples,num);
273 }
274
275 #endif
276
277 #define SOUNDINFO_STOP 32
278 #define SOUNDINFO_NOMULTIPLE 16
279 #define SOUNDINFO_HASENVELOPE 8
280 #define SOUNDINFO_HASLOOPS 4
281 #define SOUNDINFO_HASOUTPOINT 2
282 #define SOUNDINFO_HASINPOINT 1
283
284
285 void swf_SetSoundInfo(TAG*tag, SOUNDINFO*info)
286 {
287     U8 flags = (info->stop?SOUNDINFO_STOP:0)
288               |(info->nomultiple?SOUNDINFO_NOMULTIPLE:0)
289               |(info->envelopes?SOUNDINFO_HASENVELOPE:0)
290               |(info->loops?SOUNDINFO_HASLOOPS:0)
291               |(info->outpoint?SOUNDINFO_HASOUTPOINT:0)
292               |(info->inpoint?SOUNDINFO_HASINPOINT:0);
293     swf_SetU8(tag, flags);
294     if(flags&SOUNDINFO_HASINPOINT)
295         swf_SetU32(tag, info->inpoint);
296     if(flags&SOUNDINFO_HASOUTPOINT)
297         swf_SetU32(tag, info->outpoint);
298     if(flags&SOUNDINFO_HASLOOPS)
299         swf_SetU16(tag, info->loops);
300     if(flags&SOUNDINFO_HASENVELOPE) {
301         int t;
302         swf_SetU8(tag, info->envelopes);
303         for(t=0;t<info->envelopes;t++) {
304             swf_SetU32(tag, info->pos[t]);
305             swf_SetU16(tag, info->left[t]);
306             swf_SetU16(tag, info->right[t]);
307         }
308     }
309 }
310
311
312 void swf_SetSoundDefineMP3(TAG*tag, U8* data, unsigned length,
313                            unsigned SampRate,
314                            unsigned Channels,
315                            unsigned NumFrames)
316 {
317     U8 compression = 2; // 0 = raw, 1 = ADPCM, 2 = mp3, 3 = raw le, 6 = nellymoser
318     U8 rate;     // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
319     U8 size = 1; // 0 = 8 bit, 1 = 16 bit
320     U8 type = Channels==2; // 0=mono, 1=stereo
321     
322     rate = (SampRate >= 40000) ? 3
323          : (SampRate >= 19000) ? 2
324          : (SampRate >= 8000) ? 1
325          : 0;
326
327     swf_SetU8(tag,(compression<<4)|(rate<<2)|(size<<1)|type);
328
329     swf_SetU32(tag, NumFrames * 576);
330
331     swf_SetU16(tag, 0); //delayseek
332     swf_SetBlock(tag, data, length);
333 }