major design changes: use glib event loop for MIDI thread/UI; rework design of BaseUI...
[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 #include "pbd/pthread_utils.h"
26 #include "pbd/convert.h"
27
28 using namespace std;
29 using namespace sigc;
30 using namespace ARDOUR;
31 using namespace PBD;
32
33 Analyser* Analyser::the_analyser = 0;
34 Glib::StaticMutex Analyser::analysis_queue_lock = GLIBMM_STATIC_MUTEX_INIT;
35 Glib::Cond* Analyser::SourcesToAnalyse = 0;
36 list<boost::weak_ptr<Source> > Analyser::analysis_queue;
37
38 Analyser::Analyser ()
39 {
40
41 }
42
43 Analyser::~Analyser ()
44 {
45 }
46
47 static void
48 analyser_work ()
49 {
50         Analyser::work ();
51 }
52
53 void
54 Analyser::init ()
55 {
56         SourcesToAnalyse = new Glib::Cond();
57         Glib::Thread::create (sigc::ptr_fun (analyser_work), false);
58 }
59
60 void
61 Analyser::queue_source_for_analysis (boost::shared_ptr<Source> src, bool force)
62 {
63         if (!src->can_be_analysed()) {
64                 return;
65         }
66
67         if (!force && src->has_been_analysed()) {
68                 return;
69         }
70
71         Glib::Mutex::Lock lm (analysis_queue_lock);
72         analysis_queue.push_back (boost::weak_ptr<Source>(src));
73         SourcesToAnalyse->broadcast ();
74 }
75
76 void
77 Analyser::work ()
78 {
79         SessionEvent::create_per_thread_pool ("Analyser", 64);
80
81         while (true) {
82                 analysis_queue_lock.lock ();
83
84           wait:
85                 if (analysis_queue.empty()) {
86                         SourcesToAnalyse->wait (analysis_queue_lock);
87                 }
88
89                 if (analysis_queue.empty()) {
90                         goto wait;
91                 }
92
93                 boost::shared_ptr<Source> src (analysis_queue.front().lock());
94                 analysis_queue.pop_front();
95                 analysis_queue_lock.unlock ();
96
97                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
98
99                 if (afs && afs->length(afs->timeline_position())) {
100                         analyse_audio_file_source (afs);
101                 }
102         }
103 }
104
105 void
106 Analyser::analyse_audio_file_source (boost::shared_ptr<AudioFileSource> src)
107 {
108         AnalysisFeatureList results;
109
110         TransientDetector td (src->sample_rate());
111
112         if (td.run (src->get_transients_path(), src.get(), 0, results) == 0) {
113                 src->set_been_analysed (true);
114         } else {
115                 src->set_been_analysed (false);
116         }
117
118 }
119