more ru xlats, doc updates, proxy tweaks
[goodguy/history.git] / cinelerra-5.1 / cinelerra / proxy.C
index 7f729f37ebe561305178998382a48ddac87a5f92..aa9c834300225aebf9843178ec40cbf83008d5c1 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include "assets.h"
+#include "audiodevice.h"
 #include "bcsignals.h"
 #include "cache.h"
 #include "clip.h"
@@ -33,6 +34,7 @@
 #include "formattools.h"
 #include "language.h"
 #include "mainerror.h"
+#include "mainindexes.h"
 #include "mainprogress.h"
 #include "mainundo.h"
 #include "mutex.h"
@@ -47,7 +49,7 @@
 #include "vrender.h"
 
 #define WIDTH 400
-#define HEIGHT 285
+#define HEIGHT 300
 #define MAX_SCALE 16
 
 ProxyMenuItem::ProxyMenuItem(MWindow *mwindow)
@@ -83,11 +85,15 @@ ProxyDialog::ProxyDialog(MWindow *mwindow)
        asset = new Asset;
        bzero(size_text, sizeof(char*) * MAX_SIZES);
        bzero(size_factors, sizeof(int) * MAX_SIZES);
-       total_sizes = 0;
+       size_text[0] = cstrdup(_("Original size"));
+       size_factors[0] = 1;
+       total_sizes = 1;
+       proxy_beep = 0;
 }
 
 ProxyDialog::~ProxyDialog()
 {
+       delete proxy_beep;
        close_window();
        for( int i=0; i<MAX_SIZES; ++i ) delete [] size_text[i];
        asset->remove_user();
@@ -99,7 +105,7 @@ BC_Window* ProxyDialog::new_gui()
        asset->load_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0);
        mwindow->gui->lock_window("ProxyDialog::new_gui");
        int cx, cy;
-       mwindow->gui->get_abs_cursor_xy(cx, cy);
+       mwindow->gui->get_abs_cursor(cx, cy);
        gui = new ProxyWindow(mwindow, this, cx - WIDTH/2, cy - HEIGHT/2);
        gui->create_objects();
        mwindow->gui->unlock_window();
@@ -159,11 +165,14 @@ void ProxyDialog::handle_close_event(int result)
 {
        asset->save_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0); 
 
-       if( !result )
-               to_proxy();
+       if( !result && (result=to_proxy()) >= 0 && beep && new_scale != 1 )
+               if( result > 0 )
+                       mwindow->beep(2000., 1.5, 0.5);
+               else
+                       mwindow->beep(4000., 0.25, 0.5);
 }
 
-void ProxyDialog::to_proxy()
+int ProxyDialog::to_proxy()
 {
        ArrayList<Indexable*> orig_idxbls;
        ArrayList<Indexable*> proxy_assets;
@@ -196,10 +205,31 @@ void ProxyDialog::to_proxy()
                        orig_idxbls.append(orig_asset);
                        orig_asset->add_user();
                }
+               for( int i=0,n=edl->nested_edls.size(); i<n; ++i ) {
+                       EDL *orig_nested = edl->nested_edls[i];
+                       char new_path[BCTEXTLEN];
+                       if( !ProxyRender::from_proxy_path(new_path, orig_nested, proxy_scale) )
+                               continue;
+                       proxy_render.to_proxy_path(new_path, orig_nested, proxy_scale);
+// test if proxy asset was already added to proxy_assets
+                       int got_it = 0;
+                       for( int i = 0; !got_it && i<proxy_assets.size(); ++i )
+                               got_it = !strcmp(proxy_assets[i]->path, new_path);
+                       if( got_it ) continue;
+                       Asset *proxy_nested = edl->assets->get_asset(new_path);
+                       if( !proxy_nested ) continue;
+// add pointer to existing EDL asset if it exists
+// EDL won't delete it unless it's the same pointer.
+                       proxy_assets.append(proxy_nested);
+                       proxy_nested->add_user();
+                       orig_idxbls.append(orig_nested);
+                       orig_nested->add_user();
+               }
 
 // convert from the proxy assets to the original assets
-               int proxy_auto_scale = edl->session->proxy_auto_scale;
-               mwindow->set_proxy(0, 1, proxy_auto_scale, &proxy_assets, &orig_idxbls);
+               int auto_scale = edl->session->proxy_auto_scale;
+               int beep = edl->session->proxy_beep;
+               mwindow->set_proxy(0, 1, auto_scale, beep, &proxy_assets, &orig_idxbls);
 
 // remove the references
                for( int i=0; i<proxy_assets.size(); ++i ) {
@@ -207,6 +237,8 @@ void ProxyDialog::to_proxy()
                        proxy->width = proxy->actual_width;
                        proxy->height = proxy->actual_height;
                        proxy->remove_user();
+                       mwindow->edl->assets->remove_pointer(proxy);
+                       proxy->remove_user();
                }
                proxy_assets.remove_all();
                for( int i = 0; i < orig_idxbls.size(); i++ )
@@ -233,6 +265,19 @@ void ProxyDialog::to_proxy()
                                proxy_render.add_needed(orig, proxy);
                        }
                }
+               for( int i=0,n=edl->nested_edls.size(); i<n; ++i ) {
+                       EDL *orig_nested = edl->nested_edls[i];
+                       Asset *proxy = proxy_render.add_original(orig_nested, new_scale);
+                       if( !proxy ) continue;
+                       int exists = fs.get_size(proxy->path) > 0 ? 1 : 0;
+                       int got_it = exists && // if proxy exists, and is newer than orig_nested
+                           fs.get_date(proxy->path) > fs.get_date(orig_nested->path) ? 1 : 0;
+                       if( !got_it ) {
+                               if( exists ) // prompt user to overwrite
+                                       confirm_paths.append(cstrdup(proxy->path));
+                               proxy_render.add_needed(orig_nested, proxy);
+                       }
+               }
        }
 
        int result = 0;
@@ -246,7 +291,7 @@ void ProxyDialog::to_proxy()
                result = proxy_render.create_needed_proxies(new_scale);
 
        if( !result ) // resize project
-               mwindow->set_proxy(use_scaler, new_scale, auto_scale,
+               mwindow->set_proxy(use_scaler, new_scale, auto_scale, beep,
                        &proxy_render.orig_idxbls, &proxy_render.orig_proxies);
 
        mwindow->undo->update_undo_after(_("proxy"), LOAD_ALL);
@@ -254,8 +299,9 @@ void ProxyDialog::to_proxy()
        mwindow->restart_brender();
 
        mwindow->gui->lock_window("ProxyDialog::to_proxy");
-       mwindow->update_project(LOAD_ALL);
+       mwindow->update_project(LOADMODE_REPLACE);
        mwindow->gui->unlock_window();
+       return !result ? proxy_render.needed_proxies.size() : -1;
 }
 
 
@@ -286,19 +332,20 @@ void ProxyRender::to_proxy_path(char *new_path, Indexable *indexable, int scale)
 //printf("ProxyRender::to_proxy_path %d %s %s\n", __LINE__, new_path), asset->path);
 }
 
-void ProxyRender::from_proxy_path(char *new_path, Asset *asset, int scale)
+int ProxyRender::from_proxy_path(char *new_path, Indexable *indexable, int scale)
 {
        char prxy[BCTEXTLEN];
        int n = sprintf(prxy, ".proxy%d", scale);
-       strcpy(new_path, asset->path);
-       char *ptr = strstr(asset->path, prxy);
-       if( !ptr || (ptr[n] != '-' && ptr[n] != '.') ) return;
+       strcpy(new_path, indexable->path);
+       char *ptr = strstr(new_path, prxy);
+       if( !ptr || (ptr[n] != '-' && ptr[n] != '.') ) return 1;
 // remove proxy, path.proxy#-sfx.ext => path.sfx
        char *ext = strrchr(ptr, '.');
        if( !ext ) ext = ptr + strlen(ptr);
        char *cp = ptr + n;
        for( *cp='.'; cp<ext; ++cp ) *ptr++ = *cp;
        *ptr = 0;
+       return 0;
 }
 
 ProxyRender::ProxyRender(MWindow *mwindow, Asset *format_asset)
@@ -413,7 +460,6 @@ printf("failed=%d canceled=%d\n", failed, progress->is_cancelled());
        if( failed && !canceled ) {
                eprintf("Error making proxy.");
        }
-
        return !failed && !canceled ? 0 : 1;
 }
 
@@ -443,6 +489,7 @@ void ProxyWindow::create_objects()
        dialog->use_scaler = mwindow->edl->session->proxy_use_scaler;
        dialog->orig_scale = mwindow->edl->session->proxy_scale;
        dialog->auto_scale = mwindow->edl->session->proxy_auto_scale;
+       dialog->beep = mwindow->edl->session->proxy_beep;
        dialog->new_scale = dialog->orig_scale;
 
        int x = margin;
@@ -454,9 +501,6 @@ void ProxyWindow::create_objects()
        add_subwindow(text = new BC_Title(x, y, _("Scale factor:")));
        x += text->get_w() + margin;
 
-       dialog->size_text[0] = cstrdup(_("Original size"));
-       dialog->size_factors[0] = 1;
-       dialog->total_sizes = 1;
        int popupmenu_w = BC_PopupMenu::calculate_w(get_text_width(MEDIUMFONT, dialog->size_text[0]));
        add_subwindow(scale_factor = new ProxyMenu(mwindow, this, x, y, popupmenu_w, ""));
        scale_factor->update_sizes();
@@ -480,6 +524,8 @@ void ProxyWindow::create_objects()
        x = margin;
        add_subwindow(auto_scale = new ProxyAutoScale(this, x, y));
        y += auto_scale->get_h() + margin;
+       add_subwindow(beep_on_done = new ProxyBeepOnDone(this, x, y));
+       y += beep_on_done->get_h() + margin;
 
        update();
 
@@ -549,7 +595,7 @@ int ProxyUseScaler::handle_event()
 }
 
 ProxyAutoScale::ProxyAutoScale(ProxyWindow *pwindow, int x, int y)
- : BC_CheckBox(x, y, pwindow->dialog->use_scaler, _("Auto proxy/scale media loads"))
+ : BC_CheckBox(x, y, pwindow->dialog->auto_scale, _("Auto proxy/scale media loads"))
 {
        this->pwindow = pwindow;
 }
@@ -571,6 +617,19 @@ int ProxyAutoScale::handle_event()
        return 1;
 }
 
+ProxyBeepOnDone::ProxyBeepOnDone(ProxyWindow *pwindow, int x, int y)
+ : BC_CheckBox(x, y, pwindow->dialog->beep, _("Beep on done"))
+{
+       this->pwindow = pwindow;
+}
+
+int ProxyBeepOnDone::handle_event()
+{
+       pwindow->dialog->beep = get_value();
+       pwindow->update();
+       return 1;
+}
+
 
 ProxyMenu::ProxyMenu(MWindow *mwindow, ProxyWindow *pwindow,
                int x, int y, int w, const char *text)
@@ -758,6 +817,11 @@ void ProxyClient::process_package(LoadPackage *ptr)
                }
                proxy_render->update_progress();
        }
+       if( !proxy_render->failed && !proxy_render->is_canceled() ) {
+               Asset *asset = mwindow->edl->assets->update(proxy);
+               mwindow->mainindexes->add_next_asset(0, asset);
+               mwindow->mainindexes->start_build();
+       }
 }
 
 
@@ -792,3 +856,85 @@ LoadPackage* ProxyFarm::new_package()
        return new ProxyPackage;
 }
 
+
+ProxyBeep::ProxyBeep(MWindow *mwindow)
+ : Thread(1, 0, 0)
+{
+       this->mwindow = mwindow;
+       audio = new AudioDevice(mwindow);
+       playing_audio = 0;
+       interrupted = -1;
+}
+
+ProxyBeep::~ProxyBeep()
+{
+       stop(0);
+       delete audio;
+}
+
+void ProxyBeep::run()
+{
+       int channels = 2;
+       int64_t bfrsz = BEEP_SAMPLE_RATE;
+       EDL *edl = mwindow->edl;
+       EDLSession *session = edl->session;
+       AudioOutConfig *aconfig = session->playback_config->aconfig;
+       audio->open_output(aconfig, BEEP_SAMPLE_RATE, bfrsz, channels, 0);
+       audio->start_playback();
+
+       double out0[bfrsz], out1[bfrsz], *out[2] = { out0, out1 };
+       const double two_pi = 2*M_PI;
+       int64_t audio_len = BEEP_SAMPLE_RATE * secs;
+       const double dt = two_pi * freq/BEEP_SAMPLE_RATE;
+       double th = 0;
+
+       audio_pos = 0;
+       playing_audio = 1;
+       while( !interrupted ) {
+               int len = audio_len - audio_pos;
+               if( len <= 0 ) break;
+               if( len > bfrsz ) len = bfrsz;
+               int k = audio_pos;
+               for( int i=0; i<len; ++i,++k,th+=dt ) {
+                       double t = th - two_pi;
+                       if( t >= 0 ) th = t;
+                       out0[i] = out1[i] = sin(th) * gain;
+               }
+               audio->write_buffer(out, channels, len);
+               audio_pos = k;
+       }
+
+       if( !interrupted )
+               audio->set_last_buffer();
+       audio->stop_audio(interrupted ? 0 : 1);
+       playing_audio = 0;
+
+       audio->close_all();
+}
+
+void ProxyBeep::start()
+{
+       if( running() ) return;
+       audio_pos = -1;
+       interrupted = 0;
+       Thread::start();
+}
+
+void ProxyBeep::stop(int wait)
+{
+       if( running() && !interrupted ) {
+               interrupted = 1;
+               audio->stop_audio(wait);
+       }
+       Thread::join();
+}
+
+void ProxyBeep::tone(double freq, double secs, double gain)
+{
+       stop(0);
+       this->freq = freq;
+       this->secs = secs;
+       this->gain = gain;
+       start();
+}
+