| | |
Writing PCM as raw data in C/C++ in Windows
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2005
Posts: 14
Reputation:
Solved Threads: 0
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:
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:
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!
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:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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!
•
•
Join Date: Jul 2005
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
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?
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?
•
•
Join Date: Apr 2008
Posts: 1
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Writing mp3 files to data files (OS X)
- send raw data via serial (Python)
- scandisk on raw drive (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






