4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program 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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "audioconfig.h"
24 #include "audiodevice.h"
29 #include "playbackengine.h"
30 #include "preferences.h"
31 #include "recconfirmdelete.h"
33 #include "recordengine.h"
34 #include "recordgui.h"
35 #include "recordlabel.h"
36 #include "recordpreview.h"
37 #include "recordthread.h"
38 #include "recordmonitor.h"
40 #include "videodevice.h"
45 #define _(String) gettext(String)
46 #define gettext_noop(String) String
47 #define N_(String) gettext_noop (String)
49 RecordEngine::RecordEngine(MWindow *mwindow, Record *record)
51 this->mwindow = mwindow;
52 this->record = record;
59 RecordEngine::RecordEngine(MWindow *mwindow,
65 this->mwindow = mwindow;
66 this->record = record;
68 this->labels = labels;
78 adevice = new AudioDevice;
83 vdevice = new VideoDevice(mwindow);
88 RecordEngine::~RecordEngine()
90 delete monitor_thread;
92 delete preview_thread;
93 if(adevice) delete adevice;
94 if(vdevice) delete vdevice;
97 int RecordEngine::initialize()
99 // monitor_thread = new RecordThread(mwindow, record, this);
100 monitor_thread->create_objects();
102 // record_thread = new RecordThread(mwindow, record, this);
103 record_thread->create_objects();
105 preview_thread = new RecordPreview(record, this);
106 preview_thread->initialize();
108 // put at end of file
110 if(record->do_audio) current_position = file->get_audio_length();
112 if(record->do_video) current_position = Units::tosamples((float)(file->get_video_length(record->get_framerate())), record->get_samplerate(), record->get_framerate());
116 duplex_thread = mwindow->playback_engine;
118 // initialize seek buttons
125 current_jump_jumps[0] = 20;
126 current_jump_jumps[1] = 40;
127 current_jump_jumps[2] = 60;
128 current_jump_jumps[3] = 80;
129 current_jump_jumps[4] = 100;
132 int RecordEngine::run_script(FileXML *script)
134 int result = 0, script_result = 0;
137 while(!result && !script_result)
139 result = script->read_tag();
143 if(script->tag.title_is("set_mode"))
145 set_record_mode(script->tag.get_property_text(0));
146 mode_to_text(string, get_record_mode());
147 gui->rec_mode_menu->set_text(string);
150 if(script->tag.title_is("set_duration"))
152 record->set_loop_duration((long)record->get_samplerate() * script->tag.get_property_int(0));
153 gui->update_duration_boxes();
156 if(script->tag.title_is("start_recording"))
158 gui->unlock_window();
163 if(script->tag.title_is("set_monitor_video"))
165 set_monitor_video(script->tag.get_property_int(0));
166 if(!script->tag.get_property_int(0) && record->video_window_open)
168 record->video_window_open = 0;
169 gui->monitor_video_window->window->hide_window();
173 if(script->tag.title_is("set_monitor_audio"))
175 set_monitor_audio(script->tag.get_property_int(0));
178 if(script->tag.title_is("quit_when_completed"))
180 record_thread->quit_when_completed = 1;
183 if(script->tag.title_is("ok"))
189 return script_result;
192 // ============================================= accounting
194 long RecordEngine::get_dc_offset(int offset)
196 return record->dc_offset[offset];
199 int RecordEngine::set_dc_offset(long new_offset, int number)
201 adevice->set_dc_offset(new_offset, number);
204 long int RecordEngine::get_dc_offset(long *dc_offset, RecordGUIDCOffsetText **dc_offset_text)
206 return adevice->get_dc_offset(dc_offset, dc_offset_text);
209 int RecordEngine::set_gui(RecordGUI *gui)
212 update_position(current_position);
215 int RecordEngine::get_duplex_enable()
217 return record->enable_duplex();
222 // ================================================ operations
224 int RecordEngine::open_input_devices(int duplex)
226 int audio_opened = 0;
227 int video_opened = 0;
228 AudioConfig *aconfig /* = mwindow->preferences->aconfig */;
230 // Initialize sharing
231 if(record->do_audio && record->do_video)
233 vdevice->set_adevice(adevice);
234 adevice->set_vdevice(vdevice);
240 if(record->get_software_positioning())
241 adevice->set_software_positioning();
243 for(int i = 0; i < asset->channels; i++)
245 adevice->set_dc_offset(record->dc_offset[i], i);
250 // Duplex is only needed if the timeline and the recording have audio
253 mwindow->patches->total_playable_atracks())
255 // duplex device is identical to input device
256 if(aconfig->audio_in_driver == aconfig->audio_duplex_driver &&
257 !strcmp(aconfig->oss_in_device, aconfig->oss_duplex_device) &&
258 aconfig->oss_in_bits == aconfig->oss_duplex_bits &&
259 aconfig->oss_in_channels == aconfig->oss_duplex_channels)
261 // adevice->open_duplex(mwindow->preferences->aconfig,
262 // record->get_samplerate(),
267 // two separate devices
269 // adevice->open_output(mwindow->preferences->aconfig,
270 // record->get_samplerate(),
271 // record->get_out_length());
275 if(record->do_audio && !audio_opened)
277 // adevice->open_input(mwindow->preferences->aconfig,
278 // record->get_samplerate(),
285 // vdevice->open_input(mwindow->preferences->vconfig,
290 // record->video_zoom,
291 // get_frame_rate());
292 // vdevice->set_field_order(record->reverse_interlace);
293 // if(record->get_current_channel())
294 // vdevice->set_channel(record->get_current_channel());
295 // set_video_picture();
302 int RecordEngine::close_input_devices()
305 adevice->close_all();
307 vdevice->close_all();
312 int RecordEngine::start_monitor()
314 monitor_timer.update();
315 open_input_devices(0);
316 monitor_thread->start_recording(0, 0);
321 int RecordEngine::stop_monitor()
325 // is_monitoring = 0;
326 // monitor_thread->stop_recording();
331 int RecordEngine::pause_monitor()
336 monitor_thread->pause_recording();
341 int RecordEngine::resume_monitor()
346 monitor_timer.update();
347 open_input_devices(0);
348 monitor_thread->resume_recording();
353 int RecordEngine::start_saving(int duplex)
358 record_timer.update();
359 open_input_devices(duplex);
361 duplex = record->enable_duplex() && duplex;
363 // start the duplex engine if necessary
364 // OSS < 3.9 crashes if recording starts before playback
365 // OSS >= 3.9 crashes if playback starts before recording
369 record->get_duplex_range(&start, &end);
370 duplex_thread->reset_parameters();
371 duplex_thread->arm_playback(0, 0, 1, adevice);
372 duplex_thread->start_playback();
376 // record_thread->start_recording();
383 int RecordEngine::save_frame()
388 record_timer.update();
389 record->do_audio = 0;
390 open_input_devices(0);
392 // start the duplex engine if necessary
393 record_thread->start_recording(0, 0);
399 int RecordEngine::stop_saving(int no_monitor)
403 // automatically stops duplex here and resumes monitor
404 record_thread->stop_recording(no_monitor);
409 int RecordEngine::stop_duplex()
414 duplex_thread->stop_playback(0);
415 // OSS can't resume recording if buffers underrun
416 // so stop playback first
421 int RecordEngine::start_preview()
428 preview_timer.update();
429 open_output_devices();
430 preview_thread->start_preview(current_position, file);
437 int RecordEngine::stop_preview(int no_monitor)
441 preview_thread->stop_preview(no_monitor);
442 // preview engine automatically triggers monitor when finished
447 int RecordEngine::stop_operation(int no_monitor)
449 // Resumes monitoring after stopping
450 if(is_saving) stop_saving(no_monitor);
452 if(is_previewing) stop_preview(no_monitor);
456 int RecordEngine::set_video_picture()
458 if(record->do_video && vdevice)
459 vdevice->set_picture(record->video_brightness,
462 record->video_contrast,
463 record->video_whiteness);
467 int RecordEngine::open_output_devices()
471 // adevice->open_output(mwindow->preferences->aconfig,
472 // record->get_samplerate(),
473 // record->get_out_length());
474 if(record->get_software_positioning()) adevice->set_software_positioning();
477 // Video is already open for monitoring
481 int RecordEngine::close_output_devices()
484 adevice->close_all();
485 // Video is already open for monitoring
491 int RecordEngine::lock_window()
496 int RecordEngine::unlock_window()
498 gui->unlock_window();
501 int RecordEngine::update_position(long new_position)
503 if(new_position < 0) new_position = 0; // fread error in filepcm
504 current_position = new_position;
506 gui->update_position(new_position);
508 if(new_position > total_length)
510 total_length = new_position;
511 // gui->update_total_length(new_position);
514 if(prev_label != labels->get_prev_label(new_position))
516 prev_label = labels->get_prev_label(new_position);
517 gui->update_prev_label(prev_label);
520 if(next_label != labels->get_next_label(new_position))
522 next_label = labels->get_next_label(new_position);
524 gui->update_next_label(next_label);
528 int RecordEngine::goto_prev_label()
535 new_position = labels->goto_prev_label(current_position);
536 if(new_position != -1)
538 // if(record->do_audio) file->set_audio_position(new_position);
539 if(record->do_video) file->set_video_position(Units::toframes(new_position, record->get_samplerate(), record->get_framerate()), record->get_framerate());
540 update_position(new_position);
545 int RecordEngine::goto_next_label()
552 new_position = labels->goto_next_label(current_position);
553 if(new_position != -1 && new_position <= total_length)
555 // if(record->do_audio) file->set_audio_position(new_position);
556 if(record->do_video) file->set_video_position(Units::toframes(new_position, record->get_samplerate(), record->get_framerate()), record->get_framerate());
557 update_position(new_position);
563 int RecordEngine::toggle_label()
565 labels->toggle_label(current_position);
566 update_position(current_position);
570 int RecordEngine::calibrate_dc_offset()
574 get_dc_offset(record->dc_offset, gui->dc_offset_text);
579 int RecordEngine::calibrate_dc_offset(long new_value, int channel)
583 set_dc_offset(new_value, channel);
584 record->dc_offset[channel] = new_value;
589 int RecordEngine::reset_over()
593 int RecordEngine::set_done(int value)
597 gui->set_done(value);
600 int RecordEngine::start_over()
602 if((record->do_audio && file->get_audio_length() > 0) ||
603 (record->do_video && file->get_video_length(record->get_framerate()) > 0))
605 RecConfirmDelete dialog(mwindow);
606 dialog.create_objects(_("start over"));
607 int result = dialog.run_window();
616 // start the engine over
617 labels->delete_new_labels();
620 // gui->update_total_length(0);
622 record->startsource_sample = 0;
623 record->startsource_frame = 0;
628 int RecordEngine::change_channel(Channel *channel)
630 if(record->do_video && vdevice)
631 return vdevice->set_channel(channel);
636 ArrayList<char*>* RecordEngine::get_video_inputs()
638 if(record->do_video && vdevice)
639 return vdevice->get_inputs();
644 int RecordEngine::get_vu_format() { return record->get_vu_format(); }
645 int RecordEngine::get_dither() { return record->default_asset->dither * record->default_asset->bits; }
646 int RecordEngine::get_input_channels() { return asset->channels; }
647 int RecordEngine::get_format(char *string)
650 strcpy(string, file.formattostr(mwindow->plugindb, asset->format));
652 int RecordEngine::get_samplerate() { return asset->rate; }
653 int RecordEngine::get_bits() { return asset->bits; }
654 int RecordEngine::get_time_format() { return record->get_time_format(); }
655 float RecordEngine::get_frame_rate() { return record->get_frame_rate(); }
656 int RecordEngine::get_loop_hr() { return record->loop_duration / asset->rate / 3600; }
657 int RecordEngine::get_loop_min() { return record->loop_duration / asset->rate / 60 - (long)get_loop_hr() * 60; }
658 int RecordEngine::get_loop_sec() { return record->loop_duration / asset->rate - (long)get_loop_hr() * 3600 - (long)get_loop_min() * 60; }
659 long RecordEngine::get_loop_duration() { return record->loop_duration; }
660 float RecordEngine::get_min_db() { return record->get_min_db(); }
661 int RecordEngine::get_meter_over_hold(int divisions) { return divisions * 15; }
662 int RecordEngine::get_meter_peak_hold(int divisions) { return divisions * 2; }
663 int RecordEngine::get_meter_speed() { return record->get_meter_speed(); }
664 float RecordEngine::get_frames_per_foot() { /* return mwindow->preferences->frames_per_foot; */ }
666 int RecordEngine::set_monitor_video(int value)
670 int RecordEngine::set_monitor_audio(int value)
674 int RecordEngine::set_record_mode(char *text)
676 record->record_mode = text_to_mode(text);
679 int RecordEngine::get_record_mode(char *text)
681 mode_to_text(text, record->record_mode);
684 int RecordEngine::get_record_mode()
686 return record->record_mode;
689 int RecordEngine::mode_to_text(char *string, int mode)
693 case 0: sprintf(string, _("Untimed")); break;
694 case 1: sprintf(string, _("Timed")); break;
695 case 2: sprintf(string, _("Loop")); break;
699 int RecordEngine::text_to_mode(char *string)
701 if(!strcasecmp(string, _("Untimed"))) return 0;
702 if(!strcasecmp(string, _("Timed"))) return 1;
703 if(!strcasecmp(string, _("Loop"))) return 2;
706 long RecordEngine::get_current_delay()
708 if(current_jump_jump > 0) current_jump_jump--;
709 if(current_jump_jump == 0 && current_jump_delay < /*JUMP_DELAYS*/ 1)
711 current_jump_delay++;
712 current_jump_jump = current_jump_jumps[current_jump_delay];
714 return jump_delay[current_jump_delay];
717 int RecordEngine::reset_current_delay()
719 current_jump_delay = 0;
720 current_jump_jump = current_jump_jumps[current_jump_delay];
723 int RecordEngine::set_loop_duration()
725 record->set_loop_duration((long)record->get_samplerate() * (atol(gui->loop_sec->get_text()) + atol(gui->loop_min->get_text()) * 60 + atol(gui->loop_hr->get_text()) * 3600));
729 // Remember to change meters if you change this.
730 // Return the size of the fragments to read from the audio device.
731 int RecordEngine::get_in_length()
733 long fragment_size = 1;
734 while(fragment_size < asset->rate / record->get_meter_speed()) fragment_size *= 2;
736 return fragment_size;
739 // Different absolute positions are defined for each operation so threads
740 // can end at different times without screwing up the frame synchronization.
741 long RecordEngine::absolute_monitor_position()
747 // return monitor_thread->absolute_position();
751 return (long)((float)monitor_timer.get_difference() / 1000 * record->get_samplerate());
758 long RecordEngine::absolute_preview_position()
764 return preview_thread->absolute_position();
768 return (long)((float)preview_timer.get_difference() / 1000 * record->get_samplerate());
775 long RecordEngine::absolute_record_position()
781 // return record_thread->absolute_position();
785 return (long)((float)record_timer.get_difference() / 1000 * record->get_samplerate());