aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/qt_sdl/AudioSettingsDialog.cpp
blob: 8e08ef2bc4e0403f070cfc93d115c7bbba8ad2ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
    Copyright 2016-2023 melonDS team

    This file is part of melonDS.

    melonDS is free software: you can redistribute it and/or modify it under
    the terms of the GNU General Public License as published by the Free
    Software Foundation, either version 3 of the License, or (at your option)
    any later version.

    melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with melonDS. If not, see http://www.gnu.org/licenses/.
*/

#include <stdio.h>
#include <SDL2/SDL.h>
#include <QFileDialog>

#include "types.h"
#include "Platform.h"
#include "Config.h"
#include "NDS.h"
#include "DSi.h"
#include "DSi_I2C.h"

#include "AudioSettingsDialog.h"
#include "ui_AudioSettingsDialog.h"
#include "main.h"

using namespace melonDS;
AudioSettingsDialog* AudioSettingsDialog::currentDlg = nullptr;

extern std::string EmuDirectory;


AudioSettingsDialog::AudioSettingsDialog(QWidget* parent, bool emuActive, EmuThread* emuThread) : QDialog(parent), ui(new Ui::AudioSettingsDialog), emuThread(emuThread)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);

    oldInterp = Config::AudioInterp;
    oldBitDepth = Config::AudioBitDepth;
    oldVolume = Config::AudioVolume;
    oldDSiSync = Config::DSiVolumeSync;

    ui->cbInterpolation->addItem("None");
    ui->cbInterpolation->addItem("Linear");
    ui->cbInterpolation->addItem("Cosine");
    ui->cbInterpolation->addItem("Cubic");
    ui->cbInterpolation->addItem("Gaussian (SNES)");
    ui->cbInterpolation->setCurrentIndex(Config::AudioInterp);

    ui->cbBitDepth->addItem("Automatic");
    ui->cbBitDepth->addItem("10-bit");
    ui->cbBitDepth->addItem("16-bit");
    ui->cbBitDepth->setCurrentIndex(Config::AudioBitDepth);

    bool state = ui->slVolume->blockSignals(true);
    ui->slVolume->setValue(Config::AudioVolume);
    ui->slVolume->blockSignals(state);

    ui->chkSyncDSiVolume->setChecked(Config::DSiVolumeSync);

    // Setup volume slider accordingly
    if (emuActive && emuThread->NDS->ConsoleType == 1)
    {
        on_chkSyncDSiVolume_clicked(Config::DSiVolumeSync);
    }
    else
    {
        ui->chkSyncDSiVolume->setEnabled(false);
    }
    bool isext = (Config::MicInputType == 1);
    ui->cbMic->setEnabled(isext);

    const int count = SDL_GetNumAudioDevices(true);
    for (int i = 0; i < count; i++)
    {
        ui->cbMic->addItem(SDL_GetAudioDeviceName(i, true));
    }
    if (Config::MicDevice == "" && count > 0)
    {
        Config::MicDevice = SDL_GetAudioDeviceName(0, true);
    }

    ui->cbMic->setCurrentText(QString::fromStdString(Config::MicDevice));

    grpMicMode = new QButtonGroup(this);
    grpMicMode->addButton(ui->rbMicNone,     micInputType_Silence);
    grpMicMode->addButton(ui->rbMicExternal, micInputType_External);
    grpMicMode->addButton(ui->rbMicNoise,    micInputType_Noise);
    grpMicMode->addButton(ui->rbMicWav,      micInputType_Wav);
    connect(grpMicMode, SIGNAL(buttonClicked(int)), this, SLOT(onChangeMicMode(int)));
    grpMicMode->button(Config::MicInputType)->setChecked(true);

    ui->txtMicWavPath->setText(QString::fromStdString(Config::MicWavPath));

    bool iswav = (Config::MicInputType == micInputType_Wav);
    ui->txtMicWavPath->setEnabled(iswav);
    ui->btnMicWavBrowse->setEnabled(iswav);

    int inst = Platform::InstanceID();
    if (inst > 0)
    {
        ui->lblInstanceNum->setText(QString("Configuring settings for instance %1").arg(inst+1));
        ui->cbInterpolation->setEnabled(false);
        ui->cbBitDepth->setEnabled(false);
        for (QAbstractButton* btn : grpMicMode->buttons())
            btn->setEnabled(false);
        ui->txtMicWavPath->setEnabled(false);
        ui->btnMicWavBrowse->setEnabled(false);
        ui->cbMic->setEnabled(false);
    }
    else
        ui->lblInstanceNum->hide();
}

AudioSettingsDialog::~AudioSettingsDialog()
{
    delete ui;
}

void AudioSettingsDialog::onSyncVolumeLevel()
{
    if (Config::DSiVolumeSync && emuThread->NDS->ConsoleType == 1)
    {
        auto& dsi = static_cast<DSi&>(*emuThread->NDS);
        bool state = ui->slVolume->blockSignals(true);
        ui->slVolume->setValue(dsi.I2C.GetBPTWL()->GetVolumeLevel());
        ui->slVolume->blockSignals(state);
    }
}

void AudioSettingsDialog::onConsoleReset()
{
    on_chkSyncDSiVolume_clicked(Config::DSiVolumeSync);
    ui->chkSyncDSiVolume->setEnabled(emuThread->NDS->ConsoleType == 1);
}

void AudioSettingsDialog::on_AudioSettingsDialog_accepted()
{
    Config::MicDevice = ui->cbMic->currentText().toStdString();
    Config::MicInputType = grpMicMode->checkedId();
    Config::MicWavPath = ui->txtMicWavPath->text().toStdString();
    Config::Save();

    closeDlg();
}

void AudioSettingsDialog::on_AudioSettingsDialog_rejected()
{
    Config::AudioInterp = oldInterp;
    Config::AudioBitDepth = oldBitDepth;
    Config::AudioVolume = oldVolume;
    Config::DSiVolumeSync = oldDSiSync;

    closeDlg();
}

void AudioSettingsDialog::on_cbBitDepth_currentIndexChanged(int idx)
{
    // prevent a spurious change
    if (ui->cbBitDepth->count() < 3) return;

    Config::AudioBitDepth = ui->cbBitDepth->currentIndex();

    emit updateAudioSettings();
}

void AudioSettingsDialog::on_cbInterpolation_currentIndexChanged(int idx)
{
    // prevent a spurious change
    if (ui->cbInterpolation->count() < 5) return;

    Config::AudioInterp = ui->cbInterpolation->currentIndex();

    emit updateAudioSettings();
}

void AudioSettingsDialog::on_slVolume_valueChanged(int val)
{
    if (Config::DSiVolumeSync && emuThread->NDS->ConsoleType == 1)
    {
        auto& dsi = static_cast<DSi&>(*emuThread->NDS);
        dsi.I2C.GetBPTWL()->SetVolumeLevel(val);
        return;
    }

    Config::AudioVolume = val;
}

void AudioSettingsDialog::on_chkSyncDSiVolume_clicked(bool checked)
{
    Config::DSiVolumeSync = checked;

    bool state = ui->slVolume->blockSignals(true);
    if (Config::DSiVolumeSync && emuThread->NDS->ConsoleType == 1)
    {
        auto& dsi = static_cast<DSi&>(*emuThread->NDS);
        ui->slVolume->setMaximum(31);
        ui->slVolume->setValue(dsi.I2C.GetBPTWL()->GetVolumeLevel());
        ui->slVolume->setPageStep(4);
        ui->slVolume->setTickPosition(QSlider::TicksBelow);
    }
    else
    {
        Config::AudioVolume = oldVolume;
        ui->slVolume->setMaximum(256);
        ui->slVolume->setValue(Config::AudioVolume);
        ui->slVolume->setPageStep(16);
        ui->slVolume->setTickPosition(QSlider::NoTicks);
    }
    ui->slVolume->blockSignals(state);
}

void AudioSettingsDialog::onChangeMicMode(int mode)
{
    bool iswav = (mode == 3);
    bool isext = (mode == 1);
    ui->txtMicWavPath->setEnabled(iswav);
    ui->btnMicWavBrowse->setEnabled(iswav);
    ui->cbMic->setEnabled(isext);
}

void AudioSettingsDialog::on_btnMicWavBrowse_clicked()
{
    QString file = QFileDialog::getOpenFileName(this,
                                                "Select WAV file...",
                                                QString::fromStdString(EmuDirectory),
                                                "WAV files (*.wav);;Any file (*.*)");

    if (file.isEmpty()) return;

    ui->txtMicWavPath->setText(file);
}