简体   繁体   中英

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). 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. 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 , 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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