ardour
filter.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004-2007 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 <time.h>
21 #include <cerrno>
22 
23 #include "pbd/basename.h"
24 
25 #include "ardour/analyser.h"
26 #include "ardour/audiofilesource.h"
27 #include "ardour/audioregion.h"
28 #include "ardour/filter.h"
29 #include "ardour/region.h"
30 #include "ardour/region_factory.h"
31 #include "ardour/session.h"
32 #include "ardour/smf_source.h"
33 #include "ardour/source_factory.h"
34 
35 #include "i18n.h"
36 
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40 
41 int
42 Filter::make_new_sources (boost::shared_ptr<Region> region, SourceList& nsrcs, string suffix)
43 {
44  vector<string> names = region->master_source_names();
45  assert (region->n_channels() <= names.size());
46 
47  for (uint32_t i = 0; i < region->n_channels(); ++i) {
48 
49  string name = PBD::basename_nosuffix (names[i]);
50 
51  /* remove any existing version of suffix by assuming it starts
52  with some kind of "special" character.
53  */
54 
55  if (!suffix.empty()) {
56  string::size_type pos = name.find (suffix[0]);
57  if (pos != string::npos && pos > 2) {
58  name = name.substr (0, pos - 1);
59  }
60  }
61 
62  const string path = (region->data_type() == DataType::MIDI)
63  ? session.new_midi_source_path (name)
64  : session.new_audio_source_path (name, region->n_channels(), i, false, false);
65 
66  if (path.empty()) {
67  error << string_compose (_("filter: error creating name for new file based on %1"), region->name())
68  << endmsg;
69  return -1;
70  }
71 
72  try {
73  nsrcs.push_back (boost::dynamic_pointer_cast<Source> (
74  SourceFactory::createWritable (region->data_type(), session,
75  path, false, session.frame_rate())));
76  }
77 
78  catch (failed_constructor& err) {
79  error << string_compose (_("filter: error creating new file %1 (%2)"), path, strerror (errno)) << endmsg;
80  return -1;
81  }
82  }
83 
84  return 0;
85 }
86 
87 int
88 Filter::finish (boost::shared_ptr<Region> region, SourceList& nsrcs, string region_name)
89 {
90  /* update headers on new sources */
91 
92  time_t xnow;
93  struct tm* now;
94 
95  time (&xnow);
96  now = localtime (&xnow);
97 
98  /* this is ugly. */
99  for (SourceList::iterator si = nsrcs.begin(); si != nsrcs.end(); ++si) {
101  if (afs) {
103  afs->update_header (region->position(), *now, xnow);
104  afs->mark_immutable ();
105  }
106 
108  if (smfs) {
109  smfs->set_timeline_position (region->position());
110  smfs->flush ();
111  }
112 
113  /* now that there is data there, requeue the file for analysis */
114 
115  Analyser::queue_source_for_analysis (*si, false);
116  }
117 
118  /* create a new region */
119 
120  if (region_name.empty()) {
121  region_name = RegionFactory::new_region_name (region->name());
122  }
123  results.clear ();
124 
125  PropertyList plist;
126 
127  plist.add (Properties::start, 0);
128  plist.add (Properties::length, region->length());
129  plist.add (Properties::name, region_name);
130  plist.add (Properties::whole_file, true);
131  plist.add (Properties::position, region->position());
132 
133  boost::shared_ptr<Region> r = RegionFactory::create (nsrcs, plist);
134 
137  if (audio_region && audio_r) {
138  audio_r->set_scale_amplitude (audio_region->scale_amplitude());
139  audio_r->set_fade_in_active (audio_region->fade_in_active ());
140  audio_r->set_fade_in (audio_region->fade_in ());
141  audio_r->set_fade_out_active (audio_region->fade_out_active ());
142  audio_r->set_fade_out (audio_region->fade_out ());
143  *(audio_r->envelope()) = *(audio_region->envelope ());
144  }
145  results.push_back (r);
146 
147  return 0;
148 }
149 
150 
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r)
Definition: shared_ptr.hpp:396
boost::shared_ptr< AutomationList > fade_in()
Definition: audioregion.h:87
boost::shared_ptr< AutomationList > fade_out()
Definition: audioregion.h:89
Definition: Beats.hpp:239
LIBPBD_API Transmitter error
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:71
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > start
Definition: region.cc:63
bool fade_in_active() const
Definition: audioregion.h:84
#define _(Text)
Definition: i18n.h:11
virtual void set_timeline_position(framepos_t pos)
Definition: source.cc:264
boost::shared_ptr< AutomationList > envelope()
Definition: audioregion.h:91
const DataType & data_type() const
Definition: region.h:102
bool fade_out_active() const
Definition: audioregion.h:85
Definition: amp.h:29
gain_t scale_amplitude() const
Definition: audioregion.h:78
std::vector< std::string > master_source_names()
Definition: region.cc:1380
void done_with_peakfile_writes(bool done=true)
Definition: audiosource.cc:748
framepos_t position() const
Definition: region.h:112
LIBPBD_API Glib::ustring basename_nosuffix(Glib::ustring)
const char * name
uint32_t n_channels() const
Definition: region.h:259
std::string name() const
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > position
Definition: region.cc:65
void set_scale_amplitude(gain_t)
Definition: debug.h:30
void flush()
Definition: OldSMF.cpp:138
framecnt_t length() const
Definition: region.h:114
std::vector< boost::shared_ptr< Source > > SourceList
Definition: types.h:520
bool add(PropertyBase *prop)
LIBARDOUR_API PBD::PropertyDescriptor< bool > whole_file
Definition: region.cc:54
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208
virtual int update_header(framepos_t when, struct tm &, time_t)=0
LIBARDOUR_API PBD::PropertyDescriptor< framecnt_t > length
Definition: region.cc:64