*** NEW CODING POLICY ***
[ardour.git] / libs / ardour / export_timespan.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "ardour/export_timespan.h"
22
23 #include "ardour/export_channel_configuration.h"
24 #include "ardour/export_filename.h"
25 #include "ardour/export_file_io.h"
26 #include "ardour/export_failed.h"
27
28 namespace ARDOUR
29 {
30
31 ExportTimespan::ExportTimespan (ExportStatusPtr status, nframes_t frame_rate) :
32   status (status),
33   start_frame (0),
34   end_frame (0),
35   position (0),
36   frame_rate (frame_rate)
37 {
38 }
39
40 ExportTimespan::~ExportTimespan ()
41 {
42 }
43
44 void
45 ExportTimespan::register_channel (ExportChannelPtr channel)
46 {
47         TempFilePtr ptr (new ExportTempFile (1, frame_rate));
48         ChannelFilePair pair (channel, ptr);
49         filemap.insert (pair);
50 }
51
52 void
53 ExportTimespan::rewind ()
54 {
55         for (TempFileMap::iterator it = filemap.begin(); it != filemap.end(); ++it) {
56                 it->second->reset_read ();
57         }
58 }
59
60 nframes_t
61 ExportTimespan::get_data (float * data, nframes_t frames, ExportChannelPtr channel)
62 {
63         TempFileMap::iterator it = filemap.find (channel);
64         if (it == filemap.end()) {
65                 throw ExportFailed (X_("Trying to get data from ExportTimespan for channel that was never registered!"));
66         }
67         
68         return it->second->read (data, frames);
69 }
70
71 void
72 ExportTimespan::set_range (nframes_t start, nframes_t end)
73 {
74         start_frame = start;
75         position = start_frame;
76         end_frame = end;
77 }
78
79 int
80 ExportTimespan::process (nframes_t frames)
81 {
82         status->stage = export_ReadTimespan;
83
84         /* update position */
85
86         nframes_t frames_to_read;
87         
88         if (position + frames <= end_frame) {
89                 frames_to_read = frames;
90         } else {
91                 frames_to_read = end_frame - position;
92                 status->stop = true;
93         }
94         
95         position += frames_to_read;
96         status->progress = (float) (position - start_frame) / (end_frame - start_frame);
97
98         /* Read channels from ports and save to tempfiles */
99
100         float * data = new float[frames_to_read];
101         
102         for (TempFileMap::iterator it = filemap.begin(); it != filemap.end(); ++it) {
103                 it->first->read (data, frames_to_read);
104                 it->second->write (data, frames_to_read);
105         }
106         
107         delete [] data;
108
109         return 0;
110 }
111
112
113 } // namespace ARDOUR