Basic MacOS9 build support; libs and examples
[platform/upstream/libvorbis.git] / examples / encoder_example.c
index d8c37d4..38070a9 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: simple example encoder
- last mod: $Id: encoder_example.c,v 1.4 2000/01/22 13:28:09 xiphmont Exp $
+ last mod: $Id: encoder_example.c,v 1.9 2000/08/14 22:33:50 xiphmont Exp $
 
  ********************************************************************/
 
 #include <stdlib.h>
 #include <time.h>
 #include <math.h>
-#include "vorbis/codec.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
+
+#if defined(macintosh) && defined(__MWERKS__)
+#include <console.h>      /* CodeWarrior's Mac "command-line" support */
+#endif
 
 #define READ 1024
 signed char readbuffer[READ*4+44]; /* out of the data segment, not the stack */
@@ -36,37 +45,55 @@ int main(){
   ogg_page         og; /* one Ogg bitstream page.  Vorbis packets are inside */
   ogg_packet       op; /* one raw packet of data for decode */
   
-  vorbis_info      vi; /* struct that stores all the static vorbis bitstream
+  vorbis_info     *vi; /* struct that stores all the static vorbis bitstream
                          settings */
+  vorbis_comment   vc; /* struct that stores all the user comments */
+
   vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
   vorbis_block     vb; /* local working space for packet->PCM decode */
 
   int eos=0;
 
+#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 */
+#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. */
+  _setmode( _fileno( stdin ), _O_BINARY );
+  _setmode( _fileno( stdout ), _O_BINARY );
+#endif
+
+
   fread(readbuffer,1,44,stdin);
 
   /********** Encode setup ************/
 
   /* choose an encoding mode */
   /* (mode 0: 44kHz stereo uncoupled, roughly 128kbps VBR) */
-  vorbis_info_modeset(&vi,0); 
+  vi=&info_A;
 
   /* add a comment */
-  vorbis_info_addcomment(&vi,"Track encoded by encoder_example.c");
+  vorbis_comment_init(&vc);
+  vorbis_comment_add(&vc,"Track encoded by encoder_example.c");
 
   /* set up the analysis state and auxiliary encoding storage */
-  vorbis_analysis_init(&vd,&vi);
+  vorbis_analysis_init(&vd,vi);
   vorbis_block_init(&vd,&vb);
   
   /* 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
@@ -80,7 +107,7 @@ int main(){
     ogg_packet header_comm;
     ogg_packet header_code;
 
-    vorbis_info_headerout(&vi,&header,&header_comm,&header_code);
+    vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code);
     ogg_stream_packetin(&os,&header); /* automatically placed in its own
                                         page */
     ogg_stream_packetin(&os,&header_comm);
@@ -125,7 +152,7 @@ int main(){
 
       /* analysis */
       vorbis_analysis(&vb,&op);
-
+      
       /* weld the packet into the bitstream */
       ogg_stream_packetin(&os,&op);
 
@@ -150,7 +177,6 @@ int main(){
   ogg_stream_clear(&os);
   vorbis_block_clear(&vb);
   vorbis_dsp_clear(&vd);
-  vorbis_info_clear(&vi);
   
   /* ogg_page and ogg_packet structs always point to storage in
      libvorbis.  They're never freed or manipulated directly */