Decoding audio data

To decode audio data using libfishsound:

To decode audio data using libfishsound:

- create a FishSound* object with mode FISH_SOUND_DECODE. fish_sound_new() will return a new FishSound* object, initialised for decoding, and the FishSoundInfo structure will be cleared.
- provide a FishSoundDecoded_* callback for libfishsound to call when it has decoded audio.
- (optionally) specify whether you want to receive interleaved or per-channel PCM data, using a fish_sound_set_interleave(). The default is for per-channel (non-interleaved) PCM.
- feed encoded audio data to libfishsound via fish_sound_decode(). libfishsound will decode the audio for you, calling the FishSoundDecoded_* callback you provided earlier each time it has a block of audio ready.
- when finished, call fish_sound_delete().

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

- uses liboggz to demultiplex audio data from an Ogg encapsulated FLAC, Speex or Vorbis stream. The step of feeding encoded data to libfishsound is done within the OggzReadPacket callback.
- uses libsndfile to write the decoded audio to a WAV file.

Hence this example code demonstrates all that is needed to decode Ogg FLAC, Speex or Ogg Vorbis files:

/*
Copyright (C) 2003 Commonwealth Scientific and Industrial Research
Organisation (CSIRO) Australia
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of CSIRO Australia nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <oggz/oggz.h>
#include <fishsound/fishsound.h>
#include <sndfile.h>
static char * infilename, * outfilename;
static int begun = 0;
static FishSoundInfo fsinfo;
static SNDFILE * sndfile;
/* In general, an Ogg file may contain multiple audio tracks in parallel.
* To keep this example simple, we only decode the first track that we find.
* Tracks ("logical bitstreams" in the Ogg documentations) are identified by
* a serialno.
*/
static long decode_serialno = -1;
static int
open_output (int samplerate, int channels)
{
SF_INFO sfinfo;
sfinfo.samplerate = samplerate;
sfinfo.channels = channels;
sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
sndfile = sf_open (outfilename, SFM_WRITE, &sfinfo);
return 0;
}
static int
decoded_float (FishSound * fsound, float ** pcm, long frames, void * user_data)
{
if (!begun) {
fish_sound_command (fsound, FISH_SOUND_GET_INFO, &fsinfo,
sizeof (FishSoundInfo));
open_output (fsinfo.samplerate, fsinfo.channels);
begun = 1;
}
sf_writef_float (sndfile, (float *)pcm, frames);
return 0;
}
static int
read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
FishSound * fsound = (FishSound *)user_data;
/* If we have not yet selected an audio track to decode, then try
* to identify this one. If it is a known audio codec, then remember its
* serialno.
* NB. We only try this if we are processing a BOS (beginning of stream)
* packet, and it contains at least 8 bytes of data. If it contained less
* than 8 bytes of data, fish_sound_identify would simply return
* FISH_SOUND_ERR_SHORT_IDENTIFY.
*/
if (decode_serialno == -1 && op->b_o_s && op->bytes >= 8) {
if (fish_sound_identify (op->packet, op->bytes) != FISH_SOUND_UNKNOWN)
decode_serialno = serialno;
}
/* If this is the track we are decoding, go ahead and decode it */
if (serialno == decode_serialno) {
fish_sound_prepare_truncation (fsound, op->granulepos, op->e_o_s);
fish_sound_decode (fsound, op->packet, op->bytes);
}
return 0;
}
int
main (int argc, char ** argv)
{
OGGZ * oggz;
FishSound * fsound;
long n;
if (argc < 3) {
printf ("usage: %s infilename outfilename\n", argv[0]);
printf ("*** FishSound example program. ***\n");
printf ("Decodes an Ogg FLAC, Speex or Ogg Vorbis file producing a PCM wav file.\n");
exit (1);
}
infilename = argv[1];
outfilename = argv[2];
fsound = fish_sound_new (FISH_SOUND_DECODE, NULL);
fish_sound_set_interleave (fsound, 1);
fish_sound_set_decoded_float_ilv (fsound, decoded_float, NULL);
if ((oggz = oggz_open ((char *) infilename, OGGZ_READ)) == NULL) {
printf ("unable to open file %s\n", infilename);
exit (1);
}
oggz_set_read_callback (oggz, -1, read_packet, fsound);
while ((n = oggz_read (oggz, 1024)) > 0);
oggz_close (oggz);
fish_sound_delete (fsound);
sf_close (sndfile);
exit (0);
}
FISH_SOUND_GET_INFO
@ FISH_SOUND_GET_INFO
Retrieve the FishSoundInfo.
Definition constants.h:82
FISH_SOUND_DECODE
@ FISH_SOUND_DECODE
Decode.
Definition constants.h:43
FISH_SOUND_UNKNOWN
@ FISH_SOUND_UNKNOWN
Unknown.
Definition constants.h:52
fish_sound_decode
long fish_sound_decode(FishSound *fsound, unsigned char *buf, long bytes)
Decode a block of compressed data.
fish_sound_set_decoded_float_ilv
int fish_sound_set_decoded_float_ilv(FishSound *fsound, FishSoundDecoded_FloatIlv decoded, void *user_data)
Set the callback for libfishsound to call when it has a block of decoded PCM audio ready,...
fish_sound_set_interleave
int fish_sound_set_interleave(FishSound *fsound, int interleave)
DEPRECATED FUNCTION.
fishsound.h
The libfishsound C API.
fish_sound_delete
int fish_sound_delete(FishSound *fsound)
Delete a FishSound object.
fish_sound_identify
int fish_sound_identify(unsigned char *buf, long bytes)
Identify a codec based on the first few bytes of data.
fish_sound_command
int fish_sound_command(FishSound *fsound, int command, void *data, int datasize)
Command interface.
fish_sound_new
FishSound * fish_sound_new(int mode, FishSoundInfo *fsinfo)
Instantiate a new FishSound* handle.
fish_sound_prepare_truncation
int fish_sound_prepare_truncation(FishSound *fsound, long next_granulepos, int next_eos)
Prepare truncation details for the next block of data.
FishSound
void * FishSound
An opaque handle to a FishSound.
Definition fishsound.h:433
FishSoundInfo
Info about a particular encoder/decoder instance.
Definition fishsound.h:404
FishSoundInfo::samplerate
int samplerate
Sample rate of audio data in Hz.
Definition fishsound.h:406
FishSoundInfo::channels
int channels
Count of channels.
Definition fishsound.h:409