mainwindow.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Copyright (C) 2016 Tobias Platen
  3. This file is part of OREMO2.
  4. OREMO2 is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "mainwindow.h"
  16. #include "ui_mainwindow.h"
  17. #include "QTextStream"
  18. #include "QTreeView"
  19. #include "QFileSystemModel"
  20. #include <math.h>
  21. #include <qcustomplot.h>
  22. #include <QTableView>
  23. #include "tablemodel.h"
  24. #include "qcustomplot.h"
  25. MainWindow::MainWindow(RecordingManager* rec,QWidget *parent) :
  26. QMainWindow(parent),
  27. ui(new Ui::MainWindow)
  28. {
  29. ui->setupUi(this);
  30. this->rec = rec;
  31. connect(rec,&RecordingManager::recFinished,this,&MainWindow::recFinished);
  32. table= new QTableView();
  33. model = nullptr;
  34. QWidget* widget = new QWidget();
  35. QHBoxLayout* layout = new QHBoxLayout();
  36. this->setCentralWidget(table);
  37. layout->addWidget(table);
  38. widget->setLayout(layout);
  39. QCustomPlot* plot = new QCustomPlot();
  40. table->setMaximumWidth(480);
  41. layout->addWidget(plot);
  42. this->setCentralWidget(widget);
  43. ui->mainToolBar->addWidget(ui->btnRec);
  44. }
  45. MainWindow::~MainWindow()
  46. {
  47. delete ui;
  48. }
  49. void MainWindow::openSession(QString path)
  50. {
  51. QFileInfo file(path);
  52. if(file.exists() and file.isFile() and path.endsWith(".csv"))
  53. {
  54. model= new TableModel(path);
  55. table->setModel(model);
  56. QString basename = file.fileName().replace(".csv","");
  57. QString dirname = file.dir().path()+"/"+basename;
  58. QFileInfo dir(dirname);
  59. if(!dir.exists())
  60. {
  61. file.dir().mkdir(basename);
  62. basedir = dirname;
  63. }
  64. else if(dir.isFile())
  65. {
  66. QMessageBox msgBox;
  67. msgBox.setText("File Exists");
  68. msgBox.exec();
  69. return;
  70. }
  71. else if(dir.isDir()){
  72. //load dir contents into dirmodel
  73. basedir = dirname;
  74. }
  75. }
  76. else {
  77. QMessageBox msgBox;
  78. msgBox.setText("Not a valid CSV file");
  79. msgBox.exec();
  80. }
  81. }
  82. // FIX crash here
  83. void MainWindow::on_btnRec_clicked()
  84. {
  85. if(model==nullptr) return;
  86. //return if config data missig
  87. TableItem* item = model->getDataAtIndex(currentIndex);
  88. //FIXME recording path
  89. float length = QVariant(item->len).toFloat();
  90. activeRec = basedir+"/"+item->wav+".flac";
  91. rec->recStart(activeRec,length);
  92. this->statusBar()->showMessage("Recording "+item->wav+".wav",length*1000);
  93. }
  94. void MainWindow::recFinished()
  95. {
  96. QString result;
  97. float v = rec->getMaxVolume();
  98. if(v>0)
  99. {
  100. checkRecording();
  101. float db = 20 * log10(v);
  102. QString vol;
  103. vol.sprintf("%+06.2f", db);
  104. TableItem* item = model->getDataAtIndex(currentIndex);
  105. item->dec = vol;
  106. currentIndex++;
  107. model->selectIndex(currentIndex);
  108. model->update();
  109. }
  110. else
  111. {
  112. QTextStream(&result) << "silence";
  113. model->update();
  114. }
  115. // this->statusBar()->showMessage(result,60*1000);
  116. // TODO store in table
  117. }
  118. void MainWindow::on_actionQuit_triggered()
  119. {
  120. this->close();
  121. }
  122. void MainWindow::on_actionOpen_Directory_triggered()
  123. {
  124. QString fileName = QFileDialog::getOpenFileName(this,tr("Open VoiceBank"), "", "");
  125. qDebug() << fileName;
  126. //FIXME -- rec dir different from reclist
  127. openSession(fileName);
  128. }
  129. void MainWindow::checkRecording()
  130. {
  131. qDebug() << "checkRecording" << activeRec;
  132. //maybe use a python script for this task, left and right blank at least 100 ms, optional check of pitch
  133. }