To encode audio data using libfishsound:
More...
To encode audio data using libfishsound:
- create a FishSound* object with mode FISH_SOUND_ENCODE, and with a FishSoundInfo structure filled in with the required encoding parameters. fish_sound_new() will return a new FishSound* object initialised for encoding.
- provide a FishSoundEncoded callback for libfishsound to call when it has a block of encoded audio
- feed raw PCM audio data to libfishsound via fish_sound_encode_*(). libfishsound will encode the audio for you, calling the FishSoundEncoded callback you provided earlier each time it has a block of encoded audio ready.
- when finished, call fish_sound_delete().
This procedure is illustrated in src/examples/fishsound-encode.c. Note that this example additionally:
- uses libsndfile to read input from a PCM audio file (WAV, AIFF, etc.)
- uses liboggz to encapsulate the encoded FLAC, Speex or Vorbis data in an Ogg stream.
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.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;
SNDFILE * sndfile;
SF_INFO sfinfo;
char * infilename, * outfilename;
char * ext = NULL;
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);
ext = strrchr (outfilename, '.');
if (ext && !strncasecmp (ext, ".spx", 4))
else if (ext && !strncasecmp (ext, ".oga", 4))
else
while (sf_readf_float (sndfile, pcm, ENCODE_BLOCK_SIZE) > 0) {
oggz_run (oggz);
}
oggz_run (oggz);
oggz_close (oggz);
sf_close (sndfile);
exit (0);
}