How
to use sounds in a C# application
Various
programs use various sounds to give an alert about various tasks.
Here in C# we can use various sounds with form's load event,for a
button click, for a check box etc... Basically there are two methods
to use sound in C#.
First
one is :-By using beep tones and second is by using audio
files(.wav,.mp3 etc...).
So
this post is about beep tones.
In
Windows operating system there are various sounds to inform user
about some tasks like “low battery”, “updates available”
etc.. In some cases windows use beep tones also. So let's get know
about how to use beep tones in a C# application.
First
create new project and name it as “beeptest”.
Now
double click on your form1 and here you have code window and you are
at form's load event.
Then
type this code →
Console.Beep();
and
press “F5” or go to debug and click on “Start Debugging”.
Now
you can here a beep tone when form loads.
Now
try to use beeps with a button.
Now
get a button to form and change it's text as “Click to beep” .
Then
double click on the button and type this code in button's click
event.
Console.Beep();
Now
if you run this and click on the button you can here a beep tone.
Now
let's study about the code “Console.Beep();”.
This
is the general form of beep tones. But you can change the frequency
of beeps. Now look at the code below.
Console.Beep(Frequency,Time
period);
Here
frequency varies from 37Hz to 32767Hz. If you want to use base sounds
you can use low frequencies like 37,40 ,50 39 etc..
Time
period means the amount of time that you can here the beep. Here in
C# to here a beep sound for a 1 second you must use time period as
“1000”.
Now
get a new button and code this in it's click event.
Console.Beep(400,1000);
After
running and clicking on the button1 you can here a beep tone with
400Hz frequency for a period of one second.
Try
some more examples by changing frequency and time period.
Now
I think you have got it.
Here
we are going to make a soft organ with the help of beep tones.
Now
open a new project and name it as “softorgan”.
Now
change your form's name as you like or keep it as it's default
(form1).
Here
we have to use 13 buttons.
Get
a button to the form.
Now
change it's text property as “&S”.
Now
get a label to the form and keep it under the button and change it's
text property to “C”.
Now
get another button and change it's text property as “&E”.
Now
get another label to the form and keep it under the button and change
it's text property to “C#”.
Accordingly
get another 11 buttons and change there text properties as , “&D”,
“&R”,“&F”,“&G”,“&Y”,“&H”,“&U”,“&J”,
“&I”, “&K” and “&L”. Then get 11 labels and
change their text properties as “D”, “Eb”, “E”, “F”,
“F#”, “G”, “Ab”, “A”, “Bb”, “B”and “C”.
Now
look at the picture below and align your buttons and labels as keys
in keyboard.
Now
it's time to code.
Double
click on key C (first button) and type this code →
Console.Beep(261,200);
Now
code these codes accordingly second button and so on.
Second
button(C#) Console.Beep(277,200);
Third
button(D) Console.Beep(293,200);
Fourth
button(Eb) Console.Beep(311,200);
Fifth
button(E) Console.Beep(329,200);
Sixth
button(F) Console.Beep(349,200);
Seventh
button(F#) Console.Beep(370,200);
Eighth
button(G) Console.Beep(392,200);
Ninth
button(Ab) Console.Beep(415,200);
Tenth
button(A) Console.Beep(440,200);
Eleventh
button(Bb) Console.Beep(466,200);
Twelfth
button(B) Console.Beep(494,200);
Thirteenth
button(C) Console.Beep(523,200);
You
can change the amount of time period by increasing or decreasing the
value “200”.
Now
it' time to play our soft organ.
Press
F5 or go to debug menu and click on “start debugging”.
Now
you can play using your mouse or using appropriate key in your
keyboard.
(ex:
to play C press “s” in your keyboard)
Thank
you!
No comments:
Post a Comment