libfishsound  1.0.1
Encoding audio data

To encode audio data using libfishsound: More...

To encode audio data using libfishsound:

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 FLAC, Speex and Ogg Vorbis 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 <sndfile.h>
#define ENCODE_BLOCK_SIZE (1152)
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 = fish_sound_get_frameno (fsound);
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];
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 FLAC, Speex or Ogg Vorbis 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 if (ext && !strncasecmp (ext, ".oga", 4))
format = FISH_SOUND_FLAC;
else
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_comment_add_byname (fsound, "Encoder", "fishsound-encode");
while (sf_readf_float (sndfile, pcm, ENCODE_BLOCK_SIZE) > 0) {
fish_sound_encode (fsound, (float **)pcm, ENCODE_BLOCK_SIZE);
oggz_run (oggz);
}
fish_sound_flush (fsound);
oggz_run (oggz);
oggz_close (oggz);
sf_close (sndfile);
exit (0);
}