<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>libfishsound: Encoding audio data</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.2-20100208 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="files.html"><span>Files</span></a></li> </ul> </div> </div> <div class="contents"> <h1>Encoding audio data</h1> <p>To encode audio data using libfishsound:. <a href="#_details">More...</a></p> <table border="0" cellpadding="0" cellspacing="0"> </table> <p>To encode audio data using libfishsound:. </p> <ul> <li>create a FishSound* object with mode FISH_SOUND_ENCODE, and with a <a class="el" href="structFishSoundInfo.html" title="Info about a particular encoder/decoder instance.">FishSoundInfo</a> structure filled in with the required encoding parameters. <a class="el" href="fishsound_8h.html#adecddfef35cbbddcc8a76b28c365c527" title="Instantiate a new FishSound* handle.">fish_sound_new()</a> will return a new FishSound* object initialised for encoding.</li> <li>provide a FishSoundEncoded callback for libfishsound to call when it has a block of encoded audio</li> <li>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.</li> <li>when finished, call <a class="el" href="fishsound_8h.html#a002e2dee1a7f736699dba5bec0a81426" title="Delete a FishSound object.">fish_sound_delete()</a>.</li> </ul> <p>This procedure is illustrated in src/examples/fishsound-encode.c. Note that this example additionally:</p> <ul> <li>uses <a href="http://www.mega-nerd.com/libsndfile/">libsndfile</a> to read input from a PCM audio file (WAV, AIFF, etc.)</li> <li>uses <a href="http://www.annodex.net/software/liboggz/">liboggz</a> to encapsulate the encoded FLAC, Speex or Vorbis data in an Ogg stream.</li> </ul> <p>Hence this example code demonstrates all that is needed to encode Ogg FLAC, Speex and Ogg Vorbis files:</p> <div class="fragment"><pre class="fragment"> <span class="preprocessor">#include "config.h"</span> <span class="preprocessor">#include "fs_compat.h"</span> <span class="preprocessor">#include <stdio.h></span> <span class="preprocessor">#include <stdlib.h></span> <span class="preprocessor">#include <string.h></span> <span class="preprocessor">#include <time.h></span> <span class="preprocessor">#include <oggz/oggz.h></span> <span class="preprocessor">#include <<a class="code" href="fishsound_8h.html" title="The libfishsound C API.">fishsound/fishsound.h</a>></span> <span class="preprocessor">#include <sndfile.h></span> <span class="preprocessor">#define ENCODE_BLOCK_SIZE (1152)</span> <span class="preprocessor"></span> <span class="keywordtype">long</span> serialno; <span class="keywordtype">int</span> b_o_s = 1; <span class="keyword">static</span> <span class="keywordtype">int</span> encoded (<a class="code" href="fishsound_8h.html#a8cfb7dfb102ad1af9ff35046aed3ccd9" title="An opaque handle to a FishSound.">FishSound</a> * fsound, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> * buf, <span class="keywordtype">long</span> bytes, <span class="keywordtype">void</span> * user_data) { OGGZ * oggz = (OGGZ *)user_data; ogg_packet op; <span class="keywordtype">int</span> err; op.packet = buf; op.bytes = bytes; op.b_o_s = b_o_s; op.e_o_s = 0; op.granulepos = <a class="code" href="fishsound_8h.html#ad991f9f6887487f5d833de9480bcbe43" title="Query the current frame number of a FishSound object.">fish_sound_get_frameno</a> (fsound); op.packetno = -1; err = oggz_write_feed (oggz, &op, serialno, 0, NULL); <span class="keywordflow">if</span> (err) printf (<span class="stringliteral">"err: %d\n"</span>, err); b_o_s = 0; <span class="keywordflow">return</span> 0; } <span class="keywordtype">int</span> main (<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> ** argv) { OGGZ * oggz; <a class="code" href="fishsound_8h.html#a8cfb7dfb102ad1af9ff35046aed3ccd9" title="An opaque handle to a FishSound.">FishSound</a> * fsound; <a class="code" href="structFishSoundInfo.html" title="Info about a particular encoder/decoder instance.">FishSoundInfo</a> fsinfo; SNDFILE * sndfile; SF_INFO sfinfo; <span class="keywordtype">char</span> * infilename, * outfilename; <span class="keywordtype">char</span> * ext = NULL; <span class="keywordtype">int</span> format = <a class="code" href="constants_8h.html#aff8c305ecaa5b4dc29894d5d3fefbc08a219b41711c75ec89b1841e804139ceb1" title="Vorbis.">FISH_SOUND_VORBIS</a>; <span class="keywordtype">float</span> pcm[2048]; <span class="keywordflow">if</span> (argc < 3) { printf (<span class="stringliteral">"usage: %s infile outfile\n"</span>, argv[0]); printf (<span class="stringliteral">"*** FishSound example program. ***\n"</span>); printf (<span class="stringliteral">"Opens a PCM audio file and encodes it to an Ogg FLAC, Speex or Ogg Vorbis file.\n"</span>); exit (1); } infilename = argv[1]; outfilename = argv[2]; sndfile = sf_open (infilename, SFM_READ, &sfinfo); <span class="keywordflow">if</span> ((oggz = oggz_open (outfilename, OGGZ_WRITE)) == NULL) { printf (<span class="stringliteral">"unable to open file %s\n"</span>, outfilename); exit (1); } serialno = oggz_serialno_new (oggz); <span class="comment">/* If the given output filename ends in ".spx", encode as Speex,</span> <span class="comment"> * otherwise use Vorbis */</span> ext = strrchr (outfilename, <span class="charliteral">'.'</span>); <span class="keywordflow">if</span> (ext && !strncasecmp (ext, <span class="stringliteral">".spx"</span>, 4)) format = <a class="code" href="constants_8h.html#aff8c305ecaa5b4dc29894d5d3fefbc08aa708255d1f23a5a5db6f1c2d60778eb9" title="Speex.">FISH_SOUND_SPEEX</a>; <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ext && !strncasecmp (ext, <span class="stringliteral">".oga"</span>, 4)) format = <a class="code" href="constants_8h.html#aff8c305ecaa5b4dc29894d5d3fefbc08a528dd24db0ba08599924e7476efd0469" title="Flac.">FISH_SOUND_FLAC</a>; <span class="keywordflow">else</span> format = <a class="code" href="constants_8h.html#aff8c305ecaa5b4dc29894d5d3fefbc08a219b41711c75ec89b1841e804139ceb1" title="Vorbis.">FISH_SOUND_VORBIS</a>; fsinfo.<a class="code" href="structFishSoundInfo.html#ac1e2bc71184e6311f54ff6bcd9160123" title="Count of channels.">channels</a> = sfinfo.channels; fsinfo.<a class="code" href="structFishSoundInfo.html#a9b19fb535b78f4df6cd4a275a595e736" title="Sample rate of audio data in Hz.">samplerate</a> = sfinfo.samplerate; fsinfo.<a class="code" href="structFishSoundInfo.html#a3fd250e7150ce5eb58737f715264e913" title="FISH_SOUND_VORBIS, FISH_SOUND_SPEEX, FISH_SOUND_FLAC etc.">format</a> = format; fsound = <a class="code" href="fishsound_8h.html#adecddfef35cbbddcc8a76b28c365c527" title="Instantiate a new FishSound* handle.">fish_sound_new</a> (<a class="code" href="constants_8h.html#ac949e5a5c7f16cc7fd9d096a39608f34a3270a746cfa774a469f9783b6848279d" title="Encode.">FISH_SOUND_ENCODE</a>, &fsinfo); <a class="code" href="encode_8h.html#a31549d7a9181bfd33945c3f516f05d12" title="Set the callback for libfishsound to call when it has a block of encoded data ready...">fish_sound_set_encoded_callback</a> (fsound, encoded, oggz); <a class="code" href="deprecated_8h.html#a8e6b76134675d948015fe4afa3fe4104" title="DEPRECATED FUNCTION.">fish_sound_set_interleave</a> (fsound, 1); <a class="code" href="comments_8h.html#aef6ec7d8894aac5da78cb0f5711e7d67" title="Add a comment by name and value.">fish_sound_comment_add_byname</a> (fsound, <span class="stringliteral">"Encoder"</span>, <span class="stringliteral">"fishsound-encode"</span>); <span class="keywordflow">while</span> (sf_readf_float (sndfile, pcm, ENCODE_BLOCK_SIZE) > 0) { <a class="code" href="deprecated_8h.html#a6cd1117b2d09f0f9589c8b10cccae75c" title="DEPRECATED FUNCTION.">fish_sound_encode</a> (fsound, (<span class="keywordtype">float</span> **)pcm, ENCODE_BLOCK_SIZE); oggz_run (oggz); } <a class="code" href="fishsound_8h.html#ab1672c542e09528b3dad3ee694b124d8" title="Flush any internally buffered data, forcing encode.">fish_sound_flush</a> (fsound); oggz_run (oggz); oggz_close (oggz); <a class="code" href="fishsound_8h.html#a002e2dee1a7f736699dba5bec0a81426" title="Delete a FishSound object.">fish_sound_delete</a> (fsound); sf_close (sndfile); exit (0); } </pre></div> </div> <hr class="footer"/><address style="text-align: right;"><small>Generated by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2-20100208 </small></address> </body> </html>