annotate widgets/CSVAudioFormatDialog.cpp @ 1320:6440ba1ffc86 import-audio-data

Sample range
author Chris Cannam
date Thu, 06 Sep 2018 16:26:40 +0100
parents fbda05431ce0
children 4616e1e89189
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@1320 41 m_sampleRange(RangeSigned1),
Chris@581 42 m_maxDisplayCols(maxDisplayCols),
Chris@581 43 m_fuzzyColumn(-1)
Chris@378 44 {
Chris@378 45 setModal(true);
Chris@1319 46 setWindowTitle(tr("Select Audio Data Format"));
Chris@378 47
Chris@378 48 QGridLayout *layout = new QGridLayout;
Chris@378 49
Chris@560 50 int row = 0;
Chris@378 51
Chris@1318 52 layout->addWidget
Chris@1318 53 (new QLabel(tr("Please select the correct data format for this file.")),
Chris@1318 54 row++, 0, 1, 4);
Chris@560 55
Chris@560 56 QFrame *exampleFrame = new QFrame;
Chris@560 57 exampleFrame->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
Chris@560 58 exampleFrame->setLineWidth(2);
Chris@560 59 QGridLayout *exampleLayout = new QGridLayout;
Chris@561 60 exampleLayout->setSpacing(4);
Chris@560 61 exampleFrame->setLayout(exampleLayout);
Chris@560 62
Chris@560 63 QPalette palette = exampleFrame->palette();
Chris@560 64 palette.setColor(QPalette::Window, palette.color(QPalette::Base));
Chris@560 65 exampleFrame->setPalette(palette);
Chris@560 66
Chris@560 67 QFont fp;
Chris@908 68 fp.setPointSize(int(floor(fp.pointSize() * 0.9)));
Chris@560 69
Chris@560 70 int columns = format.getColumnCount();
Chris@560 71 QList<QStringList> example = m_format.getExample();
Chris@560 72
Chris@560 73 for (int i = 0; i < columns; ++i) {
Chris@581 74
Chris@560 75 QComboBox *cpc = new QComboBox;
Chris@560 76 m_columnPurposeCombos.push_back(cpc);
Chris@560 77 exampleLayout->addWidget(cpc, 0, i);
Chris@581 78 connect(cpc, SIGNAL(activated(int)), this, SLOT(columnPurposeChanged(int)));
Chris@1318 79
Chris@581 80 if (i == m_maxDisplayCols && columns > i + 2) {
Chris@581 81 m_fuzzyColumn = i;
Chris@1318 82
Chris@1319 83 cpc->addItem(tr("<ignore>"));
Chris@1319 84 cpc->addItem(tr("Audio channels"));
Chris@1319 85 cpc->setCurrentIndex
Chris@1319 86 (m_format.isColumnNumeric(i-1) ? 1 : 0);
Chris@1319 87
Chris@1318 88 exampleLayout->addWidget
Chris@1318 89 (new QLabel(tr("(%1 more)").arg(columns - i)), 1, i);
Chris@581 90 break;
Chris@581 91 }
Chris@560 92
Chris@1319 93 cpc->addItem(tr("<ignore>"));
Chris@1319 94 cpc->addItem(tr("Audio channel"));
Chris@1319 95 if (m_format.isColumnNumeric(i)) {
Chris@1319 96 cpc->setCurrentIndex(1);
Chris@1318 97 } else {
Chris@1319 98 cpc->setCurrentIndex(0);
Chris@1318 99 }
Chris@1318 100
Chris@581 101 for (int j = 0; j < example.size() && j < 6; ++j) {
Chris@866 102 if (i >= example[j].size()) {
Chris@866 103 continue;
Chris@866 104 }
Chris@581 105 QLabel *label = new QLabel;
Chris@581 106 label->setTextFormat(Qt::PlainText);
Chris@674 107 QString text = TextAbbrev::abbreviate(example[j][i], 35);
Chris@674 108 label->setText(text);
Chris@581 109 label->setFont(fp);
Chris@581 110 label->setPalette(palette);
Chris@581 111 label->setIndent(8);
Chris@581 112 exampleLayout->addWidget(label, j+1, i);
Chris@560 113 }
Chris@560 114 }
Chris@560 115
Chris@560 116 layout->addWidget(exampleFrame, row, 0, 1, 4);
Chris@560 117 layout->setColumnStretch(3, 10);
Chris@560 118 layout->setRowStretch(row++, 10);
Chris@560 119
Chris@1320 120 layout->addWidget(new QLabel(tr("Audio sample rate (Hz):")), row, 0);
Chris@378 121
Chris@807 122 int sampleRates[] = {
Chris@1266 123 8000, 11025, 12000, 22050, 24000, 32000,
Chris@1266 124 44100, 48000, 88200, 96000, 176400, 192000
Chris@378 125 };
Chris@378 126
Chris@378 127 m_sampleRateCombo = new QComboBox;
Chris@807 128 for (int i = 0; i < int(sizeof(sampleRates) / sizeof(sampleRates[0])); ++i) {
Chris@1266 129 m_sampleRateCombo->addItem(QString("%1").arg(sampleRates[i]));
Chris@1266 130 if (sampleRates[i] == m_format.getSampleRate()) {
Chris@560 131 m_sampleRateCombo->setCurrentIndex(i);
Chris@560 132 }
Chris@378 133 }
Chris@378 134 m_sampleRateCombo->setEditable(true);
Chris@378 135
Chris@560 136 layout->addWidget(m_sampleRateCombo, row++, 1);
Chris@378 137 connect(m_sampleRateCombo, SIGNAL(activated(QString)),
Chris@1266 138 this, SLOT(sampleRateChanged(QString)));
Chris@378 139 connect(m_sampleRateCombo, SIGNAL(editTextChanged(QString)),
Chris@1266 140 this, SLOT(sampleRateChanged(QString)));
Chris@378 141
Chris@1320 142 m_sampleRange = RangeSigned1;
Chris@1320 143 bool knownSigned = false;
Chris@1320 144 bool knownNonIntegral = false;
Chris@1320 145
Chris@1320 146 for (int i = 0; i < columns; ++i) {
Chris@1320 147 if (!(format.getColumnQualities()[i] & CSVFormat::ColumnIntegral)) {
Chris@1320 148 knownNonIntegral = true;
Chris@1320 149 if (m_sampleRange == RangeUnsigned255 ||
Chris@1320 150 m_sampleRange == RangeSigned32767) {
Chris@1320 151 m_sampleRange = RangeOther;
Chris@1320 152 }
Chris@1320 153 }
Chris@1320 154 if (format.getColumnQualities()[i] & CSVFormat::ColumnLarge) {
Chris@1320 155 if (m_sampleRange == RangeSigned1 ||
Chris@1320 156 m_sampleRange == RangeUnsigned255) {
Chris@1320 157 if (knownNonIntegral) {
Chris@1320 158 m_sampleRange = RangeOther;
Chris@1320 159 } else {
Chris@1320 160 m_sampleRange = RangeSigned32767;
Chris@1320 161 }
Chris@1320 162 }
Chris@1320 163 }
Chris@1320 164 if (format.getColumnQualities()[i] & CSVFormat::ColumnSigned) {
Chris@1320 165 knownSigned = true;
Chris@1320 166 if (m_sampleRange == RangeUnsigned255) {
Chris@1320 167 m_sampleRange = RangeSigned32767;
Chris@1320 168 }
Chris@1320 169 }
Chris@1320 170 if (!(format.getColumnQualities()[i] & CSVFormat::ColumnSmall)) {
Chris@1320 171 if (m_sampleRange == RangeSigned1) {
Chris@1320 172 if (knownNonIntegral) {
Chris@1320 173 m_sampleRange = RangeOther;
Chris@1320 174 } else if (knownSigned) {
Chris@1320 175 m_sampleRange = RangeSigned32767;
Chris@1320 176 } else {
Chris@1320 177 m_sampleRange = RangeUnsigned255;
Chris@1320 178 }
Chris@1320 179 }
Chris@1320 180 }
Chris@1320 181 }
Chris@1320 182
Chris@1320 183 layout->addWidget(new QLabel(tr("Sample values are:")), row, 0);
Chris@1320 184
Chris@1320 185 m_sampleRangeCombo = new QComboBox;
Chris@1320 186 // NB must be in the same order as the CSVSampleRange enum
Chris@1320 187 m_sampleRangeCombo->addItem(tr("Floating-point in range -1 to 1"));
Chris@1320 188 m_sampleRangeCombo->addItem(tr("8-bit in range 0 to 255"));
Chris@1320 189 m_sampleRangeCombo->addItem(tr("16-bit in range -32768 to 32767"));
Chris@1320 190 m_sampleRangeCombo->addItem(tr("Unknown range: normalise on load"));
Chris@1320 191 m_sampleRangeCombo->setCurrentIndex(int(m_sampleRange));
Chris@1320 192
Chris@1320 193 layout->addWidget(m_sampleRangeCombo, row++, 1);
Chris@1320 194 connect(m_sampleRangeCombo, SIGNAL(activated(int)),
Chris@1320 195 this, SLOT(sampleRangeChanged(int)));
Chris@1320 196
Chris@512 197 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@512 198 QDialogButtonBox::Cancel);
Chris@560 199 layout->addWidget(bb, row++, 0, 1, 4);
Chris@512 200 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@512 201 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@378 202
Chris@512 203 setLayout(layout);
Chris@378 204
Chris@1319 205 updateFormatFromDialog();
Chris@378 206 }
Chris@378 207
Chris@1319 208 CSVAudioFormatDialog::~CSVAudioFormatDialog()
Chris@378 209 {
Chris@378 210 }
Chris@378 211
Chris@378 212 CSVFormat
Chris@1319 213 CSVAudioFormatDialog::getFormat() const
Chris@378 214 {
Chris@560 215 return m_format;
Chris@378 216 }
Chris@378 217
Chris@1320 218 CSVAudioFormatDialog::CSVSampleRange
Chris@1320 219 CSVAudioFormatDialog::getSampleRange() const
Chris@1320 220 {
Chris@1320 221 return m_sampleRange;
Chris@1320 222 }
Chris@1320 223
Chris@378 224 void
Chris@1319 225 CSVAudioFormatDialog::sampleRateChanged(QString rateString)
Chris@378 226 {
Chris@378 227 bool ok = false;
Chris@378 228 int sampleRate = rateString.toInt(&ok);
Chris@560 229 if (ok) m_format.setSampleRate(sampleRate);
Chris@378 230 }
Chris@378 231
Chris@378 232 void
Chris@1320 233 CSVAudioFormatDialog::sampleRangeChanged(int range)
Chris@1320 234 {
Chris@1320 235 m_sampleRange = (CSVSampleRange) range;
Chris@1320 236 }
Chris@1320 237
Chris@1320 238 void
Chris@1319 239 CSVAudioFormatDialog::columnPurposeChanged(int)
Chris@378 240 {
Chris@1318 241 updateFormatFromDialog();
Chris@1318 242 }
Chris@1318 243
Chris@1318 244 void
Chris@1319 245 CSVAudioFormatDialog::updateFormatFromDialog()
Chris@1318 246 {
Chris@1319 247 m_format.setModelType(CSVFormat::WaveFileModel);
Chris@1319 248 m_format.setTimingType(CSVFormat::ImplicitTiming);
Chris@1319 249 m_format.setTimeUnits(CSVFormat::TimeAudioFrames);
Chris@1318 250
Chris@959 251 for (int i = 0; i < m_columnPurposeCombos.size(); ++i) {
Chris@959 252
Chris@959 253 QComboBox *thisCombo = m_columnPurposeCombos[i];
Chris@959 254
Chris@1319 255 CSVFormat::ColumnPurpose purpose = (thisCombo->currentIndex() == 1 ?
Chris@1319 256 CSVFormat::ColumnValue :
Chris@1319 257 CSVFormat::ColumnUnknown);
Chris@1319 258
Chris@959 259 if (i == m_fuzzyColumn) {
Chris@992 260 for (int j = i; j < m_format.getColumnCount(); ++j) {
Chris@1319 261 m_format.setColumnPurpose(j, purpose);
Chris@959 262 }
Chris@992 263 } else {
Chris@992 264 m_format.setColumnPurpose(i, purpose);
Chris@959 265 }
Chris@561 266 }
Chris@560 267 }
Chris@560 268
Chris@560 269
Chris@959 270