1 /* Copyright 2009-2010 Yorba Foundation
3 * This software is licensed under the GNU Lesser General Public License
4 * (version 2.1 or later). See the COPYING file in this distribution.
10 class RecordFetcherCompletion : FetcherCompletion {
15 public RecordFetcherCompletion(Project project, Track track, int64 position) {
18 this.project = project;
19 this.position = position;
22 public override void complete(Fetcher fetch) {
24 Clip the_clip = new Clip(fetch.clipfile, MediaType.AUDIO,
25 isolate_filename(fetch.clipfile.filename), 0, 0, fetch.clipfile.length, false);
26 project.undo_manager.start_transaction("Record");
27 track.append_at_time(the_clip, position, true);
28 project.undo_manager.end_transaction("Record");
32 class AudioProject : Project {
35 public AudioProject(string? filename) throws Error {
36 base(filename, false);
37 // TODO: When vala supports throwing from base, remove this check
39 has_been_saved = filename != null;
40 if (!has_been_saved) {
41 project_file = generate_filename();
43 media_engine.callback_pulse.connect(media_engine.on_callback_pulse);
44 media_engine.record_completed.connect(on_record_completed);
48 public override TimeCode get_clip_time(ClipFile f) {
51 t.get_from_length(f.length);
55 public override string? get_project_file() {
56 if (!has_been_saved) {
59 return base.get_project_file();
63 string generate_filename() {
64 Time now = Time.local(time_t());
65 string timestring = now.to_string();
66 timestring = timestring.replace(":", "_");
67 string pathname = Path.build_filename(GLib.Environment.get_home_dir(), ".fillmore",
69 GLib.DirUtils.create(pathname, 0777);
70 string filename = "%s.%s".printf(timestring, "fill");
71 return Path.build_filename(pathname, filename);
74 public override string get_app_name() {
78 public override void add_track(Track track) {
79 if (track.media_type() == MediaType.VIDEO) {
81 inactive_tracks.add(track);
85 base.add_track(track);
88 public void record(AudioTrack track) {
89 media_engine.record(track);
92 public void on_record_completed() {
93 emit(this, Facility.SIGNAL_HANDLERS, Level.INFO, "on_record_completed");
95 create_clip_fetcher(new Model.RecordFetcherCompletion(this, media_engine.record_track,
96 media_engine.record_region.start), media_engine.record_region.clipfile.filename);
98 error_occurred("Could not complete recording", e.message);
102 public override void load(string? filename) {
103 has_been_saved = filename != null;
104 if (!has_been_saved) {
105 project_file = generate_filename();
110 public override void save(string? filename) {
111 if (!has_been_saved && filename != null) {
112 move_audio_files(filename);
113 GLib.FileUtils.remove(project_file);
114 GLib.DirUtils.remove(Path.get_dirname(project_file));
118 has_been_saved = true;
121 void move_audio_files(string filename) {
122 string audio_path = get_audio_path();
123 string destination_path = Path.build_filename(Path.get_dirname(filename), "audio files");
124 GLib.DirUtils.create(destination_path, 0777);
127 dir = Dir.open(audio_path);
128 } catch (FileError e) {
132 // First, move all of the files over, even if they aren't currently in the project
133 weak string? base_name = null;
135 base_name = dir.read_name();
136 string destination = Path.build_filename(destination_path, base_name);
137 FileUtils.rename(Path.build_filename(audio_path, base_name), destination);
138 } while (base_name != null);
140 // Next, update the model so that the project file is saved properly
141 foreach (ClipFile clip_file in clipfiles) {
142 if (Path.get_dirname(clip_file.filename) == audio_path) {
143 string file_name = Path.get_basename(clip_file.filename);
144 string destination = Path.build_filename(destination_path, file_name);
145 clip_file.filename = destination;
149 GLib.DirUtils.remove(audio_path);