User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,753 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,715 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2741 | Replies: 6 | Solved
Reply
Join Date: Jul 2005
Posts: 14
Reputation: fluidDelusions is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
fluidDelusions fluidDelusions is offline Offline
Newbie Poster

Writing PCM as raw data in C/C++ in Windows

  #1  
Jan 15th, 2006
Hello!
I'm having a very difficult time with this problem. What I'm trying to do is generate a sine wave for a specific frequency and output it as raw pcm data to a file.
The sine wave is generated as follows:

 samples[i]=static_cast<int>(32767 * amplitude * sin(static_cast<double>(i)*scale) ); 

where scale = 2*PI/sample_rate.

And to write a specific sample in raw format, I'm first converting it to an array of char in little endian byte order, like this:

void writeInt(std::ofstream& out, int integer, int num_bytes)
{
	int y = (integer);
	for(int i=0; i<num_bytes; i++)
	{
		int x = y&255;
		out.put(x);
		y = y>>8;
	}
}

Now, this is fine in Linux (using gcc). I am able to play a smooth frequency. The problem is when I try to run the same code under Windows (compiled with mingw or bcc32), I get a very distorted sounding wave. Clearly, the data is not being written or converted properly. So to get right down to it, what is causing this difference between Linux and Windows? And, if at all possible, how do I do this the right way in Windows?
Thanks for your consideration!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,640
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 418
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Writing PCM as raw data in C/C++ in Windows

  #2  
Jan 15th, 2006
Well did you open the file for output in binary mode?
Did you check the sizes of the file on the two different systems was the same?
Have you copied a file generated on your Linux box to the windows box, and tried to play that?
Reply With Quote  
Join Date: Jul 2005
Posts: 14
Reputation: fluidDelusions is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
fluidDelusions fluidDelusions is offline Offline
Newbie Poster

Re: Writing PCM as raw data in C/C++ in Windows

  #3  
Jan 15th, 2006
Originally Posted by Salem
Well did you open the file for output in binary mode?
Did you check the sizes of the file on the two different systems was the same?
Have you copied a file generated on your Linux box to the windows box, and tried to play that?

Yes, yes, and yes. I can make a wave file out of the linux and the windows pcm data, and the linux sounds correct on both linux and windows. I can make a wave in windows and it sounds the same in windows and linux (scratchy and bad). I can make the raw data in windows, and use sox to convert that to a wave in linux, and it sounds the same on linux as it does windows.
The difference is happening at the conversion from integer to bytes, I'm almost sure of that. Another thing: in linux, you can just call ostream.write((char*)(&samples[i]),2) and this will work fine. Again, it doesn't work in windows. Why?
Reply With Quote  
Join Date: Jul 2005
Posts: 14
Reputation: fluidDelusions is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
fluidDelusions fluidDelusions is offline Offline
Newbie Poster

Re: Writing PCM as raw data in C/C++ in Windows

  #4  
Jan 15th, 2006
Uh, heh heh. Problem solved. I wasn't opening the ofstream for binary output. Thanks Salem. I'm so ashamed...
Reply With Quote  
Join Date: Dec 2005
Posts: 3,640
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 418
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Writing PCM as raw data in C/C++ in Windows

  #5  
Jan 15th, 2006
> y = y>>8;
Right shift of negative numbers is implementation defined.
On some systems, the sign bit is propagated
11110000 would become 11111000

and on others zero is inserted.
11110000 would become 01111000
Reply With Quote  
Join Date: Jul 2005
Posts: 14
Reputation: fluidDelusions is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
fluidDelusions fluidDelusions is offline Offline
Newbie Poster

Re: Writing PCM as raw data in C/C++ in Windows

  #6  
Jan 15th, 2006
That might be true, but then I had tried using standard division, and it did the exact same thing...
Reply With Quote  
Join Date: Apr 2008
Posts: 1
Reputation: ims is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ims ims is offline Offline
Newbie Poster

Re: Writing PCM as raw data in C/C++ in Windows

  #7  
Apr 4th, 2008
Hi,
I am facing the same problem as fluidDelusions.
I can make a wave file out of the linux and the windows pcm data, and the linux sounds correct on both linux and windows. I can make a wave in windows and it sounds the same in windows and linux (scratchy and bad).
If a play the recordfile which is PCM data, 16 bit mono, rate 8000 using the API, it plays fine, bu when i add wave header to it, it becomes bad and scratchy. I am using the same code to write wavheader in Linux and there it works perfectly fine. What can be the problem?
I am in urgent need of the reply. Kindly reply As soon as possible.

Thanks
IMS
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:57 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC