+ if(len == 0) {
+ fprintf(stderr, "error: mp3 empty block, %d samples, first:%d, framesize:%d\n",
+ numsamples, first, fs);
+ }/* else {
+ fprintf(stderr, "ok: mp3 nonempty block, %d samples, first:%d, framesize:%d\n",
+ numsamples, first, fs);
+ }*/
+ rfx_free(buf);
+}
+
+void swf_SetSoundStreamEnd(TAG*tag)
+{
+ lame_close (lame_flags);
+}
+
+void swf_SetSoundDefine(TAG*tag, S16*samples, int num)
+{
+ char*buf;
+ int oldlen=0,len = 0;
+ int bufsize = 16384;
+ int blocksize = (int)(((swf_mp3_out_samplerate > 22050) ? 1152 : 576) * ((double)swf_mp3_in_samplerate/swf_mp3_out_samplerate));
+ int t;
+ int blocks;
+
+ U8 compression = 2; // 0 = raw, 1 = ADPCM, 2 = mp3, 3 = raw le, 6 = nellymoser
+ U8 rate = 1; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz
+ U8 size = 1; // 0 = 8 bit, 1 = 16 bit
+ U8 type = 0; // 0 = mono, 1 = stereo
+
+ if(swf_mp3_out_samplerate == 5512) rate = 0;
+ else if(swf_mp3_out_samplerate == 11025) rate = 1;
+ else if(swf_mp3_out_samplerate == 22050) rate = 2;
+ else if(swf_mp3_out_samplerate == 44100) rate = 3;
+ else fprintf(stderr, "Invalid samplerate: %d\n", swf_mp3_out_samplerate);
+
+ blocks = num / (blocksize);
+
+ swf_SetU8(tag,(compression<<4)|(rate<<2)|(size<<1)|type);
+
+ swf_SetU32(tag, (int)(tag,blocks*blocksize /
+ ((double)swf_mp3_in_samplerate/swf_mp3_out_samplerate)) // account for resampling
+ );
+
+ buf = rfx_alloc(bufsize);
+ if(!buf)
+ return;
+
+ initlame();
+
+ swf_SetU16(tag, 0); //delayseek
+ for(t=0;t<blocks;t++) {
+ int s;
+ U16*pos;
+ pos= &samples[t*blocksize];
+ len += lame_encode_buffer(lame_flags, pos, pos, blocksize, &buf[len], bufsize-len);
+ len += lame_encode_flush_nogap(lame_flags, &buf[len], bufsize-len);
+ swf_SetBlock(tag, buf, len);
+ len = 0;
+ }
+
+ rfx_free(buf);
+}
+
+#endif
+
+#endif
+
+#ifndef HAVE_SOUND
+
+// supply stubs
+
+void swf_SetSoundStreamHead(TAG*tag, int avgnumsamples)
+{
+ fprintf(stderr, "Error: no mp3 soundstream support compiled in.\n");exit(1);
+}
+void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int seek, char first)
+{
+ fprintf(stderr, "Error: no mp3 soundstream support compiled in.\n");exit(1);
+}
+void swf_SetSoundStreamEnd(TAG*tag)
+{
+ fprintf(stderr, "Error: no mp3 soundstream support compiled in.\n");exit(1);
+}
+void swf_SetSoundDefine(TAG*tag, S16*samples, int num)
+{
+ swf_SetSoundDefineRaw(tag, samples,num);
+}
+
+#endif
+
+#define SOUNDINFO_STOP 32
+#define SOUNDINFO_NOMULTIPLE 16
+#define SOUNDINFO_HASENVELOPE 8
+#define SOUNDINFO_HASLOOPS 4
+#define SOUNDINFO_HASOUTPOINT 2
+#define SOUNDINFO_HASINPOINT 1
+
+
+void swf_SetSoundInfo(TAG*tag, SOUNDINFO*info)
+{
+ U8 flags = (info->stop?SOUNDINFO_STOP:0)
+ |(info->nomultiple?SOUNDINFO_NOMULTIPLE:0)
+ |(info->envelopes?SOUNDINFO_HASENVELOPE:0)
+ |(info->loops?SOUNDINFO_HASLOOPS:0)
+ |(info->outpoint?SOUNDINFO_HASOUTPOINT:0)
+ |(info->inpoint?SOUNDINFO_HASINPOINT:0);
+ swf_SetU8(tag, flags);
+ if(flags&SOUNDINFO_HASINPOINT)
+ swf_SetU32(tag, info->inpoint);
+ if(flags&SOUNDINFO_HASOUTPOINT)
+ swf_SetU32(tag, info->outpoint);
+ if(flags&SOUNDINFO_HASLOOPS)
+ swf_SetU16(tag, info->loops);
+ if(flags&SOUNDINFO_HASENVELOPE) {
+ int t;
+ swf_SetU8(tag, info->envelopes);
+ for(t=0;t<info->envelopes;t++) {
+ swf_SetU32(tag, info->pos[t]);
+ swf_SetU16(tag, info->left[t]);
+ swf_SetU16(tag, info->right[t]);
+ }
+ }