fix crash when copy'ing latent plugins
[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/rc_configuration.h"
23 #include "ardour/session_event.h"
24 #include "ardour/transient_detector.h"
25
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28 #include "pbd/i18n.h"
29
30 using namespace std;
31 using namespace ARDOUR;
32 using namespace PBD;
33
34 Analyser* Analyser::the_analyser = 0;
35 Glib::Threads::Mutex Analyser::analysis_active_lock;
36 Glib::Threads::Mutex Analyser::analysis_queue_lock;
37 Glib::Threads::Cond  Analyser::SourcesToAnalyse;
38 list<boost::weak_ptr<Source> > Analyser::analysis_queue;
39
40 Analyser::Analyser ()
41 {
42
43 }
44
45 Analyser::~Analyser ()
46 {
47 }
48
49 static void
50 analyser_work ()
51 {
52         Analyser::work ();
53 }
54
55 void
56 Analyser::init ()
57 {
58         Glib::Threads::Thread::create (sigc::ptr_fun (analyser_work));
59 }
60
61 void
62 Analyser::queue_source_for_analysis (boost::shared_ptr<Source> src, bool force)
63 {
64         if (!src->can_be_analysed()) {
65                 return;
66         }
67
68         if (!force && src->has_been_analysed()) {
69                 return;
70         }
71
72         Glib::Threads::Mutex::Lock lm (analysis_queue_lock);
73         analysis_queue.push_back (boost::weak_ptr<Source>(src));
74         SourcesToAnalyse.broadcast ();
75 }
76
77 void
78 Analyser::work ()
79 {
80         SessionEvent::create_per_thread_pool ("Analyser", 64);
81
82         while (true) {
83                 analysis_queue_lock.lock ();
84
85           wait:
86                 if (analysis_queue.empty()) {
87                         SourcesToAnalyse.wait (analysis_queue_lock);
88                 }
89
90                 if (analysis_queue.empty()) {
91                         goto wait;
92                 }
93
94                 boost::shared_ptr<Source> src (analysis_queue.front().lock());
95                 analysis_queue.pop_front();
96                 analysis_queue_lock.unlock ();
97
98                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
99
100                 if (afs && afs->length(afs->timeline_position())) {
101                         Glib::Threads::Mutex::Lock lm (analysis_active_lock);
102                         analyse_audio_file_source (afs);
103                 }
104         }
105 }
106
107 void
108 Analyser::analyse_audio_file_source (boost::shared_ptr<AudioFileSource> src)
109 {
110         AnalysisFeatureList results;
111
112         try {
113                 TransientDetector td (src->sample_rate());
114                 td.set_sensitivity (3, Config->get_transient_sensitivity()); // "General purpose"
115                 if (td.run (src->get_transients_path(), src.get(), 0, results) == 0) {
116                         src->set_been_analysed (true);
117                 } else {
118                         src->set_been_analysed (false);
119                 }
120         } catch (...) {
121                 error << string_compose(_("Transient Analysis failed for %1."), _("Audio File Source")) << endmsg;;
122                 src->set_been_analysed (false);
123                 return;
124         }
125 }
126
127 void
128 Analyser::flush ()
129 {
130         Glib::Threads::Mutex::Lock lq (analysis_queue_lock);
131         Glib::Threads::Mutex::Lock la (analysis_active_lock);
132         analysis_queue.clear();
133 }