xtd 0.2.0
Loading...
Searching...
No Matches

◆ beep() [2/2]

static void xtd::console::beep ( uint32  frequency,
uint32  duration 
)
static

Plays the sound of a beep of a specified frequency and duration through the console speaker.

Parameters
frequencyThe frequency of the beep, ranging from 37 to 32767 hertz
durationThe duration of the beep measured in milliseconds
Examples
This example demonstrates the beep method by playing the first few notes of a song through the console speaker.
#include <xtd/collections/generic/list>
#include <xtd/threading/thread>
#include <xtd/as>
#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::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".
auto mary = list {
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:72
static void beep()
Plays the sound of a beep through the console speaker.
static void sleep(int32 milliseconds_timeout)
Suspends the current thread for a specified time.
@ none
No modifier key.
@ a
The A key.
@ c
The C key.
@ n
The N key.
@ d
The D key.
@ f
The F key.
@ b
The B key.
@ play
The Play key.
@ g
The G key.
@ e
The E key.
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