1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Rosegarden
5     A MIDI and audio sequencer and musical notation editor.
6     Copyright 2000-2021 the Rosegarden development team.
7 
8     This file is Copyright 2005-2011 Chris Cannam.
9 
10     This program is free software; you can redistribute it and/or
11     modify it under the terms of the GNU General Public License as
12     published by the Free Software Foundation; either version 2 of the
13     License, or (at your option) any later version.  See the file
14     COPYING included with this distribution for more information.
15 */
16 
17 
18 #include "AudioReadStreamFactory.h"
19 #include "AudioReadStream.h"
20 
21 #include "base/ThingFactory.h"
22 #include "misc/Debug.h"
23 
24 #include <QFileInfo>
25 
26 #define DEBUG_AUDIO_READ_STREAM_FACTORY 1
27 
28 namespace Rosegarden {
29 
30 typedef ThingFactory<AudioReadStream, QString>
31 AudioReadStreamFactoryImpl;
32 
33 template <>
34 AudioReadStreamFactoryImpl *
35 AudioReadStreamFactoryImpl::m_instance = nullptr;
36 
37 AudioReadStream *
createReadStream(QString audioFileName)38 AudioReadStreamFactory::createReadStream(QString audioFileName)
39 {
40     AudioReadStream *s = nullptr;
41 
42     QString extension = QFileInfo(audioFileName).suffix().toLower();
43 
44     AudioReadStreamFactoryImpl *f = AudioReadStreamFactoryImpl::getInstance();
45 
46     // Try to use a reader that has actually registered an interest in
47     // this extension, first
48 
49     try {
50         s = f->createFor(extension, audioFileName);
51     } catch (...) {
52     }
53 
54     if (s && s->isOK() && s->getError() == "") {
55         return s;
56     } else if (s) {
57         std::cerr << "Error with recommended reader: \""
58                   << s->getError().toStdString() << "\""
59                   << std::endl;
60     }
61 
62     delete s;
63     s = nullptr;
64 
65     // If that fails, try all readers in arbitrary order
66 
67     AudioReadStreamFactoryImpl::URISet uris = f->getURIs();
68 
69     for (AudioReadStreamFactoryImpl::URISet::const_iterator i = uris.begin();
70          i != uris.end(); ++i) {
71 
72         try {
73             s = f->create(*i, audioFileName);
74         } catch (UnknownThingException) { }
75 
76         if (s && s->isOK() && s->getError() == "") {
77             return s;
78         }
79 
80         delete s;
81         s = nullptr;
82     }
83 
84     return nullptr;
85 }
86 
87 QStringList
getSupportedFileExtensions()88 AudioReadStreamFactory::getSupportedFileExtensions()
89 {
90     return AudioReadStreamFactoryImpl::getInstance()->getTags();
91 }
92 
93 }
94 
95