OSDN Git Service

[OSD][Qt][SOUND][CONFIG] Important: Now, sound output device has recorded as NAME...
[csp-qt/common_source_project-fm7.git] / source / src / qt / gui / menu_sound.cpp
1 /*
2  * Common Source Project/ Qt
3  * (C) 2015 K.Ohta <whatisthis.sowhat _at_ gmail.com>
4  *  Qt: Menu->Sound Implementations.
5  *  History: Jan 14, 2015 : Initial
6  */
7
8 #include <QImageReader>
9 #include <QImage>
10 #include <QMenu>
11 #include <QActionGroup>
12
13 #include <cstdint>
14
15 #include "commonclasses.h"
16 #include "mainwidget_base.h"
17 //#include "menuclasses.h"
18 #include "sound_dialog.h"
19 #include "menu_flags.h"
20
21
22 // WIP: Will move to another file
23 const double s_late_table[5] = {0.05, 0.1, 0.2, 0.3, 0.4};
24
25 void Ui_MainWindowBase::do_update_sound_output_list(void)
26 {
27         if((p_config == nullptr) || (using_flags == nullptr)) return;
28         int _match = -1;
29         QString matched_devname = QString::fromUtf8("Default");
30         do_set_host_sound_name(0, QString::fromUtf8("Default"));
31         
32         if(action_HostSoundDevice[0] != nullptr) {
33                 action_HostSoundDevice[0]->setChecked(true);
34         }
35         QString _setname = QString::fromLocal8Bit(p_config->sound_device_name);
36         _setname.truncate(1023);
37         for(int i = 1; i < 16; i++) {
38                 if(action_HostSoundDevice[i] != nullptr) {
39                         action_HostSoundDevice[i]->setChecked(false);
40                 }
41                 const _TCHAR* sp = using_flags->get_sound_device_name(i - 1);
42                 QString sname;
43                 sname.clear();
44                 if(sp != NULL) {
45                         sname = QString::fromUtf8(sp);
46                         sname.truncate(1023);
47                 }
48                 
49                 if(sname == _setname) {
50                         _match = i;
51                         matched_devname = sname;
52                 }
53                 do_set_host_sound_name(i, sname);
54         }
55         if(_match > 0) {
56                 if(action_HostSoundDevice[_match] != nullptr) {
57                         action_HostSoundDevice[_match]->setChecked(true);
58                 }
59         } else {
60                 if(action_HostSoundDevice[0] != nullptr) {
61                         action_HostSoundDevice[0]->setChecked(true);
62                 }
63         }
64         emit sig_osd_sound_output_device(matched_devname);
65 }
66
67 void Ui_MainWindowBase::do_set_host_sound_output_device(void)
68 {
69         QAction *cp = qobject_cast<QAction*>(QObject::sender());
70         if(cp == nullptr) return;
71         QString _name = cp->data().value<QString>();
72         
73         if(p_config != NULL) {
74                 QString _old = QString::fromLocal8Bit(p_config->sound_device_name, 1023);
75                 _name.truncate(1023);
76                 if((_old != _name) && !(_name.isEmpty())) {
77                         my_tcscpy_s(p_config->sound_device_name, 1023, _name.toLocal8Bit().constData());
78                         emit sig_osd_sound_output_device(_name);
79                 }
80         }
81         emit sig_emu_update_config();
82 }
83
84 void Ui_MainWindowBase::do_set_host_sound_name(int num, QString s)
85 {
86         if(num < 0) return;
87         if(num >= 16) return;
88         
89         if(s.isEmpty()) {
90                 if(action_HostSoundDevice[num] != NULL) {
91                         action_HostSoundDevice[num]->setVisible(false);
92                         QVariant v = QVariant(QString::fromUtf8(""));
93                         action_HostSoundDevice[num]->setData(v);
94                 }
95         } else {
96                 if(action_HostSoundDevice[num] != NULL) {
97                         action_HostSoundDevice[num]->setVisible(true);
98                         action_HostSoundDevice[num]->setText(s);
99                         QVariant v = QVariant(s);
100                         action_HostSoundDevice[num]->setData(v);
101                 }
102         }
103 }
104
105 void Ui_MainWindowBase::do_set_sound_strict_rendering(bool f)
106 {
107         if(using_flags != NULL) {
108                 p_config->sound_strict_rendering = f;
109         }
110 }
111
112 void Ui_MainWindowBase::do_set_sound_tape_signal(bool f)
113 {
114         if(using_flags != NULL) {
115                 p_config->sound_tape_signal = f;
116         }
117 }
118
119 void Ui_MainWindowBase::do_set_sound_tape_voice(bool f)
120 {
121         if(using_flags != NULL) {
122                 p_config->sound_tape_voice = f;
123         }
124 }
125
126
127 void Ui_MainWindowBase::rise_volume_dialog(void)
128 {
129         Ui_SoundDialog *dlg = new Ui_SoundDialog(using_flags, this);
130         QString tmps, s_val;
131         float n;
132         QIcon  img = QIcon(":/icon_speaker.png");
133         
134         dlg->setWindowIcon(img);
135         this->retranslateVolumeLabels(dlg);
136
137         n = (float)(((p_config->general_sound_level + 32768) * 1000) / 65535) / 10.0;
138         s_val.setNum(n, 'f', 1);
139         tmps = QApplication::translate("Ui_SoundDialog", "Set Volume", 0);
140         tmps = tmps + QString::fromUtf8(" (") + s_val + QString::fromUtf8("%)");
141         dlg->setWindowTitle(tmps);
142         dlg->show();
143         //dlg->exec();
144 }
145
146 void Ui_MainWindowBase::retranslateVolumeLabels(Ui_SoundDialog *)
147 {
148 }
149
150 void Ui_MainWindowBase::CreateSoundMenu(void)
151 {
152         if(using_flags->is_without_sound()) return;
153         int i;
154         //  menuRecord = new QMenu(menuSound);
155         //  menuRecord->setObjectName(QString::fromUtf8("menuRecord_Sound"));
156   
157         menuSound->addAction(actionStart_Record);
158         menuSound->addSeparator();
159         SET_ACTION_CHECKABLE_SINGLE_CONNECT(menuSound, actionSoundStrictRendering,
160                                                                                 "actionSoundStrictRendering", p_config->sound_strict_rendering,
161                                                                                 SIGNAL(toggled(bool)), SLOT(do_set_sound_strict_rendering(bool)));
162         SET_ACTION_CHECKABLE_SINGLE_CONNECT(menuSound, actionSoundTapeSignal,
163                                                                                 "actionSoundTapeSignal", p_config->sound_tape_signal,
164                                                                                 SIGNAL(toggled(bool)), SLOT(do_set_sound_tape_signal(bool)));
165         SET_ACTION_CHECKABLE_SINGLE_CONNECT(menuSound, actionSoundTapeVoice,
166                                                                                 "actionSoundTapeVoice", p_config->sound_tape_voice,
167                                                                                 SIGNAL(toggled(bool)), SLOT(do_set_sound_tape_voice(bool)));
168         
169         //actionSoundStrictRendering = new Action_Control(this, using_flags);
170         //actionSoundStrictRendering->setObjectName(QString::fromUtf8("actionSoundStrictRendering"));
171         //actionSoundStrictRendering->setCheckable(true);
172         //if(p_config->sound_strict_rendering) actionSoundStrictRendering->setChecked(true);
173         //connect(actionSoundStrictRendering, SIGNAL(toggled(bool)),
174         //              this, SLOT(do_set_sound_strict_rendering(bool)));
175         //menuSound->addAction(actionSoundStrictRendering);
176
177         menuSound_HostDevices = new QMenu(menuSound);
178         menuSound_HostDevices->setObjectName(QString::fromUtf8("menuSound_HostDevices"));
179         menuSound->addAction(menuSound_HostDevices->menuAction());
180         for(i = 0; i < 16; i++) {
181                 if(action_HostSoundDevice[i] != NULL) {
182                         menuSound_HostDevices->addAction(action_HostSoundDevice[i]);
183                         connect(action_HostSoundDevice[i], SIGNAL(triggered()),
184                                         this, SLOT(do_set_host_sound_output_device()));
185                 }
186         }
187         
188         menuSound->addSeparator();
189         
190         menuOutput_Frequency = new QMenu(menuSound);
191         menuOutput_Frequency->setObjectName(QString::fromUtf8("menuOutput_Frequency"));
192         menuSound->addAction(menuOutput_Frequency->menuAction());
193         for(i = 0; i < 8; i++) {
194                 menuOutput_Frequency->addAction(action_Freq[i]);
195                 connect(action_Freq[i], SIGNAL(triggered()),
196                         this, SLOT(do_set_freq()));
197         }
198         menuSound_Latency = new QMenu(menuSound);
199         menuSound_Latency->setObjectName(QString::fromUtf8("menuSound_Latency"));
200         menuSound->addAction(menuSound_Latency->menuAction());
201         for(i = 0; i < 5; i++) {
202                 menuSound_Latency->addAction(action_Latency[i]);
203                 connect(action_Latency[i], SIGNAL(triggered()),
204                         this, SLOT(do_set_latency()));
205         }
206
207         menuSound->addSeparator();
208         if(using_flags->is_use_sound_files_fdd()) {
209                 SET_ACTION_SINGLE(action_SoundFilesFDD, true, true, (p_config->sound_noise_fdd != 0));
210         }
211         if(using_flags->is_use_sound_files_relay()) {
212                 SET_ACTION_SINGLE(action_SoundFilesRelay, true, true, (p_config->sound_noise_cmt != 0));
213         }
214         if(using_flags->is_use_sound_files_fdd() || using_flags->is_use_sound_files_relay()) {
215                 if(using_flags->is_use_sound_files_fdd())     menuSound->addAction(action_SoundFilesFDD);
216                 if(using_flags->is_use_sound_files_relay())   menuSound->addAction(action_SoundFilesRelay);
217                 menuSound->addSeparator();
218         }
219         menuSound->addAction(action_VolumeDialog);
220 }
221
222 void Ui_MainWindowBase::ConfigSoundMenu(void)
223 {
224         if(using_flags->is_without_sound()) return;
225         int i;
226         QString tmps;
227         double dval;
228         //int freq = 48000;
229
230         actionGroup_Sound_HostDevices = new QActionGroup(this);
231         for(i = 0; i < 16; i++) {
232                 action_HostSoundDevice[i] = new Action_Control(this, using_flags);
233                 tmps.setNum(i); 
234                 tmps = QString::fromUtf8("action_HostSoundDevice") + tmps;
235                 action_HostSoundDevice[i]->setObjectName(tmps);
236                 action_HostSoundDevice[i]->setCheckable(true);
237                 QVariant v = QVariant(QString::fromUtf8(""));
238                 action_HostSoundDevice[i]->setData(v);
239                 actionGroup_Sound_HostDevices->addAction(action_HostSoundDevice[i]);
240         }
241         
242         actionGroup_Sound_Freq = new QActionGroup(this);
243         actionGroup_Sound_Freq->setExclusive(true);
244         for(i = 0; i < 8; i++) {
245                 action_Freq[i] = new Action_Control(this, using_flags);
246                 int _freq = using_flags->get_s_freq_table(i);
247                 tmps.setNum(_freq);
248                 tmps = QString::fromUtf8("action") + tmps + QString::fromUtf8("Hz");
249                 action_Freq[i]->setObjectName(tmps);
250                 action_Freq[i]->setCheckable(true);
251                 action_Freq[i]->setData(QVariant(i));
252                 if(i == p_config->sound_frequency) {
253                         action_Freq[i]->setChecked(true);
254                         //freq = using_flags->get_s_freq_table(i);
255                 }
256                 actionGroup_Sound_Freq->addAction(action_Freq[i]);
257         }
258         actionGroup_Sound_Latency = new QActionGroup(this);
259         actionGroup_Sound_Latency->setExclusive(true);
260
261         for(i = 0; i < 5; i++) {
262                 action_Latency[i] = new Action_Control(this, using_flags);
263                 dval = s_late_table[i];
264                 dval = dval * 1000.0;
265                 tmps.setNum(dval);
266                 tmps = QString::fromUtf8("action") + tmps + QString::fromUtf8("ms");
267                 action_Latency[i]->setObjectName(tmps);
268                 action_Latency[i]->setCheckable(true);
269                 action_Latency[i]->setData(QVariant(i));
270                 if(i == p_config->sound_latency) action_Latency[i]->setChecked(true);
271                 actionGroup_Sound_Latency->addAction(action_Latency[i]);
272         }
273
274         SET_ACTION_SINGLE(actionStart_Record, true, true, false);
275         //actionStart_Record = new Action_Control(this, using_flags);
276         //actionStart_Record->setObjectName(QString::fromUtf8("actionStart_Record"));
277         //actionStart_Record->setCheckable(true);
278         //actionStart_Record->setChecked(false);
279         connect(actionStart_Record, SIGNAL(toggled(bool)), this, SLOT(start_record_sound(bool)));
280
281         action_VolumeDialog = new Action_Control(this, using_flags);
282         connect(action_VolumeDialog, SIGNAL(triggered()), this, SLOT(rise_volume_dialog()));
283         action_VolumeDialog->setObjectName(QString::fromUtf8("actionVolumedialog"));
284 }
285
286 void Ui_MainWindowBase::do_update_volume(int level)
287 {
288         if(using_flags->is_without_sound()) return;
289         if(level <= INT16_MIN) {
290                 action_VolumeDialog->setIcon(VolumeMutedIcon);
291         } else if(level < -4096) {
292                 action_VolumeDialog->setIcon(VolumeLowIcon);
293         } else if(level < 16384) {
294                 action_VolumeDialog->setIcon(VolumeMidIcon);
295         } else {
296                 action_VolumeDialog->setIcon(VolumeHighIcon);
297         }
298         emit sig_update_master_volume(level);
299 }
300         
301 void Ui_MainWindowBase::retranslateSoundMenu(void)
302 {
303         int i;
304         QString tmps;
305         double dval;
306         if(using_flags->is_without_sound()) return;
307   
308         for(i = 0; i < 8; i++) {
309                 tmps.setNum(using_flags->get_s_freq_table(i));
310                 tmps = tmps + QApplication::translate("MenuSound", "Hz", 0);
311                 action_Freq[i]->setText(tmps);
312         }
313         for(i = 0; i < 5; i++) {
314                 dval = s_late_table[i];
315                 dval = dval * 1000.0;
316                 tmps.setNum((int)dval);
317                 tmps = tmps + QApplication::translate("MenuSound", "mSec", 0);
318                 action_Latency[i]->setText(tmps);
319         }
320         actionStart_Record->setIcon(RecordSoundIcon);
321         actionStart_Record->setText(QApplication::translate("MenuSound", "Start Recording Sound", 0));
322         actionStart_Record->setToolTip(QApplication::translate("MenuSound", "Record sound as WAV file.", 0));
323         
324         actionSoundStrictRendering->setText(QApplication::translate("MenuSound", "Strict Rendering", 0));
325         actionSoundStrictRendering->setToolTip(QApplication::translate("MenuSound", "Rendering per a sample.Select to slower, but accurate rendering sound.", 0));
326         actionSoundTapeSignal->setText(QApplication::translate("MenuSound", "Play CMT Signal", 0));
327         actionSoundTapeSignal->setToolTip(QApplication::translate("MenuSound", "Play Signal from CMTs.", 0));
328
329         actionSoundTapeVoice->setText(QApplication::translate("MenuSound", "Play CMT Voice", 0));
330         actionSoundTapeVoice->setToolTip(QApplication::translate("MenuSound", "Play Audio/Voice from CMTs.", 0));
331         
332         if(using_flags->is_tape_binary_only()) {
333                 actionSoundTapeSignal->setEnabled(false);
334                 actionSoundTapeVoice->setEnabled(false);
335                 actionSoundTapeSignal->setVisible(false);
336                 actionSoundTapeVoice->setVisible(false);
337         }
338
339         menuSound_HostDevices->setTitle(QApplication::translate("MenuSound", "Output to:", 0));
340         menuSound_HostDevices->setToolTip(QApplication::translate("MenuSound", "Select sound device to output.\nThis effects after re-start this emulator.", 0));
341         
342         menuSound->setTitle(QApplication::translate("MenuSound", "Sound", 0));
343         menuOutput_Frequency->setTitle(QApplication::translate("MenuSound", "Output Frequency", 0));
344         menuSound_Latency->setTitle(QApplication::translate("MenuSound", "Sound Latency", 0));
345         
346         action_VolumeDialog->setText(QApplication::translate("MenuSound", "Set Volumes", 0));
347         action_VolumeDialog->setToolTip(QApplication::translate("MenuSound", "Open a VOLUME dialog.", 0));
348         if(using_flags->is_use_sound_files_fdd()) {
349                 action_SoundFilesFDD->setText(QApplication::translate("MenuSound", "Sound FDD Seek", 0));
350                 action_SoundFilesFDD->setToolTip(QApplication::translate("MenuSound", "Enable FDD HEAD seeking sound.\nNeeds sound file.\nSee HELP->READMEs->Bios and Key assigns", 0));
351         }
352         if(using_flags->is_use_sound_files_relay()) {
353                 action_SoundFilesRelay->setText(QApplication::translate("MenuSound", "Sound CMT Relay and Buttons", 0));
354                 action_SoundFilesRelay->setToolTip(QApplication::translate("MenuSound", "Enable CMT relay's sound and buttons's sounds.\nNeeds sound file.\nSee HELP->READMEs->Bios and Key assigns", 0));
355                 if(using_flags->is_tape_binary_only()) action_SoundFilesRelay->setEnabled(false);
356         }
357
358         menuSound->setToolTipsVisible(true);
359         do_update_volume(p_config->general_sound_level);
360 }