Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Code, in Linux Machine!!! 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

C++ Code, in Linux Machine!!!

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. A sample program called SDLaudioPlayer.cpp is provided.

SDLaudioPlayer.cpp:

#include  #include  #include  #include  #include  /** 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 onKeyDown(SDL_KeyboardEvent&,Beeper*); void createWindow(SDL_Window*); void destroyWindow(SDL_Window*); void handleInput(SDL_Event,Beeper*); 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; std::queue beeps; public: Beeper(); ~Beeper(); void beep(double, int); void generateSamples(Sint16 *stream, int length); void wait(); // TODO: Add input overloaded function to read a "song" }; bool closingTime = false; //used if running in "interactive" mode int main(int argc, char* argv[]) { /* All initialization stuff */ SDL_Init(SDL_INIT_AUDIO); Beeper b; /* TODO: Place all your main code from here */ /* Run this loop if you would like * to "play" a song on the keyboard * and close the app by pressing the 'escape' key */ // below is sample code (that you should get rid of for your program) SDL_Window* window; createWindow(window); while(!closingTime) { SDL_Event event; if(SDL_WaitEvent(&event)) { handleInput(event,&b); } } destroyWindow(window); return 0; } /** SHOULD NOT NEED TO TOUCH ANYTHING BELOW HERE **/ /** USE IT TO LEARN BUT NO CHANGES NEED TO BE MADE **/ /* ... But you can use on key down as reference on * using the beeper class */ 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); } void createWindow(SDL_Window *window) { window = SDL_CreateWindow("HW8", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 100, 100, SDL_WINDOW_SHOWN); if (window == NULL) std::cout beep(Hz,250); // comment out this line if having trouble remote connecting b->wait(); } } } } 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); } 

When you run this program, it will open a small window. 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 Note: The last word in the line above is minus ell (lower case) SDL (capitol) 2 (the number) Then use type this to run the program (Linux 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

You can begin this problem with the startB.cpp The main() program is already written for you and you have to write code to make main() work as desired (do not change main()).

startB.cpp:

#include  #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; /** 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() // again, no messing with main() (I MEAN IT!) { SDL_Init(SDL_INIT_AUDIO); Beeper b; ifstream in; string fname; cout > fname; in.open(fname.c_str()); in >> b; b.wait(); return 0; } /** SHOULD NOT NEED TO TOUCH ANYTHING BELOW HERE **/ /** USE IT TO LEARN BUT NO CHANGES NEED TO BE MADE **/ /* ... But you can use on key down as reference on * using the beeper class */ 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); } 

Modifications: (1) You will read from a file, rather than a keyboard (the file will always start with the bpm then a list of notes).

(2) You will have to take into account different octaves (e.g. C4 is one octave lower than C5... see below for octave description).

(3) The songs might contains a tilde (~), which indicate that the sound should continue to be played. Thus if the song is 60 bpm (1 note a second), then A will play for 1 second and A~ will play for 2 seconds (A~~ for 3 seconds and so on).

(4) Take into account flats (b) or sharps (#) after notes. Flats change the pitch down one half-step and sharps change the pitch up by one half-step. Notes E and F are only one half-step apart, so E# is the same as F (also Fb is the same as E). For this program notes can only have at most one sharp or flat after a note. So we will not test for something like: C## or C#b.

The pattern of notes (C D E F G A B) repeats, so the full pattern is: (... C D E F G A B C D E F G A B C D E F G A B ...). To differentiate these notes, each note is assigned an octave which will be denoted in the file in parenthesis after the name. The note A (440 Hz) from part A is considered to be in the 4th octave, so in the text file it will be written as A(4). One octave difference is equal to 12 half-steps. So A(5) is 12 half-steps higher than A(4) and similarly D(3) is 12 half-steps lower than D(4).image text in transcribed

for example

song1.txt:

480 E(4)~~~D(4)~~~C(4)~~~~~..E(4)~~~D(4)~~~C(4)~~~~~..C(4).C(4).C(4).C(4).D(4).D(4).D(4).D(4).E(4)~~~D(4)~~~C(4)~~~~~.. 
CORRECTION: I originally stated that octaves were 13 half-steps apart, but I was terribly wrong! They are 12 steps apart. Example 1 (user input is underlined, desired sound posted on website): Enter song file song 1 txt Example 2 (user input is underlined, desired sound posted on website): Enter song file Song 2.txt Example 3 (user input is underlined, desired sound posted on website): Enter song file song txt

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

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

3. Existing organizations and programs constrain behavior.

Answered: 1 week ago

Question

What is electric dipole explain with example

Answered: 1 week ago

Question

What is polarization? Describe it with examples.

Answered: 1 week ago