xtd 0.2.0
Loading...
Searching...
No Matches
console_song3.cpp

Shows how to use xtd::beep class.

#include <xtd/collections/generic/list>
#include <xtd/threading/thread>
#include <xtd/as>
#include <xtd/beep>
#include <xtd/console>
using namespace xtd;
using namespace xtd::collections::generic;
// Define the frequencies of notes in an octave, as well as
// silence (rest).
enum class tone {
rest = 0,
g_below_c = 196,
a = 220,
a_sharp = 233,
b = 247,
c = 262,
c_sharp = 277,
d = 294,
d_sharp = 311,
e = 330,
f = 349,
f_sharp = 370,
g = 392,
g_sharp = 415,
};
// Define the duration of a note in units of milliseconds.
enum class duration {
none = 0,
whole = 1600,
half = whole / 2,
quarter = half / 2,
eighth = quarter / 2,
sixteenth = eighth / 2,
};
// Define a note as a frequency (tone) and the amount of
// time (duration) the note plays.
struct note final {
private:
tone tone_val = tone::rest;
duration dur_val = duration::none;
public:
// Define a constructor to create a specific note.
note(tone frequency, duration time) : tone_val(frequency), dur_val(time) {}
note() = default;
note(const note& note) = default;
note& operator=(const note& note) = default;
bool operator ==(const note& rhs) const noexcept {return tone_val == rhs.tone_val && dur_val == rhs.dur_val;}
// Define properties to return the note's tone and duration.
tone note_tone() const noexcept {return tone_val;}
duration note_duration() const noexcept {return dur_val;}
};
// Play the notes in a song.
void play(const list<note>& tune) {
for (auto n : tune) {
if (n.note_tone() == tone::rest)
threading::thread::sleep(as<int>(n.note_duration()));
else
console::out << beep(as<unsigned int>(n.note_tone()), as<unsigned int>(n.note_duration()));
}
}
auto main() -> int {
// Declare the first few notes of the song, "Mary Had A Little Lamb".
list mary = {
note(tone::b, duration::quarter),
note(tone::a, duration::quarter),
note(tone::g_below_c, duration::quarter),
note(tone::a, duration::quarter),
note(tone::b, duration::quarter),
note(tone::b, duration::quarter),
note(tone::b, duration::half),
note(tone::a, duration::quarter),
note(tone::a, duration::quarter),
note(tone::a, duration::half),
note(tone::b, duration::quarter),
note(tone::d, duration::quarter),
note(tone::d, duration::half)
};
// Play the song
play(mary);
}
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
@ n
The N key.
@ play
The Play key.
@ time
The date_time_picker control displays the date/time value in the time format set by the user's operat...
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10