* bitrate is now 32 and accessible from the outside
[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 file is distributed under the GPL, see file COPYING for details 
11
12 */
13
14 #ifndef RFXSWF_DISABLESOUND
15
16 #include "../rfxswf.h"
17
18 #ifdef BLADEENC
19 fjokjklj
20 CodecInitOut * init = 0;
21 void swf_SetSoundStreamHead(TAG*tag, U16 avgnumsamples)
22 {
23     U8 playbackrate = 3; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
24     U8 playbacksize = 1; // 0 = 8 bit, 1 = 16 bit
25     U8 playbacktype = 0; // 0 = mono, 1 = stereo
26     U8 compression = 2; // 0 = raw, 1 = ADPCM, 2 = mp3, 3 = raw le, 6 = nellymoser
27     U8 rate = 3; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
28     U8 size = 1; // 0 = 8 bit, 1 = 16 bit
29     U8 type = 0; // 0 = mono, 1 = stereo
30
31     CodecInitIn params;
32     memset(&params, 0, sizeof(params));
33     params.frequency = 44100;  //48000, 44100 or 32000
34     params.mode = 3;      //0 = Stereo, 2 = Dual Channel, 3 = Mono
35     params.emphasis = 0;  //0 = None, 1 = 50/15 microsec, 3 = CCITT J.17
36     params.bitrate = 128;         //default is 128 (64 for mono)
37     init = codecInit(&params);
38
39     swf_SetU8(tag,(playbackrate<<2)|(playbacksize<<1)|playbacktype);
40     swf_SetU8(tag,(compression<<4)|(rate<<2)|(size<<1)|type);
41     swf_SetU16(tag,avgnumsamples);
42
43     printf("numSamples:%d\n",init->nSamples);
44     printf("bufferSize:%d\n",init->bufferSize);
45 }
46
47 void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int numsamples, char first)
48 {
49     char*buf;
50     int len = 0;
51
52     buf = malloc(init->bufferSize);
53     if(!buf)
54         return;
55     
56     len = codecEncodeChunk (numsamples, samples, buf);
57     len += codecFlush (&buf[len]);
58     len += codecExit (&buf[len]);
59
60     if(first) {
61         swf_SetU16(tag, numsamples); // number of samples
62         swf_SetU16(tag, 0); // seek
63     }
64     swf_SetBlock(tag, buf, len);
65     free(buf);
66 }
67 #endif
68
69
70 #ifdef LAME
71
72 #include "../lame/lame.h"
73
74 /* TODO: find a way to set these from the outside */
75 int swf_mp3_samplerate = 44100;
76 int swf_mp3_channels = 1;
77 int swf_mp3_bitrate = 32;
78
79 static lame_global_flags*lame_flags;
80
81 static void initlame(unsigned char*buf, int bufsize)
82 {
83     lame_flags = lame_init();
84
85     lame_set_in_samplerate(lame_flags, swf_mp3_samplerate);
86     lame_set_num_channels(lame_flags, swf_mp3_channels);
87     lame_set_scale(lame_flags, 0);
88
89     // MPEG1    32, 44.1,   48khz
90     // MPEG2    16, 22.05,  24
91     // MPEG2.5   8, 11.025, 12
92     // (not used by decoding routines)
93     lame_set_out_samplerate(lame_flags, 11025);
94
95     lame_set_quality(lame_flags, 0);
96     lame_set_mode(lame_flags, MONO/*3*/);
97     lame_set_brate(lame_flags, swf_mp3_bitrate);
98     //lame_set_compression_ratio(lame_flags, 11.025);
99     lame_set_bWriteVbrTag(lame_flags, 0);
100
101     lame_init_params(lame_flags);
102     lame_init_bitstream(lame_flags);
103
104     /* The first two flush calls to lame always fail, for
105        some reason. Do them here where they cause no damage. */
106     lame_encode_flush_nogap(lame_flags, buf, bufsize);
107     //printf("init:flush_nogap():%d\n", len);
108     lame_encode_flush(lame_flags, buf, bufsize);
109     //printf("init:flush():%d\n", len);
110 }
111
112 void swf_SetSoundStreamHead(TAG*tag, int avgnumsamples)
113 {
114     unsigned char buf[4096];
115     int bufsize = 1152*2;
116     int len;
117     short int samples[1152*2];
118
119     U8 playbackrate = 1; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
120     U8 playbacksize = 1; // 0 = 8 bit, 1 = 16 bit
121     U8 playbacktype = 0; // 0 = mono, 1 = stereo
122     U8 compression = 2; // 0 = raw, 1 = ADPCM, 2 = mp3, 3 = raw le, 6 = nellymoser
123     U8 rate = 1; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
124     U8 size = 1; // 0 = 8 bit, 1 = 16 bit
125     U8 type = 0; // 0 = mono, 1 = stereo
126     
127     memset(samples,0,sizeof(samples));
128
129     initlame(buf, bufsize);
130
131     swf_SetU8(tag,(playbackrate<<2)|(playbacksize<<1)|playbacktype);
132     swf_SetU8(tag,(compression<<4)|(rate<<2)|(size<<1)|type);
133     swf_SetU16(tag,avgnumsamples);
134 }
135
136 void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int seek, char first)
137 {
138     char*buf;
139     int oldlen=0,len = 0;
140     int bufsize = 16384;
141     int numsamples = 1152*2;
142
143     buf = malloc(bufsize);
144     if(!buf)
145         return;
146
147     if(first) {
148         int fs = lame_get_framesize(lame_flags);
149         //printf("framesize:%d\n", fs);
150         swf_SetU16(tag, fs * first); // samples per mp3 frame
151         swf_SetU16(tag, seek); // seek
152     }
153
154     len += lame_encode_buffer(lame_flags, samples, samples, numsamples, &buf[len], bufsize-len);
155     //printf("block: %d (+%d)\n", len, len-oldlen);
156     oldlen = len;
157
158     len += lame_encode_flush_nogap(lame_flags, &buf[len], bufsize-len);
159     //printf("flush: %d (+%d)\n", len, len-oldlen);
160     oldlen = len;
161     
162     swf_SetBlock(tag, buf, len);
163
164    /* len += lame_encode_flush(lame_flags, &buf[len], bufsize-len);
165     printf("flush! %d (+%d)\n", len, len-oldlen);*/
166
167     free(buf);
168 }
169
170 void swf_SetSoundStreamEnd(TAG*tag)
171 {
172     lame_close (lame_flags);
173 }
174
175 void swf_SetSoundDefine(TAG*tag, S16*samples, int num)
176 {
177     char*buf;
178     int oldlen=0,len = 0;
179     int bufsize = 16384;
180     int blocksize = 1152*2;
181     int t;
182     int blocks;
183
184     U8 compression = 2; // 0 = raw, 1 = ADPCM, 2 = mp3, 3 = raw le, 6 = nellymoser
185     U8 rate = 1; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
186     U8 size = 1; // 0 = 8 bit, 1 = 16 bit
187     U8 type = 0; // 0 = mono, 1 = stereo
188
189     blocks = num / (blocksize);
190
191     swf_SetU8(tag,(compression<<4)|(rate<<2)|(size<<1)|type);
192
193     swf_SetU32(tag,blocks*blocksize / 4); // 44100 -> 11025
194
195     buf = malloc(bufsize);
196     if(!buf)
197         return;
198
199     initlame(buf, bufsize);
200
201     swf_SetU16(tag, 0); //delayseek
202     for(t=0;t<blocks;t++) {
203         int s;
204         U16*pos;
205         pos= &samples[t*blocksize];
206         len += lame_encode_buffer(lame_flags, pos, pos, blocksize, &buf[len], bufsize-len);
207         len += lame_encode_flush_nogap(lame_flags, &buf[len], bufsize-len);
208         swf_SetBlock(tag, buf, len);
209         len = 0;
210     }
211
212     free(buf);
213 }
214
215 #define SOUNDINFO_STOP 32
216 #define SOUNDINFO_NOMULTIPLE 16
217 #define SOUNDINFO_HASENVELOPE 8
218 #define SOUNDINFO_HASLOOPS 4
219 #define SOUNDINFO_HASOUTPOINT 2
220 #define SOUNDINFO_HASINPOINT 1
221
222 void swf_SetSoundInfo(TAG*tag, SOUNDINFO*info)
223 {
224     U8 flags = (info->stop?SOUNDINFO_STOP:0)
225               |(info->nomultiple?SOUNDINFO_NOMULTIPLE:0)
226               |(info->envelopes?SOUNDINFO_HASENVELOPE:0)
227               |(info->loops?SOUNDINFO_HASLOOPS:0)
228               |(info->outpoint?SOUNDINFO_HASOUTPOINT:0)
229               |(info->inpoint?SOUNDINFO_HASINPOINT:0);
230     swf_SetU8(tag, flags);
231     if(flags&SOUNDINFO_HASINPOINT)
232         swf_SetU32(tag, info->inpoint);
233     if(flags&SOUNDINFO_HASOUTPOINT)
234         swf_SetU32(tag, info->outpoint);
235     if(flags&SOUNDINFO_HASLOOPS)
236         swf_SetU16(tag, info->loops);
237     if(flags&SOUNDINFO_HASENVELOPE) {
238         int t;
239         swf_SetU8(tag, info->envelopes);
240         for(t=0;t<info->envelopes;t++) {
241             swf_SetU32(tag, info->pos[t]);
242             swf_SetU16(tag, info->left[t]);
243             swf_SetU16(tag, info->right[t]);
244         }
245     }
246 }
247
248 #endif
249
250 #endif // RFXSWF_DISABLESOUND