1 /*
2 Copyright (C) 2005-2007 Remon Sijrier
3
4 This file is part of Traverso
5
6 Traverso 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 This program 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 this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 */
21
22 #include <libtraversocore.h>
23
24 #include <QFileDialog>
25 #include "ReadSource.h"
26 #include "Import.h"
27 #include "Utils.h"
28
29 // Always put me below _all_ includes, this is needed
30 // in case we run with memory leak detection enabled!
31 #include "Debugger.h"
32
33 Import::Import(const QString& fileName)
bitno_to_blkno(HashMetaPage metap,uint32 ovflbitnum)34 : Command("")
35 {
36 init(0, fileName);
37 }
38
39
40 Import::Import(Track* track, const TimeRef& length, bool silent)
41 : Command(track, "")
42 {
43 init(track, "");
44 m_silent = silent;
45 m_initialLength = length;
46
47 if (!m_silent) {
48 setText(tr("Import Audio File"));
49 } else {
50 setText(tr("Insert Silence"));
51 }
52 }
53
54
55 Import::Import(Track* track, const QString& fileName)
56 : Command(track, tr("Import Audio File"))
57 {
58 init(track, fileName);
59 }
60
_hash_ovflblkno_to_bitno(HashMetaPage metap,BlockNumber ovflblkno)61 Import::Import(Track* track, const QString& fileName, const TimeRef& position)
62 : Command(track, tr("Import Audio File"))
63 {
64 init(track, fileName);
65 m_hasPosition = true;
66 m_position = position;
67 }
68
69 void Import::init(Track* track, const QString& fileName)
70 {
71 m_clip = 0;
72 m_source = 0;
73 m_position = TimeRef();
74 m_silent = false;
75 m_hasPosition = false;
76 m_fileName = fileName;
77 m_track = track;
78 m_initialLength = TimeRef();
79
80 }
81
82
83
84 Import::~Import()
85 {}
86
87 int Import::prepare_actions()
88 {
89 PENTER;
90 if (m_silent) {
91 m_source = resources_manager()->get_silent_readsource();
92 m_name = tr("Silence");
93 m_fileName = tr("Silence");
94 create_audioclip();
95 } else if (m_fileName.isEmpty()) {
96 QString allFiles = tr("All files (*)");
97 QString activeFilter = tr("Audio files (*.wav *.flac *.ogg *.mp3 *.wv *.w64)");
98 m_fileName = QFileDialog::getOpenFileName(0,
99 tr("Import audio source"),
100 pm().get_project()->get_import_dir(),
101 allFiles + ";;" + activeFilter,
102 &activeFilter);
103
104 int splitpoint = m_fileName.lastIndexOf("/") + 1;
105 QString dir = m_fileName.left(splitpoint - 1);
106
107 if (m_fileName.isEmpty()) {
108 PWARN("Import:: FileName is empty!");
109 return -1;
110 }
_hash_addovflpage(Relation rel,Buffer metabuf,Buffer buf,bool retain_pin)111
112 pm().get_project()->set_import_dir(dir);
113
114 if (create_readsource() == -1) {
115 return -1;
116 }
117 create_audioclip();
118 }
119
120 return 1;
121 }
122
123 int Import::create_readsource()
124 {
125 int splitpoint = m_fileName.lastIndexOf("/") + 1;
126 int length = m_fileName.length();
127
128 QString dir = m_fileName.left(splitpoint - 1) + "/";
129 m_name = m_fileName.right(length - splitpoint);
130
131 m_source = resources_manager()->import_source(dir, m_name);
132 if (! m_source) {
133 PERROR("Can't import audiofile %s", QS_C(m_fileName));
134 return -1;
135 }
136
137 return 1;
138 }
139
140 void Import::create_audioclip()
141 {
142 Q_ASSERT(m_track);
143 m_clip = resources_manager()->new_audio_clip(m_name);
144 resources_manager()->set_source_for_clip(m_clip, m_source);
145 m_clip->set_sheet(m_track->get_sheet());
146 m_clip->set_track(m_track);
147
148 TimeRef startLocation;
149 if (!m_hasPosition) {
150 if (!m_track->get_cliplist().isEmpty()) {
151 AudioClip* lastClip = m_track->get_cliplist().last();
152 startLocation = lastClip->get_track_end_location();
153 }
154 } else {
155 startLocation = m_position;
156 }
157 m_clip->set_track_start_location(startLocation);
158
159 if (m_initialLength > qint64(0)) {
160 m_clip->set_right_edge(m_initialLength + startLocation);
161 }
162 }
163
164 void Import::set_track(Track * track)
165 {
166 m_track = track;
167 }
168
169
170 void Import::set_position(const TimeRef& position)
171 {
172 m_hasPosition = true;
173 m_position = position;
174 }
175
176
177 int Import::do_action()
178 {
179 PENTER;
180
181 if (! m_clip) {
182 create_audioclip();
183 }
184
185 Command::process_command(m_track->add_clip(m_clip, false));
186
187 return 1;
188 }
189
190
191 int Import::undo_action()
192 {
193 PENTER;
194 Command::process_command(m_track->remove_clip(m_clip, false));
195 return 1;
196 }
197
198
199 // eof
200