线程困境,至少我认为是
我正在尝试输出音频样本,并使用 http://www 中的 cswavplay 来输出音频样本.codeproject.com/KB/audio-video/cswavplay.aspx 似乎又使用了 winmm.dll 中的 DllImports
。
我确实让它使用 8 位样本进行播放,但是当我尝试为其提供 16 位样本时,它却严重失败。我尽可能地深入研究了代码,我的理解是这样的:
每次 cswavplay 播放完最后一个缓冲区时,我都会得到一个指向缓冲区的指针来填充。它适用于一次迭代,播放一个缓冲区,有时...... 我遇到了各种有趣的异常,例如,当我尝试使用 44100 的缓冲区大小来更清楚地听到播放量时,就出现了 AccessViolationException。但是,当我在 WaveOut 类(cswavplay 的一部分)内的各个位置放置断点时,它使用的所有对象(例如缓冲区和 AutoResetEvent 实例)似乎在第二次迭代中仍然存在。我最好的猜测是这些问题与线程或 GC 有关。这些例外似乎相当随机,而且我还缺乏经验,无法完全理解发生了什么。
我要求提供以下任一信息:
1) 对可能出现问题的粗略猜测
2) 对可能出现问题的有根据的猜测
3) 指向的指针使用 C# 实时输出声音的另一种方法
我并不要求对我没有编写的软件进行彻底的错误跟踪,所以不要介意 cswavplay...
在一天结束时,我可能做错了什么在这里,但是很难知道我何时没有收到相关异常(沿着 BufferAllocationException 或其他内容)...
编辑:
感谢所有关于其他声音 API 的建议。它们似乎都假定为 .wav 文件。抱歉,我没有说清楚,我不是在播放 .wav 文件,而是实时合成样本。
I am trying to output audio samples, and do so with cswavplay from http://www.codeproject.com/KB/audio-video/cswavplay.aspx which in turn seem to use DllImports
from winmm.dll.
I did get it to play using 8-bit samples, however it fails miserably when I try to feed it 16-bit samples. I dug through the code as best as I can and I understand it as this:
I get a pointer to a buffer to fill each time cswavplay finished playing the last buffer. It works for one iteration, it plays back one buffer, sometimes...
I get all sorts of funny exceptions, AccessViolationException just now for instance when I tried to use a buffer size of 44100 to hear more clearly how much gets played. But when I put breakpoints in various places inside the WaveOut class (part of cswavplay) it seems none of the objects it uses, like the buffers and an instance of AutoResetEvent, are still alive the second iteration. My best guess is that these problems are related to threading or GC. The exceptions seem pretty random, and I am far too inexperienced to comprehend fully what's going on.
I'm asking for either of the following:
1) Wild guesses as to what could be the problem
2) Educated guesses as to what could be the problem
3) Pointers to an alternative way of outputting sound in realtime using C#
I'm not asking for a thorough bug tracking of software i didn't write, so don't mind cswavplay...
At the end of the day, i might be doing something wrong here, but it's hard to know when i don't get a relevant exception (along the lines of BufferAllocationException
or something)...
EDIT:
Thanks for all the suggestions about other sound API's. They all seem to assume a .wav file. I'm sorry for not being clear, i'm not playing .wav files, i synthesize samples in realtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DirectSound 以及对于 .NET我想到了 XNA 框架。有许多非常高质量的示例 如何使用 .NET 同时播放声音和动画图形。
DirectSound and for .NET the XNA framework comes to my mind. There are many very high quality samples out there how to play sound and animate graphics at the same time with .NET.