only write the init header once per SWF.
[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 #include "../rfxswf.h"
15
16 CodecInitOut * init = 0;
17 void swf_SetSoundStreamHead(TAG*tag, U16 avgnumsamples)
18 {
19     U8 playbackrate = 3; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
20     U8 playbacksize = 1; // 0 = 8 bit, 1 = 16 bit
21     U8 playbacktype = 0; // 0 = mono, 1 = stereo
22     U8 compression = 2; // 0 = raw, 1 = ADPCM, 2 = mp3
23     U8 rate = 3; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
24     U8 size = 1; // 0 = 8 bit, 1 = 16 bit
25     U8 type = 0; // 0 = mono, 1 = stereo
26
27     CodecInitIn params;
28     memset(&params, 0, sizeof(params));
29     params.frequency = 44100;  //48000, 44100 or 32000
30     params.mode = 3;      //0 = Stereo, 2 = Dual Channel, 3 = Mono
31     params.emphasis = 0;  //0 = None, 1 = 50/15 microsec, 3 = CCITT J.17
32     params.bitrate = 128;         //default is 128 (64 for mono)
33     init = codecInit(&params);
34
35     swf_SetU8(tag,(playbackrate<<2)|(playbacksize<<1)|playbacktype);
36     swf_SetU8(tag,(compression<<4)|(rate<<2)|(size<<1)|type);
37     swf_SetU16(tag,avgnumsamples);
38
39     printf("numSamples:%d\n",init->nSamples);
40     printf("bufferSize:%d\n",init->bufferSize);
41 }
42
43 void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int numsamples, char first)
44 {
45     char*buf;
46     int len = 0;
47
48     if(!buf)
49         return;
50     
51     buf = malloc(init->bufferSize);
52     
53     len = codecEncodeChunk (numsamples, samples, buf);
54     len += codecFlush (&buf[len]);
55     len += codecExit (&buf[len]);
56
57     if(first) {
58         swf_SetU16(tag, numsamples); // number of samples
59         swf_SetU16(tag, 0); // seek
60     }
61     swf_SetBlock(tag, buf, len);
62     free(buf);
63 }
64