Monday, 11 November 2013

# Audio Programming in C

Monday, November 11, 2013
9
Welcome to Audio Programming in C

sound() function develops the sound of a specified frequency. It's used for contributing musical notes into c programs, try to use some random values in loop, vary delay.

Declaration: - void sound(unsigned frequency);
 Author: Arnab Nandy

The frequency spectrum can be returned through a Fourier transform of the signal, and the resulting values can usually presented as amplitude and phase, both plotted vs frequency. The frequency spectrum of a time-domain signal is a representation of that signal between the frequency domain.

Any signal that can be corresponded as amplitude that alters with time has a matching frequency spectrum. This involves associate concepts such as visible light, musical notes, radio/TV channels, and even the regular rotation of the earth. When these physical developments are represented in the form of a frequency spectrum, certain physical descriptions of their internal processes become much simpler. Often, the frequency of spectrum plainly shows harmonics, visible as discrete spikes or lines at particular frequencies, which provide insight into the mechanisms that, generate the entire signal.

Spectrum analysis, also adverted to as frequency domain analysis or spectral density estimation, is the technical process of decomposing a complex signal into simpler parts. As described above, many physical processes are best described as a sum of many individual frequency components. Any process that quantifies the various amounts (e.g. amplitudes, powers, intensities, or phases), versus frequency can be called spectrum analysis. Spectrum analysis can be performed on the entire signal. Alternatively, a signal can be broken into short segments (sometimes called frames), and spectrum analysis may be applied to these individual segments. Periodic functions (such as sin (t)) are particularly well-suited for this sub-division. General mathematical techniques for analyzing non-periodic functions fall into the category of Fourier analysis.

So far in our theoretical excursions, we’ve only examined sound as a function of pressure as it varies over time. Another useful way of looking at sound is to plot amplitude and see how it varies over frequency. This results in graphs where the domain (x-axis) is in units of frequency (Hz). Graphs of sound plotted this way are said to be in the frequency domain. The relationship between the time-domain and frequency-domain graphs is based on the idea of Fourier decomposition. Sound waves are often cyclical in nature. Mathematically, periodic sound waves can be seen as the sum of multiple simple sine waves of different frequency and amplitude. The more such sine waves we add together, the better an approximation of the original function we can get. We can take a signal and find its component sine waves by applying a Fourier transformation, the details of which are outside the scope of this book. Many algorithms exist to get this decomposition too, the best known of which is the Fast Fourier Transform (FFT).

So Here I have decided to show you this using a program as follows:

```
`#include<stdio.h>`

`#include<conio.h>`

`#include<math.h>`

`#include<graphics.h>`

float function(float x);

`void ploat();`

`void axis();`

void main()

`{`

`    int gdriver=DETECT,gmode;`

` `  initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

`    cleardevice();`

`    axis();`

`    ploat();`

` `  getch();

`    closegraph();`

`    nosound();`

`}`

` `float function(float x)

`{`

`    float y;`

`    y=pow(cos(x),2)+sin(90)+1;`

`    return y;`

`}`

` `void ploat()

`{`

`    int y=0,x,y1;`

`    float m=0;`

` `  for(m=-15;m<=-0.00001&&y>=-500;m=m+0.00001)

`    {`

`            y=50*function(m)+0.5;`

`            x=25*m+0.5;`

` `

`            putpixel(x+314,y+250,GREEN);`

`            sound(y);`

`    }`

` `  for(m=0.00001;m<=15&&y<=500;m=m+0.00001)

`    {`

` `           y=50*function(m)+0.5;

`            x=25*m+0.5;`

` `          putpixel(x+315,y+250,GREEN);

`            sound(y);`

`    }`

`}`

`void axis()`

`{`

`            int i;`

`            line(1,250,675,250);`

`            line(314,1,314,499);`

`            for(i=1;i<=676;i=i+10)`

`            line(i,249,i,251);`

`            for(i=1;i<=500;i=i+10)`

`            line(314,i,316,i);`

}

```
Output: Watch the video

These code will help you to convert a 2D graph of equation cos(x)2+sin(90)+1=0 to sound waves for a particular values. I will also provide all of you a simulation of it as video output of this program.

Your computer likely had the hardware to do this already (a sound card). Many software packages for sound analysis or sound editing have the software that can take a curt sample of a sound recording, perform the computation to obtain a spectrum (a digital Fourier transform or DFT) and display it in 'real time'. If how have these, you can learn a lot about spectra by singing sustained notes into the microphone and looking at their spectra. If you shift the loudness, the size (or amplitude) of the spectral elements gets bigger. If you modify the pitch, the frequency of all of the components increases. If you change a sound without altering its loudness or its pitch then you are, by definition, changing its quality. One of the things that specify the quality is the relative size of the different spectral components. If you sing "ah" and "ee" at the same pitch and loudness, you will notice that there is a big difference between the spectra.

Connect with Arnab :

1. Innovation distinguishes between a leader and a follower.
Grt Buddy!

1. :) thanks alot Sandip

2. Arnab is really a great programmer. We help such students to show what they can do. We intend to create a platform to help them to show what they can do. Our doors are always open to such good minds. :)

1. thanks for providing this platform i'll find some more interesting topics which can be shown further in another article

2. Always happy to help :)

3. Wonderful dear (h) :)

4. thank you mam :))

3. Yes a fine experiment ... :)