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;
42 regExp.setPattern("^\\d{2}:\\d{2}:\\d{2};\\d{2}$");
44 regExp.setPattern("^\\d{2}:\\d{2}:\\d{2}:\\d{2}$");
45 m_validator->setRegExp(regExp);
48 double Timecode::fps() const
50 return m_realFps; //m_displayedFramesPerSecond;
53 bool Timecode::df() const
58 const QValidator *Timecode::validator() const
65 QString Timecode::reformatSeparators(QString duration) const
68 return duration.replace(8, 1, ';');
69 return duration.replace(8, 1, ':');
72 int Timecode::getDisplayFrameCount(const QString duration, bool frameDisplay) const
74 if (frameDisplay) return duration.toInt();
75 return getFrameCount(duration);
78 int Timecode::getFrameCount(const QString duration) const
81 //Get Hours, Minutes, Seconds, Frames from timecode
82 int hours, minutes, seconds, frames;
84 hours = duration.section(':', 0, 0).toInt();
85 minutes = duration.section(':', 1, 1).toInt();
86 if (duration.contains(';')) {
87 seconds = duration.section(';', 0, 0).section(':', 2, 2).toInt();
88 frames = duration.section(';', 1, 1).toInt();
90 //Handle Drop Frame timecode frame calculations, even if the timecode supplied uses incorrect "99:99:99:99" format instead of "99:99:99;99"
91 seconds = duration.section(':', 2, 2).toInt();
92 frames = duration.section(':', 3, 3).toInt();
95 //Calculate the frame count
96 int dropRate = (int)((ceil(m_displayedFramesPerSecond) / 30) * 2);
97 frames += ((hours * 60 + minutes) * 60 + seconds) * m_displayedFramesPerSecond;
98 frames -= dropRate * ((hours * 60 + minutes) - (floor((hours * 60 + minutes) / 10)));
101 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());
104 QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
106 if (frameDisplay) return QString::number((int) time.frames(m_realFps));
107 return getTimecode(time);
110 QString Timecode::getTimecode(const GenTime & time) const
114 return getTimecodeHH_MM_SS_FF(time);
117 return getTimecodeHH_MM_SS_HH(time);
120 return getTimecodeFrames(time);
123 return getTimecodeSeconds(time);
127 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
129 return getTimecodeHH_MM_SS_FF(time);
133 const QString Timecode::getDisplayTimecodeFromFrames(int frames, bool frameDisplay) const
135 if (frameDisplay) return QString::number(frames);
136 return getTimecodeHH_MM_SS_FF(frames);
139 const QString Timecode::getTimecodeFromFrames(int frames) const
141 return getTimecodeHH_MM_SS_FF(frames);
146 QString Timecode::getStringTimecode(int frames, const double &fps)
148 // Returns the timecode in an hh:mm:ss format
149 int seconds = (int)(frames / fps);
150 int minutes = seconds / 60;
151 seconds = seconds % 60;
152 int hours = minutes / 60;
153 minutes = minutes % 60;
155 text.append(QString::number(hours).rightJustified(2, '0', false));
157 text.append(QString::number(minutes).rightJustified(2, '0', false));
159 text.append(QString::number(seconds).rightJustified(2, '0', false));
165 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
167 // Returns the timecode in an easily read display, like 3 min. 5 sec.
168 int frames = (int) time.frames(fps);
169 int seconds = (int)(frames / fps);
170 frames = frames - ((int)(fps * seconds));
172 int minutes = seconds / 60;
173 seconds = seconds % 60;
174 int hours = minutes / 60;
175 minutes = minutes % 60;
181 text.append(QString::number(hours).rightJustified(2, '0', false));
182 text.append(' ' + i18n("hour") + ' ');
185 if (minutes != 0 || trim) {
187 text.append(QString::number(minutes));
189 text.append(QString::number(minutes).rightJustified(2, '0', false));
190 text.append(' ' + i18n("min.") + ' ');
193 if (seconds != 0 || trim) {
195 text.append(QString::number(seconds));
197 text.append(QString::number(seconds).rightJustified(2, '0', false));
198 text.append(' ' + i18n("sec."));
202 text.append(QString::number(frames));
203 text.append(' ' + i18n("frames"));
210 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
213 return getTimecodeDropFrame(time);
215 return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
218 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
221 return getTimecodeDropFrame(frames);
223 int seconds = frames / m_displayedFramesPerSecond;
224 frames = frames % m_displayedFramesPerSecond;
226 int minutes = seconds / 60;
227 seconds = seconds % 60;
228 int hours = minutes / 60;
229 minutes = minutes % 60;
232 text.append(QString::number(hours).rightJustified(2, '0', false));
234 text.append(QString::number(minutes).rightJustified(2, '0', false));
236 text.append(QString::number(seconds).rightJustified(2, '0', false));
238 text.append(QString::number(frames).rightJustified(2, '0', false));
243 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
245 int hundredths = (int)(time.seconds() * 100);
246 int seconds = hundredths / 100;
247 hundredths = hundredths % 100;
248 int minutes = seconds / 60;
249 seconds = seconds % 60;
250 int hours = minutes / 60;
251 minutes = minutes % 60;
255 text.append(QString::number(hours).rightJustified(2, '0', false));
257 text.append(QString::number(minutes).rightJustified(2, '0', false));
259 text.append(QString::number(seconds).rightJustified(2, '0', false));
264 text.append(QString::number(hundredths).rightJustified(2, '0', false));
269 QString Timecode::getTimecodeFrames(const GenTime & time) const
271 return QString::number(time.frames(m_realFps));
274 QString Timecode::getTimecodeSeconds(const GenTime & time) const
276 return QString::number(time.seconds());
279 QString Timecode::getTimecodeDropFrame(const GenTime & time) const
281 return getTimecodeDropFrame((int)time.frames(m_realFps));
284 QString Timecode::getTimecodeDropFrame(int frames) const
286 // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
287 // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
289 // calculate how many frames need to be dropped every minute.
292 dropRate = (int)((ceil(m_displayedFramesPerSecond) / 30) * 2);
295 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
296 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - dropRate;
297 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60);
299 // Number of actual frames in a 10 minute interval :
300 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
302 int tenMinuteIntervals = frames / tenMinutes;
303 frames = frames % tenMinutes;
305 int hours = tenMinuteIntervals / 6;
306 tenMinuteIntervals = tenMinuteIntervals % 6;
308 // At the point, we have figured out HH:M?:??:??
312 if (frames < tenthMinuteFrames) {
313 // tenth minute logic applies.
316 // normal minute logic applies.
317 numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
318 frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
321 // We now have HH:MM:??:??
323 int seconds = frames / m_displayedFramesPerSecond;
324 frames = frames % m_displayedFramesPerSecond;
326 // We now have HH:MM:SS:FF
328 // THANK FUCK FOR THAT.
331 text.append(QString::number(hours).rightJustified(2, '0', false));
333 text.append(QString::number(tenMinuteIntervals));
334 text.append(QString::number(numMinutes));
336 text.append(QString::number(seconds).rightJustified(2, '0', false));
341 text.append(QString::number(frames).rightJustified(2, '0', false));