-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsoundcarddialog.cpp
165 lines (149 loc) · 3.99 KB
/
soundcarddialog.cpp
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
#include "soundcarddialog.h"
#include "ui_soundcarddialog.h"
#include "portaudio_helper.h"
SoundcardDialog::SoundcardDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SoundcardDialog)
{
ui->setupUi(this);
// populate comboboxes
PaDeviceIndex count = Pa_GetDeviceCount();
for(PaDeviceIndex idx=0; idx<count; idx++)
{
const PaDeviceInfo *info = Pa_GetDeviceInfo(idx);
QString deviceName = PA_Helper::getDeviceString(idx);
if (info->maxInputChannels > 0)
{
ui->inputComboBox->addItem(deviceName, QVariant(idx));
}
if (info->maxOutputChannels > 0)
{
ui->outputComboBox->addItem(deviceName, QVariant(idx));
}
}
checkSupport();
}
SoundcardDialog::~SoundcardDialog()
{
delete ui;
}
PaDeviceIndex SoundcardDialog::getInputSource()
{
int index = ui->inputComboBox->currentIndex();
if (index == -1)
{
// no item was selected, so we return the
// default input device
return Pa_GetDefaultInputDevice();
}
else
{
bool ok;
PaDeviceIndex idx = ui->inputComboBox->itemData(index).toInt(&ok);
if (!ok)
{
// conversion to int not succesfull
// return the default input device .. :(
return Pa_GetDefaultInputDevice();
}
return idx;
}
}
PaDeviceIndex SoundcardDialog::getOutputSource()
{
int index = ui->outputComboBox->currentIndex();
if (index == -1)
{
// no item was selected, so we return the
// default input device
return Pa_GetDefaultOutputDevice();
}
else
{
bool ok;
PaDeviceIndex idx = ui->outputComboBox->itemData(index).toInt(&ok);
if (!ok)
{
// conversion to int not succesfull
// return the default input device .. :(
return Pa_GetDefaultOutputDevice();
}
return idx;
}
}
void SoundcardDialog::setInputSource(PaDeviceIndex device)
{
int N = ui->inputComboBox->count();
for(int i=0; i<N; i++)
{
bool ok;
PaDeviceIndex idx = ui->inputComboBox->itemData(i).toInt(&ok);
if ((ok) && (idx == device))
{
ui->inputComboBox->setCurrentIndex(i);
return;
}
}
}
void SoundcardDialog::setOutputSource(PaDeviceIndex device)
{
int N = ui->outputComboBox->count();
for(int i=0; i<N; i++)
{
bool ok;
PaDeviceIndex idx = ui->outputComboBox->itemData(i).toInt(&ok);
if ((ok) && (idx == device))
{
ui->outputComboBox->setCurrentIndex(i);
return;
}
}
}
void SoundcardDialog::setSamplerate(float rate)
{
ui->sampleRate->setText(QString("%1").arg(rate,5,'d',0));
}
float SoundcardDialog::getSamplerate()
{
bool ok;
float rate = ui->sampleRate->text().toFloat(&ok);
if (!ok)
{
return 44100.0;
}
return rate;
}
void SoundcardDialog::checkSupport()
{
// check if the selected device can do what we want
PaSampleFormat sampleFormat = paFloat32;
PaStreamParameters inputParams;
PaStreamParameters outputParams;
memset(&inputParams, 0, sizeof(inputParams));
inputParams.device = getInputSource();
inputParams.suggestedLatency = 0.2f;
inputParams.channelCount = 2;
inputParams.sampleFormat = sampleFormat;
memset(&outputParams, 0, sizeof(outputParams));
outputParams.device = getOutputSource();
outputParams.suggestedLatency = 0.2f;
outputParams.channelCount = 2;
outputParams.sampleFormat = sampleFormat;
PaError err = Pa_IsFormatSupported(&inputParams, &outputParams, getSamplerate());
if (err==paFormatIsSupported)
{
ui->warningText->setText("");
}
else
{
ui->warningText->setText("Format NOT supported");
}
}
void SoundcardDialog::on_inputComboBox_currentIndexChanged(int index)
{
checkSupport();
}
void SoundcardDialog::on_outputComboBox_currentIndexChanged(int index)
{
checkSupport();
}