Sound chords in C#? - c#

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.

Related

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:
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.

Using Sound files C# - modifying tempo

Does anyone have any idea how to modify the sound speed and play a sound file faster/slower than the original in C#?
At the moment I am using .wav files, I have searched a lot and found that SoundPlayer and AudioVideoPlayback are the only two options to play sound, but none of them contain any special method to increase or decrease the sound speed with which to play with.
I am building a piano application which lets user modify the tempo, this is the situation I am in at the moment. I hope a few musical programmers can help me! Cheers
A similar question was asked earlier:
C# Audio Library
One of the answers there suggested the following tool which might be helpful to you:
http://naudio.codeplex.com/
What you are looking for is a time-stretch audio library, which changes the playback speed but not the pitch -- with almost no artefacts.
A few excellent timestretch libraries available:
dirac: http://www.dspdimension.com/ (best)
rubberband: http://breakfastquay.com/rubberband/

c# How do I play multiple .wav files, and set the volume of each

I am currently playing around in c#, and I would like to have some music play in the background and to have other little sounds happen when a user hits a button or when other little actions take place. (specifically, I am making a small game, using mogre3d, and I am just to the point in which I need to add a little sound).
1) Is there a way already built in to c# to play multiple wav files at the same time? I have tried using System.Media.SoundPlayer, but that can only play one wav file at a time (and I can not set the volume in which it is played).
2) What is the best/easiest way to play multiple sounds at a time and to be able to set each of the sounds volume?
I am dreadfully new to C#, and I am sure their must be a simple solution that I am just not seeing. It does not need to be wav, it can easily be mp3/et al. but the simpler the solution the better.
Thanks kindly for your time!
Fast
Change overall volume with that method: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/42b46e40-4d4a-48f8-8681-9b0167cfe781 and you can play .wav files simultaneously with separate SoundPlayer instances.
Better and seperate Volumes
I would use Bass.Net. Good documentation and simple.
You can play multiple .wav files simultaneously with the System.Media.SoundPlayer class: for this, just create an instance of soundPlayer for each .wav file you wish to play. But you cannot set the volume independently this way.
If you want more functionalities with audio files, I recommend you check the NAudio library.

Best way to implement audio playback

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.

WAV file auto repeat in C#

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.

Categories