ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've been playing around a little bit with mpg123 and libao, after reading a blog. To keep it short when I play the PCM data during the write cycle it works just fine. However when I take the PCM data and try to import it to audacity everything goes south real quick. Why this is the case is something that I do not know and thats why I am here. The code is below of that helps any.
Code:
#include <mpg123.h>
#include <ao/ao.h>
#include <string>
using namespace std;
int decodeMp3(string thePath)
{
// Back ends formal stuff
mpg123_handle *mh;
unsigned char *buffer = new char unsigned[34];
size_t buffer_size;
size_t done;
int err;
int driver;
ao_device *dev;
ao_sample_format format;
int channels, encoding;
long rate;
ao_initialize();
driver = ao_default_driver_id();
mpg123_init();
mh = mpg123_new(NULL, &err);
buffer_size = mpg123_outblock(mh);
mpg123_open(mh, thePath.c_str());
mpg123_getformat(mh, &rate, &channels, &encoding);
format.bits = mpg123_encsize(encoding) * BITS;
format.rate = rate;
format.channels = channels;
format.byte_format = AO_FMT_NATIVE;
format.matrix = 0;
dev = ao_open_live(driver, &format, NULL);
char *workAround = new char[16];
ofstream out("raw", ios::binary);
while (mpg123_read(mh, buffer, 16, &done) == MPG123_OK)
{
for (auto i = 0; i != 16; i++)
{
workAround[i] = static_cast<char>(buffer[i]);
}
// This is a test that play's the PCM data to ensure that is good.
ao_play(dev, workAround, 16);
out << workAround; // The data cannot be played dispite the test
}
/* clean up */
free(buffer);
ao_close(dev);
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
ao_shutdown();
return 0;
}
// This is a test that play's the PCM data to ensure that is good.
ao_play(dev, workAround, 16);
// out << workAround; // The data cannot be played dispite the test
out.write(workAround, 16);
}
/* clean up */
For the record, a brief explanation of why the first version doesn't work: if there is a null byte '\0' in the array pointed to by workAround, depending on its position in the array, operator<<() will write 0 to 15 bytes to the file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.