Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Code ---------------------------------------------------- -Sound of Music- Start with the startA. cpp file for this part. You CANNOT change main() from how it is currently written.

C++ Code

----------------------------------------------------

-Sound of Music-

Start with the startA.cpp file for this part. You CANNOT change main() from how it is currently written. Instead, you need to add to the Beeper class to make main() work and play sound. You should first be able to enter the beats-per-minute of the song. Thus 60 bpm is 1 note a second and 120 bpm is 2 notes a second. Computers typically calculate time in milliseconds (1 / 1,000th of a second) so you will have to convert units. Next the user will input notes (in upper case), which you should play at the bpm entered (the playing should happen on the cin >> b; line of code).

To play notes, you will need to specify the pitch in Hertz (Hz) of the notes. You can reference this webpage:

http://www.phy.mtu.edu/~suits/NoteFreqCalcs.html

---------------------------------------------------------------------------

//StartA.cpp code is as followed

#include  #include  #include  #include  #include  using namespace std; /** Consts used for standard audio playback **/ const int AMPLITUDE = 28000*2; const int FREQUENCY = 44100; const int DURATION = 250; class Beeper; /** FUNCTIONS THAT ARE DONE **/ void audio_callback(void*, Uint8*, int); /** Helper struct used to generate samples **/ struct BeepObject { double freq; int samplesLeft; }; /** You will need to add to this class ** Code given can be played interactively **/ class Beeper { private: double v; queue beeps; public: Beeper(); ~Beeper(); void beep(double, int); void generateSamples(Sint16 *stream, int length); void wait(); }; int main() // YOU CANNOT CHANGE MAIN! { SDL_Init(SDL_INIT_AUDIO); Beeper b; cout << "Enter tempo (bpm)" << endl; int bpm; cin >> bpm; b.setTempo(bpm); // Make this! cout << "Enter song" << endl; cin >> b; // this line should play the song b.wait(); // ensures song finishes playing before program stops return 0; } /** SHOULD NOT NEED TO TOUCH ANYTHING BELOW HERE **/ /** USE IT TO LEARN BUT NO CHANGES NEED TO BE MADE **/ void audio_callback(void *_beeper, Uint8 *_stream, int _length) { Sint16 *stream = (Sint16*) _stream; int length = _length / 2; Beeper* beeper = (Beeper*) _beeper; beeper->generateSamples(stream, length); } Beeper::Beeper() { SDL_AudioSpec desiredSpec; desiredSpec.freq = FREQUENCY; desiredSpec.format = AUDIO_S16SYS; desiredSpec.channels = 1; desiredSpec.samples = 2048; desiredSpec.callback = audio_callback; desiredSpec.userdata = this; SDL_AudioSpec obtainedSpec; SDL_OpenAudio(&desiredSpec, &obtainedSpec); // start play audio SDL_PauseAudio(0); } Beeper::~Beeper() { SDL_CloseAudio(); } void Beeper::generateSamples(Sint16 *stream, int length) { int i = 0; while (i < length) { if (beeps.empty()) { while (i < length) { stream[i] = 0; i++; } return; } BeepObject& bo = beeps.front(); int samplesToDo = min(i + bo.samplesLeft, length); bo.samplesLeft -= samplesToDo - i; while (i < samplesToDo) { stream[i] = AMPLITUDE * sin(v * 2 * M_PI / FREQUENCY); i++; v += bo.freq; } if (bo.samplesLeft == 0) { beeps.pop(); } } } void Beeper::beep(double freq, int duration) { BeepObject bo; bo.freq = freq; bo.samplesLeft = duration * FREQUENCY / 1000; SDL_LockAudio(); beeps.push(bo); SDL_UnlockAudio(); } void Beeper::wait() { int size; do { SDL_Delay(20); SDL_LockAudio(); size = beeps.size(); SDL_UnlockAudio(); } while (size > 0); } 

--------------------------------------------------------

-StartA.cpp file's url

http://www-users.cselabs.umn.edu/classes/Spring-2017/csci1113/assignments/startA.cpp

-The desired sound's Url is

http://www-users.cselabs.umn.edu/classes/Spring-2017/csci1113/assignments/CEG.GEC.mp3

----------------------------------------------------------------------

For those of you unfamiliar with music, there are seven notes:

C D E F G A B

The link above says to start the A note at 440 Hz (please choose this definition). Then they give you a formula based on the number of half-steps. These are:

A -> B = 2 half-steps

A -> A = 0 half-steps

A -> G = -2 half-steps

A -> F = -4 half-steps

A -> E = -5 half-steps

A -> D = -7 half-steps

A -> C = -9 half-steps

(Hint: you should look around the SDLaudioPlayer.cpp example to figure out how to play sound.)

http://www-users.cselabs.umn.edu/classes/Spring-2017/csci1113/assignments/SDLaudioPlayer.cpp

In addition to reading notes, your program should also read periods (.) as rests (no sound). To do this, simply put a very low frequency Hz (outside of the hearing range).

-----------------------------------------

Example 1 (user input is bolded, desired sound posted on website):

Enter tempo (bpm)

60

Enter song

CEG.GEC

------------------------------------------------------------

C++ Code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

What are the types of forms of communication ?

Answered: 1 week ago

Question

Explain the process of MBO

Answered: 1 week ago

Question

7. What decisions would you make as the city manager?

Answered: 1 week ago