Find file
Fetching contributors…
Cannot retrieve contributors at this time
99 lines (82 sloc) 2.71 KB
/**
* Usage example for gstreamer_file.
* Written by (2012) René Kijewski <rene.SURNAME@fu-berlin.de> using
* http://www.mpg123.de/api/mpg123__to__wav_8c_source.shtml as a template.
*/
#include "gstreamer_file.h"
#include <unistd.h>
/*
mpg123_to_wav.c
copyright 2007 by the mpg123 project - free software under the terms of the LGPL 2.1
see COPYING and AUTHORS files in distribution or http://mpg123.org
initially written by Nicholas Humfrey
*/
#include <stdio.h>
#include <strings.h>
#include <mpg123.h>
static mpg123_handle *mh = NULL;
static FILE *output = NULL;
static void cleanup(mpg123_handle *mh)
{
/* It's really to late for error checks here;-) */
fclose(output);
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
}
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("Usage: %s <input>\n", argv[0]);
return 1;
}
int err = mpg123_init();
if(err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL) {
fprintf(stderr, "Basic setup goes wrong: %s",
mpg123_plain_strerror(err));
cleanup(mh);
return -1;
}
/* Let mpg123 work with the file, that excludes MPG123_NEED_MORE messages. */
int channels = 0, encoding = 0;
long rate = 0;
if(mpg123_open(mh, argv[1]) != MPG123_OK
/* Peek into track and get first output format. */
|| mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK )
{
fprintf( stderr, "Trouble with mpg123: %s\n", mpg123_strerror(mh) );
cleanup(mh);
return -1;
}
if(encoding != MPG123_ENC_SIGNED_16 && encoding != MPG123_ENC_SIGNED_32)
{ /* Signed 16 is the default output format anyways; it would actually by only different if we forced it.
So this check is here just for this explanation. */
cleanup(mh);
fprintf(stderr, "Bad encoding: 0x%x!\n", encoding);
return -2;
}
/* Ensure that this output format will not change (it could, when we allow it). */
mpg123_format_none(mh);
mpg123_format(mh, rate, channels, encoding);
output = fopen_gst(rate, channels, encoding == MPG123_ENC_SIGNED_16 ? 16 : 32);
if (!output) {
fprintf( stderr, "Could not open Gstreamer output file!\n" );
cleanup(mh);
return -3;
}
/* Buffer could be almost any size here, mpg123_outblock() is just some recommendation.
Important, especially for sndfile writing, is that the size is a multiple of sample size. */
int buffer_size = mpg123_outblock(mh);
unsigned char* buffer = malloc( buffer_size );
do {
size_t done = 0;
err = mpg123_read( mh, buffer, buffer_size, &done );
fwrite(buffer, done, 1, output);
} while (err == MPG123_OK);
if(err != MPG123_DONE) {
fprintf( stderr, "Warning: Decoding ended prematurely because: %s\n",
err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err) );
}
cleanup(mh);
return 0;
}