In: Computer Science
IN C
Write a function in the form:
void play( int key, int duration) // duration units are tenths of a second
which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where:
n = (duration/10.0)*8000 w = (2π440rkey-49)/8000 r = 21/12
In the main program call your function to play the first three notes of three blind mice.
#code for above statement
Solution:
#include <stdio.h>
#include <math.h>
//incluuding the math header to file to compute the sin
function
#define PI 3.14159265
//constant variable pi
//declaring the function play with parameters key and
duration
void play(int key,int duration);
int main()
{
//calling the play function passing for example key=2
and duration=10
play(2,1);
}
//function of play
void play(int key,int duration)
{
//declaring variable for n and t r and w
int n,t;
double r,w;
//calucluating the n,r, according to the given
information
n=(duration/10.0)*80000;
r=21/12;
w=((2*PI*440*r*key)-(49))/8000;
printf("%d",n);
//for loop to find all the values of t from 0 to
n-1
for(t=0;t<n;t++)
{
printf("%lf\n",sin(w*t));
}
}
#screenshot of the program
#output of the given program
I hope this is helpful to you!
Thank you!
- 0 C:\Users\sandeep\Desktop\a.c- [Executing) - Dev-C++ 5.11 File Edit Search View Project Execute Tools AStyle Window 2 * BB EE Help 1 :: 0 :: ~ * d TDM-GCC 4.9.2 32-bit Release v a.c 2 3 4 5 6 7 8 9 #include <stdio.h> #include <math.h> //incluuding the math header to file to compute the sin function #define PI 3.14159265 //constant variable pi /declaring the function play with parameters key and duration void play (int key, int duration); int main() 10 //calling the play function passing for example key=2 and duration=10 play (2,1); 12 } 13 14 //function of play void play(int key, int duration), 7/declaring variable for n and tr and w int n,t; double r,w; //calucluating the n,r, according to the given information n=(duration/10.0)*80000; r=21/12; w=((2*PI*440*r*key)-(49)/8000; printf("%d",n); //for loop to find all the values of t from 0 to n-1 for(t=0;t<n;t++) printf("%1f\n", sin(w*t));
-ox C:\Users\sandeep\Desktop\a.exe 0.359053 0.868555 0.986170 0.658831 0.034232 -0.605813 -0.972518 -0.900429 -0.422072 0.246722 0.804196 0.998822 0.742785 0.151608 -0.507973 -0.938360 -0.945365 -0.525826 0.130962 0.728661 0.997592 0.816416 0.266878 -0.403074 -0.891161 -0.977162 -0.622273 0.013382 0.642999 0.982499 0.878701 0.378439 -0.292573 -0.831577 -0.995379 -0.710072 -0.104384 0.548401 Process exited after 7.434 seconds with return value 8000 Press any key to continue ...