Debian Codegraph @

src/speexdec.c

raw
1 /* Copyright (C) 2002-2006 Jean-Marc Valin
2 File: speexdec.c
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 - Neither the name of the Xiph.org Foundation nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <stdio.h>
37 #if !defined WIN32 && !defined _WIN32
38 #include <unistd.h>
39 #endif
40 #ifdef HAVE_GETOPT_H
41 #include <getopt.h>
42 #endif
43 #ifndef HAVE_GETOPT_LONG
44 #include "getopt_win.h"
45 #endif
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include <ogg/ogg.h>
50
51 #if defined WIN32 || defined _WIN32
52 #include "wave_out.h"
53 /* We need the following two to set stdout to binary */
54 #include <io.h>
55 #include <fcntl.h>
56 #endif
57 #include <math.h>
58
59 #ifdef __MINGW32__
60 #include "wave_out.c"
61 #endif
62
63 #ifdef HAVE_SYS_SOUNDCARD_H
64 #include <sys/soundcard.h>
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #include <fcntl.h>
68 #include <sys/ioctl.h>
69
70 #elif defined HAVE_SYS_AUDIOIO_H
71 #include <sys/types.h>
72 #include <fcntl.h>
73 #include <sys/ioctl.h>
74 #include <sys/audioio.h>
75 #ifndef AUDIO_ENCODING_SLINEAR
76 #define AUDIO_ENCODING_SLINEAR AUDIO_ENCODING_LINEAR /* Solaris */
77 #endif
78
79 #endif
80
81 #include <string.h>
82 #include "wav_io.h"
83 #include "speex/speex_header.h"
84 #include "speex/speex_stereo.h"
85 #include "speex/speex_callbacks.h"
86
87 #define MAX_FRAME_SIZE 2000
88
89 #define readint(buf) ((((unsigned)buf[3]<<24)&0xff000000)| \
90 (((unsigned)buf[2]<<16)&0xff0000)| \
91 (((unsigned)buf[1]<<8)&0xff00)| \
92 ((unsigned)buf[0]&0xff))
93
94 static void print_comments(char *comments, int length)
95 {
96 char *c=comments;
97 int len, i, nb_fields;
98 char *end;
99
100 if (length<8)
101 {
102 fprintf (stderr, "Invalid/corrupted comments\n");
103 return;
104 }
105 end = c+length;
106 len=readint(c);
107 c+=4;
108 if (len < 0 || c+len>end)
109 {
110 fprintf (stderr, "Invalid/corrupted comments\n");
111 return;
112 }
113 fwrite(c, 1, len, stderr);
114 c+=len;
115 fprintf (stderr, "\n");
116 if (c+4>end)
117 {
118 fprintf (stderr, "Invalid/corrupted comments\n");
119 return;
120 }
121 nb_fields=readint(c);
122 c+=4;
123 for (i=0;i<nb_fields;i++)
124 {
125 if (c+4>end)
126 {
127 fprintf (stderr, "Invalid/corrupted comments\n");
128 return;
129 }
130 len=readint(c);
131 c+=4;
132 if (len < 0 || c+len>end)
133 {
134 fprintf (stderr, "Invalid/corrupted comments\n");
135 return;
136 }
137 fwrite(c, 1, len, stderr);
138 c+=len;
139 fprintf (stderr, "\n");
140 }
141 }
142
143 FILE *out_file_open(char *outFile, int rate, int *channels)
144 {
145 FILE *fout=NULL;
146 /*Open output file*/
147 if (strlen(outFile)==0)
148 {
149 #if defined HAVE_SYS_SOUNDCARD_H
150 int audio_fd, format, stereo;
151 audio_fd=open("/dev/dsp", O_WRONLY);
152 if (audio_fd<0)
153 {
154 perror("Cannot open /dev/dsp");
155 exit(1);
156 }
157
158 format=AFMT_S16_NE;
159 if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format)==-1)
160 {
161 perror("SNDCTL_DSP_SETFMT");
162 close(audio_fd);
163 exit(1);
164 }
165
166 stereo=0;
167 if (*channels==2)
168 stereo=1;
169 if (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo)==-1)
170 {
171 perror("SNDCTL_DSP_STEREO");
172 close(audio_fd);
173 exit(1);
174 }
175 if (stereo!=0)
176 {
177 if (*channels==1)
178 fprintf (stderr, "Cannot set mono mode, will decode in stereo\n");
179 *channels=2;
180 }
181
182 if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate)==-1)
183 {
184 perror("SNDCTL_DSP_SPEED");
185 close(audio_fd);
186 exit(1);
187 }
188 fout = fdopen(audio_fd, "w");
189 #elif defined HAVE_SYS_AUDIOIO_H
190 audio_info_t info;
191 int audio_fd;
192
193 audio_fd = open("/dev/audio", O_WRONLY);
194 if (audio_fd<0)
195 {
196 perror("Cannot open /dev/audio");
197 exit(1);
198 }
199
200 AUDIO_INITINFO(&info);
201 #ifdef AUMODE_PLAY /* NetBSD/OpenBSD */
202 info.mode = AUMODE_PLAY;
203 #endif
204 info.play.encoding = AUDIO_ENCODING_SLINEAR;
205 info.play.precision = 16;
206 info.play.sample_rate = rate;
207 info.play.channels = *channels;
208
209 if (ioctl(audio_fd, AUDIO_SETINFO, &info) < 0)
210 {
211 perror ("AUDIO_SETINFO");
212 exit(1);
213 }
214 fout = fdopen(audio_fd, "w");
215 #elif defined WIN32 || defined _WIN32
216 {
217 unsigned int speex_channels = *channels;
218 if (Set_WIN_Params (INVALID_FILEDESC, rate, SAMPLE_SIZE, speex_channels))
219 {
220 fprintf (stderr, "Can't access %s\n", "WAVE OUT");
221 exit(1);
222 }
223 }
224 #else
225 fprintf (stderr, "No soundcard support\n");
226 exit(1);
227 #endif
228 } else {
229 if (strcmp(outFile,"-")==0)
230 {
231 #if defined WIN32 || defined _WIN32
232 _setmode(_fileno(stdout), _O_BINARY);
233 #elif defined OS2
234 _fsetmode(stdout,"b");
235 #endif
236 fout=stdout;
237 }
238 else
239 {
240 fout = fopen(outFile, "wb");
241 if (!fout)
242 {
243 perror(outFile);
244 exit(1);
245 }
246 if (strcmp(outFile+strlen(outFile)-4,".wav")==0 || strcmp(outFile+strlen(outFile)-4,".WAV")==0)
247 write_wav_header(fout, rate, *channels, 0, 0);
248 }
249 }
250 return fout;
251 }
252
253 void usage()
254 {
255 printf ("Usage: speexdec [options] input_file.spx [output_file]\n");
256 printf ("\n");
257 printf ("Decodes a Speex file and produce a WAV file or raw file\n");
258 printf ("\n");
259 printf ("input_file can be:\n");
260 printf (" filename.spx regular Speex file\n");
261 printf (" - stdin\n");
262 printf ("\n");
263 printf ("output_file can be:\n");
264 printf (" filename.wav Wav file\n");
265 printf (" filename.* Raw PCM file (any extension other that .wav)\n");
266 printf (" - stdout\n");
267 printf (" (nothing) Will be played to soundcard\n");
268 printf ("\n");
269 printf ("Options:\n");
270 printf (" --enh Enable perceptual enhancement (default)\n");
271 printf (" --no-enh Disable perceptual enhancement\n");
272 printf (" --force-nb Force decoding in narrowband\n");
273 printf (" --force-wb Force decoding in wideband\n");
274 printf (" --force-uwb Force decoding in ultra-wideband\n");
275 printf (" --mono Force decoding in mono\n");
276 printf (" --stereo Force decoding in stereo\n");
277 printf (" --rate n Force decoding at sampling rate n Hz\n");
278 printf (" --packet-loss n Simulate n %% random packet loss\n");
279 printf (" -V Verbose mode (show bit-rate)\n");
280 printf (" -h, --help This help\n");
281 printf (" -v, --version Version information\n");
282 printf (" --pf Deprecated, use --enh instead\n");
283 printf (" --no-pf Deprecated, use --no-enh instead\n");
284 printf ("\n");
285 printf ("More information is available from the Speex site: http://www.speex.org\n");
286 printf ("\n");
287 printf ("Please report bugs to the mailing list `[email protected]'.\n");
288 }
289
290 void version()
291 {
292 const char* speex_version;
293 speex_lib_ctl(SPEEX_LIB_GET_VERSION_STRING, (void*)&speex_version);
294 printf ("speexdec (Speex decoder) version %s (compiled " __DATE__ ")\n", speex_version);
295 printf ("Copyright (C) 2002-2006 Jean-Marc Valin\n");
296 }
297
298 void version_short()
299 {
300 const char* speex_version;
301 speex_lib_ctl(SPEEX_LIB_GET_VERSION_STRING, (void*)&speex_version);
302 printf ("speexdec version %s\n", speex_version);
303 printf ("Copyright (C) 2002-2006 Jean-Marc Valin\n");
304 }
305
306 static void *process_header(ogg_packet *op, spx_int32_t enh_enabled, spx_int32_t *frame_size, int *granule_frame_size, spx_int32_t *rate, int *nframes, int forceMode, int *channels, SpeexStereoState *stereo, int *extra_headers, int quiet)
307 {
308 void *st;
309 const SpeexMode *mode;
310 SpeexHeader *header;
311 int modeID;
312 SpeexCallback callback;
313
314 header = speex_packet_to_header((char*)op->packet, op->bytes);
315 if (!header)
316 {
317 fprintf (stderr, "Cannot read header\n");
318 return NULL;
319 }
320 if (header->mode >= SPEEX_NB_MODES || header->mode<0)
321 {
322 fprintf (stderr, "Mode number %d does not (yet/any longer) exist in this version\n",
323 header->mode);
324 free(header);
325 return NULL;
326 }
327
328 modeID = header->mode;
329 if (forceMode!=-1)
330 modeID = forceMode;
331
332 mode = speex_lib_get_mode (modeID);
333
334 if (header->speex_version_id > 1)
335 {
336 fprintf (stderr, "This file was encoded with Speex bit-stream version %d, which I don't know how to decode\n", header->speex_version_id);
337 free(header);
338 return NULL;
339 }
340
341 if (mode->bitstream_version < header->mode_bitstream_version)
342 {
343 fprintf (stderr, "The file was encoded with a newer version of Speex. You need to upgrade in order to play it.\n");
344 free(header);
345 return NULL;
346 }
347 if (mode->bitstream_version > header->mode_bitstream_version)
348 {
349 fprintf (stderr, "The file was encoded with an older version of Speex. You would need to downgrade the version in order to play it.\n");
350 free(header);
351 return NULL;
352 }
353
354 st = speex_decoder_init(mode);
355 if (!st)
356 {
357 fprintf (stderr, "Decoder initialization failed.\n");
358 free(header);
359 return NULL;
360 }
361 speex_decoder_ctl(st, SPEEX_SET_ENH, &enh_enabled);
362 speex_decoder_ctl(st, SPEEX_GET_FRAME_SIZE, frame_size);
363 *granule_frame_size = *frame_size;
364
365 if (!*rate)
366 *rate = header->rate;
367 /* Adjust rate if --force-* options are used */
368 if (forceMode!=-1)
369 {
370 if (header->mode < forceMode)
371 {
372 *rate <<= (forceMode - header->mode);
373 *granule_frame_size >>= (forceMode - header->mode);
374 }
375 if (header->mode > forceMode)
376 {
377 *rate >>= (header->mode - forceMode);
378 *granule_frame_size <<= (header->mode - forceMode);
379 }
380 }
381
382
383 speex_decoder_ctl(st, SPEEX_SET_SAMPLING_RATE, rate);
384
385 *nframes = header->frames_per_packet;
386
387 if (*channels==-1)
388 *channels = header->nb_channels;
389
390 if (!(*channels==1))
391 {
392 *channels = 2;
393 callback.callback_id = SPEEX_INBAND_STEREO;
394 callback.func = speex_std_stereo_request_handler;
395 callback.data = stereo;
396 speex_decoder_ctl(st, SPEEX_SET_HANDLER, &callback);
397 }
398
399 if (!quiet)
400 {
401 fprintf (stderr, "Decoding %d Hz audio using %s mode",
402 *rate, mode->modeName);
403
404 if (*channels==1)
405 fprintf (stderr, " (mono");
406 else
407 fprintf (stderr, " (stereo");
408
409 if (header->vbr)
410 fprintf (stderr, ", VBR)\n");
411 else
412 fprintf(stderr, ")\n");
413 /*fprintf (stderr, "Decoding %d Hz audio at %d bps using %s mode\n",
414 *rate, mode->bitrate, mode->modeName);*/
415 }
416
417 *extra_headers = header->extra_headers;
418
419 free(header);
420 return st;
421 }
422
423 int main(int argc, char **argv)
424 {
425 int c;
426 int option_index = 0;
427 char *inFile, *outFile;
428 FILE *fin, *fout=NULL;
429 short out[MAX_FRAME_SIZE];
430 short output[MAX_FRAME_SIZE];
431 int frame_size=0, granule_frame_size=0;
432 void *st=NULL;
433 SpeexBits bits;
434 int packet_count=0;
435 int stream_init = 0;
436 int quiet = 0;
437 ogg_int64_t page_granule=0, last_granule=0;
438 int skip_samples=0, page_nb_packets;
439 struct option long_options[] =
440 {
441 {"help", no_argument, NULL, 0},
442 {"quiet", no_argument, NULL, 0},
443 {"version", no_argument, NULL, 0},
444 {"version-short", no_argument, NULL, 0},
445 {"enh", no_argument, NULL, 0},
446 {"no-enh", no_argument, NULL, 0},
447 {"pf", no_argument, NULL, 0},
448 {"no-pf", no_argument, NULL, 0},
449 {"force-nb", no_argument, NULL, 0},
450 {"force-wb", no_argument, NULL, 0},
451 {"force-uwb", no_argument, NULL, 0},
452 {"rate", required_argument, NULL, 0},
453 {"mono", no_argument, NULL, 0},
454 {"stereo", no_argument, NULL, 0},
455 {"packet-loss", required_argument, NULL, 0},
456 {0, 0, 0, 0}
457 };
458 ogg_sync_state oy;
459 ogg_page og;
460 ogg_packet op;
461 ogg_stream_state os;
462 int enh_enabled;
463 int nframes=2;
464 int print_bitrate=0;
465 int close_in=0;
466 int eos=0;
467 int forceMode=-1;
468 int audio_size=0;
469 float loss_percent=-1;
470 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
471 int channels=-1;
472 int rate=0;
473 int extra_headers=0;
474 int wav_format=0;
475 int lookahead;
476 int speex_serialno = -1;
477
478 enh_enabled = 1;
479
480 /*Process options*/
481 while(1)
482 {
483 c = getopt_long (argc, argv, "hvV",
484 long_options, &option_index);
485 if (c==-1)
486 break;
487
488 switch(c)
489 {
490 case 0:
491 if (strcmp(long_options[option_index].name,"help")==0)
492 {
493 usage();
494 exit(0);
495 } else if (strcmp(long_options[option_index].name,"quiet")==0)
496 {
497 quiet = 1;
498 } else if (strcmp(long_options[option_index].name,"version")==0)
499 {
500 version();
501 exit(0);
502 } else if (strcmp(long_options[option_index].name,"version-short")==0)
503 {
504 version_short();
505 exit(0);
506 } else if (strcmp(long_options[option_index].name,"enh")==0)
507 {
508 enh_enabled=1;
509 } else if (strcmp(long_options[option_index].name,"no-enh")==0)
510 {
511 enh_enabled=0;
512 } else if (strcmp(long_options[option_index].name,"pf")==0)
513 {
514 fprintf (stderr, "--pf is deprecated, use --enh instead\n");
515 enh_enabled=1;
516 } else if (strcmp(long_options[option_index].name,"no-pf")==0)
517 {
518 fprintf (stderr, "--no-pf is deprecated, use --no-enh instead\n");
519 enh_enabled=0;
520 } else if (strcmp(long_options[option_index].name,"force-nb")==0)
521 {
522 forceMode=0;
523 } else if (strcmp(long_options[option_index].name,"force-wb")==0)
524 {
525 forceMode=1;
526 } else if (strcmp(long_options[option_index].name,"force-uwb")==0)
527 {
528 forceMode=2;
529 } else if (strcmp(long_options[option_index].name,"mono")==0)
530 {
531 channels=1;
532 } else if (strcmp(long_options[option_index].name,"stereo")==0)
533 {
534 channels=2;
535 } else if (strcmp(long_options[option_index].name,"rate")==0)
536 {
537 rate=atoi (optarg);
538 } else if (strcmp(long_options[option_index].name,"packet-loss")==0)
539 {
540 loss_percent = atof(optarg);
541 }
542 break;
543 case 'h':
544 usage();
545 exit(0);
546 break;
547 case 'v':
548 version();
549 exit(0);
550 break;
551 case 'V':
552 print_bitrate=1;
553 break;
554 case '?':
555 usage();
556 exit(1);
557 break;
558 }
559 }
560 if (argc-optind!=2 && argc-optind!=1)
561 {
562 usage();
563 exit(1);
564 }
565 inFile=argv[optind];
566
567 if (argc-optind==2)
568 outFile=argv[optind+1];
569 else
570 outFile = "";
571 wav_format = strlen(outFile)>=4 && (
572 strcmp(outFile+strlen(outFile)-4,".wav")==0
573 || strcmp(outFile+strlen(outFile)-4,".WAV")==0);
574 /*Open input file*/
575 if (strcmp(inFile, "-")==0)
576 {
577 #if defined WIN32 || defined _WIN32
578 _setmode(_fileno(stdin), _O_BINARY);
579 #endif
580 fin=stdin;
581 }
582 else
583 {
584 fin = fopen(inFile, "rb");
585 if (!fin)
586 {
587 perror(inFile);
588 exit(1);
589 }
590 close_in=1;
591 }
592
593
594 /*Init Ogg data struct*/
595 ogg_sync_init(&oy);
596
597 speex_bits_init(&bits);
598 /*Main decoding loop*/
599
600 while (1)
601 {
602 char *data;
603 int i, j, nb_read;
604 /*Get the ogg buffer for writing*/
605 data = ogg_sync_buffer(&oy, 200);
606 /*Read bitstream from input file*/
607 nb_read = fread(data, sizeof(char), 200, fin);
608 ogg_sync_wrote(&oy, nb_read);
609
610 /*Loop for all complete pages we got (most likely only one)*/
611 while (ogg_sync_pageout(&oy, &og)==1)
612 {
613 int packet_no;
614 if (stream_init == 0) {
615 ogg_stream_init(&os, ogg_page_serialno(&og));
616 stream_init = 1;
617 }
618 if (ogg_page_serialno(&og) != os.serialno) {
619 /* so all streams are read. */
620 ogg_stream_reset_serialno(&os, ogg_page_serialno(&og));
621 }
622 /*Add page to the bitstream*/
623 ogg_stream_pagein(&os, &og);
624 page_granule = ogg_page_granulepos(&og);
625 page_nb_packets = ogg_page_packets(&og);
626 if (page_granule>0 && frame_size)
627 {
628 /* FIXME: shift the granule values if --force-* is specified */
629 skip_samples = frame_size*(page_nb_packets*granule_frame_size*nframes - (page_granule-last_granule))/granule_frame_size;
630 if (ogg_page_eos(&og))
631 skip_samples = -skip_samples;
632 /*else if (!ogg_page_bos(&og))
633 skip_samples = 0;*/
634 } else
635 {
636 skip_samples = 0;
637 }
638 /*printf ("page granulepos: %d %d %d\n", skip_samples, page_nb_packets, (int)page_granule);*/
639 last_granule = page_granule;
640 /*Extract all available packets*/
641 packet_no=0;
642 while (!eos && ogg_stream_packetout(&os, &op) == 1)
643 {
644 if (op.bytes>=5 && !memcmp(op.packet, "Speex", 5)) {
645 speex_serialno = os.serialno;
646 }
647 if (speex_serialno == -1 || os.serialno != speex_serialno)
648 break;
649 /*If first packet, process as Speex header*/
650 if (packet_count==0)
651 {
652 st = process_header(&op, enh_enabled, &frame_size, &granule_frame_size, &rate, &nframes, forceMode, &channels, &stereo, &extra_headers, quiet);
653 if (!st)
654 exit(1);
655 speex_decoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead);
656 if (!nframes)
657 nframes=1;
658 fout = out_file_open(outFile, rate, &channels);
659
660 } else if (packet_count==1)
661 {
662 if (!quiet)
663 print_comments((char*)op.packet, op.bytes);
664 } else if (packet_count<=1+extra_headers)
665 {
666 /* Ignore extra headers */
667 } else {
668 int lost=0;
669 packet_no++;
670 if (loss_percent>0 && 100*((float)rand())/RAND_MAX<loss_percent)
671 lost=1;
672
673 /*End of stream condition*/
674 if (op.e_o_s && os.serialno == speex_serialno) /* don't care for anything except speex eos */
675 eos=1;
676
677 /*Copy Ogg packet to Speex bitstream*/
678 speex_bits_read_from(&bits, (char*)op.packet, op.bytes);
679 for (j=0;j!=nframes;j++)
680 {
681 int ret;
682 /*Decode frame*/
683 if (!lost)
684 ret = speex_decode_int(st, &bits, output);
685 else
686 ret = speex_decode_int(st, NULL, output);
687
688 /*for (i=0;i<frame_size*channels;i++)
689 printf ("%d\n", (int)output[i]);*/
690
691 if (ret==-1)
692 break;
693 if (ret==-2)
694 {
695 fprintf (stderr, "Decoding error: corrupted stream?\n");
696 break;
697 }
698 if (speex_bits_remaining(&bits)<0)
699 {
700 fprintf (stderr, "Decoding overflow: corrupted stream?\n");
701 break;
702 }
703 if (channels==2)
704 speex_decode_stereo_int(output, frame_size, &stereo);
705
706 if (print_bitrate) {
707 spx_int32_t tmp;
708 char ch=13;
709 speex_decoder_ctl(st, SPEEX_GET_BITRATE, &tmp);
710 fputc (ch, stderr);
711 fprintf (stderr, "Bitrate is use: %d bps ", tmp);
712 }
713 /*Convert to short and save to output file*/
714 if (strlen(outFile)!=0)
715 {
716 for (i=0;i<frame_size*channels;i++)
717 out[i]=le_short(output[i]);
718 } else {
719 for (i=0;i<frame_size*channels;i++)
720 out[i]=output[i];
721 }
722 {
723 int frame_offset = 0;
724 int new_frame_size = frame_size;
725 /*printf ("packet %d %d\n", packet_no, skip_samples);*/
726 /*fprintf (stderr, "packet %d %d %d\n", packet_no, skip_samples, lookahead);*/
727 if (packet_no == 1 && j==0 && skip_samples > 0)
728 {
729 /*printf ("chopping first packet\n");*/
730 new_frame_size -= skip_samples+lookahead;
731 frame_offset = skip_samples+lookahead;
732 }
733 if (packet_no == page_nb_packets && skip_samples < 0)
734 {
735 int packet_length = nframes*frame_size+skip_samples+lookahead;
736 new_frame_size = packet_length - j*frame_size;
737 if (new_frame_size<0)
738 new_frame_size = 0;
739 if (new_frame_size>frame_size)
740 new_frame_size = frame_size;
741 /*printf ("chopping end: %d %d %d\n", new_frame_size, packet_length, packet_no);*/
742 }
743 if (new_frame_size>0)
744 {
745 #if defined WIN32 || defined _WIN32
746 if (strlen(outFile)==0)
747 WIN_Play_Samples (out+frame_offset*channels, sizeof(short) * new_frame_size*channels);
748 else
749 #endif
750 fwrite(out+frame_offset*channels, sizeof(short), new_frame_size*channels, fout);
751
752 audio_size+=sizeof(short)*new_frame_size*channels;
753 }
754 }
755 }
756 }
757 packet_count++;
758 }
759 }
760 if (feof(fin))
761 break;
762
763 }
764
765 if (fout && wav_format)
766 {
767 if (fseek(fout,4,SEEK_SET)==0)
768 {
769 int tmp;
770 tmp = le_int(audio_size+36);
771 fwrite(&tmp,4,1,fout);
772 if (fseek(fout,32,SEEK_CUR)==0)
773 {
774 tmp = le_int(audio_size);
775 fwrite(&tmp,4,1,fout);
776 } else
777 {
778 fprintf (stderr, "First seek worked, second didn't\n");
779 }
780 } else {
781 fprintf (stderr, "Cannot seek on wave file, size will be incorrect\n");
782 }
783 }
784
785 if (st)
786 speex_decoder_destroy(st);
787 else
788 {
789 fprintf (stderr, "This doesn't look like a Speex file\n");
790 }
791 speex_bits_destroy(&bits);
792 if (stream_init)
793 ogg_stream_clear(&os);
794 ogg_sync_clear(&oy);
795
796 #if defined WIN32 || defined _WIN32
797 if (strlen(outFile)==0)
798 WIN_Audio_close ();
799 #endif
800
801 if (close_in)
802 fclose(fin);
803 if (fout != NULL)
804 fclose(fout);
805
806 return 0;
807 }