简体   繁体   English

WaveOut程序崩溃

[英]WaveOut Program Crashing

I'm working on trying to read in raw .wav data and output the data to the speakers, mostly for self-experimentation and teaching. 我正在尝试读取原始的.wav数据并将数据输出到扬声器,主要用于自我实验和教学。 A secondary reason I want the data myself is so I could parse it to try separating pitches perhaps, or building an equalizer or visualization of some sort. 我自己想要数据的第二个原因是,我可以解析它以尝试分离音高,或者构建某种均衡器或可视化效果。 So far, I have reading in the data from the file working, verifying the header and whatnot, but then I go to WaveOut and it starts messing up. 到目前为止,我已经从正在工作的文件中读取数据,验证了标头和其他内容,但是随后我去了WaveOut,它开始变得混乱。 My code is currently rather spaghetti code, sure, but it's more of an initial test to see if I can or cannot do this. 当然,我的代码目前是意大利面条式代码,但这更多的是初步测试,以了解我是否可以这样做。 As far as I can tell right now, my program is crashing at waveOutWrite(), and I honestly have no idea why, and I can't find any good examples of how to get WaveOut to work. 据我目前所知,我的程序在waveOutWrite()处崩溃,老实说,我不知道为什么,而且找不到如何使WaveOut正常工作的好例子。 Here's the relevant bit: 这是相关的位:

BYTE *sound=(BYTE*)malloc(sizeof(BYTE)*data_size);
readByte(&in,data_size,sound);
cout<<"Loaded. Now playing...\n";
HWAVEOUT waveOut;
WAVEFORMATEX wF={format_tag,channels,sample_rate,byteRate,block_align,bits_per_sample,0};
MMRESULT result;
waveOutOpen(&waveOut,WAVE_MAPPER,&wF,0,0,CALLBACK_NULL);
WAVEHDR hdr;
ZeroMemory(&hdr,sizeof(WAVEHDR));
hdr.dwBufferLength=data_size;
hdr.lpData=(LPSTR)&sound;
waveOutPrepareHeader(waveOut,&hdr,sizeof(WAVEHDR));
waveOutWrite(waveOut,&hdr,sizeof(WAVEHDR));//Crashes here, no crash if commented out. No sound either way.
Sleep(500);
while(waveOutUnprepareHeader(waveOut,&hdr,sizeof(WAVEHDR))==WAVERR_STILLPLAYING)
  Sleep(100);
waveOutClose(waveOut);

I managed to find what was wrong, for everybody else who comes through. 我成功地找到了问题所在,对于遇到的其他所有人。 The data I was pulling was just purely the sound data, minus the header. 我提取的数据纯粹是声音数据,减去标题。 My problem is that it requires the header on top of the sound data, which is another 44 bytes. 我的问题是,它要求在声音数据的顶部加上另一个头,即44个字节。 What I had to do was bump the size of the array by those 44 bytes, slap the header back at the beginning, and change this line: 我要做的是将数组的大小增加44个字节,将标头拍回开头,然后更改此行:

hdr.lpData=(LPSTR)&sound;

to this: 对此:

hdr.lpData=(LPSTR)sound;

Now, thanks to me parsing out the header, I can also correctly choose a bitrate on the fly, calculate the length of the song, and do various other things with it, too. 现在,多亏我解析了标题,我还可以即时正确地选择比特率,计算歌曲的长度以及使用它来做其他各种事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM