I've been trying to use MPG123 to convert mp3 data to pcm. I've seen solution's related to this right here on stack overflow. However there is one major issue I can't seem to get around. No matter what changes I make to the code below the PCM data always produces heavily distorted audio. Why does this specific piece of code below always produce distorted audio when used in audacity. Thank you in advance to anyone who decides to reply.
Haven't really examined your code closely, but I have a funny feeling it has to do with line 31 of your snippet.
Depending on your operating system, this line could produce different results. I know that on windows, by default, unless the file is a binary file (which yours is not), a byte with the value of 10 (newline on the ascii table) will be immediately followed by a byte with a value of 13 (carriage return), because this is what windows expects for non-binary files. In other words, the file writing operation appends a byte with a value of 13 after any instance of a byte whose value happens to be 10. This could result in the corruption of PCM audio data or even file header meta data (samplerate, bitdepth, etc), producing definite distortion. I know, because I've had this exact problem some time ago, a simple oversight.
To fix this, simply change line 31 to: std::ofstream out(outString.c_str(), std::ios::binary);
This flag will signify that the file is a binary file.