1
0
mirror of http://mpg123.de/trunk/.git synced 2025-07-03 11:22:31 +03:00
Files
mpg123/doc/examples/dumb_decoder.c
thor c4b7f976ea examples: update for mpg123_init() and sensible copyright
git-svn-id: svn://scm.orgis.org/mpg123/trunk@4831 35dc7657-300d-0410-a2e5-dc2837fedb53
2021-04-12 06:01:39 +00:00

64 lines
1.4 KiB
C

/*
A simple and dumb decoder that does not even query the mpg123 output format.
It shall get MPG123_NEW_FORMAT once, but after that data!
This is example code only sensible to be considered in the public domain.
Beware: It writes raw audio to standard out!
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <mpg123.h>
void decode_track(mpg123_handle *mh);
int main(int argc, char **argv)
{
int i;
int err;
mpg123_handle* mh;
#if MPG123_API_VERSION < 46
// Newer versions of the library don't need that anymore, but it is safe
// to have the no-op call present for compatibility with old versions.
mpg123_init();
#endif
mh = mpg123_new(NULL, NULL);
for(i=1; i<argc; ++i)
{
fprintf(stderr, "Working on %s\n", argv[i]);
err = mpg123_open(mh, argv[i]);
if(err == MPG123_OK)
{
decode_track(mh);
mpg123_close(mh);
}
else fprintf(stderr, "Failed to open file: %s\n", mpg123_strerror(mh));
}
}
void decode_track(mpg123_handle *mh)
{
unsigned char buf[16*1024];
size_t fill;
off_t sum = 0;
int ret;
while( (ret = mpg123_read(mh, buf, sizeof(buf), &fill)) != MPG123_DONE)
{
if(fill == MPG123_ERR)
{
fprintf(stderr, "error decoding: %s\n", mpg123_strerror(mh));
return;
}
fprintf(stderr, "read returned: %i\n", ret);
if(fill > 0)
{
sum += fill;
write(STDOUT_FILENO, buf, fill);
}
}
fprintf(stderr, "decoded bytes: %lli\n", (long long)sum);
}