OSDN Git Service

9dd3efd9d286b0765a508ba0f86651690dfe8f75
[csp-qt/common_source_project-fm7.git] / source / src / qt / 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
16 #include "../common.h"
17 #include "./sound_buffer_qt.h"
18
19 QT_BEGIN_NAMESPACE
20
21 class OSD_BASE;
22 class USING_FLAGS;
23 class CSP_Logger;
24
25 class DLL_PREFIX SOUND_OUTPUT_MODULE_BASE : public QObject
26 {
27         Q_OBJECT
28 protected:
29         OSD_BASE    *m_OSD;
30         std::shared_ptr<USING_FLAGS> m_using_flags;
31         std::shared_ptr<CSP_Logger>  m_logger;
32
33         std::atomic_bool    m_config_ok;
34         std::atomic<void *> m_extconfig;
35         std::atomic<int>        m_rate;
36         std::atomic<int>        m_channels;
37         std::atomic<int>        m_latency_ms;
38         std::recursive_mutex             m_locker;
39         std::atomic<int>        m_loglevel;
40         std::atomic<int>        m_logdomain;
41         std::string                     m_device_name;
42         
43         bool                            m_initialized;
44         
45 public:
46         SOUND_OUTPUT_MODULE_BASE(
47                 OSD_BASE *parent,
48                 _TCHAR* device_name,
49                 const std::shared_ptr<CSP_Logger> logger,
50                 const std::shared_ptr<USING_FLAGS> pflags,
51                 int base_rate = 48000,
52                 int base_latency_ms = 100,
53                 int base_channels = 2,
54                 void *extra_config_values = nullptr);
55         ~SOUND_OUTPUT_MODULE_BASE();
56
57         virtual bool initialize_driver() { return true; }
58         virtual bool release_driver() { return true; }
59         int get_sound_rate()
60         {
61                 return m_rate.load();
62         }
63         int get_latency()
64         {
65                 return m_latency_ms.load();
66         }
67         int get_channels()
68         {
69                 return m_channels.load();
70         }
71         virtual bool real_reconfig_sound(int& rate,int& channels,int& latency_ms)
72         {
73                 return true;
74         }
75         template <class... Args>
76                 bool debug_log(Args... args)
77         {
78                 _TCHAR buf[512] = {0};
79                 my_sprintf_s(buf, sizeof(buf) - 1, args);
80                 return do_send_log(m_loglevel.load(), m_logdomain.load(), (const _TCHAR*)buf, (sizeof(buf) / sizeof(_TCHAR)) - 1);
81         }
82         template <class... Args>
83                 bool debug_log(imt level, int domain, Args... args)
84         {
85                 _TCHAR buf[512] = {0};
86                 my_sprintf_s(buf, sizeof(buf) - 1, args);
87                 return do_send_log(level, domain, (const _TCHAR*)buf, (sizeof(buf) / sizeof(_TCHAR)) - 1);
88         }
89         bool config_ok()
90         {
91                 return m_config_ok.load();
92         }
93         virtual std::list<std::string> get_sound_devices_list()
94         {
95                 static std::list<std::string> dummy_list;
96                 return dummy_list;
97         }
98         const _TCHAR* get_current_device_name()
99         {
100                 return (const _TCHAR*)(m_device_name.c_str());
101         }
102         virtual void set_logger(const std::shared_ptr<CSP_Logger> logger);
103         virtual void set_system_flags(const std::shared_ptr<USING_FLAGS> p);
104
105 public slot:
106         bool update_rate(int& rate)
107         {
108                 return reconfig_sound(rate, m_channels, m_latency_ms);
109         }
110         bool update_latency(int& latency_ms)
111         {
112                 return reconfig_sound(m_rate, m_channels, latency_ms);
113         }
114         bool update_channels(int& channels)
115         {
116                 return reconfig_sound(rate, m_channels, m_latency_ms);
117         }
118         bool reconfig_sound(int& rate, int& channels, int& latency_ms)
119         {
120                 // ToDo
121                 std::lock_guard<std::recursive_mutex> locker(m_locker);
122                 if((rate != m_rate.load()) || (channels != m_channels.load()) || (latency_ms != m_latency_ms.load())) {
123                         if(real_reconfig_sound(rate, channels, latency_ms)) {
124                                 m_rate = rate;
125                                 m_channels = channels;
126                                 m_latency_ms = latency_ms;
127                                 m_config_ok = true;
128                                 return true;
129                         }
130                 }
131                 return false;
132         }
133         void request_to_release();
134         virtual const std::string set_device_sound(const _TCHAR* driver_name, int& rate,int& channels,int& latency_ms)
135         {
136                 return std::string(_T("Empty Device"));
137         }
138         virtual bool do_send_log(imt level, int domain, const _TCHAR* str, int maxlen);
139         virtual void do_set_device_by_name(QString) {};
140         virtual void do_set_device_by_number(int) {};
141         virtual void do_set_device_from_sender_object(void) {};
142         
143         virtual void initialize_sound(int rate, int samples, int* presented_rate, int* presented_samples) {}
144         virtual void release_sound() {}
145
146         virtual void update_sound(int* extra_frames) {}
147
148         virtual void mute_sound() {}
149         virtual void stop_sound() {}
150
151         // *PURE* SLOTS
152         virtual void do_start_recording_sound() {}
153         virtual void do_stop_recording_sound() {}
154         virtual void do_restart_recording_sound() {}
155         virtual void do_request_capture_sound(int ch) {}
156         
157         virtual void set_osd(OSD_BASE* p);
158         virtual void update_extra_config(void* p);
159         virtual int result_opening_external_file();
160         {
161                 return 0;
162         }
163         virtual int64_t wrote_data_to()
164         {
165                 return 0;
166         }
167         virtual bool result_closing_external_file()
168         {
169                 return true;
170         }
171 signals:
172         // loglevel, logdomain, message
173         void sig_send_log(int, int, QString);
174         // rate, channels, path
175         void sig_req_open_sound(int, int, QString);
176         // 
177         void sig_req_close_sound();
178         // samples, channel, data
179         void sig_send_output_sound_data(int64_t, int, int16_t[]);
180         // update device list for ui
181         void sig_clear_sound_device();
182         // add/update device, number and name. If number < 0 append.
183         void sig_add_sound_device_name(int, QString);
184         // Send current device number and name to UI. 
185         void sig_send_current_device_description(int, QString);
186         // Notify updating devices list to UI. size, current_device_number, need_to_reconfig 
187         void sig_notify_update_devices_list(int, int, bool);
188         // notify device changed status, true = success.
189         void sig_notify_changed_device_status(bool);
190         // notify completed to release sound driver.
191         void sig_released(bool);                
192 };
193
194 QT_END_NAMESPACE
195