Chris@378: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@378: Chris@378: /* Chris@378: Sonic Visualiser Chris@378: An audio file viewer and annotation editor. Chris@378: Centre for Digital Music, Queen Mary, University of London. Chris@1319: This file copyright 2006-2018 Chris Cannam and QMUL. Chris@378: Chris@378: This program is free software; you can redistribute it and/or Chris@378: modify it under the terms of the GNU General Public License as Chris@378: published by the Free Software Foundation; either version 2 of the Chris@378: License, or (at your option) any later version. See the file Chris@378: COPYING included with this distribution for more information. Chris@378: */ Chris@378: Chris@1319: #include "CSVAudioFormatDialog.h" Chris@378: Chris@561: #include "layer/LayerFactory.h" Chris@561: Chris@674: #include "TextAbbrev.h" Chris@674: Chris@378: #include Chris@378: #include Chris@378: #include Chris@378: #include Chris@378: #include Chris@378: #include Chris@378: #include Chris@378: #include Chris@512: #include Chris@378: Chris@560: #include Chris@913: #include Chris@378: Chris@682: #include "base/Debug.h" Chris@682: Chris@1319: CSVAudioFormatDialog::CSVAudioFormatDialog(QWidget *parent, CSVFormat format, Chris@1319: int maxDisplayCols) : Chris@378: QDialog(parent), Chris@581: m_format(format), Chris@581: m_maxDisplayCols(maxDisplayCols), Chris@581: m_fuzzyColumn(-1) Chris@378: { Chris@378: setModal(true); Chris@1319: setWindowTitle(tr("Select Audio Data Format")); Chris@378: Chris@378: QGridLayout *layout = new QGridLayout; Chris@378: Chris@560: int row = 0; Chris@378: Chris@1318: layout->addWidget Chris@1318: (new QLabel(tr("Please select the correct data format for this file.")), Chris@1318: row++, 0, 1, 4); Chris@560: Chris@560: QFrame *exampleFrame = new QFrame; Chris@560: exampleFrame->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken); Chris@560: exampleFrame->setLineWidth(2); Chris@560: QGridLayout *exampleLayout = new QGridLayout; Chris@561: exampleLayout->setSpacing(4); Chris@560: exampleFrame->setLayout(exampleLayout); Chris@560: Chris@560: QPalette palette = exampleFrame->palette(); Chris@560: palette.setColor(QPalette::Window, palette.color(QPalette::Base)); Chris@560: exampleFrame->setPalette(palette); Chris@560: Chris@560: QFont fp; Chris@908: fp.setPointSize(int(floor(fp.pointSize() * 0.9))); Chris@560: Chris@560: int columns = format.getColumnCount(); Chris@560: QList example = m_format.getExample(); Chris@560: Chris@560: for (int i = 0; i < columns; ++i) { Chris@581: Chris@560: QComboBox *cpc = new QComboBox; Chris@560: m_columnPurposeCombos.push_back(cpc); Chris@560: exampleLayout->addWidget(cpc, 0, i); Chris@581: connect(cpc, SIGNAL(activated(int)), this, SLOT(columnPurposeChanged(int))); Chris@1318: Chris@581: if (i == m_maxDisplayCols && columns > i + 2) { Chris@581: m_fuzzyColumn = i; Chris@1318: Chris@1319: cpc->addItem(tr("")); Chris@1319: cpc->addItem(tr("Audio channels")); Chris@1319: cpc->setCurrentIndex Chris@1322: (m_format.getColumnPurpose(i-1) == CSVFormat::ColumnValue ? Chris@1322: 1 : 0); Chris@1319: Chris@1318: exampleLayout->addWidget Chris@1318: (new QLabel(tr("(%1 more)").arg(columns - i)), 1, i); Chris@581: break; Chris@581: } Chris@560: Chris@1319: cpc->addItem(tr("")); Chris@1319: cpc->addItem(tr("Audio channel")); Chris@1322: cpc->setCurrentIndex Chris@1322: (m_format.getColumnPurpose(i) == CSVFormat::ColumnValue ? 1 : 0); Chris@1318: Chris@581: for (int j = 0; j < example.size() && j < 6; ++j) { Chris@866: if (i >= example[j].size()) { Chris@866: continue; Chris@866: } Chris@581: QLabel *label = new QLabel; Chris@581: label->setTextFormat(Qt::PlainText); Chris@674: QString text = TextAbbrev::abbreviate(example[j][i], 35); Chris@674: label->setText(text); Chris@581: label->setFont(fp); Chris@581: label->setPalette(palette); Chris@581: label->setIndent(8); Chris@581: exampleLayout->addWidget(label, j+1, i); Chris@560: } Chris@560: } Chris@560: Chris@560: layout->addWidget(exampleFrame, row, 0, 1, 4); Chris@560: layout->setColumnStretch(3, 10); Chris@560: layout->setRowStretch(row++, 10); Chris@560: Chris@1320: layout->addWidget(new QLabel(tr("Audio sample rate (Hz):")), row, 0); Chris@378: Chris@807: int sampleRates[] = { Chris@1266: 8000, 11025, 12000, 22050, 24000, 32000, Chris@1266: 44100, 48000, 88200, 96000, 176400, 192000 Chris@378: }; Chris@378: Chris@378: m_sampleRateCombo = new QComboBox; Chris@807: for (int i = 0; i < int(sizeof(sampleRates) / sizeof(sampleRates[0])); ++i) { Chris@1266: m_sampleRateCombo->addItem(QString("%1").arg(sampleRates[i])); Chris@1266: if (sampleRates[i] == m_format.getSampleRate()) { Chris@560: m_sampleRateCombo->setCurrentIndex(i); Chris@560: } Chris@378: } Chris@378: m_sampleRateCombo->setEditable(true); Chris@378: Chris@560: layout->addWidget(m_sampleRateCombo, row++, 1); Chris@378: connect(m_sampleRateCombo, SIGNAL(activated(QString)), Chris@1266: this, SLOT(sampleRateChanged(QString))); Chris@378: connect(m_sampleRateCombo, SIGNAL(editTextChanged(QString)), Chris@1266: this, SLOT(sampleRateChanged(QString))); Chris@1320: Chris@1320: layout->addWidget(new QLabel(tr("Sample values are:")), row, 0); Chris@1320: Chris@1320: m_sampleRangeCombo = new QComboBox; Chris@1321: // NB must be in the same order as the CSVFormat::AudioSampleRange enum Chris@1320: m_sampleRangeCombo->addItem(tr("Floating-point in range -1 to 1")); Chris@1320: m_sampleRangeCombo->addItem(tr("8-bit in range 0 to 255")); Chris@1320: m_sampleRangeCombo->addItem(tr("16-bit in range -32768 to 32767")); Chris@1320: m_sampleRangeCombo->addItem(tr("Unknown range: normalise on load")); Chris@1321: m_sampleRangeCombo->setCurrentIndex(int(m_format.getAudioSampleRange())); Chris@1320: Chris@1320: layout->addWidget(m_sampleRangeCombo, row++, 1); Chris@1320: connect(m_sampleRangeCombo, SIGNAL(activated(int)), Chris@1320: this, SLOT(sampleRangeChanged(int))); Chris@1320: Chris@512: QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok | Chris@512: QDialogButtonBox::Cancel); Chris@560: layout->addWidget(bb, row++, 0, 1, 4); Chris@512: connect(bb, SIGNAL(accepted()), this, SLOT(accept())); Chris@512: connect(bb, SIGNAL(rejected()), this, SLOT(reject())); Chris@378: Chris@512: setLayout(layout); Chris@378: Chris@1319: updateFormatFromDialog(); Chris@378: } Chris@378: Chris@1319: CSVAudioFormatDialog::~CSVAudioFormatDialog() Chris@378: { Chris@378: } Chris@378: Chris@378: CSVFormat Chris@1319: CSVAudioFormatDialog::getFormat() const Chris@378: { Chris@560: return m_format; Chris@378: } Chris@378: Chris@378: void Chris@1319: CSVAudioFormatDialog::sampleRateChanged(QString rateString) Chris@378: { Chris@378: bool ok = false; Chris@378: int sampleRate = rateString.toInt(&ok); Chris@560: if (ok) m_format.setSampleRate(sampleRate); Chris@378: } Chris@378: Chris@378: void Chris@1320: CSVAudioFormatDialog::sampleRangeChanged(int range) Chris@1320: { Chris@1321: m_format.setAudioSampleRange((CSVFormat::AudioSampleRange)range); Chris@1320: } Chris@1320: Chris@1320: void Chris@1319: CSVAudioFormatDialog::columnPurposeChanged(int) Chris@378: { Chris@1318: updateFormatFromDialog(); Chris@1318: } Chris@1318: Chris@1318: void Chris@1319: CSVAudioFormatDialog::updateFormatFromDialog() Chris@1318: { Chris@1319: m_format.setModelType(CSVFormat::WaveFileModel); Chris@1319: m_format.setTimingType(CSVFormat::ImplicitTiming); Chris@1319: m_format.setTimeUnits(CSVFormat::TimeAudioFrames); Chris@1318: Chris@959: for (int i = 0; i < m_columnPurposeCombos.size(); ++i) { Chris@959: Chris@959: QComboBox *thisCombo = m_columnPurposeCombos[i]; Chris@959: Chris@1319: CSVFormat::ColumnPurpose purpose = (thisCombo->currentIndex() == 1 ? Chris@1319: CSVFormat::ColumnValue : Chris@1319: CSVFormat::ColumnUnknown); Chris@1319: Chris@959: if (i == m_fuzzyColumn) { Chris@992: for (int j = i; j < m_format.getColumnCount(); ++j) { Chris@1319: m_format.setColumnPurpose(j, purpose); Chris@959: } Chris@992: } else { Chris@992: m_format.setColumnPurpose(i, purpose); Chris@959: } Chris@561: } Chris@560: } Chris@560: Chris@560: Chris@959: