-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioWall.cpp
384 lines (346 loc) · 11.4 KB
/
AudioWall.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "BaseWidget.h"
#include "AudioWall.h"
#include "AudioTask.h"
#include "WAVFile.h"
#include <QFile>
#include <QAudioFormat>
#include <QAudioOutput>
#include <QAudioDeviceInfo>
#include <QMediaPlayer>
#include <QToolTip>
#include <QComboBox>
#include <QDebug>
#include <QFileDialog>
#include <QFont>
#include <QLabel>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QEvent>
#include <QMenu>
#include <QMessageBox>
#include <QPalette>
#include <QPushButton>
#include <QtCore/QUrl>
#include <QUrl>
#include <QWidget>
//extern "C"
//{
//#include <libavcodec\avcodec.h>
//#include <libavformat\avformat.h>
//#include <libswscale\swscale.h>
//#include <libswresample\swresample.h>
//}
AudioWall::AudioWall(QWidget *parent) : BaseWidget(parent)
{
setWindowTitle(QString::fromLocal8Bit("音频输出窗口"));
setFixedSize(650, 500);
nth = 0;
layout = new QVBoxLayout(this);
layout->setAlignment(Qt::AlignTop);
layout->setMargin(5);
font = QFont("Cambria", 12, 28, false);
font.setBold(true);
font_pe.setColor(QPalette::WindowText, Qt::white);
pe.setColor(QPalette::ButtonText, Qt::white);
installEventFilter(this);
}
AudioWall::~AudioWall()
{
for each (auto task in tasks) delete task;
}
bool AudioWall::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::ContextMenu)
{
QContextMenuEvent *e = static_cast<QContextMenuEvent*>(event);
QMenu *menu = new QMenu();
menu->addAction(tr("Add Task"), this, SLOT(addAudioView()));
menu->addAction(tr("Start All"), this, SLOT(playAll()));
menu->addAction(tr("Pause All"), this, SLOT(pauseAll()));
menu->addAction(tr("Exit"), this, SLOT(close()));
menu->popup(e->globalPos());
menu->exec();
return true;
}
return false;
}
void AudioWall::addAudioView()
{
AddView = new BaseWidget();
QVBoxLayout *vLayout = new QVBoxLayout(AddView);
vLayout->setAlignment(Qt::AlignHCenter);
vLayout->setSpacing(45);
//设置采样率
sampleRateComboBox = new QComboBox(AddView);
sampleRateComboBox->setFixedSize(QSize(150, 45));
sampleRateComboBox->insertItems(0, QStringList() << "48000" << "44100" << "32000" << "24000" << "22050" << "16000" << "12050" << "8000");
sampleRateComboBox->setStatusTip(tr("SAMPLERATE"));
sampleRateComboBox->setFont(font);
sampleRateComboBox->setPalette(pe);
sampleRateComboBox->setStyleSheet("QComboBox{ background - color: #454545; }");
vLayout->addWidget(sampleRateComboBox);
//设置通道
m_deviceBox = new QComboBox(AddView);
m_deviceBox->setFixedSize(QSize(150, 45));
qDebug() << "l:" << QAudioDeviceInfo::defaultOutputDevice().deviceName();
foreach(const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput))
{
m_deviceBox->addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo));
}
m_deviceBox->setStatusTip(tr("CHANNEL"));
m_deviceBox->setFont(font);
m_deviceBox->setPalette(pe);
m_deviceBox->setStyleSheet("QComboBox{ background - color: #454545; }");
vLayout->addWidget(m_deviceBox);
//设置文件
QPushButton *selectFileButton;
selectFileButton = new QPushButton(QIcon("./Resources/file.png"), tr(""), AddView);
selectFileButton->setStatusTip(tr("Choose a File"));
selectFileButton->setFixedSize(QSize(45, 45));
selectFileButton->setIconSize(QSize(45, 45));
vLayout->addWidget(selectFileButton);
QPushButton *BackButton;//取消
BackButton = new QPushButton(QIcon("./Resources/cancel.png"), tr(""), AddView);
BackButton->setStatusTip(tr("返回"));
BackButton->setFixedSize(QSize(45, 45));
BackButton->setIconSize(QSize(45, 45));
QPushButton *addOKButton;//添加
addOKButton = new QPushButton(QIcon("./Resources/ok.png"), tr(""), AddView);
addOKButton->setStatusTip(tr("返回"));
addOKButton->setFixedSize(QSize(45, 45));
addOKButton->setIconSize(QSize(45, 45));
QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->addWidget(BackButton);
hLayout->addWidget(addOKButton);
vLayout->addLayout(hLayout);
connect(sampleRateComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setSampleRate(int)));
connect(m_deviceBox, SIGNAL(activated(int)), this, SLOT(deviceChanged(int)));
connect(selectFileButton, SIGNAL(clicked()), this, SLOT(openLocalFile()));
connect(BackButton, SIGNAL(clicked()), AddView, SLOT(close()));
connect(addOKButton, SIGNAL(clicked()), this, SLOT(AudioOK()));
AddView->show();
}
void AudioWall::playAll()
{
for each (auto task in tasks)
{
task->start();
}
}
void AudioWall::pauseAll()
{
for each (auto task in tasks)
{
task->pause();
}
}
void AudioWall::setSampleRate(const int &index)
{
sampleRate = sampleRateComboBox->itemText(index);
}
void AudioWall::deviceChanged(const int &index)
{
taskName = QString::fromLocal8Bit("通道") + QString::number(index + 1);
//taskName = m_deviceBox->itemText(index);
m_device = m_deviceBox->itemData(index).value<QAudioDeviceInfo>();
}
void AudioWall::openLocalFile()
{
/*
aif : AIFF (Apple/SGI)
wav : WAV (Microsoft)
au : AU (Sun/NeXT)
caf : CAF (Apple Core Audio File)
flac : FLAC (FLAC Lossless Audio Codec)
snd : AU (Sun/NeXT)
svx : IFF (Amiga IFF/SVX8/SV16)
paf : PAF (Ensoniq PARIS)
fap : PAF (Ensoniq PARIS)
gsm : RAW (header-less)
nist : WAV (NIST Sphere)
htk : HTK (HMM Tool Kit)
ircam : SF (Berkeley/IRCAM/CARL)
sf : SF (Berkeley/IRCAM/CARL)
voc : VOC (Creative Labs)
w64 : W64 (SoundFoundry WAVE 64)
raw : RAW (header-less)
mat4 : MAT4 (GNU Octave 2.0 / Matlab 4.2)
mat5 : MAT5 (GNU Octave 2.1 / Matlab 5.0)
mat : MAT4 (GNU Octave 2.0 / Matlab 4.2)
pvf : PVF (Portable Voice Format)
sds : SDS (Midi Sample Dump Standard)
sd2 : SD2 (Sound Designer II)
vox : RAW (header-less)
xi : XI (FastTracker 2)
wve : WVE (Psion Series 3)
oga : OGG (OGG Container format)
mpc : MPC (Akai MPC 2k)
rf64 : RF64 (RIFF 64)
*/
file_path = QFileDialog::getOpenFileName(0, tr("Open an Audio File"), "./", "Vedio(*.mpc *.mp3 *.flac *.aiff *.ogg *.wav)");
taskInfo = QFileInfo(file_path);
}
void AudioWall::AudioOK()
{
QWidget *row = new QWidget(this);
row->setFixedHeight(50);
rows.append(row);
layout->addWidget(row);
QHBoxLayout *hLayout = new QHBoxLayout(row);
hLayout->setSpacing(5);
QLabel* taskNameLable = new QLabel(row);
taskNameLable->setText(taskName);
taskNameLable->setFixedHeight(45);
taskNameLable->setFont(font);
taskNameLable->setPalette(font_pe);
QLabel* fileNameLable = new QLabel(row);
fileNameLable->setText(taskInfo.fileName());
fileNameLable->setFixedHeight(45);
fileNameLable->setFont(font);
fileNameLable->setPalette(font_pe);
QSlider *TimeSlider = new QSlider(row);
TimeSlider->setOrientation(Qt::Horizontal);
TimeSlider->setTickPosition(QSlider::TicksAbove);//设置刻度
TimeSlider->setTickInterval(5);
TimeSlider->setMinimum(0);
TimeSlider->setMaximum(100);
TimeSlider->setValue(0);
timeSliders.append(TimeSlider);
QSlider *VolumeSlider = new QSlider(row);
VolumeSlider->setOrientation(Qt::Vertical);
VolumeSlider->setTracking(true);//实时改变
VolumeSlider->setMinimum(0);
VolumeSlider->setMaximum(100);
VolumeSlider->setValue(100);
volumeSliders.append(VolumeSlider);
QPushButton* start = new QPushButton(QIcon("./Resources/start.png"), tr(""), row);
start->setFixedSize(QSize(45, 45));
start->setIconSize(QSize(45, 45));
QPushButton* remove = new QPushButton(QIcon("./Resources/remove.png"), tr(""), row);
remove->setFixedSize(QSize(45, 45));
remove->setIconSize(QSize(45, 45));
hLayout->addWidget(taskNameLable, 2);
hLayout->addWidget(fileNameLable, 4);
hLayout->addWidget(TimeSlider, 4);
hLayout->addWidget(VolumeSlider, 1);
hLayout->addWidget(start, 1);
hLayout->addWidget(remove, 1);
WAVFile *inputFile = new WAVFile();
inputFile->open(file_path, QIODevice::ReadOnly);
m_format.setSampleRate(sampleRate.toInt());//设置采样率
m_format.setChannelCount(1);//设置通道数
m_format.setSampleSize(16);//设置采样大小,一般为8位或16位
m_format.setCodec("audio/pcm");//设置编码方式
m_format.setByteOrder(QAudioFormat::LittleEndian);//设置字节序
m_format.setSampleType(QAudioFormat::UnSignedInt);//设置样本数据类型
m_format = m_device.preferredFormat();
QAudioOutput *m_audioOutput = new QAudioOutput(m_device, m_format);
m_audioOutput->setNotifyInterval(1000);
if (inputFile->isReadable())
{
AudioTask* task = new AudioTask(tr("Audio"), taskName, taskInfo, m_audioOutput, sampleRate);
m_audioOutput->start(inputFile);
players.append(m_audioOutput);
task->file = inputFile;
task->size = inputFile->size();
task->timeSlider = TimeSlider;
task->taskTime = QDateTime::currentDateTime();
task->isFinished = false;
inputFile->seek(0);
emit updateAudioList(task);
tasks.append(task);
AddView->close();
row->show();
}
else
{
QMessageBox::warning(0, tr("warnning"), tr("The file is unreadable!\n"));
return;
}
connect(m_audioOutput, SIGNAL(stateChanged(QAudio::State)), this, SLOT(updateAudioState(QAudio::State)));
connect(m_audioOutput, SIGNAL(notify()), this, SLOT(setSliderPosition()));
connect(TimeSlider, SIGNAL(sliderMoved(int)), this, SLOT(setPlayerPosition(int)));
connect(VolumeSlider, SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
connect(start, SIGNAL(clicked()), this, SLOT(setState()));
connect(remove, SIGNAL(clicked()), this, SLOT(Remove()));
}
void AudioWall::setAudioState(QAudio::State state)
{
emit updateAudioState(nth);
}
void AudioWall::setState()
{
QPushButton *test = qobject_cast<QPushButton *>(sender());
QWidget *w = test->parentWidget();
nth = w->y() / 50;
if (tasks[nth]->getState() == QAudio::ActiveState)
{
tasks[nth]->stop();
test->setIcon(QIcon("./Resources/start.png"));
}
else
{
tasks[nth]->start();
test->setIcon(QIcon("./Resources/stop.png"));
}
emit updateAudioState(nth);
}
void AudioWall::Remove()
{
QPushButton *test = qobject_cast<QPushButton *>(sender());
QWidget *w = test->parentWidget();
nth = w->y() / 50;
if (!tasks[nth]->audio->signalsBlocked())
{
tasks[nth]->audio->blockSignals(true);
tasks[nth]->audio->deleteLater();
tasks[nth]->isFinished = true;
}
w->close();
emit updateAudioList(tasks[nth]);
delete tasks[nth];
tasks.removeAt(nth);
players.removeAt(nth);
timeSliders.removeAt(nth);
volumeSliders.removeAt(nth);
rows.removeAt(nth);
}
void AudioWall::setPlayerPosition(int value)
{
QSlider *test = qobject_cast<QSlider *>(sender());
QWidget *w = test->parentWidget();
nth = w->y() / 50;
qDebug() << nth << ": " << value;
tasks[nth]->file->seek(tasks[nth]->size / 100 * value);
test->setToolTip(QString::number(value));
emit updateAudioProgress(nth, value);
}
void AudioWall::setSliderPosition()
{
static int processed = 0;
static int position = 0;
QAudioOutput *audio = qobject_cast<QAudioOutput *>(sender());
nth = players.indexOf(audio);
QSlider *slider = tasks[nth]->timeSlider;
int n = audio->bufferSize();
if (position != slider->sliderPosition()) //如果进度条被拖放,则将该位置置为起点
processed += slider->sliderPosition() * tasks[nth]->size / 100 + n;
else
processed += n;
qDebug() << "size: " << tasks[nth]->size << " \t " << processed;
int value = processed * 100 / tasks[nth]->size;
slider->setValue(value);
position = slider->sliderPosition();
emit updateAudioProgress(nth, value);
}
void AudioWall::setVolume(int value)
{
QSlider *test = qobject_cast<QSlider *>(sender());
QWidget *w = test->parentWidget();
nth = w->y() / 50;
qDebug() << nth << "\n" << value;
QAudioOutput *temp = tasks[nth]->audio;
if (temp) temp->setVolume(value*1.0 / 100.0);
test->setToolTip(QString::number(value));
}