[xiph-cvs] cvs commit: vorbis/examples chaining_example.c decoder_example.c encoder_example.c seeking_example.c vorbisfile_example.c

Chris Wolf cwolf at xiph.org
Fri Sep 14 21:47:51 PDT 2001



cwolf       01/09/14 21:47:50

  Modified:    examples chaining_example.c decoder_example.c
                        encoder_example.c seeking_example.c
                        vorbisfile_example.c
  Log:
  Add support for named files on the command line, while maintaining
  current filter pipeline (stdin/stdout) mode of operation.

Revision  Changes    Path
1.12      +31 -22    vorbis/examples/chaining_example.c

Index: chaining_example.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis/examples/chaining_example.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- chaining_example.c	2001/09/13 23:34:46	1.11
+++ chaining_example.c	2001/09/15 04:47:47	1.12
@@ -11,7 +11,7 @@
  ********************************************************************
 
  function: illustrate simple use of chained bitstream and vorbisfile.a
- last mod: $Id: chaining_example.c,v 1.11 2001/09/13 23:34:46 cwolf Exp $
+ last mod: $Id: chaining_example.c,v 1.12 2001/09/15 04:47:47 cwolf Exp $
 
  ********************************************************************/
 
@@ -23,8 +23,10 @@
 #include <fcntl.h>
 #endif
 
-int main(){
+int main(int argc, char *argv[]){
   OggVorbis_File ov;
+  char msg[256];
+  FILE *fpin=NULL;
   int i;
 
 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
@@ -34,8 +36,22 @@
   _setmode( _fileno( stdout ), _O_BINARY );
 #endif
 
+  /* If command line args were supplied, open the named file(s)
+     for i/o, else maintain use of stdin/stdout.*/
+  if (argc == 2)
+  {
+    if ((fpin = fopen(argv[1], "rb")) == (FILE*)NULL)
+    {
+      (void)sprintf(msg, "Can't open \"%s\" for input", argv[1]);
+      perror(msg);
+      return 1;
+    }
+  }
+  else
+    fpin = stdin;
+
   /* open the file/pipe on stdin */
-  if(ov_open(stdin,&ov,NULL,-1)<0){
+  if(ov_open(fpin,&ov,NULL,-1)<0){
     printf("Could not open input as an OggVorbis file.\n\n");
     exit(1);
   }
@@ -43,40 +59,33 @@
   /* print details about each logical bitstream in the input */
   if(ov_seekable(&ov)){
     printf("Input bitstream contained %ld logical bitstream section(s).\n",
-	   ov_streams(&ov));
+     ov_streams(&ov));
     printf("Total bitstream playing time: %ld seconds\n\n",
-	   (long)ov_time_total(&ov,-1));
+    (long)ov_time_total(&ov,-1));
 
   }else{
     printf("Standard input was not seekable.\n"
-	   "First logical bitstream information:\n\n");
+    "First logical bitstream information:\n\n");
   }
 
   for(i=0;i<ov_streams(&ov);i++){
     vorbis_info *vi=ov_info(&ov,i);
     printf("\tlogical bitstream section %d information:\n",i+1);
     printf("\t\t%ldHz %d channels bitrate %ldkbps serial number=%ld\n",
-	   vi->rate,vi->channels,ov_bitrate(&ov,i)/1000,
-	   ov_serialnumber(&ov,i));
+    vi->rate,vi->channels,ov_bitrate(&ov,i)/1000,
+    ov_serialnumber(&ov,i));
     printf("\t\theader length: %ld bytes\n",(long)
-	   (ov.dataoffsets[i]-ov.offsets[i]));
+    (ov.dataoffsets[i]-ov.offsets[i]));
     printf("\t\tcompressed length: %ld bytes ",(long)(ov_raw_total(&ov,i)));
     printf(" play time: %lds\n",(long)ov_time_total(&ov,i));
   }
 
   ov_clear(&ov);
-  return 0;
-}
-
-
-
-
-
 
-
-
-
-
-
-
+  if (argc == 2)
+  {
+    (void)fclose(fpin);
+  }
 
+  return 0;
+}

1.21      +31 -5     vorbis/examples/decoder_example.c

Index: decoder_example.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis/examples/decoder_example.c,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- decoder_example.c	2001/05/27 20:33:18	1.20
+++ decoder_example.c	2001/09/15 04:47:48	1.21
@@ -11,7 +11,7 @@
  ********************************************************************
 
  function: simple example decoder
- last mod: $Id: decoder_example.c,v 1.20 2001/05/27 20:33:18 xiphmont Exp $
+ last mod: $Id: decoder_example.c,v 1.21 2001/09/15 04:47:48 cwolf Exp $
 
  ********************************************************************/
 
@@ -53,6 +53,9 @@
   
   char *buffer;
   int  bytes;
+  FILE *fpin=NULL;
+  FILE *fpout=NULL;
+  char msg[512];
 
 #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 
@@ -67,6 +70,29 @@
                           /* this also lets the user set stdin and stdout */
 #endif
 
+  /* If command line args were supplied, open the named file(s)
+     for i/o, else maintain use of stdin/stdout.*/
+  if (argc == 3)
+  {
+    if ((fpin = fopen(argv[1], "rb")) == (FILE*)NULL)
+    {
+      (void)sprintf(msg, "Can't open %s for reading.", argv[1]);
+      perror(msg);
+      return 1;
+    }
+
+    if ((fpout = fopen(argv[2], "wb")) == (FILE*)NULL)
+    {
+      (void)sprintf(msg, "Can't open %s for writing.", argv[2]);
+      perror(msg);
+      return 1;
+    }
+  }
+  else
+  {
+    fpin = stdin;
+    fpout = stdout;
+  }
   /********** Decode setup ************/
 
   ogg_sync_init(&oy); /* Now we can read pages */
@@ -82,7 +108,7 @@
 
     /* submit a 4k block to libvorbis' Ogg layer */
     buffer=ogg_sync_buffer(&oy,4096);
-    bytes=fread(buffer,1,4096,stdin);
+    bytes=fread(buffer,1,4096,fpin);
     ogg_sync_wrote(&oy,bytes);
     
     /* Get the first page. */
@@ -165,7 +191,7 @@
       }
       /* no harm in not checking before adding more */
       buffer=ogg_sync_buffer(&oy,4096);
-      bytes=fread(buffer,1,4096,stdin);
+      bytes=fread(buffer,1,4096,fpin);
       if(bytes==0 && i<2){
         fprintf(stderr,"End of file before finding all Vorbis headers!\n");
         exit(1);
@@ -261,7 +287,7 @@
                   fprintf(stderr,"Clipping in frame %ld\n",(long)(vd.sequence));
                 
                 
-		fwrite(convbuffer,2*vi.channels,bout,stdout);
+		fwrite(convbuffer,2*vi.channels,bout,fpout);
                 
                 vorbis_synthesis_read(&vd,bout); /* tell libvorbis how
                                                    many samples we
@@ -274,7 +300,7 @@
       }
       if(!eos){
         buffer=ogg_sync_buffer(&oy,4096);
-	bytes=fread(buffer,1,4096,stdin);
+	bytes=fread(buffer,1,4096,fpin);
         ogg_sync_wrote(&oy,bytes);
         if(bytes==0)eos=1;
       }

1.24      +66 -16    vorbis/examples/encoder_example.c

Index: encoder_example.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis/examples/encoder_example.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- encoder_example.c	2001/08/13 11:40:43	1.23
+++ encoder_example.c	2001/09/15 04:47:48	1.24
@@ -11,7 +11,7 @@
  ********************************************************************
 
  function: simple example encoder
- last mod: $Id: encoder_example.c,v 1.23 2001/08/13 11:40:43 xiphmont Exp $
+ last mod: $Id: encoder_example.c,v 1.24 2001/09/15 04:47:48 cwolf Exp $
 
  ********************************************************************/
 
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include <math.h>
+#include <string.h>
 #include <vorbis/vorbisenc.h>
 
 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
@@ -38,7 +39,7 @@
 #define READ 1024
 signed char readbuffer[READ*4+44]; /* out of the data segment, not the stack */
 
-int main(){
+int main(int argc, char *argv[]){
   ogg_stream_state os; /* take physical pages, weld into a logical
                           stream of packets */
   ogg_page         og; /* one Ogg bitstream page.  Vorbis packets are inside */
@@ -52,18 +53,18 @@
   vorbis_block     vb; /* local working space for packet->PCM decode */
 
   int eos=0;
+  FILE *fpin=NULL;
+  FILE *fpout=NULL;
+  char msg[512];
+  int i, founddata;
 
 #if defined(macintosh) && defined(__MWERKS__)
-  int argc = 0;
-  char **argv = NULL;
-  argc = ccommand(&argv); /* get a "command line" from the Mac user */
-                          /* this also lets the user set stdin and stdout */
+  int ac = 0;
+  char **av = NULL;
+  ac = ccommand(&av); /* get a "command line" from the Mac user */
+                      /* this also lets the user set stdin and stdout */
 #endif
 
-  /* we cheat on the WAV header; we just bypass 44 bytes and never
-     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. */
@@ -71,8 +72,57 @@
   _setmode( _fileno( stdout ), _O_BINARY );
 #endif
 
+  /* If command line args were supplied, open the named file(s)
+     for i/o, else maintain use of stdin/stdout.*/
+  if (argc == 3)
+  {
+    if ((fpin = fopen(argv[1], "rb")) == (FILE*)NULL)
+    {
+      (void)sprintf(msg, "Can't open %s for reading.", argv[1]);
+      perror(msg);
+      return 1;
+    }
+
+    if ((fpout = fopen(argv[2], "wb")) == (FILE*)NULL)
+    {
+      (void)sprintf(msg, "Can't open %s for writing.", argv[2]);
+      perror(msg);
+      return 1;
+    }
+  }
+  else
+  {
+    fpin = stdin;
+    fpout = stdout;
+  }
 
-  fread(readbuffer,1,44,stdin);
+  /* we cheat on the WAV header; we just bypass the header and never
+     verify that it matches 16bit/stereo/44.1kHz.  This is just an
+     example, after all. */
+
+  readbuffer[0] = '\0';
+  for (i=0, founddata=0; i<40 && ! feof(fpin) && ! ferror(fpin); i++)
+  {
+    fread(readbuffer,1,2,fpin);
+
+    if ( ! strncmp(readbuffer, "da", 2) )
+    {
+      founddata = 1;
+      fread(readbuffer,1,6,fpin);
+    }
+  }
+
+  if ( feof(fpin) || ferror(fpin) )
+  {
+    (void)fprintf(stderr, "Error: Input WAV too short, or corrupt.\n");
+    return(1);
+  }
+
+  if ( ! founddata )
+  {
+    (void)fprintf(stderr, "Error: Can't find \"data\" chunk in WAV input.\n");
+    return(1);
+  }
 
   /********** Encode setup ************/
 
@@ -120,15 +170,15 @@
         while(!eos){
                 int result=ogg_stream_flush(&os,&og);
                 if(result==0)break;
-		fwrite(og.header,1,og.header_len,stdout);
-		fwrite(og.body,1,og.body_len,stdout);
+		fwrite(og.header,1,og.header_len,fpout);
+		fwrite(og.body,1,og.body_len,fpout);
         }
 
   }
   
   while(!eos){
     long i;
-    long bytes=fread(readbuffer,1,READ*4,stdin); /* stereo hardwired here */
+    long bytes=fread(readbuffer,1,READ*4,fpin); /* stereo hardwired here */
 
     if(bytes==0){
       /* end of file.  this can be done implicitly in the mainline,
@@ -170,8 +220,8 @@
       while(!eos){
         int result=ogg_stream_pageout(&os,&og);
         if(result==0)break;
-	fwrite(og.header,1,og.header_len,stdout);
-	fwrite(og.body,1,og.body_len,stdout);
+	fwrite(og.header,1,og.header_len,fpout);
+	fwrite(og.body,1,og.body_len,fpout);
 
         /* this could be set above, but for illustrative purposes, I do
            it here (to show that vorbis does know where the stream ends) */

1.9       +29 -4     vorbis/examples/seeking_example.c

Index: seeking_example.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis/examples/seeking_example.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- seeking_example.c	2001/05/27 06:43:58	1.8
+++ seeking_example.c	2001/09/15 04:47:48	1.9
@@ -11,7 +11,7 @@
  ********************************************************************
 
  function: illustrate seeking, and test it too
- last mod: $Id: seeking_example.c,v 1.8 2001/05/27 06:43:58 xiphmont Exp $
+ last mod: $Id: seeking_example.c,v 1.9 2001/09/15 04:47:48 cwolf Exp $
 
  ********************************************************************/
 
@@ -19,8 +19,12 @@
 #include <stdio.h>
 #include "vorbis/codec.h"
 #include "vorbis/vorbisfile.h"
-#include "../lib/misc.h"
+//#include "../lib/misc.h"
 
+#ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
+# include <io.h>
+# include <fcntl.h>
+#endif
 
 void _verify(OggVorbis_File *ov,ogg_int64_t pos,
              ogg_int64_t val,ogg_int64_t pcmval,
@@ -56,15 +60,36 @@
   }
 }
 
-int main(){
+int main(int argc, char *argv[]){
   OggVorbis_File ov;
   int i,ret;
   ogg_int64_t pcmlength;
   char *bigassbuffer;
   int dummy;
+  char msg[256];
+  FILE *fpin=NULL;
 
+#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
+  _setmode( _fileno( stdin ), _O_BINARY );
+  _setmode( _fileno( stdout ), _O_BINARY );
+#endif
+
+  /* If command line args were supplied, open the named file(s)
+     for i/o, else maintain use of stdin/stdout.*/
+  if (argc == 2)
+  {
+    if ((fpin = fopen(argv[1], "rb")) == (FILE*)NULL)
+    {
+      (void)sprintf(msg, "Can't open \"%s\" for input", argv[1]);
+      perror(msg);
+      return 1;
+    }
+  }
+  else
+    fpin = stdin;
+
   /* open the file/pipe on stdin */
-  if(ov_open(stdin,&ov,NULL,-1)<0){
+  if(ov_open(fpin,&ov,NULL,-1)<0){
     printf("Could not open input as an OggVorbis file.\n\n");
     exit(1);
   }

1.6       +28 -3     vorbis/examples/vorbisfile_example.c

Index: vorbisfile_example.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis/examples/vorbisfile_example.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- vorbisfile_example.c	2001/02/26 03:50:38	1.5
+++ vorbisfile_example.c	2001/09/15 04:47:48	1.6
@@ -11,7 +11,7 @@
  ********************************************************************
 
  function: simple example decoder using vorbisfile
- last mod: $Id: vorbisfile_example.c,v 1.5 2001/02/26 03:50:38 xiphmont Exp $
+ last mod: $Id: vorbisfile_example.c,v 1.6 2001/09/15 04:47:48 cwolf Exp $
 
  ********************************************************************/
 
@@ -36,6 +36,9 @@
   OggVorbis_File vf;
   int eof=0;
   int current_section;
+  FILE *fpin=NULL;
+  FILE *fpout=NULL;
+  char msg[512];
 
 #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 
@@ -44,7 +47,29 @@
   _setmode( _fileno( stdout ), _O_BINARY );
 #endif
 
-  if(ov_open(stdin, &vf, NULL, 0) < 0) {
+  if (argc == 3)
+  {
+    if ((fpin = fopen(argv[1], "rb")) == (FILE*)NULL)
+    {
+      (void)sprintf(msg, "Can't open %s for reading.", argv[1]);
+      perror(msg);
+      return 1;
+    }
+
+    if ((fpout = fopen(argv[2], "wb")) == (FILE*)NULL)
+    {
+      (void)sprintf(msg, "Can't open %s for writing.", argv[2]);
+      perror(msg);
+      return 1;
+    }
+  }
+  else
+  {
+    fpin = stdin;
+    fpout = stdout;
+  }
+
+  if(ov_open(fpin, &vf, NULL, 0) < 0) {
       fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
       exit(1);
   }
@@ -75,7 +100,7 @@
     } else {
       /* we don't bother dealing with sample rate changes, etc, but
          you'll have to*/
-      fwrite(pcmout,1,ret,stdout);
+      fwrite(pcmout,1,ret,fpout);
     }
   }
 

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list