Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Combining the comment of Artefact2 (using <code>aplay</code> for output) and the answer of kay (using <code>pipe()</code>, which I had not touched before), I came up with this "minimal" example. For the ALSA version, it creates a pipe, forks an <code>aplay</code> process with the appropriate parameters, and feeds the decoded audio to it.</p> <p>Using lots of <code>assert()</code> to show the error codes associated with the individual function calls. The next step, of course, would not be the adding of <code>-DNDEBUG</code> (which would make this program <em>really</em> quick and <em>really</em> useless), but the replacing of the asserts with appropriate error handling including human-readable error messages.</p> <pre><code>// A small example program showing how to decode an MP3 file. #include &lt;assert.h&gt; #include &lt;stdlib.h&gt; #include &lt;mpg123.h&gt; #include &lt;unistd.h&gt; #include &lt;fcntl.h&gt; int main( int argc, char * argv[] ) { // buffer and counter for decoded audio data size_t OUTSIZE = 65536; unsigned char outmem[OUTSIZE]; size_t outbytes; // output file descriptor int outfile; // handle, return code for mpg123 mpg123_handle * handle; int rc; // one command line parameter, being the MP3 filename assert( argc == 2 ); #ifdef OSS assert( ( outfile = open( "/dev/audio", O_WRONLY ) ) != -1 ); #else // ALSA // pipe file descriptors int piped[2]; assert( pipe( piped ) != -1 ); // fork into decoder (parent) and player (child) if ( fork() ) { // player (child) assert( dup2( piped[0], 0 ) != -1 ); // make pipe-in the new stdin assert( close( piped[1] ) == 0 ); // pipe-out, not needed assert( execlp( "aplay", "aplay", "-q", "-r44100", "-c2", "-fS16_LE", "-traw", NULL ) != -1 ); // should not return } else { // decoder (parent) close( piped[0] ); // pipe-in, not needed outfile = piped[1]; } #endif // initializing assert( mpg123_init() == MPG123_OK ); assert( atexit( mpg123_exit ) == 0 ); // setting up handle assert( ( handle = mpg123_new( NULL, NULL ) ) != NULL ); // clearing the format list, and setting the one preferred format assert( mpg123_format_none( handle ) == MPG123_OK ); assert( mpg123_format( handle, 44100, MPG123_STEREO, MPG123_ENC_SIGNED_16 ) == MPG123_OK ); // open input MP3 file assert( mpg123_open( handle, argv[1] ) == MPG123_OK ); // loop over input while ( rc != MPG123_DONE ) { rc = mpg123_read( handle, outmem, OUTSIZE, &amp;outbytes ); assert( rc != MPG123_ERR &amp;&amp; rc != MPG123_NEED_MORE ); assert( write( outfile, outmem, outbytes ) != -1 ); } // cleanup assert( close( outfile ) == 0 ); mpg123_delete( handle ); return EXIT_SUCCESS; } </code></pre> <p>I hope this helps others with similar problems, as a template.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload