Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Code, work on a Linux Machine! One large skill needed to be successful in the computer science industry, is to be able to use

C++ Code, work on a Linux Machine!

One large skill needed to be successful in the computer science industry, is to be able to use additional libraries and already built code, and make it better. Here, you will be using SDL and knowledge in arrays, strings, etc. to be able to build your own single oscillator synthesizer. This code uses the SDL2 libraries ( https://www.libsdl.org/download-2.0.php , make sure you download Development Libraries not Runtime Binaries) to generate sound.

https://www.libsdl.org/download-2.0.php

A sample program called SDLaudioPlayer.cpp is provided. http://www-users.cselabs.umn.edu/classes/Spring-2017/csci1113/assignments/SDLaudioPlayer.cpp

When you run this program, it will open a small window (called HW8). If you click this window you can then press asdfghj to play sounds. Press the Esc key to close the window and stop the program. To compile the program, you need to type (in the terminal after moving into the directory with the cpp file): g++ SDLaudioPlayer.cpp -lSDL2

Then use type this to run the program (on cselabs machines): ./a.out (If that doesn't work, try typing this:) LD_PRELOAD="/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21" ./a.out

Start with the startA.cpp file for this part.

#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 > bpm; b.setTempo(bpm); // Make this! cout > 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  0); } 

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

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

image text in transcribed

For those of you unfamiliar with music, there are seven notes: CDE F GAB 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 7 half-steps A D A C 9 half-steps (Hint: you should look around the "SDLaudioPlayer.cpp" example to figure out how to play sound.) 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 underlined, desired sound posted on website): Enter tempo (bpm) 60 Enter song CEG. GEC Hint: try playing around with Beeper's "beep0 function

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

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions