libfishsound 1.0.1
Encoding audio data

To encode audio data using libfishsound:

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.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);
}
int fish_sound_comment_add_byname(FishSound *fsound, const char *name, const char *value)
Add a comment by name and value.
@ FISH_SOUND_ENCODE
Encode.
Definition constants.h:46
@ FISH_SOUND_VORBIS
Vorbis.
Definition constants.h:55
@ FISH_SOUND_FLAC
Flac.
Definition constants.h:61
@ FISH_SOUND_SPEEX
Speex.
Definition constants.h:58
long fish_sound_encode(FishSound *fsound, float **pcm, long frames)
DEPRECATED FUNCTION.
int fish_sound_set_interleave(FishSound *fsound, int interleave)
DEPRECATED FUNCTION.
int fish_sound_set_encoded_callback(FishSound *fsound, FishSoundEncoded encoded, void *user_data)
Set the callback for libfishsound to call when it has a block of encoded data ready.
The libfishsound C API.
int fish_sound_delete(FishSound *fsound)
Delete a FishSound object.
FishSound * fish_sound_new(int mode, FishSoundInfo *fsinfo)
Instantiate a new FishSound* handle.
void * FishSound
An opaque handle to a FishSound.
Definition fishsound.h:433
long fish_sound_flush(FishSound *fsound)
Flush any internally buffered data, forcing encode.
long fish_sound_get_frameno(FishSound *fsound)
Query the current frame number of a FishSound object.
Info about a particular encoder/decoder instance.
Definition fishsound.h:404