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 ***************************************************************************/
22 Timecode::Timecode(Formats format, double framesPerSecond, bool dropFrame) :
24 m_dropFrame(dropFrame),
25 m_displayedFramesPerSecond(framesPerSecond + 0.5),
26 m_realFps(framesPerSecond)
34 double Timecode::fps() const
36 return m_realFps; //m_displayedFramesPerSecond;
39 bool Timecode::df() const
44 QString Timecode::inputMask() const
46 if (m_dropFrame) return "99:99.99:99";
50 QString Timecode::reformatSeparators(QString duration) const
53 return duration.replace(5, 1, '.');
55 return duration.replace(5, 1, ':');
58 int Timecode::getDisplayFrameCount(const QString duration, bool frameDisplay) const
60 if (frameDisplay) return duration.toInt();
61 return getFrameCount(duration);
64 int Timecode::getFrameCount(const QString duration) const
67 //Get Hours, Minutes, Seconds, Frames from timecode
68 int hours, minutes, seconds, frames;
70 hours = duration.section(':', 0, 0).toInt();
71 if (duration.contains('.')) {
72 minutes = duration.section('.', 0, 0).section(':', 1, 1).toInt();
73 seconds = duration.section('.', 1, 1).section(':', 0, 0).toInt();
74 frames = duration.section('.', 1, 1).section(':', 1, 1).toInt();
76 //Handle Drop Frame timecode frame calculations, even if the timecode supplied uses incorrect "99:99:99:99" format instead of "99:99.99:99"
77 minutes = duration.section(':', 1, 1).toInt();
78 seconds = duration.section(':', 2, 2).toInt();
79 frames = duration.section(':', 3, 3).toInt();
82 //Calculate the frame count
83 int dropRate = (int) ((ceil(m_displayedFramesPerSecond) / 30) * 2);
84 frames += ((hours * 60 + minutes) * 60 + seconds) * m_displayedFramesPerSecond;
85 frames -= dropRate * ((hours * 60 + minutes) - (floor((hours * 60 + minutes) / 10)));
88 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());
91 QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
93 if (frameDisplay) return QString::number((int) time.frames(m_realFps));
94 return getTimecode(time);
97 QString Timecode::getTimecode(const GenTime & time) const
101 return getTimecodeHH_MM_SS_FF(time);
104 return getTimecodeHH_MM_SS_HH(time);
107 return getTimecodeFrames(time);
110 return getTimecodeSeconds(time);
114 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
116 return getTimecodeHH_MM_SS_FF(time);
120 const QString Timecode::getDisplayTimecodeFromFrames(int frames, bool frameDisplay) const
122 if (frameDisplay) return QString::number(frames);
123 return getTimecodeHH_MM_SS_FF(frames);
126 const QString Timecode::getTimecodeFromFrames(int frames) const
128 return getTimecodeHH_MM_SS_FF(frames);
133 QString Timecode::getStringTimecode(int frames, const double &fps)
135 // Returns the timecode in an hh:mm:ss format
136 int seconds = (int)(frames / fps);
137 int minutes = seconds / 60;
138 seconds = seconds % 60;
139 int hours = minutes / 60;
140 minutes = minutes % 60;
142 text.append(QString::number(hours).rightJustified(2, '0', false));
144 text.append(QString::number(minutes).rightJustified(2, '0', false));
146 text.append(QString::number(seconds).rightJustified(2, '0', false));
152 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
154 // Returns the timecode in an easily read display, like 3 min. 5 sec.
155 int frames = (int) time.frames(fps);
156 int seconds = (int)(frames / fps);
157 frames = frames - ((int)(fps * seconds));
159 int minutes = seconds / 60;
160 seconds = seconds % 60;
161 int hours = minutes / 60;
162 minutes = minutes % 60;
168 text.append(QString::number(hours).rightJustified(2, '0', false));
169 text.append(' ' + i18n("hour") + ' ');
172 if (minutes != 0 || trim) {
174 text.append(QString::number(minutes));
176 text.append(QString::number(minutes).rightJustified(2, '0', false));
177 text.append(' ' + i18n("min.") + ' ');
180 if (seconds != 0 || trim) {
182 text.append(QString::number(seconds));
184 text.append(QString::number(seconds).rightJustified(2, '0', false));
185 text.append(' ' + i18n("sec."));
189 text.append(QString::number(frames));
190 text.append(' ' + i18n("frames"));
197 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
200 return getTimecodeDropFrame(time);
202 return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
205 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
208 return getTimecodeDropFrame(frames);
210 int seconds = frames / m_displayedFramesPerSecond;
211 frames = frames % m_displayedFramesPerSecond;
213 int minutes = seconds / 60;
214 seconds = seconds % 60;
215 int hours = minutes / 60;
216 minutes = minutes % 60;
219 text.append(QString::number(hours).rightJustified(2, '0', false));
221 text.append(QString::number(minutes).rightJustified(2, '0', false));
223 text.append(QString::number(seconds).rightJustified(2, '0', false));
225 text.append(QString::number(frames).rightJustified(2, '0', false));
230 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
232 int hundredths = (int)(time.seconds() * 100);
233 int seconds = hundredths / 100;
234 hundredths = hundredths % 100;
235 int minutes = seconds / 60;
236 seconds = seconds % 60;
237 int hours = minutes / 60;
238 minutes = minutes % 60;
242 text.append(QString::number(hours).rightJustified(2, '0', false));
244 text.append(QString::number(minutes).rightJustified(2, '0', false));
250 text.append(QString::number(seconds).rightJustified(2, '0', false));
252 text.append(QString::number(hundredths).rightJustified(2, '0', false));
257 QString Timecode::getTimecodeFrames(const GenTime & time) const
259 return QString::number(time.frames(m_realFps));
262 QString Timecode::getTimecodeSeconds(const GenTime & time) const
264 return QString::number(time.seconds());
267 QString Timecode::getTimecodeDropFrame(const GenTime & time) const
269 return getTimecodeDropFrame((int)time.frames(m_realFps));
272 QString Timecode::getTimecodeDropFrame(int frames) const
274 // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
275 // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
277 // calculate how many frames need to be dropped every minute.
280 dropRate = (int) ((ceil(m_displayedFramesPerSecond) / 30) * 2);
283 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
284 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - dropRate;
285 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60);
287 // Number of actual frames in a 10 minute interval :
288 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
290 int tenMinuteIntervals = frames / tenMinutes;
291 frames = frames % tenMinutes;
293 int hours = tenMinuteIntervals / 6;
294 tenMinuteIntervals = tenMinuteIntervals % 6;
296 // At the point, we have figured out HH:M?:??:??
300 if (frames < tenthMinuteFrames) {
301 // tenth minute logic applies.
304 // normal minute logic applies.
305 numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
306 frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
309 // We now have HH:MM:??:??
311 int seconds = frames / m_displayedFramesPerSecond;
312 frames = frames % m_displayedFramesPerSecond;
314 // We now have HH:MM:SS:FF
316 // THANK FUCK FOR THAT.
319 text.append(QString::number(hours).rightJustified(2, '0', false));
321 text.append(QString::number(tenMinuteIntervals));
322 text.append(QString::number(numMinutes));
328 text.append(QString::number(seconds).rightJustified(2, '0', false));
330 text.append(QString::number(frames).rightJustified(2, '0', false));