Produce Different Frequencies Sound in C/C++





 You can produce sound in programming languages by many ways. But there is a function "Beep()" in Win API for C and C++ programming languages, with help of this function we can produce sound for different durations and of different frequencies.



To use the Beep( ) function you have to include the <Windows.h> header file. This the header file which is used for windows programming. It is an window API. The beep() function is included in this header file only, so you have to use it. By using this function you can create great apps related to music instruments. Just track my articles because in coming days I will tell you how to make Piano program/application by using this function. Beep( ) is a two parametrized function. It takes two parameters they can be float, double or int type. First parameter is for frequency in Hz and second Parameter for duration in milliseconds. Beep(frequency in Hz, duration in milliseconds).

Now go through the codes given below, it will produce the sound with different frequencies and for different durations.

This is the sample program for producing beep of  50000 Hz and for duration of 1 sec in C++ :

#include<windows.h> 
#include<iostream>
using namespace std;
main()
{
Beep(50000,1000); 
}


This is the sample program for producing beep of  20000 Hz and for duration of 5 sec in C++ :

#include<windows.h> 
#include<iostream>
using namespace std;
main()
{
Beep(20000,5000); 
}

This is the sample program for producing beep of  50000 Hz and for duration of 1 sec in C :
#include<stdio.h>
#include<windows.h> 
#include<conio.h>
main()
{
Beep(50000,1000);
getch(); 
}

This is the sample program for producing beep of  20000 Hz and for duration of 5 sec in C :

#include<stdio.h>
#include<windows.h> 
#include<conio.h>
main()
{
Beep(20000,5000);
getch(); 
}


If You are having any query, then mail me or comment here

Keep Enjoying!

0 comments: