Addition of win32 support to the examples (changing stdin/stdout to binary
authorMike Smith <msmith@xiph.org>
Fri, 12 May 2000 08:38:20 +0000 (08:38 +0000)
committerMike Smith <msmith@xiph.org>
Fri, 12 May 2000 08:38:20 +0000 (08:38 +0000)
mode).

svn path=/trunk/vorbis/; revision=389

examples/decoder_example.c
examples/encoder_example.c

index 23542af..8e6abf2 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: simple example decoder
- last mod: $Id: decoder_example.c,v 1.6 2000/05/12 05:50:11 msmith Exp $
+ last mod: $Id: decoder_example.c,v 1.7 2000/05/12 08:38:20 msmith Exp $
 
  ********************************************************************/
 
 /* Note that this is POSIX, not ANSI code */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <math.h>
 #include "vorbis/codec.h"
 
+#ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
+#include <io.h>
+#include <fcntl.h>
+#endif
+
 int16_t convbuffer[4096]; /* take 8k out of the data segment, not the stack */
 int convsize=4096;
 
-int main(){
+int main(int argc, char **argv){
   ogg_sync_state   oy; /* sync and verify incoming physical bitstream */
   ogg_stream_state os; /* take physical pages, weld into a logical
                          stream of packets */
@@ -45,6 +51,14 @@ int main(){
   char *buffer;
   int  bytes;
 
+#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
+  /* Beware the evil ifdef. We avoid these where we can, but this one we 
+     cannot. Don't add any more, you'll probably go to hell if you do. */
+  _setmode( _fileno( stdin ), _O_BINARY );
+  _setmode( _fileno( stdout ), _O_BINARY );
+#endif
+
+
   /********** Decode setup ************/
 
   ogg_sync_init(&oy); /* Now we can read pages */
@@ -144,7 +158,7 @@ int main(){
       /* no harm in not checking before adding more */
       buffer=ogg_sync_buffer(&oy,4096);
       bytes=fread(buffer,1,4096,stdin);
-      if(bytes==0 && i < 2){
+      if(bytes==0 && i<2){
        fprintf(stderr,"End of file before finding all Vorbis headers!\n");
        exit(1);
       }
@@ -207,14 +221,14 @@ int main(){
              while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){
                int j;
                int clipflag=0;
-               int out=(samples<convsize?samples:convsize);
+               int bout=(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++){
+                 for(j=0;j<bout;j++){
                    int val=mono[j]*32767.;
                    /* might as well guard against clipping */
                    if(val>32767){
@@ -234,9 +248,9 @@ int main(){
                  fprintf(stderr,"Clipping in frame %ld\n",vd.sequence);
                
                
-               fwrite(convbuffer,2*vi.channels,out,stdout);
+               fwrite(convbuffer,2*vi.channels,bout,stdout);
                
-               vorbis_synthesis_read(&vd,out); /* tell libvorbis how
+               vorbis_synthesis_read(&vd,bout); /* tell libvorbis how
                                                   many samples we
                                                   actually consumed */
              }     
index 530e27f..a2851bf 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: simple example encoder
- last mod: $Id: encoder_example.c,v 1.6 2000/01/28 15:25:07 xiphmont Exp $
+ last mod: $Id: encoder_example.c,v 1.7 2000/05/12 08:38:20 msmith Exp $
 
  ********************************************************************/
 
 #include <math.h>
 #include "vorbis/modes.h"
 
+#ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
+#include <io.h>
+#include <fcntl.h>
+#endif
+
+
 #define READ 1024
 signed char readbuffer[READ*4+44]; /* out of the data segment, not the stack */
 
@@ -49,6 +55,14 @@ int main(){
      verify that it matches 16bit/stereo/44.1kHz.  This is just an
      example, after all. */
 
+#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
+  /* Beware the evil ifdef. We avoid these where we can, but this one we 
+     cannot. Don't add any more, you'll probably go to hell if you do. */
+  _setmode( _fileno( stdin ), _O_BINARY );
+  _setmode( _fileno( stdout ), _O_BINARY );
+#endif
+
+
   fread(readbuffer,1,44,stdin);
 
   /********** Encode setup ************/
@@ -68,8 +82,8 @@ int main(){
   /* set up our packet->stream encoder */
   /* pick a random serial number; that way we can more likely build
      chained streams just by concatenation */
-  srandom(time(NULL));
-  ogg_stream_init(&os,random());
+  srand(time(NULL));
+  ogg_stream_init(&os,rand());
 
   /* Vorbis streams begin with three headers; the initial header (with
      most of the codec setup parameters) which is mandated by the Ogg