Trim include dependency graph, especially for io.h and session.h.
[ardour.git] / libs / ardour / region_factory.cc
1 /*
2     Copyright (C) 2000-2006 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 <pbd/error.h>
21
22 #include <ardour/session.h>
23
24 #include <ardour/region_factory.h>
25 #include <ardour/region.h>
26 #include <ardour/audioregion.h>
27 #include <ardour/audiosource.h>
28 #include <ardour/midi_source.h>
29 #include <ardour/midi_region.h>
30
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace PBD;
35
36 sigc::signal<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
37
38 boost::shared_ptr<Region>
39 RegionFactory::create (boost::shared_ptr<Region> region, nframes_t start, 
40                        nframes_t length, const std::string& name, 
41                        layer_t layer, Region::Flag flags, bool announce)
42 {
43         boost::shared_ptr<const AudioRegion> other_a;
44         boost::shared_ptr<const MidiRegion> other_m;
45
46         if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
47                 AudioRegion* ar = new AudioRegion (other_a, start, length, name, layer, flags);
48                 boost::shared_ptr<AudioRegion> arp (ar);
49                 boost::shared_ptr<Region> ret (boost::static_pointer_cast<Region> (arp));
50                 if (announce) {
51                         CheckNewRegion (ret);
52                 }
53                 return ret;
54         } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
55                 MidiRegion* ar = new MidiRegion (other_m, start, length, name, layer, flags);
56                 boost::shared_ptr<MidiRegion> arp (ar);
57                 boost::shared_ptr<Region> ret (boost::static_pointer_cast<Region> (arp));
58                 if (announce) {
59                         CheckNewRegion (ret);
60                 }
61                 return ret;
62         } else {
63                 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
64                       << endmsg;
65                 /*NOTREACHED*/
66                 return boost::shared_ptr<Region>();
67         }
68 }
69
70 boost::shared_ptr<Region>
71 RegionFactory::create (boost::shared_ptr<const Region> region)
72 {
73         boost::shared_ptr<const AudioRegion> ar;
74         boost::shared_ptr<const MidiRegion> mr;
75
76         if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
77                 boost::shared_ptr<Region> ret (new AudioRegion (ar));
78                 /* pure copy constructor - no CheckNewRegion emitted */
79                 return ret;
80         } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
81                 boost::shared_ptr<Region> ret (new MidiRegion (mr));
82                 /* pure copy constructor - no CheckNewRegion emitted */
83                 return ret;
84         } else {
85                 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
86                       << endmsg;
87                 /*NOTREACHED*/
88                 return boost::shared_ptr<Region>();
89         }
90 }
91
92 boost::shared_ptr<Region>
93 RegionFactory::create (boost::shared_ptr<AudioRegion> region, nframes_t start, 
94                        nframes_t length, const std::string& name, 
95                        layer_t layer, Region::Flag flags, bool announce)
96 {
97         return create (boost::static_pointer_cast<Region> (region), start, length, name, layer, flags, announce);
98 }
99
100 boost::shared_ptr<Region>
101 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs,
102                        const std::string& name, layer_t layer, Region::Flag flags, bool announce)
103
104 {
105         boost::shared_ptr<const AudioRegion> other;
106
107         /* used by AudioFilter when constructing a new region that is intended to have nearly
108            identical settings to an original, but using different sources.
109         */
110
111         if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
112                 AudioRegion* ar = new AudioRegion (other, srcs, srcs.front()->length(), name, layer, flags);
113                 boost::shared_ptr<AudioRegion> arp (ar);
114                 boost::shared_ptr<Region> ret (boost::static_pointer_cast<Region> (arp));
115                 if (announce) {
116                         CheckNewRegion (ret);
117                 }
118                 return ret;
119         } else {
120                 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
121                       << endmsg;
122                 /*NOTREACHED*/
123                 return boost::shared_ptr<Region>();
124         }
125 }
126
127 boost::shared_ptr<Region>
128 RegionFactory::create (Session& session, XMLNode& node, bool yn)
129 {
130         boost::shared_ptr<Region> r = session.XMLRegionFactory (node, yn);
131
132         if (r) {
133                 CheckNewRegion (r);
134         }
135
136         return r;
137 }
138         
139 boost::shared_ptr<Region> 
140 RegionFactory::create (const SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Region::Flag flags, bool announce)
141 {
142         if (srcs.empty()) {
143                 return boost::shared_ptr<Region>();
144         }
145
146         if (srcs[0]->type() == DataType::AUDIO) {
147                 
148                 AudioRegion* ar = new AudioRegion (srcs, start, length, name, layer, flags);
149                 boost::shared_ptr<AudioRegion> arp (ar);
150                 boost::shared_ptr<Region> ret (boost::static_pointer_cast<Region> (arp));
151                 if (announce) {
152                         CheckNewRegion (ret);
153                 }
154                 return ret;
155
156         } else if (srcs[0]->type() == DataType::MIDI) {
157                 
158                 MidiRegion* ar = new MidiRegion (srcs, start, length, name, layer, flags);
159                 boost::shared_ptr<MidiRegion> mrp (ar);
160                 boost::shared_ptr<Region> ret (boost::static_pointer_cast<Region> (mrp));
161                 if (announce) {
162                         CheckNewRegion (ret);
163                 }
164                 return ret;
165
166         }
167
168         return boost::shared_ptr<Region> ();
169 }       
170
171 boost::shared_ptr<Region> 
172 RegionFactory::create (SourceList& srcs, const XMLNode& node)
173 {
174         if (srcs.empty()) {
175                 return boost::shared_ptr<Region>();
176         }
177
178         if (srcs[0]->type() == DataType::AUDIO) {
179                 boost::shared_ptr<Region> ret (new AudioRegion (srcs, node));
180                 CheckNewRegion (ret);
181                 return ret;
182         } else if (srcs[0]->type() == DataType::MIDI) {
183                 boost::shared_ptr<Region> ret (new MidiRegion (srcs, node));
184                 CheckNewRegion (ret);
185                 return ret;
186         }
187
188         return boost::shared_ptr<Region> ();
189 }
190
191 boost::shared_ptr<Region> 
192 RegionFactory::create (boost::shared_ptr<Source> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Region::Flag flags, bool announce)
193 {
194         boost::shared_ptr<AudioSource> as;
195         boost::shared_ptr<MidiSource> ms;
196
197         if ((as = boost::dynamic_pointer_cast<AudioSource>(src)) != 0) {
198                 boost::shared_ptr<Region> ret (new AudioRegion (as, start, length, name, layer, flags));
199                 if (announce) {
200                         CheckNewRegion (ret);
201                 }
202                 return ret;
203         } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(src)) != 0) {
204                 boost::shared_ptr<Region> ret (new MidiRegion (ms, start, length, name, layer, flags));
205                 if (announce) {
206                         CheckNewRegion (ret);
207                 }
208                 return ret;
209         }
210
211         return boost::shared_ptr<Region>();
212 }