I'm facing a problem for playing a wav file which is first read into BYTE array and modifying it to decrease its volume level. The snippet I made to achieve it is taken from various web sources.
However it works well when I run the program through Visual Studio 2013 Community.
But when coming to the executable generated in Release x64 mode, it gives a runtime error and I can't hear anything (that also can't be caught using try-catch).
I want my executable to work but it works only when I comment out the line --> p[i] = (__int8)((float)p[i] * fVolume); <-- , which is actually doing the work to manipulate the volume level.
I am unable to understand why the exe is not working and Visual Studio can easily run it in Release x64 mode.
WAV file being manipulated : https://drive.google.com/file/d/1i3DACTJxRCQQqRKBK_XL4msuw5p__kXg/view?usp=sharing Function call I'm using: PlaySound_Volume("right_10.wav", 0.235);
Thanks for your attention !
void PlaySound_Volume(string fname, float fVolume = 1, bool async = false){
DWORD dwFileSize;
BYTE* pFileBytes;
ifstream f(fname, ios::binary);
f.seekg(0, ios::end);
int lim = f.tellg();
dwFileSize = lim;
pFileBytes = new BYTE[lim];
f.seekg(0, ios::beg);
f.read((char *)pFileBytes, lim);
f.close();
BYTE* pDataOffset = (pFileBytes + 40);
__int8 * p = (__int8 *)(pDataOffset + 8);
for (int i = 80 / sizeof(*p); i < dwFileSize / sizeof(*p); i++){
// COMMENT FOLLOWING LINE
p[i] = (__int8)((float)p[i] * fVolume);
}
if (async)
PlaySound((LPCSTR)pFileBytes, NULL, SND_MEMORY | SND_ASYNC);
else
PlaySound((LPCSTR)pFileBytes, NULL, SND_MEMORY);
}
Since p
is pFileBytes + 48
, you're not allowed to dereference p
at dwFileSize - 48
or beyond.
Or, put another way,pFileBytes
points to the first of dwFileSize
bytes, but p
points to the first of dwFileSize - 48
bytes.
Adjust your loop boundary.