使用SND_MEMORY在PlaySound中播放操纵的音量级别 [英] Play manipulated volume levels in PlaySound with SND_MEMORY
问题描述
我在播放wav文件时遇到问题,该文件首先被读入BYTE数组并对其进行修改以降低其音量.我为实现此目的而编写的摘录来自各种Web来源.
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.
但是,当我通过Visual Studio 2013社区运行程序时,它运行良好.
However it works well when I run the program through Visual Studio 2013 Community.
但是,当进入以Release x64模式生成的可执行文件时,它给出了运行时错误,并且我听不到任何声音(也无法使用try-catch捕获).
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).
我希望我的可执行文件能够工作,但仅当我注释掉该行时才起作用-> p [i] =(__int8)((float)p [i] * fVolume);<-实际上是在操纵音量级别.
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.
我无法理解为什么该exe无法正常运行,而Visual Studio可以在Release x64模式下轻松运行它.
I am unable to understand why the exe is not working and Visual Studio can easily run it in Release x64 mode.
正在处理的WAV文件: https://drive.google.com/file/d/1i3DACTJxRCQQqRKBK_XL4msuw5p__kXg/view?usp = sharing 我正在使用的函数调用:PlaySound_Volume("right_10.wav",0.235);
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);
感谢您的关注!
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);
}
推荐答案
由于 p
是 pFileBytes + 48
,因此您不可以取消引用 p
dwFileSize-48
或更高版本.
或者换一种说法, pFileBytes
指向 dwFileSize
字节的第一个字节,但是 p
指向 dwFileSize的第一个字节-48
字节.
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.
调整循环边界.
这篇关于使用SND_MEMORY在PlaySound中播放操纵的音量级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!