C# .NET play note frequency (sound)

S

Slickuser

I got beeping now. Is it possible to output a note like that?
Thanks.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace sound12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// Play a beep with default frequency
// and duration (800 and 200, respectively)
Console.Beep();

// Play a beep with frequency as 200 and duration as 300
Console.Beep(200, 300);

Console.Beep((int)523.2, 300);
Console.Beep((int)523.2, 300);
Console.Beep((int)523.2, 300);


// Play the sound associated with the Asterisk event
SystemSounds.Asterisk.Play();
}
}
}
 
M

Michael C

Slickuser said:
I want to play a note in C# .NET giving a frequency like the one show
below.

Is it possible?

C 261.6
C# 277.2
D 293.7
D# 311.1
E 329.6
F 349.2
F# 370.0
G 392.0
G# 415.3
A 440.0
A# 466.2
B 493.9
C 523.2

I looked at this and it doesn't work.
I can't added in kernerl32.dll (it saying it's not an assembly or
com )

You don't add a reference the kernel32.dll, you just copy/paste the code as
is.
 
P

Peter Duniho

I got beeping now. Is it possible to output a note like that?
Thanks.

I don't believe that .NET has any built-in API to do anything like that.
The closest you could come is the SoundPlayer, and all that will do is
play a fixed WAV file. You can create the WAV format file in memory as a
MemoryStream, but you have to have the complete data ahead of time so that
the header of the WAV data is properly initialized.

I suppose if that's sufficient, then you could in fact do it that way.
Obviously if you know in advance which notes you want to play, you could
do that. But then, if you knew the notes far enough in advance, you could
just create the necessary WAV file and include it as a resource in your
application. :)

As for alternatives, even in Win32 I'm not aware of a tone-generator type
API that would take a frequency and play a note. However, it's not
difficult to generate the necessary wave forms and play them through a
streaming API. You can use either DirectSound or the WinMM API for that.

Pete
 
P

Peter Duniho

I got beeping now. Is it possible to output a note like that?
Thanks.

I don't believe that .NET has any built-in API to do anything like that.
[...]

As for alternatives, even in Win32 I'm not aware of a tone-generator
type API that would take a frequency and play a note. However, it's not
difficult to generate the necessary wave forms and play them through a
streaming API. You can use either DirectSound or the WinMM API for that.

And just to clarify, I'm assuming that for some reason Console.Beep()
isn't doing what you want, since you got it to work but are still asking
the question. It's not exactly the best thing for stringing a bunch of
notes together, and I'm guessing that's what you're running into.

Reading what I wrote above, I realize that there's a strong implication in
my post that you can't generate a tone by frequency at all. I practically
said that in so many words. Duh...obviously that's not true, and not what
I meant. Sorry for the confusion. I be having trouble words to put
together this night. :)

Anyway, if the Beep() method does do what you want, I'm not sure what
question is left. And if it doesn't, well...either way, maybe you could
be more explicit about what additional advice you need.

Pete
 
S

Steve Thackery

Another approach would be to use the AudioLab component suite from
www.mitov.com. It includes a tone generator which generates sine, triangle
or rectangle waveforms with any frequency you like.

The whole suite is extremely good (usual disclaimer).

SteveT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top