1 /***************************************************************************
2 * Copyright (C) 2012 by Simon Andreas Eugster (simon.eu@gmail.com) *
3 * This file is part of kdenlive. See www.kdenlive.org. *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
18 #include <mlt++/Mlt.h>
23 #include "audioInfo.h"
24 #include "audioStreamInfo.h"
25 #include "audioEnvelope.h"
27 int main(int argc, char *argv[])
35 std::cout << "Usage: " << argv[0] << " <main audio file> <second audio file>" << std::endl;
38 std::cout << "Trying to align (1)\n\t" << fileSub << "\nto fit on (2)\n\t" << fileMain
39 << "\n, result will indicate by how much (1) has to be moved." << std::endl;
42 Mlt::Factory::init(NULL);
44 // Load an arbitrary profile
45 Mlt::Profile prof("hdv_1080_25p");
47 // Load the MLT producers
48 Mlt::Producer prodMain(prof, fileMain);
49 if (!prodMain.is_valid()) {
50 std::cout << fileMain << " is invalid." << std::endl;
53 Mlt::Producer prodSub(prof, fileSub);
54 if (!prodSub.is_valid()) {
55 std::cout << fileSub << " is invalid." << std::endl;
59 AudioEnvelope envelopeMain(&prodMain);
60 envelopeMain.loadEnvelope();
61 envelopeMain.loadStdDev();
62 envelopeMain.dumpInfo();
63 envelopeMain.normalizeEnvelope();
64 envelopeMain.dumpInfo();
66 AudioEnvelope envelopeSub(&prodSub);
67 envelopeSub.loadEnvelope();
68 envelopeMain.normalizeEnvelope();
69 envelopeSub.dumpInfo();
72 QString outImg = QString("envelope-%1.png")
73 .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd-hh:mm:ss"));
74 envelopeMain.drawEnvelope().save(outImg);
75 std::cout << "Saved volume envelope as "
76 << QFileInfo(outImg).absoluteFilePath().toStdString()
81 const int sizeX = envelopeMain.envelopeSize();
82 const int sizeY = envelopeSub.envelopeSize();
83 int64_t correlation[sizeX + sizeY + 1];
84 const int64_t *envX = envelopeMain.envelope();
85 const int64_t *envY = envelopeSub.envelope();
94 for (int shift = -sizeX; shift <= sizeY; shift++) {
99 size = std::min(sizeX+shift, sizeY);
103 size = std::min(sizeX, sizeY-shift);
107 for (int i = 0; i < size; i++) {
108 sum += (*left) * (*right);
112 correlation[sizeX+shift] = std::abs(sum);
113 std::cout << sum << " ";
120 std::cout << "Correlation calculated. Time taken: " << t.elapsed() << " ms." << std::endl;
123 QImage img(sizeX + sizeY + 1, 400, QImage::Format_ARGB32);
124 img.fill(qRgb(255,255,255));
125 for (int x = 0; x < sizeX+sizeY+1; x++) {
126 val = correlation[x]/double(max)*img.height();
127 for (int y = img.height()-1; y > img.height() - val - 1; y--) {
128 img.setPixel(x, y, qRgb(50, 50, 50));
132 outImg = QString("correlation-%1.png")
133 .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd-hh:mm:ss"));
135 std::cout << "Saved volume envelope as "
136 << QFileInfo(outImg).absoluteFilePath().toStdString()