1 #include <sys/ioctl.h>
2 #include <fcntl.h>
3 #include "mainwindow.h"
4 #include "ui_mainwindow.h"
5 #include <QMessageBox>
6 #include <QFile>
7 #include <QFileDialog>
8 #include <QDateTime>
9 #include <QDate>
10 #include <QTime>
11 #include <iostream>
12
MainWindow(QWidget * parent)13 MainWindow::MainWindow(QWidget *parent) :
14 QMainWindow(parent),
15 ui(new Ui::MainWindow)
16 {
17 ui->setupUi(this);
18
19 playing = false;
20 process = NULL;
21 process2 = NULL;
22 timer = NULL;
23
24 btg = new QButtonGroup;
25 btg->addButton(ui->radioMIC,0);
26 btg->addButton(ui->radioLINEIN,1);
27
28 ui->slider->setDisabled(true);
29 ui->list->addItem(new QListWidgetItem("/forlinx/audio/test.mp3"));
30 ui->list->setCurrentRow(0);
31 }
32
~MainWindow()33 MainWindow::~MainWindow()
34 {
35 delete ui;
36 delete btg;
37 delete process;
38 delete process2;
39 }
40
on_play_clicked()41 void MainWindow::on_play_clicked()
42 {
43 if(ui->list->currentItem() == NULL)
44 {
45 QMessageBox::about(this,"error","nothing selected!");
46 return;
47 }
48
49 QString file = ui->list->currentItem()->text();
50
51 QFile f(file);
52
53 if(!f.exists())
54 {
55 QMessageBox::about(this,"error","the file doesn't exist!");
56 return;
57 }
58
59 QString cmd = QString("/usr/bin/mplayer ")+file;
60
61 if(timer)delete timer;
62 if(process)delete process;
63
64 timer = new QTimer();
65 connect(timer,SIGNAL(timeout()),this,SLOT(timerdone()));
66 // timer->start(1000); //一秒钟后开始触发,然后一秒一次
67
68 process = new QProcess();
69 process->setProcessChannelMode(QProcess::MergedChannels);
70 connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(output()));
71
72 process->start(cmd.toLatin1());
73 // process->write(" \n");
74
75 if(!playing)
76 {
77 ui->pause->setText("pause");
78 ui->slider->setDisabled(false);
79 }
80
81 playing = true;
82
83 ui->slider->setMinimum(0);
84 ui->slider->setValue(0);
85 ui->slider->setDisabled(false);
86 }
87
on_stop_clicked()88 void MainWindow::on_stop_clicked()
89 {
90 if(process == NULL)
91 return;
92 process->write("q \n");
93 delete process;
94 delete timer;
95 process = NULL;
96 timer = NULL;
97 }
98
on_add_clicked()99 void MainWindow::on_add_clicked()
100 {
101 QString fileName = QFileDialog::getOpenFileName(this, tr("open file"), " ", tr("Allfile(*.*);;mp3file(*.mp3);;wmafile(*.wma);;wavefile(*.wav)"));
102 ui->list->addItem(new QListWidgetItem(fileName));
103 ui->list->setCurrentRow(0);
104 }
105
timerdone()106 void MainWindow::timerdone()
107 {
108 // if(playing)
109 // process->write(" \n");
110 }
111
output()112 void MainWindow::output()
113 {
114 QByteArray ba;
115 int l,r;
116 QString msg(process->readLine());
117 QString tmp = msg.mid(0,2);
118 if(tmp == "A:"){
119 ba = msg.toLatin1();
120 QString audioTime = msg.mid(2,6);
121
122 l = msg.indexOf(tr("of "),0);
123 r = msg.indexOf(tr(" ("),l);
124 QString totalTime = msg.mid(l+3,r - l - 3);;
125 bool ok;
126 r = (int)totalTime.toDouble(&ok);
127 l = (int)audioTime.toDouble(&ok);
128 ui->slider->setMaximum(r);
129 ui->slider->setValue(l);
130 tmp.setNum(r/60,10);
131 tmp.append(tr(":"));
132 if(r%60 < 10){
133 tmp.append(tr("0"));
134 tmp.append(QString::number(r%60,10));
135 } else
136 tmp.append(QString::number(r%60,10));
137 ui->length->setText(tmp);
138
139 tmp.setNum(l/60,10);
140 tmp.append(tr(":"));
141 if(l%60 < 10){
142 tmp.append(tr("0"));
143 tmp.append(QString::number(l%60,10));
144 } else
145 tmp.append(QString::number(l%60,10));
146 ui->pos->setText(tmp);
147 }
148 }
149
150
on_pause_clicked()151 void MainWindow::on_pause_clicked()
152 {
153 if(process == NULL)
154 return;
155
156 if(playing)
157 {
158 ui->pause->setText("resume");
159 ui->slider->setDisabled(true);
160 }
161 else
162 {
163 ui->pause->setText("pause");
164 ui->slider->setDisabled(false);
165 }
166
167 process->write("p");
168
169 playing = !playing;
170
171 }
172
on_slider_sliderMoved(int position)173 void MainWindow::on_slider_sliderMoved(int position)
174 {
175 Q_UNUSED(position)
176 if(process == NULL)
177 return;
178
179 // QString tmp = QString("seek ")+QString::number(ui->slider->value())+QString(" 2 \n");
180
181 // process->write(tmp.toAscii());
182 }
183
on_del_clicked()184 void MainWindow::on_del_clicked()
185 {
186 ui->list->removeItemWidget(ui->list->takeItem(ui->list->currentRow()));
187 }
188
moveEvent(QMoveEvent *)189 void MainWindow::moveEvent(QMoveEvent *)
190 {
191 this->move(QPoint(0,0));
192 }
193
resizeEvent(QResizeEvent *)194 void MainWindow::resizeEvent(QResizeEvent *)
195 {
196 this->showMaximized();
197 }
198
on_record_clicked()199 void MainWindow::on_record_clicked()
200 {
201 QDateTime dt;
202 QDate date;
203 QTime time;
204
205 if(process2)
206 {
207 delete process2;
208 process2 = NULL;
209 }
210
211
212 if(ui->record->text() == QString("record"))
213 {
214 ui->record->setText("stop");
215
216 process2 = new QProcess();
217
218 dt.setDate(date.currentDate());
219 dt.setTime(time.currentTime());
220 filename = dt.toString("yyyyMMddhhmmss")+QString(".wav");
221
222 if(btg->checkedId() == 0)
223 process2->start("arecord -t wav -r 44100 -f S16_LE /tmp/"+ filename);
224 else
225 process2->start("arecord -t wav -c 2 -r 44100 /tmp/"+filename);
226 }
227 else
228 {
229 ui->record->setText("record");
230
231 ui->list->addItem(new QListWidgetItem("/tmp/"+filename));
232 ui->list->setCurrentRow(ui->list->count()-1);
233 }
234 }
closeEvent(QCloseEvent *)235 void MainWindow::closeEvent(QCloseEvent *)
236 {
237 destroy();
238 exit(0);
239 }
240
241
242