annotate widgets/CSVAudioFormatDialog.cpp @ 1321:4616e1e89189 import-audio-data

Move out these bits which are now in CSVFormat
author Chris Cannam
date Fri, 07 Sep 2018 18:12:44 +0100
parents 6440ba1ffc86
children f33ee2702447
rev   line source
Chris@378 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@378 2
Chris@378 3 /*
Chris@378 4 Sonic Visualiser
Chris@378 5 An audio file viewer and annotation editor.
Chris@378 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1319 7 This file copyright 2006-2018 Chris Cannam and QMUL.
Chris@378 8
Chris@378 9 This program is free software; you can redistribute it and/or
Chris@378 10 modify it under the terms of the GNU General Public License as
Chris@378 11 published by the Free Software Foundation; either version 2 of the
Chris@378 12 License, or (at your option) any later version. See the file
Chris@378 13 COPYING included with this distribution for more information.
Chris@378 14 */
Chris@378 15
Chris@1319 16 #include "CSVAudioFormatDialog.h"
Chris@378 17
Chris@561 18 #include "layer/LayerFactory.h"
Chris@561 19
Chris@674 20 #include "TextAbbrev.h"
Chris@674 21
Chris@378 22 #include <QFrame>
Chris@378 23 #include <QGridLayout>
Chris@378 24 #include <QPushButton>
Chris@378 25 #include <QHBoxLayout>
Chris@378 26 #include <QVBoxLayout>
Chris@378 27 #include <QTableWidget>
Chris@378 28 #include <QComboBox>
Chris@378 29 #include <QLabel>
Chris@512 30 #include <QDialogButtonBox>
Chris@378 31
Chris@560 32 #include <iostream>
Chris@913 33 #include <cmath>
Chris@378 34
Chris@682 35 #include "base/Debug.h"
Chris@682 36
Chris@1319 37 CSVAudioFormatDialog::CSVAudioFormatDialog(QWidget *parent, CSVFormat format,
Chris@1319 38 int maxDisplayCols) :
Chris@378 39 QDialog(parent),
Chris@581 40 m_format(format),
Chris@581 41 m_maxDisplayCols(maxDisplayCols),
Chris@581 42 m_fuzzyColumn(-1)
Chris@378 43 {
Chris@378 44 setModal(true);
Chris@1319 45 setWindowTitle(tr("Select Audio Data Format"));
Chris@378 46
Chris@378 47 QGridLayout *layout = new QGridLayout;
Chris@378 48
Chris@560 49 int row = 0;
Chris@378 50
Chris@1318 51 layout->addWidget
Chris@1318 52 (new QLabel(tr("Please select the correct data format for this file.")),
Chris@1318 53 row++, 0, 1, 4);
Chris@560 54
Chris@560 55 QFrame *exampleFrame = new QFrame;
Chris@560 56 exampleFrame->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
Chris@560 57 exampleFrame->setLineWidth(2);
Chris@560 58 QGridLayout *exampleLayout = new QGridLayout;
Chris@561 59 exampleLayout->setSpacing(4);
Chris@560 60 exampleFrame->setLayout(exampleLayout);
Chris@560 61
Chris@560 62 QPalette palette = exampleFrame->palette();
Chris@560 63 palette.setColor(QPalette::Window, palette.color(QPalette::Base));
Chris@560 64 exampleFrame->setPalette(palette);
Chris@560 65
Chris@560 66 QFont fp;
Chris@908 67 fp.setPointSize(int(floor(fp.pointSize() * 0.9)));
Chris@560 68
Chris@560 69 int columns = format.getColumnCount();
Chris@560 70 QList<QStringList> example = m_format.getExample();
Chris@560 71
Chris@560 72 for (int i = 0; i < columns; ++i) {
Chris@581 73
Chris@560 74 QComboBox *cpc = new QComboBox;
Chris@560 75 m_columnPurposeCombos.push_back(cpc);
Chris@560 76 exampleLayout->addWidget(cpc, 0, i);
Chris@581 77 connect(cpc, SIGNAL(activated(int)), this, SLOT(columnPurposeChanged(int)));
Chris@1318 78
Chris@581 79 if (i == m_maxDisplayCols && columns > i + 2) {
Chris@581 80 m_fuzzyColumn = i;
Chris@1318 81
Chris@1319 82 cpc->addItem(tr("<ignore>"));
Chris@1319 83 cpc->addItem(tr("Audio channels"));
Chris@1319 84 cpc->setCurrentIndex
Chris@1319 85 (m_format.isColumnNumeric(i-1) ? 1 : 0);
Chris@1319 86
Chris@1318 87 exampleLayout->addWidget
Chris@1318 88 (new QLabel(tr("(%1 more)").arg(columns - i)), 1, i);
Chris@581 89 break;
Chris@581 90 }
Chris@560 91
Chris@1319 92 cpc->addItem(tr("<ignore>"));
Chris@1319 93 cpc->addItem(tr("Audio channel"));
Chris@1319 94 if (m_format.isColumnNumeric(i)) {
Chris@1319 95 cpc->setCurrentIndex(1);
Chris@1318 96 } else {
Chris@1319 97 cpc->setCurrentIndex(0);
Chris@1318 98 }
Chris@1318 99
Chris@581 100 for (int j = 0; j < example.size() && j < 6; ++j) {
Chris@866 101 if (i >= example[j].size()) {
Chris@866 102 continue;
Chris@866 103 }
Chris@581 104 QLabel *label = new QLabel;
Chris@581 105 label->setTextFormat(Qt::PlainText);
Chris@674 106 QString text = TextAbbrev::abbreviate(example[j][i], 35);
Chris@674 107 label->setText(text);
Chris@581 108 label->setFont(fp);
Chris@581 109 label->setPalette(palette);
Chris@581 110 label->setIndent(8);
Chris@581 111 exampleLayout->addWidget(label, j+1, i);
Chris@560 112 }
Chris@560 113 }
Chris@560 114
Chris@560 115 layout->addWidget(exampleFrame, row, 0, 1, 4);
Chris@560 116 layout->setColumnStretch(3, 10);
Chris@560 117 layout->setRowStretch(row++, 10);
Chris@560 118
Chris@1320 119 layout->addWidget(new QLabel(tr("Audio sample rate (Hz):")), row, 0);
Chris@378 120
Chris@807 121 int sampleRates[] = {
Chris@1266 122 8000, 11025, 12000, 22050, 24000, 32000,
Chris@1266 123 44100, 48000, 88200, 96000, 176400, 192000
Chris@378 124 };
Chris@378 125
Chris@378 126 m_sampleRateCombo = new QComboBox;
Chris@807 127 for (int i = 0; i < int(sizeof(sampleRates) / sizeof(sampleRates[0])); ++i) {
Chris@1266 128 m_sampleRateCombo->addItem(QString("%1").arg(sampleRates[i]));
Chris@1266 129 if (sampleRates[i] == m_format.getSampleRate()) {
Chris@560 130 m_sampleRateCombo->setCurrentIndex(i);
Chris@560 131 }
Chris@378 132 }
Chris@378 133 m_sampleRateCombo->setEditable(true);
Chris@378 134
Chris@560 135 layout->addWidget(m_sampleRateCombo, row++, 1);
Chris@378 136 connect(m_sampleRateCombo, SIGNAL(activated(QString)),
Chris@1266 137 this, SLOT(sampleRateChanged(QString)));
Chris@378 138 connect(m_sampleRateCombo, SIGNAL(editTextChanged(QString)),
Chris@1266 139 this, SLOT(sampleRateChanged(QString)));
Chris@1320 140
Chris@1320 141 layout->addWidget(new QLabel(tr("Sample values are:")), row, 0);
Chris@1320 142
Chris@1320 143 m_sampleRangeCombo = new QComboBox;
Chris@1321 144 // NB must be in the same order as the CSVFormat::AudioSampleRange enum
Chris@1320 145 m_sampleRangeCombo->addItem(tr("Floating-point in range -1 to 1"));
Chris@1320 146 m_sampleRangeCombo->addItem(tr("8-bit in range 0 to 255"));
Chris@1320 147 m_sampleRangeCombo->addItem(tr("16-bit in range -32768 to 32767"));
Chris@1320 148 m_sampleRangeCombo->addItem(tr("Unknown range: normalise on load"));
Chris@1321 149 m_sampleRangeCombo->setCurrentIndex(int(m_format.getAudioSampleRange()));
Chris@1320 150
Chris@1320 151 layout->addWidget(m_sampleRangeCombo, row++, 1);
Chris@1320 152 connect(m_sampleRangeCombo, SIGNAL(activated(int)),
Chris@1320 153 this, SLOT(sampleRangeChanged(int)));
Chris@1320 154
Chris@512 155 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@512 156 QDialogButtonBox::Cancel);
Chris@560 157 layout->addWidget(bb, row++, 0, 1, 4);
Chris@512 158 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@512 159 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@378 160
Chris@512 161 setLayout(layout);
Chris@378 162
Chris@1319 163 updateFormatFromDialog();
Chris@378 164 }
Chris@378 165
Chris@1319 166 CSVAudioFormatDialog::~CSVAudioFormatDialog()
Chris@378 167 {
Chris@378 168 }
Chris@378 169
Chris@378 170 CSVFormat
Chris@1319 171 CSVAudioFormatDialog::getFormat() const
Chris@378 172 {
Chris@560 173 return m_format;
Chris@378 174 }
Chris@378 175
Chris@378 176 void
Chris@1319 177 CSVAudioFormatDialog::sampleRateChanged(QString rateString)
Chris@378 178 {
Chris@378 179 bool ok = false;
Chris@378 180 int sampleRate = rateString.toInt(&ok);
Chris@560 181 if (ok) m_format.setSampleRate(sampleRate);
Chris@378 182 }
Chris@378 183
Chris@378 184 void
Chris@1320 185 CSVAudioFormatDialog::sampleRangeChanged(int range)
Chris@1320 186 {
Chris@1321 187 m_format.setAudioSampleRange((CSVFormat::AudioSampleRange)range);
Chris@1320 188 }
Chris@1320 189
Chris@1320 190 void
Chris@1319 191 CSVAudioFormatDialog::columnPurposeChanged(int)
Chris@378 192 {
Chris@1318 193 updateFormatFromDialog();
Chris@1318 194 }
Chris@1318 195
Chris@1318 196 void
Chris@1319 197 CSVAudioFormatDialog::updateFormatFromDialog()
Chris@1318 198 {
Chris@1319 199 m_format.setModelType(CSVFormat::WaveFileModel);
Chris@1319 200 m_format.setTimingType(CSVFormat::ImplicitTiming);
Chris@1319 201 m_format.setTimeUnits(CSVFormat::TimeAudioFrames);
Chris@1318 202
Chris@959 203 for (int i = 0; i < m_columnPurposeCombos.size(); ++i) {
Chris@959 204
Chris@959 205 QComboBox *thisCombo = m_columnPurposeCombos[i];
Chris@959 206
Chris@1319 207 CSVFormat::ColumnPurpose purpose = (thisCombo->currentIndex() == 1 ?
Chris@1319 208 CSVFormat::ColumnValue :
Chris@1319 209 CSVFormat::ColumnUnknown);
Chris@1319 210
Chris@959 211 if (i == m_fuzzyColumn) {
Chris@992 212 for (int j = i; j < m_format.getColumnCount(); ++j) {
Chris@1319 213 m_format.setColumnPurpose(j, purpose);
Chris@959 214 }
Chris@992 215 } else {
Chris@992 216 m_format.setColumnPurpose(i, purpose);
Chris@959 217 }
Chris@561 218 }
Chris@560 219 }
Chris@560 220
Chris@560 221
Chris@959 222