Vorbisfile documentation

libVorbisfile version 1.65 - 20020702

Example Code

The following is a run-through of the decoding example program supplied with libvorbisfile, vorbisfile_example.c. This program takes a vorbis bitstream from stdin and writes raw pcm to stdout.
以下は libvorbisfile と共に提供されるデコードのサンプルプログラム、vorbisfile_example.c の解説である。 このプログラムは stdin から vorbis ビットストリームを受け取り、生のPCMデータを stdout に書き出す。

First, relevant headers, including vorbis-specific "vorbis/codec.h" and "vorbisfile.h" have to be included.
最初は、関連するヘッダ、vorbisで読み込む必要のある "vorbis/codec.h" と "vorbisfile.h" を読み込んでいる。


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "vorbis/codec.h"
#include "vorbisfile.h"

We also have to make a concession to Windows users here. If we are using windows for decoding, we must declare these libraries so that we can set stdin/stdout to binary.
ここでWindowsユーザーのための設定をする。 もしデコードにWindowsを使っている場合、stdin/stdoutをバイナリモードに設定するために、 これらのライブラリを宣言しなければならない。


#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#endif

Next, a buffer for the pcm audio output is declared.
次は、PCM音声出力のためのバッファを宣言する。


char pcmout[4096];

Inside main(), we declare our primary OggVorbis_File structure. We also declare a few other helpful variables to track out progress within the file. Also, we make our final concession to Windows users by setting the stdin and stdout to binary mode.
main()の中で OggVorbis_File構造体を宣言する。 ファイル内の進行を表示するために役立ついくつかの他の変数も定義しておく。 また、Windowsユーザーのための最後の設定(stdinとstdoutをバイナリモードにすること)を行う。


int main(int argc, char **argv){
  OggVorbis_File vf;
  int eof=0;
  int current_section;

#ifdef _WIN32
  _setmode( _fileno( stdin ), _O_BINARY );
  _setmode( _fileno( stdout ), _O_BINARY );
#endif

ov_open() must be called to initialize the OggVorbis_File structure with default values. ov_open() also checks to ensure that we're reading Vorbis format and not something else.
ov_open()OggVorbis_File 構造体を初期値で初期化するために必ず呼ばなければならない。 ov_open() はまた、Vorbisフォーマットを呼んでいるのか、そうでないのかをチェックも行う。


  if(ov_open(stdin, &vf, NULL, 0) < 0) {
      fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
      exit(1);
  }

We're going to pull the channel and bitrate info from the file using ov_info() and show them to the user. We also want to pull out and show the user a comment attached to the file using ov_comment().
ov_info() を使い、ファイルからチャンネル数やビットレート情報を取り出し、ユーザーに見せる。 また、ov_comment() を使い、ファイルに付属しているコメントを取り出してユーザーに見せる。


  {
    char **ptr=ov_comment(&vf,-1)->user_comments;
    vorbis_info *vi=ov_info(&vf,-1);
    while(*ptr){
      fprintf(stderr,"%s\n",*ptr);
      ++ptr;
    }
    fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
    fprintf(stderr,"\nDecoded length: %ld samples\n",
            (long)ov_pcm_total(&vf,-1));
    fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
  }
  

Here's the read loop:
ここから読み込みループだ:



  while(!eof){
    long ret=ov_read(&vf,pcmout,sizeof(pcmout),¤t_section);
    if (ret == 0) {
      /* EOF */
      eof=1;
    } else if (ret < 0) {
      /* error in the stream.  Not a problem, just reporting it in
	 case we (the app) cares.  In this case, we don't. */
    } else {
      /* we don't bother dealing with sample rate changes, etc, but
	 you'll have to*/
      fwrite(pcmout,1,ret,stdout);
    }
  }

  

The code is reading blocks of data using ov_read(). Based on the value returned, we know if we're at the end of the file or have invalid data. If we have valid data, we write it to the pcm output.
ov_read() を使ってデータのブロックを読み込む。 戻り値によって、ファイルの終わりか、無効なデータがあったことを知ることができる。 もし有効なデータを取り出せた場合、PCM出力に書き出す。

Now that we've finished playing, we can pack up and go home. It's important to call ov_clear() when we're finished.
演奏が終わると、やめて帰る。 完了したときに ov_clear() を呼ぶことは重要。



  ov_clear(&vf);
    
  fprintf(stderr,"Done.\n");
  return(0);
}




copyright © 2002 Xiph.org

Ogg Vorbis

Vorbisfile documentation

libVorbisfile version 1.65 - 20020702