How to create lighting with sound by C Graphics with Source Code

lighting with sound

Introduction :

In the graphics programming, creating dynamic lighting effects with sound can be a complex but interesting project. You can create such program, using C or C++ programming language. In this article, I shall show you how to create dynamic lighting effects with sound using the graphics.h of Turbo C++ by C or C++ graphics programming language.

Here, I also show you the source code of the dynamic lighting effects with sound program in C or C++ graphics programming language.

About the program :

In this graphics program, I have created a graphical animation program of lighting with sound effects. When you run the program, you can see a colorful and dynamic animation with circles creating and vanishing with sound effects. The animated visual effect continues until you press a key. This program is built by C or C++ graphics programming language.

Explanation of the program :

In the program, you have to include necessary header files like graphics.h, stdlib.h, conio.h and dos.h for various functions. Now, you can declare integer variables such as “xmax”, “ymax”, “i”, “k”, “l”, “j”, “a”, “b”, “a1”, “b1”, “m”, “c”, “d”, “graphic_driver” and “graphic_mode”. Here, the “xmax” and “ymax” variables are used to store the maximum x and y coordinates of the graphics window.

The “i”, “k”, “l”, “j”, “a”, “b”, “a1”, “b1”, “m”, “c” and “d” variables are used for various loop counters and coordinates for drawing shapes. The “graphic_driver” and “graphic_mode” are used for graphic driver and the mode.After that, initialize the graphics system with specified graphic driver and mode using initgraph() function. Using getmaxx() and getmaxy() function, determine the maximum x and y coordinates of the current graphics screen respectively.

In the while loop, you can use kbhit() function to take a key input to stop the program. Then, draw small circles moving around a larger circle using circle() function with random colors. You can use setcolor(), setfillstyle() and floodfill() functions to set the color, fill style and fill color of circles respectively. Using rand() function, generate random coordinates of circles for animation effects.

You can play a sound using sound() and nosound() functions. Here, using delay() function Introduce a delay to control the speed of the animations. After a short delay, clear the entire graphics screen using cleardevice() function. Now, call getch() function that takes a key input from the user. Finally, close the graphics mode using closegraph() function.

How run the program :

To run this program on your pc, first you have to install Turbo C++ IDE. After install Turbo C++ IDE, create a C or C++ file in it. Now, copy my source code of the program and paste in C or C++ file. If you do not know how copy paste in Turbo C++ IDE, click here. Then, run the program and see the output in your pc. Do you know how to use graphics.h in Turbo C++ IDE?

Source code :

Here, you see the source code of my project. You can copy the following code and use it in your project.

/* Develop by Puskar Jasu*/
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include<dos.h>
int main(void)
{
   int xmax, ymax,i=0,k,l,j,a,b,a1,b1,m,c,d;
  int graphic_driver = DETECT, graphic_mode;
   initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
   xmax = getmaxx();
   ymax = getmaxy();
    while(!kbhit())
    {
    setcolor(i);
    k=rand()%xmax;
    l=rand()%ymax;
    for(j=0;j<=10;j++)
    {
       setcolor(i);
  setfillstyle(1,i);
  circle(k,l,2+j);
  floodfill(k,l,i);
  delay(5);
  }
 sound(24);
delay(300);
nosound();
  cleardevice();
  for(m=0;m<=50;m++)
  {
  a=rand()%50;
  b=rand()%50;
  a1=rand()%50;
  b1=rand()%50;
   c=rand()%100;
   d=rand()%100;
    setcolor(i);
  setfillstyle(1,i);
  circle(k+a+m,l+b+m,1);
  floodfill(k+a+m,l+b+m,i);
  circle(k-a-m,l-b-m,1);
  floodfill(k-a-m,l-b-m,i);
  circle(k-a-m,l+b+m,1);
  floodfill(k-a-m,l+b+m,i);
  circle(k+a+m,l-b-m,1);
  floodfill(k+a+m,l-b-m,i);
  circle(k+a1+m,l+b1+m,1);
  floodfill(k+a1+m,l+b1+m,i);
  circle(k-a1-m,l-b1-m,1);
  floodfill(k-a1-m,l-b1-m,i);
  circle(k-a1-m,l+b1+m,1);
  floodfill(k-a1-m,l+b1+m,i);
  circle(k+a1+m,l-b1-m,1);
  floodfill(k+a1+m,l-b1-m,i);
  circle(k+a+m,l+b,1);
  floodfill(k+a+m,l+b,i);
  circle(k-a-m,l-b,1);
  floodfill(k-a-m,l-b,i);
  circle(k-a,l+b+m,1);
  floodfill(k-a,l+b+m,i);
  circle(k+a,l-b-m,1);
  floodfill(k+a,l-b-m,i);
  circle(k+a1+m,l+b1,1);
  floodfill(k+a1+m,l+b1,i);
  circle(k-a1-m,l-b1,1);
  floodfill(k-a1-m,l-b1,i);
  circle(k-a1,l+b1+m,1);
  floodfill(k-a1,l+b1+m,i);
  circle(k+a1,l-b1-m,1);
  floodfill(k+a1,l-b1-m,i);
  circle(c+k,d+l,1);
  floodfill(c+k,d+l,i);
  circle(k-c,l-d,1);
  floodfill(k-c,l-d,i);
 circle(k-c,l+d,1);
  floodfill(k-c,l+d,i);
 circle(k+c,l-d,1);
  floodfill(k+c,l-d,i);
 delay(10);
 cleardevice();
  }
    i++;
    if(i==15)
    i=0;
    cleardevice();
    delay(100);
     }
   getch();
   closegraph();
   return 0;
}

Output :

You can see the output in my YouTube channel.

Conclusion :

By following the above article, you have learned how to create lighting with sound using the C or C++ graphics programming language. After that, you can create more complex programs with lighting and sound as you like. If you see my code and use it in your program, I shall be glad to you.

Scroll to Top