0

I'm learning C# and Winforms with SharpDevelop and trying to do a very bare-bones synth. I just want a sine wave to play when the button is pressed and for the sine wave to stop when it's released. Here is the relevant code:

public class SharpSynth : Form
{   
    Timer myTimer = new Timer();

    public SharpSynth()
    {
        myTimer.Interval = 500;
        myTimer.Start();

        Button SineButton = new Button();
        SineButton.MouseDown += new EventHandler(ButtonDown);
        SineButton.MouseUp   += new EventHandler(ButtonUp);
        Controls.Add(SineButton);
    }

    private void ButtonDown(object sender, System.EventArgs e) 
    {
        myTimer.Tick += new EventHandler(WriteSine);
    }

    private void ButtonUp(object sender, System.EventArgs e) 
    {
        myTimer.Tick -= EventHandler(WriteSine);
    }

    private void WriteSine()
    {}

}

I've seen wave files written to a stream with MemoryStream and SoundPlayer, but it seems like these need wave headers, one of which is the number of samples and another is the file size. I need to be able to write raw audio of arbitrary length. Or should each chunk for each tick have its own wave headers? I'm really just wondering what to put in the WriteSine function to make this work. I've seen NAudio recommended, but I'd like to learn the pure .NET libraries first.

Edit: I have some relevant questions and research here

Creating sine or square wave in C#

Best way to use the System.Media.Soundplayer class

I'm just not sure how to use them.

Community
  • 1
  • 1
Dan D
  • 153
  • 1
  • 7
  • Seriously. Don't. Use naudio, and when you get it working, try replacing naudio with your own playback library. Writing an audio playback library in C# is not a trivial undertaking. This is why naudio has become so popular. – spender Apr 14 '15 at 14:25
  • I definitely see that now. That was significantly easier than I expected and I got it up and running pretty quickly. – Dan D Apr 14 '15 at 21:05

0 Answers0