xtd 0.2.0
xtd::beep Class Referencefinal

Definition

Represent beep output manipulator class.

Header
#include <xtd/beep>
Namespace
xtd
Library
xtd.core
See also
xtd::console::beep method.
Examples:
console_beep2.cpp, and console_beep3.cpp.

Constructors

 beep ()=default
 Plays the sound of a beep through the console speaker. More...
 
 beep (uint32 frequency, uint32 duration)
 Plays the sound of a beep of a specified frequency and duration through the console speaker. More...
 

Additional Inherited Members

- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object. More...
 
bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object. More...
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type. More...
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance. More...
 
template<typename object_t >
std::unique_ptr< object_t > memberwise_clone () const noexcept
 Creates a shallow copy of the current object. More...
 
virtual xtd::ustring to_string () const noexcept
 Returns a sxd::ustring that represents the current object. More...
 
- Static Public Member Functions inherited from xtd::object
static bool equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are considered equal. More...
 
static bool reference_equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are the same instance. More...
 

Constructor & Destructor Documentation

◆ beep() [1/2]

xtd::beep::beep ( )
default

Plays the sound of a beep through the console speaker.

Remarks
By default, the beep plays at a frequency of 800 hertz for a duration of 200 milliseconds
Examples
The following example demonstrates the beep method. The example accepts a number from 1 through 9 as a command line argument, and plays the beep that number of times.
#include <xtd/threading/thread>
#include <xtd/beep>
#include <xtd/int32_object>
#include <xtd/literals>
using namespace std;
using namespace xtd;
auto main(int argc, char* argv[])->int {
int x = 0;
//
if ((argc == 2) && (int32_object::try_parse(argv[1], x) == true) && ((x >= 1) && (x <= 9))) {
for (auto i = 1; i <= x; i++) {
cout << ustring::format("Beep number {}.", i) << beep() << endl;
}
} else
cout << "Usage: Enter the number of times (between 1 and 9) to beep." << endl;
}

◆ beep() [2/2]

xtd::beep::beep ( uint32  frequency,
uint32  duration 
)
inline

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/threading/thread>
#include <xtd/as>
#include <xtd/beep>
#include <xtd/console>
using namespace std;
using namespace xtd;
// 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;
// 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 vector<note>& tune) {
for (auto n : tune) {
if (n.note_tone() == tone::rest)
threading::thread::sleep(as<int>(n.note_duration()));
else
cout << 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".
vector 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);
}

The documentation for this class was generated from the following file: