1 /*
2  * Copyright (C) 2008-2012 David Robillard <d@drobilla.net>
3  * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2008 Nick Mainsbridge <mainsbridge@gmail.com>
5  * Copyright (C) 2015-2016 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 
23 #include "ardour/analyser.h"
24 #include "ardour/audiofilesource.h"
25 #include "ardour/rc_configuration.h"
26 #include "ardour/session_event.h"
27 #include "ardour/transient_detector.h"
28 
29 #include "pbd/compose.h"
30 #include "pbd/error.h"
31 #include "pbd/pthread_utils.h"
32 
33 #include "pbd/i18n.h"
34 
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38 
39 Analyser* Analyser::the_analyser = 0;
40 Glib::Threads::Mutex Analyser::analysis_active_lock;
41 Glib::Threads::Mutex Analyser::analysis_queue_lock;
42 Glib::Threads::Cond  Analyser::SourcesToAnalyse;
43 list<boost::weak_ptr<Source> > Analyser::analysis_queue;
44 
Analyser()45 Analyser::Analyser ()
46 {
47 
48 }
49 
~Analyser()50 Analyser::~Analyser ()
51 {
52 }
53 
54 static void
analyser_work()55 analyser_work ()
56 {
57 	pthread_set_name ("Analyzer");
58 	Analyser::work ();
59 }
60 
61 void
init()62 Analyser::init ()
63 {
64 	Glib::Threads::Thread::create (sigc::ptr_fun (analyser_work));
65 }
66 
67 void
queue_source_for_analysis(boost::shared_ptr<Source> src,bool force)68 Analyser::queue_source_for_analysis (boost::shared_ptr<Source> src, bool force)
69 {
70 	if (!src->can_be_analysed()) {
71 		return;
72 	}
73 
74 	if (!force && src->has_been_analysed()) {
75 		return;
76 	}
77 
78 	Glib::Threads::Mutex::Lock lm (analysis_queue_lock);
79 	analysis_queue.push_back (boost::weak_ptr<Source>(src));
80 	SourcesToAnalyse.broadcast ();
81 }
82 
83 void
work()84 Analyser::work ()
85 {
86 	SessionEvent::create_per_thread_pool ("Analyser", 64);
87 
88 	while (true) {
89 		analysis_queue_lock.lock ();
90 
91 	  wait:
92 		if (analysis_queue.empty()) {
93 			SourcesToAnalyse.wait (analysis_queue_lock);
94 		}
95 
96 		if (analysis_queue.empty()) {
97 			goto wait;
98 		}
99 
100 		boost::shared_ptr<Source> src (analysis_queue.front().lock());
101 		analysis_queue.pop_front();
102 		analysis_queue_lock.unlock ();
103 
104 		boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
105 
106 		if (afs && afs->length(afs->natural_position())) {
107 			Glib::Threads::Mutex::Lock lm (analysis_active_lock);
108 			analyse_audio_file_source (afs);
109 		}
110 	}
111 }
112 
113 void
analyse_audio_file_source(boost::shared_ptr<AudioFileSource> src)114 Analyser::analyse_audio_file_source (boost::shared_ptr<AudioFileSource> src)
115 {
116 	AnalysisFeatureList results;
117 
118 	try {
119 		TransientDetector td (src->sample_rate());
120 		td.set_sensitivity (3, Config->get_transient_sensitivity()); // "General purpose"
121 		if (td.run (src->get_transients_path(), src.get(), 0, results) == 0) {
122 			src->set_been_analysed (true);
123 		} else {
124 			src->set_been_analysed (false);
125 		}
126 	} catch (...) {
127 		error << string_compose(_("Transient Analysis failed for %1."), _("Audio File Source")) << endmsg;;
128 		src->set_been_analysed (false);
129 		return;
130 	}
131 }
132 
133 void
flush()134 Analyser::flush ()
135 {
136 	Glib::Threads::Mutex::Lock lq (analysis_queue_lock);
137 	Glib::Threads::Mutex::Lock la (analysis_active_lock);
138 	analysis_queue.clear();
139 }
140