C#和音频生成/播放 [英] C# and Audio Generation/Playback

查看:120
本文介绍了C#和音频生成/播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作音频合成器,但在确定用于音频播放的内容时遇到了问题.我正在使用物理学和数学来计算源波形,然后需要将该波形馈送到可以将其作为声音播放的东西.我需要能够1)播放我计算出的波形和2)同时播放多种声音的东西(例如按住一个琴键同时按下其他琴键).我对此进行了相当多的研究,但找不到同时完成这两项工作的功能.据我所知,我有5种可能的选择:

I’m making an audio synthesizer and I’m having issues figuring out what to use for audio playback. I’m using physics and math to calculate the source waveforms and then need to feed that waveform to something which can play it as sound. I need something that can 1) play the waveforms I calculate and 2) play multiple sounds simultaneously (like holding one key down on a piano while pressing other keys). I’ve done a fair bit of research into this and I can’t find something that does both of those things. As far as I know, I have 5 potential options:

  1. DirectSound..它可以将波形(short [])作为参数并将其作为声音播放,并且可以同时播放多种声音.但这不适用于.NET 4.5.
  2. System.Media.SoundPlayer.它与.NET 4.5一起使用,并且具有比Direct Sound更好的音频质量,但是它必须播放.wav文件中的声音,并且不能一次播放多个声音( SoundPlayer的多个实例也不能).我通过将波形转换为内存中的.wav格式来欺骗" SoundPlayer进行工作,然后向SoundPlayer发送内存中.wav文件的MemoryStream. 是否可以通过更改流来实现对播放的控制?我无法将字节追加到流中(我尝试过),但是可以将流设置为任意大小,然后仅重写其中的所有字节每次到达流的末尾时,该流将带有下一个音频数据段.
  3. System.Windows.Controls.MediaElement .我尚未对此进行试验,但是从MSDN文档中,我看不到一种在不先将其保存到磁盘然后读取的情况下将其发送到内存中的方法.我认为我无法将其发送给它.
  4. System.Windows.Controls.MediaPlayer .我也没有尝试过这种方法,但是文档说它可以用作某种动画的伴侣.我可以不用做任何真实的(用户可感知的)动画来达到我想要的效果就可以使用它.
  5. 开源解决方案.我很犹豫使用开放源代码解决方案,因为我发现它们通常没有充分的文档记录,并且维护性很差,但是如果其中有充分的文档记录并且可以满足我的需要,我会持开放态度.
  1. DirectSound. It can take a waveform (a short[]) as a parameter and play it as sound, and can play multiple sounds simultaneously. But it won’t work with .NET 4.5.
  2. System.Media.SoundPlayer. It works with .NET 4.5 and has better quality audio than Direct Sound, but it has to play sound from a .wav file and cannot play multiple sounds at once (nor can multiple instances of SoundPlayer). I ‘trick’ SoundPlayer into working by translating my waveform into .wav format in memory and then send SoundPlayer a MemoryStream of the in-memory .wav file. Could I potentially achieve control over the playback by altering the stream? I cannot append bytes to the stream (I tried) but I could potentially make the stream an arbitrary size and just re-write all the bytes in the stream with the next segment of audio data every time the end of the stream is reached.
  3. System.Windows.Controls.MediaElement. I have not experimented with this yet, but from MSDNs documentation I don’t see a way to send it a waveform in memory without saving it to disk first and then reading it; I don’t think I can send it a stream.
  4. System.Windows.Controls.MediaPlayer. I have not experimented with this either, but the documentation says it’s meant to be used as a companion to some kind of animation. I could potentially use this without doing any real (user-perceivable) animation to achieve my desired effect.
  5. An open source solution. I’m hesitant to use an open source solution as I find they are typically poorly documented and not very maintainable, but I am open to ideas if there is one out there that is well documented and can do what I need.

有人可以为此提供任何指导或如何创建灵活的音频播放吗?

Can anyone offer me any guidance on this or how to create flexible audio playback?

推荐答案

http://naudio.codeplex.com ,毫无疑问. Mark是SO的常客,产品还不错,有很好的代码示例.

http://naudio.codeplex.com , without a doubt. Mark is a regular here on SO, the product is well alive, there are good code examples.

有效.我们用它构建了一些很棒的东西.

It works. We built some great stuff with it.

这篇关于C#和音频生成/播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆