comparison widgets/CSVAudioFormatDialog.cpp @ 1324:13d9b422f7fe zoom

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