-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainwindow.cpp
113 lines (96 loc) · 3.92 KB
/
mainwindow.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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#include "mainwindow.h"
#include "qite.h"
#include "qiteaudio.h"
#include "qiteaudiorecorder.h"
#include "ui_mainwindow.h"
#include <QAction>
#include <QComboBox>
#include <QDateTime>
#include <QDir>
#include <QDirIterator>
#include <QIcon>
#include <QStandardPaths>
#include <QStyle>
#include <random>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
recordAction = new QAction(QIcon(":/icon/recorder-microphone.png"), "Record", this);
ui->mainToolBar->addAction(recordAction);
connect(recordAction, &QAction::triggered, this, &MainWindow::recordMic);
auto fontCombo = new QComboBox(this);
for (int i = 8; i < 26; i++) {
fontCombo->addItem(QString::number(i));
auto comboAct = ui->mainToolBar->addWidget(fontCombo);
fontCombo->setCurrentText(QString::number(ui->textEdit->fontPointSize()));
comboAct->setVisible(true);
connect(fontCombo, &QComboBox::currentTextChanged, this,
[this](const QString &text) { ui->textEdit->setFontPointSize(text.toInt()); });
}
connect(ui->textEdit, &QTextEdit::destroyed, [](QObject *) { qDebug("QTextEdit destoryed"); });
auto itc = new InteractiveText(ui->textEdit); // global thing to handle all kinds of ITEs
atc = new ITEAudioController(itc, this);
atc->setAutoFetchMetadata(true);
auto musicDir = QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
auto nameFilters = QStringList() << "*.aac" << "*.flac" << "*.mp3" << "*.ogg" << "*.webm";
QDirIterator it(musicDir, nameFilters, QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);
QStringList files;
int count = 2000;
while (it.hasNext() && count--)
files << it.next();
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(files.begin(), files.end(), g);
while (files.size()) {
QString fileToPlay = files.takeLast();
QFileInfo fi(fileToPlay);
auto url = QUrl::fromLocalFile(fileToPlay);
ui->textEdit->append(fi.fileName() + "<br>");
atc->insert(url);
}
}
MainWindow::~MainWindow()
{
delete ui;
qDebug("ui destroyed");
}
void MainWindow::recordMic()
{
if (!recorder) {
recorder = new AudioRecorder(this);
connect(recorder, &AudioRecorder::finished, this, [this](bool success) {
recordAction->setIcon(QIcon(":/icon/recorder-microphone.png"));
if (success) {
auto fileName = QFileInfo(recorder->fileName()).absoluteFilePath();
atc->insert(QUrl::fromLocalFile(fileName));
ui->textEdit->append(QString("file=") + fileName);
ui->textEdit->append(QString("max volume=") + QString::number(recorder->maxVolume()));
} else {
ui->textEdit->append(recorder->errorString());
}
});
}
ui->textEdit->append("");
if (recorder->state() == AudioRecorder::StoppedState) {
recordAction->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
recorder->record(QString("test-%1.mp4").arg(QDateTime::currentSecsSinceEpoch()));
} else {
recorder->stop();
}
}