ardour
analyser.cc
Go to the documentation of this file.
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"
24 
25 #include "pbd/compose.h"
26 #include "pbd/error.h"
27 #include "i18n.h"
28 
29 using namespace std;
30 using namespace ARDOUR;
31 using namespace PBD;
32 
33 Analyser* Analyser::the_analyser = 0;
34 Glib::Threads::Mutex Analyser::analysis_queue_lock;
35 Glib::Threads::Cond Analyser::SourcesToAnalyse;
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
49 {
50  Analyser::work ();
51 }
52 
53 void
55 {
56  Glib::Threads::Thread::create (sigc::ptr_fun (analyser_work));
57 }
58 
59 void
60 Analyser::queue_source_for_analysis (boost::shared_ptr<Source> src, bool force)
61 {
62  if (!src->can_be_analysed()) {
63  return;
64  }
65 
66  if (!force && src->has_been_analysed()) {
67  return;
68  }
69 
70  Glib::Threads::Mutex::Lock lm (analysis_queue_lock);
71  analysis_queue.push_back (boost::weak_ptr<Source>(src));
72  SourcesToAnalyse.broadcast ();
73 }
74 
75 void
76 Analyser::work ()
77 {
78  SessionEvent::create_per_thread_pool ("Analyser", 64);
79 
80  while (true) {
81  analysis_queue_lock.lock ();
82 
83  wait:
84  if (analysis_queue.empty()) {
85  SourcesToAnalyse.wait (analysis_queue_lock);
86  }
87 
88  if (analysis_queue.empty()) {
89  goto wait;
90  }
91 
92  boost::shared_ptr<Source> src (analysis_queue.front().lock());
93  analysis_queue.pop_front();
94  analysis_queue_lock.unlock ();
95 
97 
98  if (afs && afs->length(afs->timeline_position())) {
99  analyse_audio_file_source (afs);
100  }
101  }
102 }
103 
104 void
105 Analyser::analyse_audio_file_source (boost::shared_ptr<AudioFileSource> src)
106 {
107  AnalysisFeatureList results;
108 
109  try {
110  TransientDetector td (src->sample_rate());
111  if (td.run (src->get_transients_path(), src.get(), 0, results) == 0) {
112  src->set_been_analysed (true);
113  } else {
114  src->set_been_analysed (false);
115  }
116  } catch (...) {
117  error << string_compose(_("Transient Analysis failed for %1."), _("Audio File Source")) << endmsg;;
118  src->set_been_analysed (false);
119  return;
120  }
121 }
virtual float sample_rate() const =0
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r)
Definition: shared_ptr.hpp:396
Definition: Beats.hpp:239
LIBPBD_API Transmitter error
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:71
bool has_been_analysed() const
Definition: source.cc:161
#define _(Text)
Definition: i18n.h:11
std::list< framepos_t > AnalysisFeatureList
Definition: types.h:530
virtual bool can_be_analysed() const
Definition: source.h:90
Definition: amp.h:29
T * get() const
Definition: shared_ptr.hpp:268
static void analyser_work()
Definition: analyser.cc:48
Definition: debug.h:30
framepos_t timeline_position() const
Definition: source.h:100
std::string get_transients_path() const
Definition: source.cc:208
framecnt_t length(framepos_t pos) const
Definition: audiosource.cc:161
LIBARDOUR_API bool init(bool with_vst, bool try_optimization, const char *localedir)
Definition: globals.cc:376
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208
virtual void set_been_analysed(bool yn)
Definition: source.cc:168