- int result=ogg_sync_pageout(&oy,&og);
- if(result==0)break; /* need more data */
- if(result==-1){ /* missing or corrupt data at this page position */
- fprintf(stderr,"Corrupt or missing data in bitstream; "
- "continuing...\n");
- }else{
- ogg_stream_pagein(&os,&og); /* can safely ignore errors at
- this point */
- while(1){
- result=ogg_stream_packetout(&os,&op);
- if(result==0)break; /* need more data */
- if(result==-1){ /* missing or corrupt data at this page position */
- /* no reason to complain; already complained above */
- }else{
- /* we have a packet. Decode it */
- double **pcm;
- int samples;
-
- vorbis_synthesis(&vb,&op);
- vorbis_synthesis_blockin(&vd,&vb);
- /*
-
- **pcm is a multichannel double vector. In stereo, for
- example, pcm[0] is left, and pcm[1] is right. samples is
- the size of each channel. Convert the float values
- (-1.<=range<=1.) to whatever PCM format and write it out */
-
- while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){
- int j;
- int clipflag=0;
- int out=(samples<convsize?samples:convsize);
-
- /* convert doubles to 16 bit signed ints (host order) and
- interleave */
- for(i=0;i<vi.channels;i++){
- int16_t *ptr=convbuffer+i;
- double *mono=pcm[i];
- for(j=0;j<out;j++){
- int val=mono[j]*32767.;
- /* might as well guard against clipping */
- if(val>32767){
- val=32767;
- clipflag=1;
- }
- if(val<-32768){
- val=-32768;
- clipflag=1;
- }
- *ptr=val;
- ptr+=2;
- }
- }
-
- if(clipflag)
- fprintf(stderr,"Clipping in frame %ld\n",vd.sequence);
-
-
- fwrite(convbuffer,2*vi.channels,out,stdout);
-
- vorbis_synthesis_read(&vd,out); /* tell libvorbis how
- many samples we
- actually consumed */
- }
- }
- }
- if(ogg_page_eos(&og))eos=1;
- }
- }
- if(!eos){
- buffer=ogg_sync_buffer(&oy,4096);
- bytes=fread(buffer,1,4096,stdin);
- ogg_sync_wrote(&oy,bytes);
- if(bytes==0)eos=1;
+ while(!eos){
+ int result=ogg_sync_pageout(&oy,&og);
+ if(result==0)break; /* need more data */
+ if(result<0){ /* missing or corrupt data at this page position */
+ fprintf(stderr,"Corrupt or missing data in bitstream; "
+ "continuing...\n");
+ }else{
+ ogg_stream_pagein(&os,&og); /* can safely ignore errors at
+ this point */
+ while(1){
+ result=ogg_stream_packetout(&os,&op);
+
+ if(result==0)break; /* need more data */
+ if(result<0){ /* missing or corrupt data at this page position */
+ /* no reason to complain; already complained above */
+ }else{
+ /* we have a packet. Decode it */
+ float **pcm;
+ int samples;
+
+ if(vorbis_synthesis(&vb,&op)==0) /* test for success! */
+ vorbis_synthesis_blockin(&vd,&vb);
+ /*
+
+ **pcm is a multichannel float vector. In stereo, for
+ example, pcm[0] is left, and pcm[1] is right. samples is
+ the size of each channel. Convert the float values
+ (-1.<=range<=1.) to whatever PCM format and write it out */
+
+ while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){
+ int j;
+ int clipflag=0;
+ int bout=(samples<convsize?samples:convsize);
+
+ /* convert floats to 16 bit signed ints (host order) and
+ interleave */
+ for(i=0;i<vi.channels;i++){
+ ogg_int16_t *ptr=convbuffer+i;
+ float *mono=pcm[i];
+ for(j=0;j<bout;j++){
+#if 1
+ int val=floor(mono[j]*32767.f+.5f);
+#else /* optional dither */
+ int val=mono[j]*32767.f+drand48()-0.5f;
+#endif
+ /* might as well guard against clipping */
+ if(val>32767){
+ val=32767;
+ clipflag=1;
+ }
+ if(val<-32768){
+ val=-32768;
+ clipflag=1;
+ }
+ *ptr=val;
+ ptr+=vi.channels;
+ }
+ }
+
+ if(clipflag)
+ fprintf(stderr,"Clipping in frame %ld\n",(long)(vd.sequence));
+
+
+ fwrite(convbuffer,2*vi.channels,bout,stdout);
+
+ vorbis_synthesis_read(&vd,bout); /* tell libvorbis how
+ many samples we
+ actually consumed */
+ }
+ }
+ }
+ if(ogg_page_eos(&og))eos=1;
+ }
+ }
+ if(!eos){
+ buffer=ogg_sync_buffer(&oy,4096);
+ bytes=fread(buffer,1,4096,stdin);
+ ogg_sync_wrote(&oy,bytes);
+ if(bytes==0)eos=1;
+ }