]> git.sesse.net Git - kdenlive/blobdiff - src/kthumb.cpp
Fix audio thumbs progress info when window was not focused
[kdenlive] / src / kthumb.cpp
index 9657399170c2ba4c3a9b7de3ca6e34ad3b8a3287..2358fc7fee225140ae9ae87902bb13a72ee2bc5b 100644 (file)
 #include <QApplication>
 #include <QCryptographicHash>
 
-
+#include "clipmanager.h"
 #include "renderer.h"
 #include "kthumb.h"
 #include "kdenlivesettings.h"
+#include "events.h"
 
-void MyThread::init(KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth)
+void MyThread::init(QObject *parent, KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth)
     {
        stop_me = false;
+       m_parent = parent;
        m_isWorking = false;
        f.setFileName(target);
        m_url = url;
@@ -54,7 +56,6 @@ void MyThread::init(KUrl url, QString target, double frame, double frameLength,
        m_frequency = frequency;
        m_channels = channels;
        m_arrayWidth = arrayWidth;
-
     }
 
     bool MyThread::isWorking()
@@ -72,34 +73,32 @@ void MyThread::init(KUrl url, QString target, double frame, double frameLength,
                        return;
                }
                m_isWorking = true;
-               Mlt::Profile prof((char*) KdenliveSettings::current_profile().data());
-               Mlt::Producer m_producer(prof, m_url.path().toAscii().data());
+               Mlt::Profile prof((char*) qstrdup(KdenliveSettings::current_profile().toUtf8()));
+               Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
                
 
                /*TODO if (KdenliveSettings::normaliseaudiothumbs()) {
-                   Mlt::Filter m_convert("volume");
+                   Mlt::Filter m_convert(prof,"volume");
                    m_convert.set("gain", "normalise");
                    m_producer.attach(m_convert);
                }*/
 
-               /*TODO if (qApp->mainWidget()) 
-                   QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
-       */
+               //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
+       
                int last_val = 0;
                int val = 0;
                kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
                for (int z=(int) m_frame;z<(int) (m_frame+m_frameLength) && m_producer.is_valid();z++){
-                       kDebug() << "starting audithumb for frame " << z;
-                       
                        if (stop_me) break;
                        val=(int)((z-m_frame)/(m_frame+m_frameLength)*100.0);
                        if (last_val!=val & val > 1){
-                               //TODO QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(val, 10005));
+                               QApplication::postEvent(m_parent, new ProgressEvent(val, (QEvent::Type)10005));
+                               
                                last_val=val;
                        }
                                m_producer.seek( z );
                                Mlt::Frame *mlt_frame = m_producer.get_frame();
-                               if ( mlt_frame->is_valid() )
+                               if ( mlt_frame && mlt_frame->is_valid() )
                                {
                                        double m_framesPerSecond = mlt_producer_get_fps( m_producer.get_producer() ); //mlt_frame->get_double( "fps" );
                                        int m_samples = mlt_sample_calculator( m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()) );
@@ -111,7 +110,7 @@ void MyThread::init(KUrl url, QString target, double frame, double frameLength,
                                                QByteArray m_array;
                                                m_array.resize(m_arrayWidth);
                                                for (uint i = 0; i < m_array.size(); i++){
-                                                       m_array[i] =  qAbs((*( m_pcm + c + i * m_samples / m_array.size() ))>>8);
+                                                       m_array[i] =  ( (*( m_pcm + c + i * m_samples / m_array.size() )) >> 9 ) +127/2 ;
                                                }
                                                f.write(m_array);
                                                
@@ -122,13 +121,16 @@ void MyThread::init(KUrl url, QString target, double frame, double frameLength,
                                if (mlt_frame)
                                        delete mlt_frame;
                }
+               kDebug() << "done";
                f.close();
                m_isWorking = false;
                if (stop_me) {
                    f.remove();
-                  //TODO  QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
+                   QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
+                  
                }
-               //TODO else QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(0, 10005));
+               QApplication::postEvent(m_parent, new ProgressEvent(0, (QEvent::Type)10005));
+               
     }
 
 
@@ -137,10 +139,14 @@ void MyThread::init(KUrl url, QString target, double frame, double frameLength,
 #define _G(y,u,v) (0x2568*(y) - 0x0c92*(v) - 0x1a1e*(u)) /0x2000
 #define _B(y,u,v) (0x2568*(y) + 0x40cf*(v))                                          /0x2000
 
-KThumb::KThumb(KUrl url, int width, int height, QObject * parent, const char *name):QObject(parent), m_url(url), m_width(width), m_height(height)
+KThumb::KThumb(ClipManager *clipManager, KUrl url, int width, int height, QObject * parent, const char *name):QObject(parent), m_clipManager(clipManager), m_url(url), m_width(width), m_height(height)
 {
-  kDebug()<<"+++++++++++  CREATING THMB PROD FOR: "<<url;
-  m_profile = new Mlt::Profile((char*) qstrdup(KdenliveSettings::current_profile().toUtf8()));
+
+  m_profile = new Mlt::Profile((char*) KdenliveSettings::current_profile().data());
+  QCryptographicHash context(QCryptographicHash::Sha1);
+  context.addData((KFileItem(m_url,"text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());  
+  m_thumbFile = KdenliveSettings::currenttmpfolder() + context.result().toHex() + ".thumb";
+  kDebug() << "thumbfile=" << m_thumbFile;
 }
 
 KThumb::~KThumb()
@@ -320,9 +326,9 @@ void KThumb::removeAudioThumb()
        f.remove();
 }
 
-void KThumb::getAudioThumbs(KUrl url, int channel, double frame, double frameLength, int arrayWidth){
+void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth){
        
-       if ((thumbProducer.isRunning () && thumbProducer.isWorking()) || channel == 0) {
+       if ((thumbProducer.isRunning() && thumbProducer.isWorking()) || channel == 0) {
            return;
        }
        
@@ -330,17 +336,6 @@ void KThumb::getAudioThumbs(KUrl url, int channel, double frame, double frameLen
        //FIXME: Hardcoded!!! 
        int m_frequency = 48000;
        int m_channels = channel;
-
-       m_thumbFile="/tmp/testfile";
-       /*FIXME WHY crash here ??????
-       if (m_url != url) {
-               m_url = url;
-               QCryptographicHash context(QCryptographicHash::Sha1);
-               context.addData((KFileItem(m_url,"text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
-               
-               m_thumbFile = KdenliveSettings::currenttmpfolder() + context.result().toHex() + ".thumb";
-               
-       }*/
        
        QFile f(m_thumbFile);
        if (f.open( QIODevice::ReadOnly )) {
@@ -363,12 +358,21 @@ void KThumb::getAudioThumbs(KUrl url, int channel, double frame, double frameLen
                emit audioThumbReady(storeIn);
        }
        else {
-               /*if (thumbProducer.isRunning()) return;
-               thumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
+               if (thumbProducer.isRunning()) return;
+               thumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
                thumbProducer.start(QThread::LowestPriority );
-               */
+               kDebug() << "STARTING GENERATE THMB FOR: "<<m_url<<" ................................";
+       }
+}
+
+void KThumb::customEvent ( QEvent * event ){
+       if (event->type()==10005){
+               ProgressEvent* p=(ProgressEvent*) event;
+               m_clipManager->setThumbsProgress(m_url, p->value());
        }
 }
 
 
 
+#include "kthumb.moc"
+