Missed update to private test repo version.
[dcpomatic.git] / src / lib / analyse_audio_job.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "analyse_audio_job.h"
23 #include "audio_analysis.h"
24 #include "compose.hpp"
25 #include "dcpomatic_log.h"
26 #include "film.h"
27 #include "filter.h"
28 #include "player.h"
29 #include "playlist.h"
30 #include "config.h"
31 #include <iostream>
32
33 #include "i18n.h"
34
35
36 using std::cout;
37 using std::dynamic_pointer_cast;
38 using std::make_shared;
39 using std::max;
40 using std::min;
41 using std::shared_ptr;
42 using std::string;
43 using std::vector;
44 using namespace dcpomatic;
45 #if BOOST_VERSION >= 106100
46 using namespace boost::placeholders;
47 #endif
48
49
50 /** @param whole_film true to analyse the whole film' audio (i.e. start from time 0 and use processors), false
51  *  to analyse just the single piece of content in the playlist (i.e. start from Playlist::start() and do not
52  *  use processors.
53  */
54 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist, bool whole_film)
55         : Job (film)
56         , _analyser(film, playlist, whole_film, boost::bind(&Job::set_progress, this, _1, false))
57         , _playlist (playlist)
58         , _path (film->audio_analysis_path(playlist))
59         , _whole_film(whole_film)
60 {
61         LOG_DEBUG_AUDIO_ANALYSIS_NC("AnalyseAudioJob::AnalyseAudioJob");
62 }
63
64
65 AnalyseAudioJob::~AnalyseAudioJob ()
66 {
67         stop_thread ();
68 }
69
70
71 string
72 AnalyseAudioJob::name () const
73 {
74         return _("Analysing audio");
75 }
76
77
78 string
79 AnalyseAudioJob::json_name () const
80 {
81         return N_("analyse_audio");
82 }
83
84
85 void
86 AnalyseAudioJob::run ()
87 {
88         LOG_DEBUG_AUDIO_ANALYSIS_NC("AnalyseAudioJob::run");
89
90         auto player = make_shared<Player>(_film, _playlist);
91         player->set_ignore_video ();
92         player->set_ignore_text ();
93         player->set_fast ();
94         player->set_play_referenced ();
95         player->Audio.connect (bind(&AudioAnalyser::analyse, &_analyser, _1, _2));
96         if (!_whole_film) {
97                 player->set_disable_audio_processor();
98         }
99
100         bool has_any_audio = false;
101         for (auto c: _playlist->content()) {
102                 if (c->audio) {
103                         has_any_audio = true;
104                 }
105         }
106
107         if (has_any_audio) {
108                 player->seek (_analyser.start(), true);
109                 while (!player->pass ()) {}
110         }
111
112         LOG_DEBUG_AUDIO_ANALYSIS_NC("Loop complete");
113
114         _analyser.finish ();
115         auto analysis = _analyser.get();
116         analysis.write (_path);
117
118         LOG_DEBUG_AUDIO_ANALYSIS_NC("Job finished");
119         set_progress (1);
120         set_state (FINISHED_OK);
121 }