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 from_zero true to analyse audio from time 0 in the playlist, otherwise begin at Playlist::start */
51 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist, bool from_zero)
52         : Job (film)
53         , _analyser (film, playlist, from_zero, boost::bind(&Job::set_progress, this, _1, false))
54         , _playlist (playlist)
55         , _path (film->audio_analysis_path(playlist))
56 {
57         LOG_DEBUG_AUDIO_ANALYSIS_NC("AnalyseAudioJob::AnalyseAudioJob");
58 }
59
60
61 AnalyseAudioJob::~AnalyseAudioJob ()
62 {
63         stop_thread ();
64 }
65
66
67 string
68 AnalyseAudioJob::name () const
69 {
70         return _("Analysing audio");
71 }
72
73
74 string
75 AnalyseAudioJob::json_name () const
76 {
77         return N_("analyse_audio");
78 }
79
80
81 void
82 AnalyseAudioJob::run ()
83 {
84         LOG_DEBUG_AUDIO_ANALYSIS_NC("AnalyseAudioJob::run");
85
86         auto player = make_shared<Player>(_film, _playlist);
87         player->set_ignore_video ();
88         player->set_ignore_text ();
89         player->set_fast ();
90         player->set_play_referenced ();
91         player->Audio.connect (bind(&AudioAnalyser::analyse, &_analyser, _1, _2));
92
93         bool has_any_audio = false;
94         for (auto c: _playlist->content()) {
95                 if (c->audio) {
96                         has_any_audio = true;
97                 }
98         }
99
100         if (has_any_audio) {
101                 player->seek (_analyser.start(), true);
102                 while (!player->pass ()) {}
103         }
104
105         LOG_DEBUG_AUDIO_ANALYSIS_NC("Loop complete");
106
107         _analyser.finish ();
108         auto analysis = _analyser.get();
109         analysis.write (_path);
110
111         LOG_DEBUG_AUDIO_ANALYSIS_NC("Job finished");
112         set_progress (1);
113         set_state (FINISHED_OK);
114 }