We help IT Professionals succeed at work.
Get Started

Generating Audio In C programming

32,188 Views
Last Modified: 2016-12-03
This is a question regarding C Programming: i am looking to generate audio using C Code. for this i am looking to generate a square wave function (does not have to be a square wave can be any wave form really) which plays musical notes which last for about 30 seconds. After looking on the internet and message boards i have come across the sound(), nosound() and delay() codes for C, but i can only generate a single tone which lasts for one second. below is the code which i attempted to put together, this code below did not produce anything just errors when i was trying to compile the code.

#include <stdio.h>
#include <dos.h>

void my_sounds(void);

int music(int pitch, int time)
{
sound(pitch);
delay(time);
nosound();
return(0);
}


void       my_sounds(void)
 {
      sound(130);            /* make a sound at 500 Hz.*/
      delay(2000);            /* pause for 2000 milliseconds = 2 seconds  */
      nosound();
      return(0);

      sound(137);
      delay(2000);
      nosound();
      return(0);

      sound(145);
      delay(2000);
      nosound();
      return(0);

      sound(155);
      delay(2000);
      nosound();
      return(0);

      sound(166);
      delay(2000);
      nosound();
      return(0);

      sound(176);
      delay(2000);
      nosound();
      return(0);

      sound(185);
      delay(2000);
      nosound();
      return(0);

      sound(195);
      delay(2000);
      nosound();
      return(0);

      sound(207);
      delay(2000);
      nosound();
      return(0);

      sound(220);
      delay(2000);
      nosound();
      return(0);

      sound(235);
      delay(2000);
      nosound();
      return(0);

      sound(260);
      delay(2000);
      nosound();
      return(0);

      sound(275);
      delay(2000);
      nosound();
      return(0);

      sound(290);
      delay(2000);
      nosound();
      return(0);

      sound(311);
      delay(2000);
      nosound();
      return(0);

      sound(333);
      delay(2000);
      nosound();
      return(0);

      sound(350);
      delay(2000);
      nosound();
      return(0);

      sound(370);
      delay(2000);
      nosound();
      return(0);

      sound(390);
      delay(2000);
      nosound();
      return(0);

      sound(415);
      delay(2000);
      nosound();
      return(0);

      sound(440);
      delay(2000);
      nosound();
      return(0);

      sound(470);
      delay(2000);
      nosound();
      return(0);

      sound(490);
      delay(2000);
      nosound();
      return(0);

      sound(520);
      delay(2000);      
      nosound();            /* turn off sound      */
      return(0);
 }

Does anyone know how i would go about generating some type of wave function which can play musical notes in C programming.

Many Thanks,

Teknologik
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 30 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE