Main Page | Modules | Data Structures | File List | Data Fields | Globals

Encoding audio data

To encode audio data using libfishsound:. More...

This procedure is illustrated in src/examples/fishsound-encode.c. Note that this example additionally:

Hence this example code demonstrates all that is needed to encode Ogg Vorbis and Ogg Speex files:

#include "config.h"
#include "fs_compat.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <oggz/oggz.h>
#include <fishsound/fishsound.h>
#include <sndfile.h>

long serialno;
int b_o_s = 1;

static int
encoded (FishSound * fsound, unsigned char * buf, long bytes, void * user_data)
{
  OGGZ * oggz = (OGGZ *)user_data;
  ogg_packet op;
  int err;

  op.packet = buf;
  op.bytes = bytes;
  op.b_o_s = b_o_s;
  op.e_o_s = 0;
  op.granulepos = 0; /* frameno */
  op.packetno = -1;

  err = oggz_write_feed (oggz, &op, serialno, 0, NULL);
  if (err) printf ("err: %d\n", err);

  b_o_s = 0;

  return 0;
}

int
main (int argc, char ** argv)
{
  OGGZ * oggz;
  FishSound * fsound;
  FishSoundInfo fsinfo;
  SNDFILE * sndfile;
  SF_INFO sfinfo;

  char * infilename, * outfilename;
  char * ext = NULL;
  int format = FISH_SOUND_VORBIS;

  float pcm[2048];
  long n;

  if (argc < 3) {
    printf ("usage: %s infile outfile\n", argv[0]);
    printf ("*** FishSound example program. ***\n");
    printf ("Opens a pcm audio file and encodes it to an Ogg Vorbis or Speex file.\n");
    exit (1);
  }

  infilename = argv[1];
  outfilename = argv[2];

  sndfile = sf_open (infilename, SFM_READ, &sfinfo);

  if ((oggz = oggz_open (outfilename, OGGZ_WRITE)) == NULL) {
    printf ("unable to open file %s\n", outfilename);
    exit (1);
  }

  serialno = oggz_serialno_new (oggz);

  /* If the given output filename ends in ".spx", encode as Speex,
   * otherwise use Vorbis */
  ext = strrchr (outfilename, '.');
  if (ext && !strncasecmp (ext, ".spx", 4))
    format = FISH_SOUND_SPEEX;
  else
    format = FISH_SOUND_VORBIS;

  fsinfo.channels = sfinfo.channels;
  fsinfo.samplerate = sfinfo.samplerate;
  fsinfo.format = format;

  fsound = fish_sound_new (FISH_SOUND_ENCODE, &fsinfo);
  fish_sound_set_encoded_callback (fsound, encoded, oggz);

  fish_sound_set_interleave (fsound, 1);

  while (sf_readf_float (sndfile, pcm, 1024) > 0) {
    fish_sound_encode (fsound, (float **)pcm, 1024);
    while ((n = oggz_write (oggz, 1024)) > 0);
  }

  fish_sound_flush (fsound);
  while ((n = oggz_write (oggz, 1024)) > 0);

  oggz_close (oggz);

  fish_sound_delete (fsound);

  sf_close (sndfile);

  exit (0);
}

Generated on Thu Jun 24 18:46:04 2004 for libfishsound by doxygen 1.3.5