2016-05-31 03:58:58 +00:00
|
|
|
#pragma once
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
#include "exception.hpp"
|
2016-06-09 23:09:54 +00:00
|
|
|
#include "utils/concurrency.hpp"
|
|
|
|
#include "utils/macros.hpp"
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-06-09 23:09:54 +00:00
|
|
|
#define StrSndErr(s) ToStr(snd_strerror(s))
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
namespace alsa
|
|
|
|
{
|
2016-06-09 23:09:54 +00:00
|
|
|
// Errors {{{
|
|
|
|
|
2016-05-19 14:41:06 +00:00
|
|
|
class Exception : public ::Exception
|
|
|
|
{
|
|
|
|
public:
|
2016-05-31 03:58:58 +00:00
|
|
|
explicit Exception(const std::string& msg) : ::Exception("[Alsa] "+ msg){}
|
2016-05-19 14:41:06 +00:00
|
|
|
};
|
|
|
|
|
2016-05-31 03:58:58 +00:00
|
|
|
class ControlInterfaceError : public Exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ControlInterfaceError(int code, const std::string& msg)
|
|
|
|
: Exception(msg +" ["+ std::to_string(code) +"]") {}
|
2016-05-19 14:41:06 +00:00
|
|
|
};
|
|
|
|
|
2016-06-09 23:09:54 +00:00
|
|
|
class MixerError : public Exception {
|
|
|
|
using Exception::Exception;
|
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
// ControlInterface {{{
|
|
|
|
|
2016-05-19 14:41:06 +00:00
|
|
|
class ControlInterface
|
|
|
|
{
|
2016-06-09 23:09:54 +00:00
|
|
|
concurrency::SpinLock lock;
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
snd_hctl_t *hctl;
|
|
|
|
snd_hctl_elem_t *elem;
|
|
|
|
|
|
|
|
snd_ctl_t *ctl;
|
|
|
|
snd_ctl_elem_info_t *info;
|
|
|
|
snd_ctl_elem_value_t *value;
|
|
|
|
snd_ctl_elem_id_t *id;
|
|
|
|
|
|
|
|
public:
|
2016-05-31 03:58:58 +00:00
|
|
|
explicit ControlInterface(int numid);
|
2016-05-19 14:41:06 +00:00
|
|
|
~ControlInterface();
|
2016-06-09 23:09:54 +00:00
|
|
|
ControlInterface(const ControlInterface &) = delete;
|
|
|
|
ControlInterface &operator=(const ControlInterface &) = delete;
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-05-31 03:58:58 +00:00
|
|
|
bool wait(int timeout = -1);
|
2016-06-09 23:09:54 +00:00
|
|
|
void process_events();
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-05-31 03:58:58 +00:00
|
|
|
bool test_device_plugged();
|
2016-05-19 14:41:06 +00:00
|
|
|
};
|
|
|
|
|
2016-06-09 23:09:54 +00:00
|
|
|
// }}}
|
|
|
|
// Mixer {{{
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
class Mixer
|
|
|
|
{
|
2016-06-09 23:09:54 +00:00
|
|
|
concurrency::SpinLock lock;
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
snd_mixer_t *hardware_mixer = nullptr;
|
|
|
|
snd_mixer_elem_t *mixer_element = nullptr;
|
|
|
|
|
|
|
|
public:
|
2016-05-31 03:58:58 +00:00
|
|
|
explicit Mixer(const std::string& mixer_control_name);
|
2016-05-19 14:41:06 +00:00
|
|
|
~Mixer();
|
2016-06-09 23:09:54 +00:00
|
|
|
Mixer(const Mixer &) = delete;
|
|
|
|
Mixer &operator=(const Mixer &) = delete;
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-05-31 03:58:58 +00:00
|
|
|
bool wait(int timeout = -1);
|
2016-06-09 23:09:54 +00:00
|
|
|
int process_events();
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
int get_volume();
|
|
|
|
void set_volume(float percentage);
|
|
|
|
void set_mute(bool mode);
|
|
|
|
void toggle_mute();
|
|
|
|
bool is_muted();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void error_handler(const std::string& message);
|
|
|
|
};
|
2016-06-09 23:09:54 +00:00
|
|
|
|
|
|
|
// }}}
|
2016-05-19 14:41:06 +00:00
|
|
|
}
|