]> git.sesse.net Git - kdenlive/blob - testingArea/audioOffset.cpp
Audio alignment: Attempts to get familiar with mlt
[kdenlive] / testingArea / audioOffset.cpp
1
2 #include <QFile>
3 #include <mlt++/Mlt.h>
4 #include <iostream>
5 #include <cstdlib>
6
7 int main(int argc, char *argv[])
8 {
9     char *fileMain;
10     char *fileSub;
11     if (argc > 2) {
12         fileMain = argv[1];
13         fileSub = argv[2];
14     } else {
15         std::cout << "Usage: " << argv[0] << " <main audio file> <second audio file>" << std::endl;
16         return 0;
17     }
18     std::cout << "Trying to align (1)\n\t" << fileSub << "\nto fit on (2)\n\t" << fileMain
19               << "\n, result will indicate by how much (1) has to be moved." << std::endl;
20
21
22     // Initialize MLT
23     Mlt::Factory::init(NULL);
24
25     // Load an arbitrary profile
26     Mlt::Profile prof("hdv_1080_25p");
27
28
29     std::cout << "MLT initialized, profile loaded. Loading clips ..." << std::endl;
30
31
32     Mlt::Producer producer(prof, fileMain);
33     if (!producer.is_valid()) {
34         std::cout << fileMain << " is invalid." << std::endl;
35         return 2;
36     }
37     std::cout << "Successfully loaded producer " << fileMain << std::endl;
38     std::cout << "Some data: " << std::endl
39               << "Length: " << producer.get_length() << std::endl
40               << "fps: " << producer.get_fps() << std::endl;
41
42     int nSamples = 50;
43     Mlt::Frame *frame = producer.get_frame();
44     float *data = frame->get_audio(mlt_audio_float, 24000, 1, nSamples);
45
46     for (int i = 0; i < nSamples; i++) {
47         std::cout << " " << data[i];
48     }
49     std::cout << std::endl;
50
51 /*
52     producer.set("video_index", "-1");
53
54     if (KdenliveSettings::normaliseaudiothumbs()) {
55         Mlt::Filter m_convert(prof, "volume");
56         m_convert.set("gain", "normalise");
57         producer.attach(m_convert);
58     }
59
60     int last_val = 0;
61     int val = 0;
62     double framesPerSecond = mlt_producer_get_fps(producer.get_producer());
63     Mlt::Frame *mlt_frame;
64
65     for (int z = (int) frame; z < (int)(frame + lengthInFrames) && producer.is_valid() &&  !m_abortAudioThumb; z++) {
66         val = (int)((z - frame) / (frame + lengthInFrames) * 100.0);
67         if (last_val != val && val > 1) {
68             setThumbsProgress(i18n("Creating audio thumbnail for %1", url.fileName()), val);
69             last_val = val;
70         }
71         producer.seek(z);
72         mlt_frame = producer.get_frame();
73         if (mlt_frame && mlt_frame->is_valid()) {
74             int samples = mlt_sample_calculator(framesPerSecond, frequency, mlt_frame_get_position(mlt_frame->get_frame()));
75             qint16* pcm = static_cast<qint16*>(mlt_frame->get_audio(audioFormat, frequency, channels, samples));
76             for (int c = 0; c < channels; c++) {
77                 QByteArray audioArray;
78                 audioArray.resize(arrayWidth);
79                 for (int i = 0; i < audioArray.size(); i++) {
80                     audioArray[i] = ((*(pcm + c + i * samples / audioArray.size())) >> 9) + 127 / 2 ;
81                 }
82                 f.write(audioArray);
83                 storeIn[z][c] = audioArray;
84             }
85         } else {
86             f.write(QByteArray(arrayWidth, '\x00'));
87         }
88         delete mlt_frame;
89     }
90     f.close();
91     setThumbsProgress(i18n("Creating audio thumbnail for %1", url.fileName()), -1);
92     if (m_abortAudioThumb) {
93         f.remove();
94     } else {
95         clip->updateAudioThumbnail(storeIn);
96     }*/
97
98     return 0;
99
100 }