switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Beeper.hh
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Thorsten Horstmann <tho@berlios.de>
27
28 /** \file
29     \brief Beeper public header */
30
31 #ifndef HH_SENF_Utils_Beeper_
32 #define HH_SENF_Utils_Beeper_ 1
33
34 // Custom includes
35 #include <string>
36 #include <senf/Scheduler/TimerEvent.hh>
37
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 namespace senf {
41
42     /** \brief Helper class to beep the pc speaker
43
44         This helper class allows you to beep the pc speaker with precision like frequency and
45         duration. The beep can be played synchronous and asynchronous.
46
47         Typically the <tt>/dev/console</tt> device is used to control the local pc
48         speaker. Note, that opening <tt>/dev/console</tt> is a privileged operation on
49         most systems.
50
51         \see manual page console_ioctl(4) for the according ioctl <tt>KIOCSOUND</tt>
52       */
53     class Beeper
54     {
55     public:
56         Beeper(std::string const & device = "/dev/console");
57                                         ///< Construct a new Beeper for the given device.
58         ~Beeper();
59
60         void beep_sync(float freq, unsigned length, unsigned count = 1, unsigned delay = 100);
61                                         ///< play beep synchronous
62                                         /**< \param freq frequency in Hz, where 0 < \e freq < 20000.
63                                                  The regular terminal beep is around 750Hz.
64                                              \param length duration in milliseconds.
65                                              \param count number of repetitions (defaults to 1).
66                                              \param delay delay between repetitions in milliseconds. */
67         void beep_async(float freq, unsigned length, unsigned count = 1, unsigned delay = 100);
68                                         ///< play beep asynchronous
69                                         /**< \param freq frequency in Hz, where 0 < \e freq < 20000.
70                                                  The regular terminal beep is around 750Hz.
71                                              \param length duration in milliseconds.
72                                              \param count number of repetitions (defaults to 1).
73                                              \param delay delay between repetitions in milliseconds. */
74
75         void stop_beep();               ///< stop playing.
76         bool start_beep(float freq);    ///< start playing.
77                                         /**< start playing until any other member is called.
78                                              \param freq frequency in Hz, where 0 < \e freq < 20000.
79                                              \returns \a true on success.*/
80
81     private:
82         static const unsigned CLOCK_TICK_RATE = 1193180;
83
84         int fd_;
85         scheduler::TimerEvent timer_;
86
87 #ifndef DOXYGEN
88         struct {
89             float freq;
90             unsigned length;
91             unsigned count;
92             unsigned delay;
93             bool action;
94         } params_;
95 #endif
96
97         void timeout();
98     };
99
100 }
101
102 //-/////////////////////////////////////////////////////////////////////////////////////////////////
103 #include "Beeper.cci"
104 //#include "Beeper.ct"
105 //#include "Beeper.cti"
106 #endif
107
108 \f
109 // Local Variables:
110 // mode: c++
111 // fill-column: 100
112 // c-file-style: "senf"
113 // indent-tabs-mode: nil
114 // ispell-local-dictionary: "american"
115 // compile-command: "scons -u test"
116 // comment-column: 40
117 // End: