rework check for old configuration files
[ardour.git] / libs / ardour / analyser.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "ardour/analyser.h"
21 #include "ardour/audiofilesource.h"
22 #include "ardour/session_event.h"
23 #include "ardour/transient_detector.h"
24
25 using namespace std;
26 using namespace ARDOUR;
27 using namespace PBD;
28
29 Analyser* Analyser::the_analyser = 0;
30 Glib::Threads::Mutex Analyser::analysis_queue_lock;
31 Glib::Threads::Cond  Analyser::SourcesToAnalyse;
32 list<boost::weak_ptr<Source> > Analyser::analysis_queue;
33
34 Analyser::Analyser ()
35 {
36
37 }
38
39 Analyser::~Analyser ()
40 {
41 }
42
43 static void
44 analyser_work ()
45 {
46         Analyser::work ();
47 }
48
49 void
50 Analyser::init ()
51 {
52         Glib::Threads::Thread::create (sigc::ptr_fun (analyser_work));
53 }
54
55 void
56 Analyser::queue_source_for_analysis (boost::shared_ptr<Source> src, bool force)
57 {
58         if (!src->can_be_analysed()) {
59                 return;
60         }
61
62         if (!force && src->has_been_analysed()) {
63                 return;
64         }
65
66         Glib::Threads::Mutex::Lock lm (analysis_queue_lock);
67         analysis_queue.push_back (boost::weak_ptr<Source>(src));
68         SourcesToAnalyse.broadcast ();
69 }
70
71 void
72 Analyser::work ()
73 {
74         SessionEvent::create_per_thread_pool ("Analyser", 64);
75
76         while (true) {
77                 analysis_queue_lock.lock ();
78
79           wait:
80                 if (analysis_queue.empty()) {
81                         SourcesToAnalyse.wait (analysis_queue_lock);
82                 }
83
84                 if (analysis_queue.empty()) {
85                         goto wait;
86                 }
87
88                 boost::shared_ptr<Source> src (analysis_queue.front().lock());
89                 analysis_queue.pop_front();
90                 analysis_queue_lock.unlock ();
91
92                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
93
94                 if (afs && afs->length(afs->timeline_position())) {
95                         analyse_audio_file_source (afs);
96                 }
97         }
98 }
99
100 void
101 Analyser::analyse_audio_file_source (boost::shared_ptr<AudioFileSource> src)
102 {
103         AnalysisFeatureList results;
104
105         TransientDetector td (src->sample_rate());
106
107         if (td.run (src->get_transients_path(), src.get(), 0, results) == 0) {
108                 src->set_been_analysed (true);
109         } else {
110                 src->set_been_analysed (false);
111         }
112
113 }
114