annotate sv/main/main.cpp @ 282:d9319859a4cf tip

(none)
author benoitrigolleau
date Fri, 31 Oct 2008 11:00:24 +0000
parents c04e768639cf
children
rev   line source
lbajardsilogic@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
lbajardsilogic@0 2
lbajardsilogic@0 3 /*
lbajardsilogic@0 4 Sonic Visualiser
lbajardsilogic@0 5 An audio file viewer and annotation editor.
lbajardsilogic@0 6 Centre for Digital Music, Queen Mary, University of London.
lbajardsilogic@0 7 This file copyright 2006 Chris Cannam and QMUL.
lbajardsilogic@0 8
lbajardsilogic@0 9 This program is free software; you can redistribute it and/or
lbajardsilogic@0 10 modify it under the terms of the GNU General Public License as
lbajardsilogic@0 11 published by the Free Software Foundation; either version 2 of the
lbajardsilogic@0 12 License, or (at your option) any later version. See the file
lbajardsilogic@0 13 COPYING included with this distribution for more information.
lbajardsilogic@0 14 */
lbajardsilogic@0 15
lbajardsilogic@0 16 #include "MainWindow.h"
lbajardsilogic@0 17
lbajardsilogic@0 18 #include "system/System.h"
lbajardsilogic@0 19 #include "system/Init.h"
lbajardsilogic@0 20 #include "base/TempDirectory.h"
lbajardsilogic@0 21 #include "base/PropertyContainer.h"
lbajardsilogic@0 22 #include "base/Preferences.h"
lbajardsilogic@0 23 #include "widgets/TipDialog.h"
benoitrigolleau@58 24 #include "widgets/EasaierStyle.h"
lbajardsilogic@0 25
lbajardsilogic@0 26 #include <QMetaType>
lbajardsilogic@0 27 #include <QApplication>
lbajardsilogic@0 28 #include <QDesktopWidget>
lbajardsilogic@0 29 #include <QMessageBox>
lbajardsilogic@0 30 #include <QTranslator>
lbajardsilogic@0 31 #include <QLocale>
lbajardsilogic@0 32 #include <QSettings>
lbajardsilogic@0 33 #include <QIcon>
lbajardsilogic@0 34 #include <QSessionManager>
lbajardsilogic@2 35 #include <QPlastiqueStyle>
benoitrigolleau@58 36 #include <QSplashScreen>
lbajardsilogic@0 37
lbajardsilogic@0 38 #include <iostream>
lbajardsilogic@0 39 #include <signal.h>
lbajardsilogic@0 40
lbajardsilogic@0 41 /*! \mainpage Sonic Visualiser
lbajardsilogic@0 42
lbajardsilogic@0 43 \section interesting Summary of interesting classes
lbajardsilogic@0 44
lbajardsilogic@0 45 - Data models: Model and subclasses, e.g. WaveFileModel
lbajardsilogic@0 46
lbajardsilogic@0 47 - Graphical layers: Layer and subclasses, displayed on View and its
lbajardsilogic@0 48 subclass widgets.
lbajardsilogic@0 49
lbajardsilogic@0 50 - Main window class, document class, and file parser: MainWindow,
lbajardsilogic@0 51 Document, SVFileReader
lbajardsilogic@0 52
lbajardsilogic@0 53 - Turning one model (e.g. audio) into another (e.g. more audio, or a
lbajardsilogic@0 54 curve extracted from it): Transform and subclasses
lbajardsilogic@0 55
lbajardsilogic@0 56 - Creating the plugins used by transforms: RealTimePluginFactory,
lbajardsilogic@0 57 FeatureExtractionPluginFactory. See also the API documentation for
lbajardsilogic@0 58 Vamp feature extraction plugins at
lbajardsilogic@0 59 http://www.vamp-plugins.org/code-doc/.
lbajardsilogic@0 60
lbajardsilogic@0 61 - File reading and writing code: AudioFileReader and subclasses,
lbajardsilogic@0 62 WavFileWriter, DataFileReader, SVFileReader
lbajardsilogic@0 63
lbajardsilogic@0 64 - FFT calculation and cacheing: FFTModel, FFTDataServer
lbajardsilogic@0 65
lbajardsilogic@0 66 - Widgets that show groups of editable properties: PropertyBox for
lbajardsilogic@0 67 layer properties (contained in a PropertyStack), PluginParameterBox
lbajardsilogic@0 68 for plugins (contained in a PluginParameterDialog)
lbajardsilogic@0 69
lbajardsilogic@0 70 - Audio playback: AudioCallbackPlaySource and subclasses,
lbajardsilogic@0 71 AudioCallbackPlayTarget and subclasses, AudioGenerator
lbajardsilogic@0 72
lbajardsilogic@0 73 \section model Data sources: the Model hierarchy
lbajardsilogic@0 74
lbajardsilogic@0 75 A Model is something containing, or knowing how to obtain, data.
lbajardsilogic@0 76
lbajardsilogic@0 77 For example, WaveFileModel is a model that knows how to get data
lbajardsilogic@0 78 from an audio file; SparseTimeValueModel is a model containing
lbajardsilogic@0 79 editable "curve" data.
lbajardsilogic@0 80
lbajardsilogic@0 81 Models typically subclass one of a number of abstract subclasses of
lbajardsilogic@0 82 Model. For example, WaveFileModel subclasses DenseTimeValueModel,
lbajardsilogic@0 83 which describes an interface for models that have a value at each
lbajardsilogic@0 84 time point for a given sampling resolution. (Note that
lbajardsilogic@0 85 WaveFileModel does not actually read the files itself: it uses
lbajardsilogic@0 86 AudioFileReader classes for that. It just makes data from the
lbajardsilogic@0 87 files available in a Model.) SparseTimeValueModel uses the
lbajardsilogic@0 88 SparseModel template class, which provides most of the
lbajardsilogic@0 89 implementation for models that contain a series of points of some
lbajardsilogic@0 90 sort -- also used by NoteModel, TextModel, and
lbajardsilogic@0 91 SparseOneDimensionalModel.
lbajardsilogic@0 92
lbajardsilogic@0 93 Everything that goes on the screen originates from a model, via a
lbajardsilogic@0 94 layer (see below). The models are contained in a Document object.
lbajardsilogic@0 95 There is no containment hierarchy or ordering of models in the
lbajardsilogic@0 96 document. One model is the main model, which defines the sample
lbajardsilogic@0 97 rate for playback.
lbajardsilogic@0 98
lbajardsilogic@0 99 A model may also be marked as a "derived" model, which means it was
lbajardsilogic@0 100 generated from another model using some transform (feature
lbajardsilogic@0 101 extraction or effect plugin, etc) -- the idea being that they can
lbajardsilogic@0 102 be re-generated using the same transform if a new source model is
lbajardsilogic@0 103 loaded.
lbajardsilogic@0 104
lbajardsilogic@0 105 \section layer Things that can display data: the Layer hierarchy
lbajardsilogic@0 106
lbajardsilogic@0 107 A Layer is something that knows how to draw parts of a model onto a
lbajardsilogic@0 108 timeline.
lbajardsilogic@0 109
lbajardsilogic@0 110 For example, WaveformLayer is a layer which draws waveforms, based
lbajardsilogic@0 111 on WaveFileModel; TimeValueLayer draws curves, based on
lbajardsilogic@0 112 SparseTimeValueModel; SpectrogramLayer draws spectrograms, based on
lbajardsilogic@0 113 WaveFileModel (via FFTModel).
lbajardsilogic@0 114
lbajardsilogic@0 115 The most basic functions of a layer are: to draw itself onto a
lbajardsilogic@0 116 Pane, against a timeline on the x axis; and to permit user
lbajardsilogic@0 117 interaction. If you were thinking of adding the capability to
lbajardsilogic@0 118 display a new sort of something, then you would want to add a new
lbajardsilogic@0 119 layer type. (You may also need a new model type, depending on
lbajardsilogic@0 120 whether any existing model can capture the data you need.)
lbajardsilogic@0 121 Depending on the sort of data in question, there are various
lbajardsilogic@0 122 existing layers that might be appropriate to start from -- for
lbajardsilogic@0 123 example, a layer that displays images that the user has imported
lbajardsilogic@0 124 and associated with particular times might have something in common
lbajardsilogic@0 125 with the existing TextLayer which displays pieces of text that are
lbajardsilogic@0 126 associated with particular times.
lbajardsilogic@0 127
lbajardsilogic@0 128 Although layers are visual objects, they are contained in the
lbajardsilogic@0 129 Document in Sonic Visualiser rather than being managed together
lbajardsilogic@0 130 with display widgets. The Sonic Visualiser file format has
lbajardsilogic@0 131 separate data and layout sections, and the layers are defined in
lbajardsilogic@0 132 the data section and then referred to in the layout section which
lbajardsilogic@0 133 determines which layers may go on which panes (see Pane below).
lbajardsilogic@0 134
lbajardsilogic@0 135 Once a layer class is defined, some basic data about it needs to be
lbajardsilogic@0 136 set up in the LayerFactory class, and then it will appear in the
lbajardsilogic@0 137 menus and so on on the main window.
lbajardsilogic@0 138
lbajardsilogic@0 139 \section view Widgets that are used to show layers: The View hierarchy
lbajardsilogic@0 140
lbajardsilogic@0 141 A View is a widget that displays a stack of layers. The most
lbajardsilogic@0 142 important subclass is Pane, the widget that is used to show most of
lbajardsilogic@0 143 the data in the main window of Sonic Visualiser.
lbajardsilogic@0 144
lbajardsilogic@0 145 All a pane really does is contain a set of layers and get them to
lbajardsilogic@0 146 render themselves (one on top of the other, with the topmost layer
lbajardsilogic@0 147 being the one that is currently interacted with), cache the
lbajardsilogic@0 148 results, negotiate user interaction with them, and so on. This is
lbajardsilogic@0 149 generally fiddly, if not especially interesting. Panes are
lbajardsilogic@0 150 strictly layout objects and are not stored in the Document class;
lbajardsilogic@0 151 instead the MainWindow contains a PaneStack widget (the widget that
lbajardsilogic@0 152 takes up most of Sonic Visualiser's main window) which contains a
lbajardsilogic@0 153 set of panes stacked vertically.
lbajardsilogic@0 154
lbajardsilogic@0 155 Another View subclass is Overview, which is the widget that
lbajardsilogic@0 156 contains that green waveform showing the entire file at the bottom
lbajardsilogic@0 157 of the window.
lbajardsilogic@0 158
lbajardsilogic@0 159 */
lbajardsilogic@197 160
lbajardsilogic@197 161 #ifdef QT_NODLL
lbajardsilogic@197 162 #include <QtPlugin>
lbajardsilogic@197 163 #endif
lbajardsilogic@0 164
lbajardsilogic@0 165 static QMutex cleanupMutex;
lbajardsilogic@0 166
lbajardsilogic@0 167 static void
lbajardsilogic@0 168 signalHandler(int /* signal */)
lbajardsilogic@0 169 {
lbajardsilogic@0 170 // Avoid this happening more than once across threads
lbajardsilogic@0 171
lbajardsilogic@0 172 cleanupMutex.lock();
lbajardsilogic@0 173 std::cerr << "signalHandler: cleaning up and exiting" << std::endl;
lbajardsilogic@0 174 TempDirectory::getInstance()->cleanup();
ivand_qmul@125 175
ivand_qmul@125 176 exit(0); // without releasing mutex
lbajardsilogic@0 177 }
lbajardsilogic@0 178
lbajardsilogic@0 179 class SVApplication : public QApplication
lbajardsilogic@0 180 {
lbajardsilogic@0 181 public:
lbajardsilogic@0 182 SVApplication(int argc, char **argv) :
lbajardsilogic@0 183 QApplication(argc, argv),
lbajardsilogic@0 184 m_mainWindow(0) { }
lbajardsilogic@0 185 virtual ~SVApplication() { }
lbajardsilogic@0 186
lbajardsilogic@0 187 void setMainWindow(MainWindow *mw) { m_mainWindow = mw; }
lbajardsilogic@0 188 void releaseMainWindow() { m_mainWindow = 0; }
lbajardsilogic@0 189
lbajardsilogic@0 190 virtual void commitData(QSessionManager &manager) {
lbajardsilogic@0 191 if (!m_mainWindow) return;
lbajardsilogic@0 192 bool mayAskUser = manager.allowsInteraction();
lbajardsilogic@0 193 bool success = m_mainWindow->commitData(mayAskUser);
lbajardsilogic@0 194 manager.release();
lbajardsilogic@0 195 if (!success) manager.cancel();
lbajardsilogic@0 196 }
lbajardsilogic@0 197
lbajardsilogic@0 198 protected:
lbajardsilogic@0 199 MainWindow *m_mainWindow;
lbajardsilogic@0 200 };
lbajardsilogic@197 201
lbajardsilogic@197 202 #ifdef QT_NODLL
lbajardsilogic@197 203 Q_IMPORT_PLUGIN(qjpeg)
lbajardsilogic@197 204 Q_IMPORT_PLUGIN(qgif)
lbajardsilogic@197 205 Q_IMPORT_PLUGIN(qmng)
lbajardsilogic@197 206 #endif
lbajardsilogic@0 207
benoitrigolleau@217 208
lbajardsilogic@197 209 int main(int argc, char **argv)
lbajardsilogic@0 210 {
benoitrigolleau@58 211
lbajardsilogic@0 212 SVApplication application(argc, argv);
benoitrigolleau@217 213
benoitrigolleau@58 214 QSplashScreen *splash = new QSplashScreen;
benoitrigolleau@58 215 splash->setPixmap(QPixmap(":icons/splashscreen.png"));
benoitrigolleau@58 216 splash->show();
benoitrigolleau@217 217
lbajardsilogic@0 218 QStringList args = application.arguments();
lbajardsilogic@0 219
lbajardsilogic@0 220 signal(SIGINT, signalHandler);
lbajardsilogic@0 221 signal(SIGTERM, signalHandler);
lbajardsilogic@0 222
lbajardsilogic@0 223 #ifndef Q_WS_WIN32
lbajardsilogic@0 224 signal(SIGHUP, signalHandler);
lbajardsilogic@0 225 signal(SIGQUIT, signalHandler);
lbajardsilogic@0 226 #endif
lbajardsilogic@0 227
lbajardsilogic@62 228 bool redirectStderr = true;
lbajardsilogic@62 229
lbajardsilogic@62 230 if (args.contains("-log"))
lbajardsilogic@62 231 {
lbajardsilogic@62 232 redirectStderr = false;
lbajardsilogic@62 233 }
lbajardsilogic@62 234
lbajardsilogic@62 235 svSystemSpecificInitialisation(redirectStderr);
lbajardsilogic@0 236
lbajardsilogic@0 237 bool audioOutput = true;
lbajardsilogic@0 238 bool oscSupport = true;
lbajardsilogic@0 239
lbajardsilogic@0 240 if (args.contains("--help") || args.contains("-h") || args.contains("-?")) {
lbajardsilogic@0 241 std::cerr << QApplication::tr(
lbajardsilogic@41 242 "\nSound Access is a program for viewing and exploring multimedia archives for semantic multimedia analysis and retrieval.\n").arg(argv[0]).toStdString() << std::endl;
lbajardsilogic@0 243 exit(2);
lbajardsilogic@0 244 }
lbajardsilogic@0 245
lbajardsilogic@0 246 if (args.contains("--no-audio")) audioOutput = false;
lbajardsilogic@0 247 if (args.contains("--no-osc")) oscSupport = false;
lbajardsilogic@0 248
lbajardsilogic@41 249 QApplication::setOrganizationName("sound-access");
lbajardsilogic@41 250 QApplication::setOrganizationDomain("easaier.org");
lbajardsilogic@41 251 QApplication::setApplicationName("sound-access");
lbajardsilogic@0 252 QApplication::setWindowIcon(QIcon(":icons/svicon16.png"));
benoitrigolleau@58 253 //QApplication::setStyle(new QPlastiqueStyle);
benoitrigolleau@58 254 QApplication::setStyle(new EasaierStyle);
lbajardsilogic@2 255
lbajardsilogic@0 256 QString language = QLocale::system().name();
lbajardsilogic@0 257
lbajardsilogic@0 258 QTranslator qtTranslator;
lbajardsilogic@0 259 QString qtTrName = QString("qt_%1").arg(language);
lbajardsilogic@0 260 std::cerr << "Loading " << qtTrName.toStdString() << "..." << std::endl;
lbajardsilogic@0 261 qtTranslator.load(qtTrName);
lbajardsilogic@0 262 application.installTranslator(&qtTranslator);
lbajardsilogic@0 263
lbajardsilogic@0 264 QTranslator svTranslator;
lbajardsilogic@41 265 QString svTrName = QString("sound-access_%1").arg(language);
lbajardsilogic@0 266 std::cerr << "Loading " << svTrName.toStdString() << "..." << std::endl;
lbajardsilogic@55 267 if (svTranslator.load(svTrName, ":/i18n"))
lbajardsilogic@55 268 application.installTranslator(&svTranslator);
lbajardsilogic@0 269
lbajardsilogic@0 270 // Permit size_t and PropertyName to be used as args in queued signal calls
lbajardsilogic@0 271 qRegisterMetaType<size_t>("size_t");
lbajardsilogic@0 272 qRegisterMetaType<PropertyContainer::PropertyName>("PropertyContainer::PropertyName");
lbajardsilogic@0 273
lbajardsilogic@0 274 MainWindow gui(audioOutput, oscSupport);
lbajardsilogic@0 275 application.setMainWindow(&gui);
lbajardsilogic@0 276
lbajardsilogic@0 277 QDesktopWidget *desktop = QApplication::desktop();
lbajardsilogic@0 278 QRect available = desktop->availableGeometry();
lbajardsilogic@0 279
lbajardsilogic@0 280 int width = available.width() * 2 / 3;
lbajardsilogic@0 281 int height = available.height() / 2;
lbajardsilogic@0 282 if (height < 450) height = available.height() * 2 / 3;
lbajardsilogic@0 283 if (width > height * 2) width = height * 2;
lbajardsilogic@0 284
lbajardsilogic@0 285 QSettings settings;
lbajardsilogic@0 286 settings.beginGroup("MainWindow");
lbajardsilogic@0 287 QSize size = settings.value("size", QSize(width, height)).toSize();
lbajardsilogic@0 288 gui.resize(size);
lbajardsilogic@0 289 if (settings.contains("position")) {
lbajardsilogic@0 290 gui.move(settings.value("position").toPoint());
lbajardsilogic@0 291 }
lbajardsilogic@0 292 settings.endGroup();
lbajardsilogic@0 293
benoitrigolleau@58 294 //delay(5000);
benoitrigolleau@58 295
lbajardsilogic@0 296 gui.show();
benoitrigolleau@58 297 splash->finish(&gui);
benoitrigolleau@58 298 delete splash;
lbajardsilogic@0 299
lbajardsilogic@0 300 bool haveSession = false;
lbajardsilogic@0 301 bool haveMainModel = false;
lbajardsilogic@0 302
lbajardsilogic@0 303 for (QStringList::iterator i = args.begin(); i != args.end(); ++i) {
lbajardsilogic@0 304
lbajardsilogic@0 305 MainWindow::FileOpenStatus status = MainWindow::FileOpenFailed;
lbajardsilogic@0 306
lbajardsilogic@0 307 if (i == args.begin()) continue;
lbajardsilogic@0 308 if (i->startsWith('-')) continue;
lbajardsilogic@0 309
lbajardsilogic@0 310 if (i->startsWith("http:") || i->startsWith("ftp:")) {
lbajardsilogic@0 311 status = gui.openURL(QUrl(*i));
lbajardsilogic@0 312 continue;
lbajardsilogic@0 313 }
lbajardsilogic@0 314
lbajardsilogic@0 315 QString path = *i;
lbajardsilogic@0 316
lbajardsilogic@0 317 if (path.endsWith("sv")) {
lbajardsilogic@0 318 if (!haveSession) {
lbajardsilogic@0 319 status = gui.openSessionFile(path);
lbajardsilogic@0 320 if (status == MainWindow::FileOpenSucceeded) {
lbajardsilogic@0 321 haveSession = true;
lbajardsilogic@0 322 haveMainModel = true;
lbajardsilogic@0 323 }
lbajardsilogic@0 324 } else {
lbajardsilogic@0 325 std::cerr << "WARNING: Ignoring additional session file argument \"" << path.toStdString() << "\"" << std::endl;
lbajardsilogic@0 326 status = MainWindow::FileOpenSucceeded;
lbajardsilogic@0 327 }
lbajardsilogic@0 328 }
lbajardsilogic@0 329 if (status != MainWindow::FileOpenSucceeded) {
lbajardsilogic@0 330 if (!haveMainModel) {
lbajardsilogic@0 331 status = gui.openSomeFile(path, MainWindow::ReplaceMainModel);
lbajardsilogic@0 332 if (status == MainWindow::FileOpenSucceeded) haveMainModel = true;
lbajardsilogic@0 333 } else {
lbajardsilogic@0 334 status = gui.openSomeFile(path, MainWindow::CreateAdditionalModel);
lbajardsilogic@0 335 }
lbajardsilogic@0 336 }
lbajardsilogic@0 337 if (status == MainWindow::FileOpenFailed) {
lbajardsilogic@0 338 QMessageBox::critical
lbajardsilogic@0 339 (&gui, QMessageBox::tr("Failed to open file"),
lbajardsilogic@0 340 QMessageBox::tr("File \"%1\" could not be opened").arg(path));
lbajardsilogic@0 341 }
lbajardsilogic@0 342 }
lbajardsilogic@0 343 /*
lbajardsilogic@0 344 TipDialog tipDialog;
lbajardsilogic@0 345 if (tipDialog.isOK()) {
lbajardsilogic@0 346 tipDialog.exec();
lbajardsilogic@0 347 }
lbajardsilogic@0 348 */
lbajardsilogic@0 349 int rv = application.exec();
lbajardsilogic@0 350 // std::cerr << "application.exec() returned " << rv << std::endl;
ivand_qmul@125 351
lbajardsilogic@0 352 cleanupMutex.lock();
lbajardsilogic@0 353 TempDirectory::getInstance()->cleanup();
lbajardsilogic@0 354 application.releaseMainWindow();
lbajardsilogic@0 355
lbajardsilogic@0 356 return rv;
lbajardsilogic@0 357 }