LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-17-2017, 12:26 PM   #1
Atton_Risk
LQ Newbie
 
Registered: May 2017
Posts: 3

Rep: Reputation: Disabled
MPG123 PCM Noise


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;
}
 
Old 05-18-2017, 01:38 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Try this:
Code:
		// 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 */
 
1 members found this post helpful.
Old 05-19-2017, 02:04 AM   #3
Atton_Risk
LQ Newbie
 
Registered: May 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Works like a beast norobro thank you!
 
Old 05-19-2017, 05:56 PM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
You're welcome.

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.
 
1 members found this post helpful.
Old 05-20-2017, 01:28 AM   #5
Atton_Risk
LQ Newbie
 
Registered: May 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Darn programing is a bitch
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to mix S/PDIF input and PCM Out (to hear PCM and S/PDIF simultaneously)? *Dark Dragon* Linux - General 11 07-20-2012 07:53 AM
(Sound) what is PCM? what's the difference between PCM volume and master volume? snr8fl3 Linux - General 2 01-27-2009 06:12 AM
mpg123 pilatus666 Mandriva 10 12-23-2006 05:16 AM
Run mpg123: system("mpg123"); How to know what PID it gets? And a Emacs highlightin q kornerr Programming 10 08-06-2005 10:17 AM
mpg123 doralsoral Linux - Software 9 09-07-2003 09:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration