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
63 QString Timecode::reformatSeparators(QString duration) const
66 return duration.replace(8, 1, ';');
67 return duration.replace(8, 1, ':');
70 int Timecode::getDisplayFrameCount(const QString duration, bool frameDisplay) const
72 if (frameDisplay) return duration.toInt();
73 return getFrameCount(duration);
76 int Timecode::getFrameCount(const QString duration) const
79 //Get Hours, Minutes, Seconds, Frames from timecode
80 int hours, minutes, seconds, frames;
82 hours = duration.section(':', 0, 0).toInt();
83 minutes = duration.section(':', 1, 1).toInt();
84 if (duration.contains(';')) {
85 seconds = duration.section(';', 0, 0).section(':', 2, 2).toInt();
86 frames = duration.section(';', 1, 1).toInt();
88 //Handle Drop Frame timecode frame calculations, even if the timecode supplied uses incorrect "99:99:99:99" format instead of "99:99:99;99"
89 seconds = duration.section(':', 2, 2).toInt();
90 frames = duration.section(':', 3, 3).toInt();
93 //Calculate the frame count
94 int dropRate = (int)((ceil(m_displayedFramesPerSecond) / 30) * 2);
95 frames += ((hours * 60 + minutes) * 60 + seconds) * m_displayedFramesPerSecond;
96 frames -= dropRate * ((hours * 60 + minutes) - (floor((hours * 60 + minutes) / 10)));
99 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());
102 QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
104 if (frameDisplay) return QString::number((int) time.frames(m_realFps));
105 return getTimecode(time);
108 QString Timecode::getTimecode(const GenTime & time) const
112 return getTimecodeHH_MM_SS_FF(time);
115 return getTimecodeHH_MM_SS_HH(time);
118 return getTimecodeFrames(time);
121 return getTimecodeSeconds(time);
125 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
127 return getTimecodeHH_MM_SS_FF(time);
131 const QString Timecode::getDisplayTimecodeFromFrames(int frames, bool frameDisplay) const
133 if (frameDisplay) return QString::number(frames);
134 return getTimecodeHH_MM_SS_FF(frames);
137 const QString Timecode::getTimecodeFromFrames(int frames) const
139 return getTimecodeHH_MM_SS_FF(frames);
144 QString Timecode::getStringTimecode(int frames, const double &fps)
146 // Returns the timecode in an hh:mm:ss format
147 int seconds = (int)(frames / fps);
148 int minutes = seconds / 60;
149 seconds = seconds % 60;
150 int hours = minutes / 60;
151 minutes = minutes % 60;
153 text.append(QString::number(hours).rightJustified(2, '0', false));
155 text.append(QString::number(minutes).rightJustified(2, '0', false));
157 text.append(QString::number(seconds).rightJustified(2, '0', false));
163 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
165 // Returns the timecode in an easily read display, like 3 min. 5 sec.
166 int frames = (int) time.frames(fps);
167 int seconds = (int)(frames / fps);
168 frames = frames - ((int)(fps * seconds));
170 int minutes = seconds / 60;
171 seconds = seconds % 60;
172 int hours = minutes / 60;
173 minutes = minutes % 60;
179 text.append(QString::number(hours).rightJustified(2, '0', false));
180 text.append(' ' + i18n("hour") + ' ');
183 if (minutes != 0 || trim) {
185 text.append(QString::number(minutes));
187 text.append(QString::number(minutes).rightJustified(2, '0', false));
188 text.append(' ' + i18n("min.") + ' ');
191 if (seconds != 0 || trim) {
193 text.append(QString::number(seconds));
195 text.append(QString::number(seconds).rightJustified(2, '0', false));
196 text.append(' ' + i18n("sec."));
200 text.append(QString::number(frames));
201 text.append(' ' + i18n("frames"));
208 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
211 return getTimecodeDropFrame(time);
213 return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
216 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
219 return getTimecodeDropFrame(frames);
221 int seconds = frames / m_displayedFramesPerSecond;
222 frames = frames % m_displayedFramesPerSecond;
224 int minutes = seconds / 60;
225 seconds = seconds % 60;
226 int hours = minutes / 60;
227 minutes = minutes % 60;
230 text.append(QString::number(hours).rightJustified(2, '0', false));
232 text.append(QString::number(minutes).rightJustified(2, '0', false));
234 text.append(QString::number(seconds).rightJustified(2, '0', false));
236 text.append(QString::number(frames).rightJustified(2, '0', false));
241 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
243 int hundredths = (int)(time.seconds() * 100);
244 int seconds = hundredths / 100;
245 hundredths = hundredths % 100;
246 int minutes = seconds / 60;
247 seconds = seconds % 60;
248 int hours = minutes / 60;
249 minutes = minutes % 60;
253 text.append(QString::number(hours).rightJustified(2, '0', false));
255 text.append(QString::number(minutes).rightJustified(2, '0', false));
257 text.append(QString::number(seconds).rightJustified(2, '0', false));
262 text.append(QString::number(hundredths).rightJustified(2, '0', false));
267 QString Timecode::getTimecodeFrames(const GenTime & time) const
269 return QString::number(time.frames(m_realFps));
272 QString Timecode::getTimecodeSeconds(const GenTime & time) const
274 return QString::number(time.seconds());
277 QString Timecode::getTimecodeDropFrame(const GenTime & time) const
279 return getTimecodeDropFrame((int)time.frames(m_realFps));
282 QString Timecode::getTimecodeDropFrame(int frames) const
284 // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
285 // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
287 // calculate how many frames need to be dropped every minute.
290 dropRate = (int)((ceil(m_displayedFramesPerSecond) / 30) * 2);
293 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
294 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - dropRate;
295 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60);
297 // Number of actual frames in a 10 minute interval :
298 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
300 int tenMinuteIntervals = frames / tenMinutes;
301 frames = frames % tenMinutes;
303 int hours = tenMinuteIntervals / 6;
304 tenMinuteIntervals = tenMinuteIntervals % 6;
306 // At the point, we have figured out HH:M?:??:??
310 if (frames < tenthMinuteFrames) {
311 // tenth minute logic applies.
314 // normal minute logic applies.
315 numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
316 frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
319 // We now have HH:MM:??:??
321 int seconds = frames / m_displayedFramesPerSecond;
322 frames = frames % m_displayedFramesPerSecond;
324 // We now have HH:MM:SS:FF
326 // THANK FUCK FOR THAT.
329 text.append(QString::number(hours).rightJustified(2, '0', false));
331 text.append(QString::number(tenMinuteIntervals));
332 text.append(QString::number(numMinutes));
334 text.append(QString::number(seconds).rightJustified(2, '0', false));
339 text.append(QString::number(frames).rightJustified(2, '0', false));