Question

IN C Write a function in the form: void play( int key, int duration) // duration...

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.

The first three notes are keys 44, 42, and 40 respectively. The frequency for a key on the keyboard is f = 440(21/12)key-49

0 0
Add a comment Improve this question Transcribed image text
Answer #1

this is the C code:

#include<stdio.h>
#include<math.h>
void play( int key, int duration)
{
   int n= (duration/10.0)*8000;
   double pi=3.1415926535;
   double r=21.0/12.0;
   double w=(2*pi*440*r*key-49)/8000;
   int i;
   for(i=0;i<n;i++)
   {
       double x=i*w;
       double ans=sin(x);
       printf("%lf ",ans);
   }
   printf("\n");
}
int main()
{
   //let the duration is 1 second so we will pas 10 becasue it s given as 10th of second
   play(44,10);
   play(42,10);
   play(40,10);
return 0;
}

PLEASE UPVOTE

Add a comment
Know the answer?
Add Answer to:
IN C Write a function in the form: void play( int key, int duration) // duration...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write an M-file to produce a desired note, expressed in piano key number, for a given...

    Write an M-file to produce a desired note, expressed in piano key number, for a given duration. Your M-file should be in the form of a function called key2note.m. Your function should have the following form: function xx = key2note(X, keynum, dur, fsamp) % KEY2NOTE Produce a sinusoidal waveform corresponding to a % given piano key number % % usage: xx = key2note (X, keynum, dur) % % xx = the output sinusoidal waveform % X = complex amplitude for...

  • Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which...

    Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which prints each character of the given string str on a new line, with each line numbered 1, 2, 3, …. For example calling the function with printNumberedChars("hello"); should output the following: 1. h 2. e 3. l 4. l 5. o Q2. Write a recursive function in C++ int sumArray(const int* arr, unsigned int size) { ... } which takes an array of integers,...

  • Code in C++: Please help me fix the error in function: void write_account(); //function to write...

    Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...

  • How do I start this c++ code homework? Write a code in C++ for a BlackJack...

    How do I start this c++ code homework? Write a code in C++ for a BlackJack card game using the following simplified rules: Each card has a numerical value. Numbered cards are counted at their face value (two counts as 2 points, three, 3 points, and so on) An Ace count as either 1 point or 11 points (whichever suits the player best) Jack, queen and king count 10 points each The player will compete against the computer which represents...

  • Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a =...

    Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a = [ [93, 80, 99, 72, 86, 84, 85, 41, 69, 31], [15, 37, 58, 59, 98, 40, 63, 84, 87, 15], [48, 50, 43, 68, 69, 43, 46, 83, 11, 50], [52, 49, 87, 77, 39, 21, 84, 13, 27, 82], [64, 49, 12, 42, 24, 54, 43, 69, 62, 44], [54, 90, 67, 43, 72, 17, 22, 83, 28, 68], [18, 12, 10,...

  • Write a C++ program with a function to read in the file parts.txt into either parallel...

    Write a C++ program with a function to read in the file parts.txt into either parallel vectors or a vector of structs. The data file is on ANGEL in zipped format. The first few lines of the file look like: P-13725 A 23 61.46 P-13726 B 12 51.08 P-13754 D 27 4.56 P-13947 C 34 27.71 Representing part number, Class, On hand balance, cost. After the vector(s) has/have been filled, display a menu which allows the user to request the...

  • so i have my c++ code and ive been working on this for hours but i...

    so i have my c++ code and ive been working on this for hours but i cant get it to run im not allowed to use arrays. im not sure how to fix it thank you for the help our job is to write a menu driven program that can convert to display Morse Code ere is the menu the program should display Menu Alphabet Initials N-Numbers - Punctuations S = User Sentence Q- Quit Enter command the user chooses...

  • okay so here is my c++ code and the errors im really stuck on fixing what...

    okay so here is my c++ code and the errors im really stuck on fixing what i did wrong it seems to be the same repeated error our job is to write a menu driven program that can convert to display Morse Code ere is the menu the program should display Menu Alphabet Initials N-Numbers - Punctuations S = User Sentence Q- Quit Enter command the user chooses A your program should use a loop and your morse code printing...

  • 1. According to the paper, what does lactate dehydrogenase (LDH) do and what does it allow...

    1. According to the paper, what does lactate dehydrogenase (LDH) do and what does it allow to happen within the myofiber? (5 points) 2. According to the paper, what is the major disadvantage of relying on glycolysis during high-intensity exercise? (5 points) 3. Using Figure 1 in the paper, briefly describe the different sources of ATP production at 50% versus 90% AND explain whether you believe this depiction of ATP production applies to a Type IIX myofiber in a human....

  • Please use own words. Thank you. CASE QUESTIONS AND DISCUSSION > Analyze and discuss the questions...

    Please use own words. Thank you. CASE QUESTIONS AND DISCUSSION > Analyze and discuss the questions listed below in specific detail. A minimum of 4 pages is required; ensure that you answer all questions completely Case Questions Who are the main players (name and position)? What business (es) and industry or industries is the company in? What are the issues and problems facing the company? (Sort them by importance and urgency.) What are the characteristics of the environment in which...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT