Skip to content

Commit 5a03da1

Browse files
committed
Adding .as*() convenience methods to MediaTime
1 parent d2afbfa commit 5a03da1

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

include/cinder/MediaTime.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ struct MediaTime {
5050
//! Scale value to 'newBase'. value is set to floor if necessary.
5151
void setBase( int32_t newBase );
5252

53+
void setEpoch( int64_t newEpoch ) { epoch = newEpoch; }
54+
5355
//! Returns value as scaled to \a otherBase
5456
int64_t getValueAsBase( int32_t otherBase ) const { return ( base == otherBase ) ? value : ( value * otherBase / base ); }
5557

58+
MediaTime asBase( int32_t otherBase ) const { auto temp = *this; temp.setBase( otherBase ); return temp; }
59+
MediaTime asEpoch( int64_t otherEpoch ) const { return MediaTime( value, base, otherEpoch ); }
60+
MediaTime as( int32_t otherBase, int64_t otherEpoch ) const { auto temp = *this; temp.setBase( otherBase ); temp.setEpoch( otherEpoch ); return temp; }
61+
5662
bool operator<( const MediaTime &rhs ) const {
5763
if( epoch != rhs.epoch ) return epoch < rhs.epoch;
5864
if( base == rhs.base ) return value < rhs.value;
@@ -170,7 +176,7 @@ struct MediaTime {
170176
//! Divides the value and base by the greatest common common divisor of both. Does not affect epoch
171177
void simplify();
172178

173-
//! returns a new common base, and re-bases 'lhsValue' and 'rhsValue' to this new base. If no legal common base, then uses DEFAULT_TIME_BASE
179+
//! returns a new common base, and re-bases \a lhsValue and \a rhsValue to this new base. If no legal common base, then uses DEFAULT_TIME_BASE
174180
static int32_t simplifyBases( int64_t *lhsValue, int32_t lhsBase, int64_t *rhsValue, int32_t rhsBase );
175181

176182
int64_t value;

test/unit/src/MediaTime.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ TEST_CASE( "MediaTime" )
141141
// suffix
142142
{
143143
REQUIRE( MediaTime( 2 ) == 2_sec );
144+
REQUIRE( MediaTime( 0 ) == 0_sec );
144145
REQUIRE( (2.5_sec).getSeconds() == Approx(2.5) );
145146
}
146147

@@ -150,5 +151,20 @@ TEST_CASE( "MediaTime" )
150151
auto twoOverTwoSimplified = twoOverTwo; twoOverTwoSimplified.simplify();
151152
REQUIRE( twoOverTwoSimplified == MediaTime( 1 ) );
152153
}
154+
155+
// "as" convenience
156+
{
157+
auto oneOverTwo = MediaTime( 1, 2 );
158+
REQUIRE( oneOverTwo.asBase( 4 ) == oneOverTwo );
159+
REQUIRE( oneOverTwo.asBase( 4 ).base == 4 );
160+
REQUIRE( oneOverTwo.asEpoch( 1 ) != oneOverTwo );
161+
REQUIRE( oneOverTwo.asEpoch( 1 ) == MediaTime( 1, 2, 1 ) );
162+
REQUIRE( oneOverTwo.asEpoch( 1 ).epoch == 1 );
163+
REQUIRE( oneOverTwo.asEpoch( 1 ) != oneOverTwo );
164+
REQUIRE( oneOverTwo.asEpoch( 1 ) == MediaTime( 1, 2, 1 ) );
165+
REQUIRE( oneOverTwo.as( 4, 1 ) != oneOverTwo );
166+
REQUIRE( oneOverTwo.as( 4, 1 ).base == 4 );
167+
REQUIRE( oneOverTwo.as( 4, 1 ).epoch == 1 );
168+
}
153169
}
154170
}

0 commit comments

Comments
 (0)