简体   繁体   English

C#和音频生成/播放

[英]C# and Audio Generation/Playback

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). 我需要的东西可以1)播放我计算的波形和2)同时播放多个声音(比如按住其他键的同时按住钢琴上的一个键)。 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: 据我所知,我有5个可能的选择:

  1. DirectSound. 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. 但它不适用于.NET 4.5。
  2. System.Media.SoundPlayer. 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). 它适用于.NET 4.5并且具有比Direct Sound更高质量的音频,但它必须从.wav文件播放声音,并且不能一次播放多个声音(也不能播放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. 我通过将我的波形转换为内存中的.wav格式,然后向SoundPlayer发送内存中.wav文件的MemoryStream来''使SoundPlayer工作。 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 . 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; 我还没有尝试过这个,但是从MSDN文档中我看不到一种方法可以在内存中发送波形而不先将其保存到磁盘然后再读取它; I don't think I can send it a stream. 我认为我不能发送它。
  4. System.Windows.Controls.MediaPlayer . 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 , without a doubt. http://naudio.codeplex.com ,毫无疑问。 Mark is a regular here on SO, the product is well alive, there are good code examples. 马克是SO的常客,产品很好,有很好的代码示例。

It works. 有用。 We built some great stuff with it. 我们用它制作了一些很棒的东西。

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

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