OSDN Git Service

[OSD][SOUND][Qt] Move sound-drivers into separate directories.
[csp-qt/common_source_project-fm7.git] / source / src / qt / sound-drivers / osd_sound_mod_template.h
1 /*
2         Skelton for retropc emulator
3
4         Author : K.Ohta <whatisthis.sowhat _at_ gmail.com>
5         Date   : 2022.07.15-
6
7         [ OSD / Sound driver / Template ]
8 */
9
10 #pragma once
11
12 #include <QObject>
13 #include <mutex>
14 #include <memory>
15 #include <string>
16
17 #include "../../common.h"
18 #include "../../config.h"
19 #include "../osd_types.h"
20
21
22 #if !defined(__debug_log_func)
23 #define __debug_log_func(...) debug_log_func(__func__, __VA_ARGS__)
24 #endif
25
26
27
28 QT_BEGIN_NAMESPACE
29
30
31 class SOUND_BUFFER_QT;
32 class OSD_BASE;
33 class USING_FLAGS;
34 class CSP_Logger;
35
36 namespace SOUND_MODULE {
37 /* SOUND_MODULE */
38         enum class __FORMAT;
39         enum class __BYTEORDER;
40         
41         namespace OUTPUT {
42         /* SOUND_MODULE::OUTPUT */
43                 
44 class DLL_PREFIX M_BASE : public QObject
45 {
46         Q_OBJECT
47 protected:
48         OSD_BASE*                                                       m_OSD;
49         std::string                                                     m_classname;
50         
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;
55         
56         std::atomic<bool>                                       m_config_ok;
57         std::atomic<bool>                                       m_prev_started;
58         std::atomic<bool>                                       m_mute;
59
60         int64_t                                                         m_chunk_bytes;
61         int64_t                                                         m_buffer_bytes;
62         int64_t                                                         m_before_rendered;
63         int                                                                     m_samples;
64         
65         int                                                                     m_rate;
66         int                                                                     m_latency_ms;
67         int                                                                     m_channels;
68         size_t                                                          m_wordsize;
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;
73
74         virtual void update_driver_fileio()
75         {
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);
79                 // Optional:
80                 // connect(m_fileio.get(), SIGNAL(readyRead()), real_driver, SLOT, QObject::DirectConnection);
81         }
82
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);
86
87         config_t*                                        get_config_ptr();
88         
89         bool debug_log_func(const _TCHAR *_funcname, const _TCHAR *_fmt, ...);
90         bool debug_log(const _TCHAR *_fmt, ...);
91         
92 public:
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);
100         
101         ~M_BASE();
102
103         std::recursive_timed_mutex                              m_locker;
104
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()
108         {
109                 // AT LEAST:
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);
117
118                 // For Logging
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);
121                 return true;
122         }
123         virtual bool release_driver()
124         {
125                 // Maybe You should:
126                 // Stop driver,
127                 // then, m_fileio.reset() @ driver (not this).
128                 return true;
129         }
130         
131         virtual int64_t update_sound(void* datasrc, int samples);
132         
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()
136         {
137                 return m_fileio;
138         }
139         bool is_io_device_exists();
140         
141         virtual uint64_t wrote_data_to()
142         {
143                 return 0;
144         }
145         virtual int64_t driver_elapsed_usec()
146         {
147                 return 0;
148         }
149         virtual int64_t driver_processed_usec()
150         {
151                 return 0;
152         }
153         virtual bool check_elapsed_to_render();
154         virtual void update_render_point_usec();
155         virtual bool check_enough_to_render();
156         
157         bool config_ok()
158         {
159                 return m_config_ok.load();
160         }
161         
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();
166         int get_channels();
167         int get_sample_rate();
168         virtual __FORMAT get_sound_format();
169         
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();
175         
176         virtual M_BASE* get_real_driver()
177         {
178                 return dynamic_cast<SOUND_MODULE::OUTPUT::M_BASE*>(this);
179         }
180
181         virtual std::list<std::string> get_sound_devices_list()
182         {
183                 static std::list<std::string> dummy_list;
184                 return dummy_list;
185         }
186         
187         virtual const _TCHAR* get_sound_device_name(int num)
188         {
189                 return (const _TCHAR*)nullptr;
190         }
191         virtual const _TCHAR* get_current_device_name()
192         {
193                 return (const _TCHAR*)(_T("Empty"));
194         }
195         
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()
199         {
200                 return m_extconfig_ptr.load();
201         }
202         int get_extra_config_bytes()
203         {
204                 return m_extconfig_bytes.load();
205         }
206         virtual bool set_extra_config(void* p, int bytes);
207         virtual bool modify_extra_config(void* p, int& bytes);
208 public slots:
209         
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();
214         
215         virtual void update_config() {}
216         virtual void update_extra_config() {}
217         
218         bool start();
219         bool pause();
220         bool resume();
221         bool stop();
222         bool discard();
223
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()
228         {
229                 return true;
230         }
231         bool update_rate(int rate)
232         {
233                 return reconfig_sound(rate, m_channels);
234         }
235         bool update_channels(int channels)
236         {
237                 return reconfig_sound(m_rate, channels);
238         }
239         bool update_latency(int latency_ms, bool fortce = false);
240         bool reconfig_sound(int rate, int channels);
241         void request_to_release();
242         
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)
246         {
247                 return do_send_log(m_loglevel.load(), m_logdomain.load(), _str, maxlen);
248         }
249         virtual bool do_send_log(const QString _str)
250         {
251                 return do_send_log(m_loglevel.load(), m_logdomain.load(), _str);
252         }
253         
254         virtual void do_set_device_by_name(QString name) {};
255         virtual void do_set_device_by_name(const _TCHAR *name)
256         {
257                 if(name != nullptr) {
258                         do_set_device_by_name(QString::fromUtf8(name));
259                 }
260         }
261         virtual void do_set_device_by_name(const _TCHAR *name, int maxlen)
262         {
263                 if((name != nullptr) && (maxlen > 0)) {
264                         do_set_device_by_name(QString::fromUtf8(name, maxlen));
265                 }
266         }
267         virtual void do_set_device_by_number(int) {};
268         
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);
272
273         // From real driver: notify to update sound devices list.
274         virtual void do_update_device_list() {}
275         
276         virtual void set_osd(OSD_BASE* p);
277
278 signals:
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);
284         //
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();
291
292         void sig_set_volume(double);
293         // 
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);
302 };
303 /* SOUND_MODULE::OUTPUT */
304 }
305 /* SOUND_MODULE */
306 }
307 QT_END_NAMESPACE
308         
309