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:
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.
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.
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.
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.
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.
Related
I've tried using Console.Beep() at low millisecond rates two play two frequencies 'at once', but the pause between beeps ruins it. I have tried researching it but I've found nothing, and don't know where to start, aside from DirectSound, which I'm looking in to. All I need is to make a program that plays two or more frequencies simultaneously out of one speaker, in C#.
Thanks.
I suggest you look at DirectSound, which has nice .NET bindings. You can use two (or more) Buffer objects and invoke their Play methods to play them simultaneously.
This tutorial shows how to implement a simple drum machine in C# by synthesising sounds on the fly. Hope it helps.
Try looking at using MIDI. This example should get you started. It uses a MIDI library that several people recommend highly.
Console.Beep() is synchronous and does not return until the sound is finished.
Try Simple DirectMedia Layer (SDL.net).
Here's some very simple sample code that will let you play a WAV or MIDI file. This way you can write your code once then just maintain the wav or mid file if you want to make changes to the sound effect.
Play Any Sound File - C# ( Uses DirectX 9.0 for Managed Code )
If you know the frequencies beforehand you can synthesize them into a wave file (audacity or another similar program) and then play the wave file with the SoundPlayer class.
string path = /*path goes here*/;
player = new SoundPlayer(path);
player.Play();
If you need to synthesize them at runtime you would have to write the file by hand. Here's a SO question that you can reference to build the wave files.
I am looking to create an application that will allow me to record from my mic and playback the recording through other pc's. At this point however I would just like it to play back on my own computer so I can get it working.
I have been looking at NAudio for the past few hours and it seems like it may be able to help me achieve this goal.
I am just wondering if anyone else has had any experience with this and if it is at all possible?
Thanks,
Stuart
There is an example project on codeproject doing this:
http://www.codeproject.com/KB/cs/Streaming_wave_audio.aspx
I don't know how low the latency is.
As a codec I'd recommend Speex(at least for speech). It's free, open source and offers low latency and low bandwidth.
Bass Audio Library is another solid option worth looking into.
It is possible to do, but you are unlikely to get low latency with WaveIn/WaveOut (possibly better results with WASAPI). You could use the BufferedWaveProvider (in the latest source code) to store up the audio being recorded from the microphone and supplying the output to soundcard.
NAudio is great as a starting point for audio capture and playback but as Mark pointed out the latency might be an issue.
If you take the next step and want to sent the audio data across the network you will need a codec to compress the data as PCM or WAV are uncompressed and for voice you only need a small part of the bandwidth needed for WAV.
As you are working with C# there is a C# port of Speex available, called NSpeex, which might be worth having a look at..
I'm working on an application that will read in file paths and play audio files. I'm trying to keep this as simple as possible--by using existing codecs and free/open utilities. I'd like some suggestions on the best way to do this. I've had two ideas, both involving FFmpeg:
Create a simple GUI that allows the user to read pass in file(s) to be played, and then a ffplay.exe process is run in the background to play the file(s).
Go more in-depth by just using libavcodec and basing my project off the functionality available with that.
There are only a few main goals I have for this.
Be able to read in and play multiple files without breaks between them
Start playback at an arbitrary spot (based on a percent of total duration) within the track
Stop playback after an arbitrary amount of time, and move to the next track
Which of my two methods seem the most practical for this project? Is there a better--or perhaps less feature-intensive--alternative to FFmpeg that you would suggest.
This is for a Windows application written in C#.
Edit: One of the reasons that I started with FFmpeg is that it can handle many file types, notably MP3, AAC, Flac.
Edit2: If the use of libavcode.dll is the best option, it would also be helpful to get some info on implementing that in C#.
The BASS audio library has C# bindings and works very well with common audio formats (e.g. MP3) with plug-ins for other formats (e.g. AAC).
However, for commercial development, you require a license to use BASS.
I'm looking to develop a Silverlight application which will take a stream of data (not an audio stream as such) from a web server.
The data stream would then be manipulated to give audio of a certain format (G.711 a-Law for example) which would then be converted into PCM so that additional effects can be applied (such as boosting the volume).
I'm OK up to this point. I've got my data, converted the G.711 into PCM but my problem is being able to output this PCM audio to the sound card.
I basing a solution on some C# code intended for a .Net application but in Silverlight there is a problem with trying to take a copy of a delegate (function pointer) which will be the topic of a separate question once I've produced a simple code sample.
So, the question is... How can I output the PCM audio that I have held in a data structure (currently an array) in my Silverlight to the user? (Please don't say write the byte values to a text box)
If it were a MP3 or WMA file I would play it using a MediaElement but I don't want to have to make it into a file as this would put a crimp on applying dynamic effects to the audio.
I've seen a few posts from people saying low level audio support is poor/non-existant in Silverlight so I'm open to any suggestions/ideas people may have.
The simple answer is that there is no support for PCM playback from Silverlight in version 2. So unless you want to write a fully managed PCM to MP3 converter you are stuck. Even then I'm not sure you could get the MediaElement to play from isolated storage.
Is there any chance you could use a web service to perform the conversion?
See also this question:
Where's the sound API in Silverlight? Or, how do I write a music app to run in the browser?
Update: Silverlight 3 supports your custom audio sources. However, it won't let you intercept samples to perform effects on WMA or MP3, presumably for DRM reasons, so you would still potentially need to write your own decoder.
Short answer is use a MediaElement + a MediaStreamSource
Check out these:
http://blogs.msdn.com/gillesk/archive/2009/03/23/playing-back-wave-files-in-silverlight.aspx
http://code.msdn.microsoft.com/wavmss/Release/ProjectReleases.aspx?ReleaseId=2417
Basically, write a decoder in managed code to convert G.711 a-Law to PCM, then do whatever modifications you want to the raw values, then pass those into a MediaStreamSource.
Looks like Silverlight 3 supports direct PCM output now, or will when released. I don't see anything in the docs about the raw AV pipeline yet.
Mark Heath's answer is correct - only certain formats are supported - mp3 and certain flavours of WMA (unfortunately not WMA lossless which would be 'closer' to PCM).
To play PCM data in Silverlight, you could do the following:
* Convert the PCM into mp3 data, and store it in memory.
* Play the mp3 data using the technique presented at ManagedMediaHelpers. The idea here involves a class called Mp3MediaStreamSource (derived from System.Windows.Media.MediaStreamSource) that provides mp3 chunks to a MediaElement to play. The chunks will need to be in a stream, but of course a memory stream will do.
I initially thought you might be able to provide PCM chunks via MediaStreamSource, but this does not work. It's a real shame as it would solve your problem (and the one I was facing - making a Speex audio file player) really easily!
I have a 10 second sound effect wave file. What I would like to do is take that file and repeat it n number of times and then save the longer WAV file to disk. This way I can create a much longer background effect rather than auto-repeat on the media player which is a bit stuttered between repeats. I am trying to do this in C#.
That's reasonably easy to do, given the WAV file format - assuming it's uncompressed audio (most WAV files are - or at least, they were last time I had anything to do with them).
There may well be audio APIs you can use to do this without getting stuck into the binary format, but I suspect they'd take as long to learn as just doing it yourself as you're not doing anything particularly complicated.
If you only need to do this with a small number of files, you might as well do it by hand with Audacity.
If you're using .Net 2.0 or higher then you can use the System.Media.SoundPlayer class and specifically its PlayLooping method to achieve what you want with no stuttering. This is preferable to creating a new wav file - it means less disk space for the file and less memory needed to render the sound. In general you should always use buffered techniques like this for audio playback if the sound will be looped or is longer than a few seconds.
You can do this easily in C# using the NAudio .NET audio library. You would need to use the WaveFileReader and WaveFileWriter classes. You can also use it to create a custom WaveStream that will loop your audio as many times as you want without the need to create a long WAV file.