mirror of
http://mpg123.de/trunk/.git
synced 2025-07-30 02:01:12 +03:00
Update WAV-writing examples to use libout123.
git-svn-id: svn://scm.orgis.org/mpg123/trunk@3810 35dc7657-300d-0410-a2e5-dc2837fedb53
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
/** \defgroup mpg123_examples example programs using libmpg123
|
||||
/** \defgroup mpg123_examples example programs using libmpg123 and libout123
|
||||
@{ */
|
||||
|
||||
/** \file mpg123_to_wav.c A simple MPEG audio to WAV converter using libmpg123 (read) and libsndfile (write).
|
||||
/** \file mpg123_to_wav.c A simple MPEG audio to WAV converter using libmpg123 (read) and libout123 (write).
|
||||
...an excersize on two simple APIs. */
|
||||
|
||||
/** \file mpglib.c Example program mimicking the old mpglib test program.
|
||||
|
@ -9,22 +9,24 @@ all: $(targets)
|
||||
ifeq ($(MPG123_PREFIX),)
|
||||
MPG123_CFLAGS := $(shell pkg-config --cflags libmpg123)
|
||||
MPG123_LDFLAGS := $(shell pkg-config --libs libmpg123)
|
||||
OUT123_CFLAGS := $(shell pkg-config --cflags libout123)
|
||||
OUT123_LDFLAGS := $(shell pkg-config --libs libout123)
|
||||
else # Yeah, that's with GNU/Linux in mind, at least GNU ld ...
|
||||
MPG123_CFLAGS := -I$(MPG123_PREFIX)/include
|
||||
MPG123_LDFLAGS := -L$(MPG123_PREFIX)/lib -Wl,-rpath $(MPG123_PREFIX)/lib -lmpg123
|
||||
OUT123_CFLAGS := -I$(MPG123_PREFIX)/include
|
||||
OUT123_LDFLAGS := -L$(MPG123_PREFIX)/lib -Wl,-rpath $(MPG123_PREFIX)/lib -lout123
|
||||
endif
|
||||
SND_CFLAGS := $(shell pkg-config --cflags sndfile)
|
||||
SND_LDFLAGS := $(shell pkg-config --libs sndfile)
|
||||
|
||||
# Oder of libs not that important here...
|
||||
compile = $(CC) $(CPPFLAGS) $(CFLAGS) $(MPG123_CFLAGS)
|
||||
linkflags = $(MPG123_LDFLAGS) $(LDFLAGS)
|
||||
|
||||
mpg123_to_wav: mpg123_to_wav.c
|
||||
$(compile) -o mpg123_to_wav mpg123_to_wav.c $(SND_CFLAGS) $(SND_LDFLAGS) $(linkflags)
|
||||
$(compile) -o mpg123_to_wav mpg123_to_wav.c $(OUT123_CFLAGS) $(OUT123_LDFLAGS) $(linkflags)
|
||||
|
||||
mpg123_to_wav_replaced_io: mpg123_to_wav_replaced_io.c
|
||||
$(compile) -o $@ $< $(SND_CFLAGS) $(SND_LDFLAGS) $(linkflags)
|
||||
$(compile) -o $@ $< $(OUT123_CFLAGS) $(OUT123_LDFLAGS) $(linkflags)
|
||||
|
||||
feedseek: feedseek.c
|
||||
$(compile) -o feedseek feedseek.c $(linkflags)
|
||||
|
@ -1,25 +1,25 @@
|
||||
/*
|
||||
mpg123_to_wav.c
|
||||
|
||||
copyright 2007 by the mpg123 project - free software under the terms of the LGPL 2.1
|
||||
copyright 2007-2015 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 <out123.h>
|
||||
#include <mpg123.h>
|
||||
#include <stdio.h>
|
||||
#include <strings.h>
|
||||
#include <mpg123.h>
|
||||
#include <sndfile.h>
|
||||
|
||||
|
||||
void usage()
|
||||
void usage(const char *cmd)
|
||||
{
|
||||
printf("Usage: mpg123_to_wav <input> <output> [s16|f32 [ <buffersize>]]\n");
|
||||
printf("Usage: %s <input> <output> [<encname> [ <buffersize>]]\n", cmd);
|
||||
exit(99);
|
||||
}
|
||||
|
||||
void cleanup(mpg123_handle *mh)
|
||||
void cleanup(mpg123_handle *mh, out123_handle *ao)
|
||||
{
|
||||
out123_del(ao);
|
||||
/* It's really to late for error checks here;-) */
|
||||
mpg123_close(mh);
|
||||
mpg123_delete(mh);
|
||||
@ -28,18 +28,19 @@ void cleanup(mpg123_handle *mh)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
SNDFILE* sndfile = NULL;
|
||||
SF_INFO sfinfo;
|
||||
mpg123_handle *mh = NULL;
|
||||
out123_handle *ao = NULL;
|
||||
unsigned char* buffer = NULL;
|
||||
size_t buffer_size = 0;
|
||||
size_t done = 0;
|
||||
int channels = 0, encoding = 0;
|
||||
int channels = 0;
|
||||
int encoding = 0;
|
||||
int framesize = 1;
|
||||
long rate = 0;
|
||||
int err = MPG123_OK;
|
||||
off_t samples = 0;
|
||||
|
||||
if (argc<3) usage();
|
||||
if (argc<3) usage(argv[0]);
|
||||
printf( "Input file: %s\n", argv[1]);
|
||||
printf( "Output file: %s\n", argv[2]);
|
||||
|
||||
@ -47,12 +48,30 @@ int main(int argc, char *argv[])
|
||||
if(err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Basic setup goes wrong: %s", mpg123_plain_strerror(err));
|
||||
cleanup(mh);
|
||||
cleanup(mh, ao);
|
||||
return -1;
|
||||
}
|
||||
ao = out123_new();
|
||||
if(!ao)
|
||||
{
|
||||
fprintf(stderr, "Cannot create output handle.\n");
|
||||
cleanup(mh, ao);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Simple hack to enable floating point output. */
|
||||
if(argc >= 4 && !strcmp(argv[3], "f32")) mpg123_param(mh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.);
|
||||
if(argc >= 4)
|
||||
{ /* Make mpg123 support the desired encoding only for all rates. */
|
||||
const long *rates;
|
||||
size_t rate_count;
|
||||
size_t i;
|
||||
int enc;
|
||||
/* If that is zero, you'll get the error soon enough from mpg123. */
|
||||
enc = out123_enc_byname(argv[3]);
|
||||
mpg123_format_none(mh);
|
||||
mpg123_rates(&rates, &rate_count);
|
||||
for(i=0; i<rate_count; ++i)
|
||||
mpg123_format(mh, rates[i], MPG123_MONO|MPG123_STEREO, enc);
|
||||
}
|
||||
|
||||
/* Let mpg123 work with the file, that excludes MPG123_NEED_MORE messages. */
|
||||
if( mpg123_open(mh, argv[1]) != MPG123_OK
|
||||
@ -60,29 +79,31 @@ int main(int argc, char *argv[])
|
||||
|| mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK )
|
||||
{
|
||||
fprintf( stderr, "Trouble with mpg123: %s\n", mpg123_strerror(mh) );
|
||||
cleanup(mh);
|
||||
cleanup(mh, ao);
|
||||
return -1;
|
||||
}
|
||||
if(out123_open(ao, "wav", argv[2]) != OUT123_OK)
|
||||
{
|
||||
fprintf(stderr, "Trouble with out123: %s\n", out123_strerror(ao));
|
||||
cleanup(mh, ao);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(encoding != MPG123_ENC_SIGNED_16 && encoding != MPG123_ENC_FLOAT_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). */
|
||||
/* Ensure that this output format will not change
|
||||
(it might, when we allow it). */
|
||||
mpg123_format_none(mh);
|
||||
mpg123_format(mh, rate, channels, encoding);
|
||||
|
||||
bzero(&sfinfo, sizeof(sfinfo) );
|
||||
sfinfo.samplerate = rate;
|
||||
sfinfo.channels = channels;
|
||||
sfinfo.format = SF_FORMAT_WAV|(encoding == MPG123_ENC_SIGNED_16 ? SF_FORMAT_PCM_16 : SF_FORMAT_FLOAT);
|
||||
printf("Creating WAV with %i channels and %liHz.\n", channels, rate);
|
||||
|
||||
sndfile = sf_open(argv[2], SFM_WRITE, &sfinfo);
|
||||
if(sndfile == NULL){ fprintf(stderr, "Cannot open output file!\n"); cleanup(mh); return -2; }
|
||||
printf("Creating WAV with %i channels and %liHz, encoding %s.\n"
|
||||
, channels, rate, out123_enc_name(encoding));
|
||||
if( out123_start(ao, rate, channels, encoding)
|
||||
|| out123_getformat(ao, NULL, NULL, NULL, &framesize) )
|
||||
{
|
||||
fprintf(stderr, "Cannot start output / get framesize: %s\n"
|
||||
, out123_strerror(ao));
|
||||
cleanup(mh, ao);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
@ -91,28 +112,27 @@ int main(int argc, char *argv[])
|
||||
|
||||
do
|
||||
{
|
||||
sf_count_t more_samples;
|
||||
size_t played;
|
||||
err = mpg123_read( mh, buffer, buffer_size, &done );
|
||||
more_samples = encoding == MPG123_ENC_SIGNED_16
|
||||
? sf_write_short(sndfile, (short*)buffer, done/sizeof(short))
|
||||
: sf_write_float(sndfile, (float*)buffer, done/sizeof(float));
|
||||
if(more_samples < 0 || more_samples*mpg123_encsize(encoding) != done)
|
||||
played = out123_play(ao, buffer, done);
|
||||
if(played != done)
|
||||
{
|
||||
fprintf(stderr, "Warning: Written number of samples does not match the byte count we got from libmpg123: %li != %li\n", (long)(more_samples*mpg123_encsize(encoding)), (long)done);
|
||||
fprintf(stderr
|
||||
, "Warning: written less than gotten from libmpg123: %li != %li\n"
|
||||
, (long)played, (long)done);
|
||||
}
|
||||
samples += more_samples;
|
||||
samples += played/framesize;
|
||||
/* We are not in feeder mode, so MPG123_OK, MPG123_ERR and MPG123_NEW_FORMAT are the only possibilities.
|
||||
We do not handle a new format, MPG123_DONE is the end... so abort on anything not MPG123_OK. */
|
||||
} while (err==MPG123_OK);
|
||||
} while (done && err==MPG123_OK);
|
||||
|
||||
free(buffer);
|
||||
|
||||
if(err != MPG123_DONE)
|
||||
fprintf( stderr, "Warning: Decoding ended prematurely because: %s\n",
|
||||
err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err) );
|
||||
|
||||
sf_close( sndfile );
|
||||
|
||||
samples /= channels;
|
||||
printf("%li samples written.\n", (long)samples);
|
||||
cleanup(mh);
|
||||
cleanup(mh, ao);
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
/*
|
||||
mpg123_to_wav.c
|
||||
mpg123_to_wav_replaced_io.c
|
||||
|
||||
copyright 2007-2010 by the mpg123 project - free software under the terms of the LGPL 2.1
|
||||
copyright 2007-2015 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 (moved to handle I/O by Thomas Orgis)
|
||||
|
||||
@ -25,20 +25,21 @@
|
||||
#include <stdio.h>
|
||||
#include <strings.h>
|
||||
#include <mpg123.h>
|
||||
#include <sndfile.h>
|
||||
#include <out123.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
void usage()
|
||||
void usage(const char *cmd)
|
||||
{
|
||||
printf("Usage: mpg123_to_wav <input> <output>\n");
|
||||
printf("Usage: %s <input> <output>\n", cmd);
|
||||
exit(99);
|
||||
}
|
||||
|
||||
void cleanup(mpg123_handle *mh)
|
||||
void cleanup(mpg123_handle *mh, out123_handle *ao)
|
||||
{
|
||||
out123_del(ao);
|
||||
/* It's really to late for error checks here;-) */
|
||||
mpg123_close(mh);
|
||||
mpg123_delete(mh);
|
||||
@ -83,9 +84,8 @@ void cleanup_cb(void *handle)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
SNDFILE* sndfile = NULL;
|
||||
SF_INFO sfinfo;
|
||||
mpg123_handle *mh = NULL;
|
||||
out123_handle *ao = NULL;
|
||||
unsigned char* buffer = NULL;
|
||||
size_t buffer_size = 0;
|
||||
size_t done = 0;
|
||||
@ -95,7 +95,7 @@ int main(int argc, char *argv[])
|
||||
off_t samples = 0;
|
||||
struct ioh *iohandle;
|
||||
|
||||
if (argc!=3) usage();
|
||||
if (argc!=3) usage(argv[0]);
|
||||
printf( "Input file: %s\n", argv[1]);
|
||||
printf( "Output file: %s\n", argv[2]);
|
||||
|
||||
@ -119,14 +119,14 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
fprintf( stderr, "Trouble with mpg123: %s\n",
|
||||
mh==NULL ? mpg123_plain_strerror(err) : mpg123_strerror(mh) );
|
||||
cleanup(mh);
|
||||
cleanup(mh, ao);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(encoding != MPG123_ENC_SIGNED_16)
|
||||
{ /* 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);
|
||||
cleanup(mh, ao);
|
||||
fprintf(stderr, "Bad encoding: 0x%x!\n", encoding);
|
||||
return -2;
|
||||
}
|
||||
@ -134,38 +134,42 @@ int main(int argc, char *argv[])
|
||||
mpg123_format_none(mh);
|
||||
mpg123_format(mh, rate, channels, encoding);
|
||||
|
||||
printf("Creating 16bit WAV with %i channels and %liHz.\n", channels, rate);
|
||||
if(
|
||||
!(ao = out123_new())
|
||||
|| out123_open(ao, "wav", argv[2])
|
||||
|| out123_start(ao, rate, channels, encoding)
|
||||
)
|
||||
{
|
||||
fprintf(stderr, "Cannot create / start output: %s\n"
|
||||
, out123_strerror(ao));
|
||||
cleanup(mh, ao);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
buffer_size = mpg123_outblock( mh );
|
||||
buffer = malloc( buffer_size );
|
||||
|
||||
bzero(&sfinfo, sizeof(sfinfo) );
|
||||
sfinfo.samplerate = rate;
|
||||
sfinfo.channels = channels;
|
||||
sfinfo.format = SF_FORMAT_WAV|SF_FORMAT_PCM_16;
|
||||
printf("Creating 16bit WAV with %i channels and %liHz.\n", channels, rate);
|
||||
|
||||
sndfile = sf_open(argv[2], SFM_WRITE, &sfinfo);
|
||||
if(sndfile == NULL){ fprintf(stderr, "Cannot open output file!\n"); cleanup(mh); return -2; }
|
||||
|
||||
do
|
||||
{
|
||||
err = mpg123_read( mh, buffer, buffer_size, &done );
|
||||
sf_write_short( sndfile, (short*)buffer, done/sizeof(short) );
|
||||
out123_play(ao, buffer, done);
|
||||
samples += done/sizeof(short);
|
||||
/* We are not in feeder mode, so MPG123_OK, MPG123_ERR and MPG123_NEW_FORMAT are the only possibilities.
|
||||
We do not handle a new format, MPG123_DONE is the end... so abort on anything not MPG123_OK. */
|
||||
} while (err==MPG123_OK);
|
||||
} while (done && err==MPG123_OK);
|
||||
|
||||
free(buffer);
|
||||
|
||||
if(err != MPG123_DONE)
|
||||
fprintf( stderr, "Warning: Decoding ended prematurely because: %s\n",
|
||||
err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err) );
|
||||
|
||||
sf_close( sndfile );
|
||||
|
||||
samples /= channels;
|
||||
printf("%li samples written.\n", (long)samples);
|
||||
cleanup(mh);
|
||||
cleanup(mh, ao);
|
||||
free(iohandle);
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user