1 /***************************************************************************
4 begin : Wed Dec 17 2003
5 copyright : (C) 2003 by Jason Wood
6 email : jasonwood@blueyonder.co.uk
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
24 Timecode::Timecode(Formats format, double framesPerSecond, bool dropFrame)
26 m_validator = new QRegExpValidator(0);
27 setFormat(framesPerSecond, dropFrame, format);
34 void Timecode::setFormat(double framesPerSecond, bool dropFrame, Formats format)
36 m_displayedFramesPerSecond = (int)(framesPerSecond + 0.5);
37 m_dropFrame = dropFrame;
39 m_realFps = framesPerSecond;
40 m_dropFrames = round(m_realFps * .066666); //Number of frames to drop on the minute marks is the nearest integer to 6% of the framerate
43 regExp.setPattern("^\\d{2}:\\d{2}:\\d{2};\\d{2}$");
45 regExp.setPattern("^\\d{2}:\\d{2}:\\d{2}:\\d{2}$");
46 m_validator->setRegExp(regExp);
49 double Timecode::fps() const
51 return m_realFps; //m_displayedFramesPerSecond;
54 bool Timecode::df() const
59 const QValidator *Timecode::validator() const
64 QString Timecode::reformatSeparators(QString duration) const
67 return duration.replace(8, 1, ';');
68 return duration.replace(8, 1, ':');
71 int Timecode::getDisplayFrameCount(const QString duration, bool frameDisplay) const
73 if (frameDisplay) return duration.toInt();
74 return getFrameCount(duration);
77 int Timecode::getFrameCount(const QString duration) const
81 //CONVERT DROP FRAME TIMECODE TO A FRAME NUMBER
82 //Code by David Heidelberger, adapted from Andrew Duncan
83 //Given ints called hours, minutes, seconds, frames, and a double called framerate
85 //Get Hours, Minutes, Seconds, Frames from timecode
86 int hours, minutes, seconds, frames;
88 hours = duration.section(':', 0, 0).toInt();
89 minutes = duration.section(':', 1, 1).toInt();
90 if (duration.contains(';')) {
91 seconds = duration.section(';', 0, 0).section(':', 2, 2).toInt();
92 frames = duration.section(';', 1, 1).toInt();
94 //Handle Drop Frame timecode frame calculations, even if the timecode supplied uses incorrect "99:99:99:99" format instead of "99:99:99;99"
95 seconds = duration.section(':', 2, 2).toInt();
96 frames = duration.section(':', 3, 3).toInt();
99 int totalMinutes = (60 * hours) + minutes; //Total number of minutes
100 int frameNumber = ((m_displayedFramesPerSecond * 60 * 60 * hours) + (m_displayedFramesPerSecond * 60 * minutes) + (m_displayedFramesPerSecond * seconds) + frames) - (m_dropFrames * (totalMinutes - floor(totalMinutes / 10)));
104 //Calculate the frame count
105 int dropRate = (int)((ceil(m_displayedFramesPerSecond) / 30) * 2);
106 frames += ((hours * 60 + minutes) * 60 + seconds) * m_displayedFramesPerSecond;
107 frames -= dropRate * ((hours * 60 + minutes) - (floor((hours * 60 + minutes) / 10)));
110 return (int)((duration.section(':', 0, 0).toInt()*3600.0 + duration.section(':', 1, 1).toInt()*60.0 + duration.section(':', 2, 2).toInt()) * m_realFps + duration.section(':', 3, 3).toInt());
113 QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
115 if (frameDisplay) return QString::number((int) time.frames(m_realFps));
116 return getTimecode(time);
119 QString Timecode::getTimecode(const GenTime & time) const
123 return getTimecodeHH_MM_SS_FF(time);
126 return getTimecodeHH_MM_SS_HH(time);
129 return getTimecodeFrames(time);
132 return getTimecodeSeconds(time);
136 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
138 return getTimecodeHH_MM_SS_FF(time);
142 const QString Timecode::getDisplayTimecodeFromFrames(int frames, bool frameDisplay) const
144 if (frameDisplay) return QString::number(frames);
145 return getTimecodeHH_MM_SS_FF(frames);
148 const QString Timecode::getTimecodeFromFrames(int frames) const
150 return getTimecodeHH_MM_SS_FF(frames);
155 QString Timecode::getStringTimecode(int frames, const double &fps)
157 // Returns the timecode in an hh:mm:ss format
158 int seconds = (int)(frames / fps);
159 int minutes = seconds / 60;
160 seconds = seconds % 60;
161 int hours = minutes / 60;
162 minutes = minutes % 60;
164 text.append(QString::number(hours).rightJustified(2, '0', false));
166 text.append(QString::number(minutes).rightJustified(2, '0', false));
168 text.append(QString::number(seconds).rightJustified(2, '0', false));
174 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
176 // Returns the timecode in an easily read display, like 3 min. 5 sec.
177 int frames = (int) time.frames(fps);
178 int seconds = (int)(frames / fps);
179 frames = frames - ((int)(fps * seconds));
181 int minutes = seconds / 60;
182 seconds = seconds % 60;
183 int hours = minutes / 60;
184 minutes = minutes % 60;
190 text.append(QString::number(hours).rightJustified(2, '0', false));
191 text.append(' ' + i18n("hour") + ' ');
194 if (minutes != 0 || trim) {
196 text.append(QString::number(minutes));
198 text.append(QString::number(minutes).rightJustified(2, '0', false));
199 text.append(' ' + i18n("min.") + ' ');
202 if (seconds != 0 || trim) {
204 text.append(QString::number(seconds));
206 text.append(QString::number(seconds).rightJustified(2, '0', false));
207 text.append(' ' + i18n("sec."));
211 text.append(QString::number(frames));
212 text.append(' ' + i18n("frames"));
219 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
222 return getTimecodeDropFrame(time);
224 return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
227 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
230 return getTimecodeDropFrame(frames);
232 int seconds = frames / m_displayedFramesPerSecond;
233 frames = frames % m_displayedFramesPerSecond;
235 int minutes = seconds / 60;
236 seconds = seconds % 60;
237 int hours = minutes / 60;
238 minutes = minutes % 60;
241 text.append(QString::number(hours).rightJustified(2, '0', false));
243 text.append(QString::number(minutes).rightJustified(2, '0', false));
245 text.append(QString::number(seconds).rightJustified(2, '0', false));
247 text.append(QString::number(frames).rightJustified(2, '0', false));
252 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
254 int hundredths = (int)(time.seconds() * 100);
255 int seconds = hundredths / 100;
256 hundredths = hundredths % 100;
257 int minutes = seconds / 60;
258 seconds = seconds % 60;
259 int hours = minutes / 60;
260 minutes = minutes % 60;
264 text.append(QString::number(hours).rightJustified(2, '0', false));
266 text.append(QString::number(minutes).rightJustified(2, '0', false));
268 text.append(QString::number(seconds).rightJustified(2, '0', false));
273 text.append(QString::number(hundredths).rightJustified(2, '0', false));
278 QString Timecode::getTimecodeFrames(const GenTime & time) const
280 return QString::number(time.frames(m_realFps));
283 QString Timecode::getTimecodeSeconds(const GenTime & time) const
285 return QString::number(time.seconds());
288 QString Timecode::getTimecodeDropFrame(const GenTime & time) const
290 return getTimecodeDropFrame((int)time.frames(m_realFps));
293 QString Timecode::getTimecodeDropFrame(int framenumber) const
295 //CONVERT A FRAME NUMBER TO DROP FRAME TIMECODE
296 //Based on code by David Heidelberger, adapted from Andrew Duncan
297 //Given an int called framenumber and a double called framerate
298 //Framerate should be 29.97, 59.94, or 23.976, otherwise the calculations will be off.
303 //int framesPerHour = round(m_realFps * 60 * 60); //Number of frames in an hour
304 //int framesPer24Hours = framesPerHour * 24; //Number of frames in a day - timecode rolls over after 24 hours
305 int framesPer10Minutes = round(m_realFps * 60 * 10); //Number of frames per ten minutes
306 //int framesPerMinute = round(framerate)*60)- dropFrames; //Number of frames per minute is the round of the framerate * 60 minus the number of dropped frames
309 * The 2 check below should not be necessary in Kdenlive
310 if (framenumber<0) //Negative time. Add 24 hours.
312 framenumber=framesPer24Hours+framenumber;
315 //If framenumber is greater than 24 hrs, next operation will rollover clock
316 framenumber = framenumber % framesPer24Hours; //% is the modulus operator, which returns a remainder. a % b = the remainder of a/b
319 d = floor(framenumber / framesPer10Minutes); // \ means integer division, which is a/b without a remainder. Some languages you could use floor(a/b)
320 m = framenumber % framesPer10Minutes;
323 framenumber = framenumber + (m_dropFrames * 9 * d) + m_dropFrames * (floor((m - m_dropFrames) / (round(m_realFps * 60) - m_dropFrames)));
325 framenumber = framenumber + m_dropFrames * 9 * d;
328 int frames = framenumber % m_displayedFramesPerSecond;
329 int seconds = (int) floor(framenumber / m_displayedFramesPerSecond) % 60;
330 int minutes = (int) floor(floor(framenumber / m_displayedFramesPerSecond) / 60) % 60;
331 int hours = floor(floor(floor(framenumber / m_displayedFramesPerSecond) / 60) / 60);
335 text.append(QString::number(hours).rightJustified(2, '0', false));
337 text.append(QString::number(minutes).rightJustified(2, '0', false));
339 text.append(QString::number(seconds).rightJustified(2, '0', false));
341 text.append(QString::number(frames).rightJustified(2, '0', false));