2 Skelton for retropc emulator
4 Author : K.Ohta <whatisthis.sowhat _at_ gmail.com>
7 [ OSD / Sound driver / Template ]
17 #include "../../common.h"
18 #include "../../config.h"
19 #include "../osd_types.h"
22 #if !defined(__debug_log_func)
23 #define __debug_log_func(...) debug_log_func(__func__, __VA_ARGS__)
31 class SOUND_BUFFER_QT;
36 namespace SOUND_MODULE {
39 enum class __BYTEORDER;
42 /* SOUND_MODULE::OUTPUT */
44 class DLL_PREFIX M_BASE : public QObject
49 std::string m_classname;
51 std::shared_ptr<SOUND_BUFFER_QT> m_fileio;
52 std::shared_ptr<SOUND_BUFFER_QT> m_driver_fileio;
53 std::shared_ptr<USING_FLAGS> m_using_flags;
54 std::shared_ptr<CSP_Logger> m_logger;
56 std::atomic<bool> m_config_ok;
57 std::atomic<bool> m_prev_started;
58 std::atomic<bool> m_mute;
60 int64_t m_chunk_bytes;
61 int64_t m_buffer_bytes;
62 int64_t m_before_rendered;
69 std::atomic<void*> m_extconfig_ptr;
70 std::atomic<int> m_extconfig_bytes;
71 std::atomic<int> m_loglevel;
72 std::atomic<int> m_logdomain;
74 virtual void update_driver_fileio()
76 // Update driver side of fileio by m_fileio;
77 //connect(m_fileio.get(), SIGNAL(bytesWritten(qint64)), real_driver, SLOT, QObject::DirectConnection);
78 //connect(m_fileio.get(), SIGNAL(aboutToClose()), real_driver, SLOT, QObject::DirectConnection);
80 // connect(m_fileio.get(), SIGNAL(readyRead()), real_driver, SLOT, QObject::DirectConnection);
83 // Maybe disconnect some signals via m_fileio.
84 virtual bool release_driver_fileio();
85 virtual bool real_reconfig_sound(int& rate,int& channels,int& latency_ms);
87 config_t* get_config_ptr();
89 bool debug_log_func(const _TCHAR *_funcname, const _TCHAR *_fmt, ...);
90 bool debug_log(const _TCHAR *_fmt, ...);
93 M_BASE(OSD_BASE *parent,
94 SOUND_BUFFER_QT* deviceIO = nullptr,
95 int base_rate = 48000,
96 int base_latency_ms = 100,
97 int base_channels = 2,
98 void *extra_config_values = nullptr,
99 int extra_config_bytes = 0);
103 std::recursive_timed_mutex m_locker;
105 virtual bool wait_driver_started(int64_t timeout_msec = INT64_MIN);
106 virtual bool wait_driver_stopped(int64_t timeout_msec = INT64_MIN);
107 virtual bool initialize_driver()
110 // connect(this, SIGNAL(sig_start_audio()), ..., QObject::QueuedConnection);
111 // connect(this, SIGNAL(sig_pause_audio()), ..., QObject::QueuedConnection);
112 // connect(this, SIGNAL(sig_resume_audio()), ..., QObject::QueuedConnection);
113 // connect(this, SIGNAL(sig_close_audio()), ..., QObject::QueuedConnection);
114 // connect(this, SIGNAL(sig_discard_audio()), ..., QObject::QueuedConnection);
115 // connect(this, SIGNAL(sig_released(bool)), ..., QObject::QueuedConnection);
116 // connect(this, SIGNAL(sig_req_open_sound(int, int, QString)), ..., QObject::QueuedConnection);
119 // connect(real_driver, SIGNAL(sig_log(QString)), this, SLOT(do_debug_log(QString)), QObject::QueuedConnection);
120 // connect(real_driver, SIGNAL(sig_log(int, int, QString)), this, SLOT(do_debug_log(int, int, QString)), QObject::QueuedConnection);
123 virtual bool release_driver()
127 // then, m_fileio.reset() @ driver (not this).
131 virtual int64_t update_sound(void* datasrc, int samples);
133 std::shared_ptr<SOUND_BUFFER_QT> set_io_device(SOUND_BUFFER_QT *p);
134 std::shared_ptr<SOUND_BUFFER_QT> set_io_device(std::shared_ptr<SOUND_BUFFER_QT> ps);
135 std::shared_ptr<SOUND_BUFFER_QT> get_io_device()
139 bool is_io_device_exists();
141 virtual uint64_t wrote_data_to()
145 virtual int64_t driver_elapsed_usec()
149 virtual int64_t driver_processed_usec()
153 virtual bool check_elapsed_to_render();
154 virtual void update_render_point_usec();
155 virtual bool check_enough_to_render();
159 return m_config_ok.load();
162 int64_t get_buffer_bytes();
163 int64_t get_chunk_bytes();
164 int get_sample_count() { return m_samples; }
165 int get_latency_ms();
167 int get_sample_rate();
168 virtual __FORMAT get_sound_format();
170 size_t get_word_size();
171 void get_buffer_parameters(int& channels, int& rate, int& latency_ms,
172 size_t& word_size, int& chunk_bytes, int& buffer_bytes);
173 virtual int64_t get_bytes_available();
174 virtual int64_t get_bytes_left();
176 virtual M_BASE* get_real_driver()
178 return dynamic_cast<SOUND_MODULE::OUTPUT::M_BASE*>(this);
181 virtual std::list<std::string> get_sound_devices_list()
183 static std::list<std::string> dummy_list;
187 virtual const _TCHAR* get_sound_device_name(int num)
189 return (const _TCHAR*)nullptr;
191 virtual const _TCHAR* get_current_device_name()
193 return (const _TCHAR*)(_T("Empty"));
196 virtual void set_logger(const std::shared_ptr<CSP_Logger> logger);
197 virtual void set_system_flags(const std::shared_ptr<USING_FLAGS> p);
198 void* get_extra_config_ptr()
200 return m_extconfig_ptr.load();
202 int get_extra_config_bytes()
204 return m_extconfig_bytes.load();
206 virtual bool set_extra_config(void* p, int bytes);
207 virtual bool modify_extra_config(void* p, int& bytes);
210 virtual void initialize_sound(int rate, int samples, int* presented_rate, int* presented_samples);
211 virtual void release_sound();
212 virtual void mute_sound();
213 virtual void stop_sound();
215 virtual void update_config() {}
216 virtual void update_extra_config() {}
224 virtual void reset_to_defalut() {}
225 virtual void set_volume(double level);
226 virtual void set_volume(int level);
227 virtual bool is_running_sound()
231 bool update_rate(int rate)
233 return reconfig_sound(rate, m_channels);
235 bool update_channels(int channels)
237 return reconfig_sound(m_rate, channels);
239 bool update_latency(int latency_ms, bool fortce = false);
240 bool reconfig_sound(int rate, int channels);
241 void request_to_release();
243 virtual bool do_send_log(int level, int domain, QString _str);
244 virtual bool do_send_log(int level, int domain, const _TCHAR* _str, int maxlen);
245 virtual bool do_send_log(const _TCHAR* _str, int maxlen)
247 return do_send_log(m_loglevel.load(), m_logdomain.load(), _str, maxlen);
249 virtual bool do_send_log(const QString _str)
251 return do_send_log(m_loglevel.load(), m_logdomain.load(), _str);
254 virtual void do_set_device_by_name(QString name) {};
255 virtual void do_set_device_by_name(const _TCHAR *name)
257 if(name != nullptr) {
258 do_set_device_by_name(QString::fromUtf8(name));
261 virtual void do_set_device_by_name(const _TCHAR *name, int maxlen)
263 if((name != nullptr) && (maxlen > 0)) {
264 do_set_device_by_name(QString::fromUtf8(name, maxlen));
267 virtual void do_set_device_by_number(int) {};
269 // This set device by device-name having QAction (as QObject).
270 virtual void do_set_device_by_name(void);
271 virtual void do_set_device_by_number(void);
273 // From real driver: notify to update sound devices list.
274 virtual void do_update_device_list() {}
276 virtual void set_osd(OSD_BASE* p);
279 // loglevel, logdomain, message
280 void sig_send_log(int, int, QString);
281 void sig_send_log(int, int, const _TCHAR*, int);
282 // rate, channels, path
283 void sig_req_open_sound(int, int, QString);
285 void sig_start_audio();
286 void sig_stop_audio();
287 void sig_pause_audio();
288 void sig_resume_audio();
289 void sig_close_audio();
290 void sig_discard_audio();
292 void sig_set_volume(double);
294 // notify completed to release sound driver.
295 void sig_released(bool);
296 // To UI: notify reset sound device list.
297 void sig_reset_sound_device_list();
298 // To UI: notify update sound device list #arg1 to #arg2.
299 void sig_set_sound_device(int, QString);
300 // To UI: notify adding sound device list #arg1.
301 void sig_add_sound_device(QString);
303 /* SOUND_MODULE::OUTPUT */