101b33e811cbe6324edf07814c010a3eeb45bb44
[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     $Id: playlist_factory.cc 629 2006-06-21 23:01:03Z paul $
19 */
20
21 #include <pbd/error.h>
22
23 #include <ardour/session.h>
24
25 #include <ardour/region_factory.h>
26 #include <ardour/region.h>
27 #include <ardour/audioregion.h>
28 #include <ardour/audiosource.h>
29
30 #include "i18n.h"
31
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 sigc::signal<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
36
37 boost::shared_ptr<Region>
38 RegionFactory::create (boost::shared_ptr<Region> region, jack_nframes_t start, 
39                              jack_nframes_t length, std::string name, 
40                              layer_t layer, Region::Flag flags, bool announce)
41 {
42         boost::shared_ptr<const AudioRegion> other;
43
44         if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
45                 AudioRegion* ar = new AudioRegion (other, start, length, name, layer, flags);
46                 boost::shared_ptr<AudioRegion> arp (ar);
47                 boost::shared_ptr<Region> ret (boost::static_pointer_cast<Region> (arp));
48                 if (announce) {
49                         CheckNewRegion (ret);
50                 }
51                 return ret;
52         } else {
53                 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
54                       << endmsg;
55                 /*NOTREACHED*/
56                 return boost::shared_ptr<Region>();
57         }
58 }
59
60 boost::shared_ptr<Region>
61 RegionFactory::create (boost::shared_ptr<Region> region)
62 {
63         boost::shared_ptr<AudioRegion> other;
64         
65         if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
66                 boost::shared_ptr<Region> ret (new AudioRegion (other));
67                 /* pure copy constructor - no CheckNewRegion emitted */
68                 return ret;
69         } else {
70                 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
71                       << endmsg;
72                 /*NOTREACHED*/
73                 return boost::shared_ptr<Region>();
74         }
75 }
76
77 boost::shared_ptr<Region>
78 RegionFactory::create (boost::shared_ptr<AudioRegion> region, jack_nframes_t start, 
79                              jack_nframes_t length, std::string name, 
80                              layer_t layer, Region::Flag flags, bool announce)
81 {
82         return create (boost::static_pointer_cast<Region> (region), start, length, name, layer, flags, announce);
83 }
84
85 boost::shared_ptr<Region>
86 RegionFactory::create (Session& session, XMLNode& node, bool yn)
87 {
88         boost::shared_ptr<Region> r = session.XMLRegionFactory (node, yn);
89         CheckNewRegion (r);
90         return r;
91 }
92         
93 boost::shared_ptr<Region> 
94 RegionFactory::create (SourceList& srcs, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Region::Flag flags, bool announce)
95 {
96         AudioRegion* ar = new AudioRegion (srcs, start, length, name, layer, flags);
97         boost::shared_ptr<AudioRegion> arp (ar);
98         boost::shared_ptr<Region> ret (boost::static_pointer_cast<Region> (arp));
99         if (announce) {
100                 CheckNewRegion (ret);
101         }
102         return ret;
103 }       
104
105 boost::shared_ptr<Region> 
106 RegionFactory::create (SourceList& srcs, const XMLNode& node)
107 {
108         if (srcs.empty()) {
109                 return boost::shared_ptr<Region>();
110         }
111
112         boost::shared_ptr<Region> ret (new AudioRegion (srcs, node));
113         CheckNewRegion (ret);
114         return ret;
115 }
116
117 boost::shared_ptr<Region> 
118 RegionFactory::create (boost::shared_ptr<Source> src, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Region::Flag flags, bool announce)
119 {
120         boost::shared_ptr<AudioSource> as;
121
122         if ((as = boost::dynamic_pointer_cast<AudioSource>(src)) != 0) {
123                 boost::shared_ptr<Region> ret (new AudioRegion (as, start, length, name, layer, flags));
124                 if (announce) {
125                         CheckNewRegion (ret);
126                 }
127                 return ret;
128         }
129
130         return boost::shared_ptr<Region>();
131 }