[DEV] add audio time interface

This commit is contained in:
Edouard DUPIN 2015-04-13 21:49:48 +02:00
parent 61d33fa59f
commit 96dc95b55a
5 changed files with 390 additions and 0 deletions

155
audio/Duration.cpp Normal file
View File

@ -0,0 +1,155 @@
/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#include <audio/Time.h>
#include <audio/Duration.h>
#include <audio/debug.h>
audio::Duration::Duration(int _val) {
m_data = std11::chrono::nanoseconds(_val);
}
audio::Duration::Duration(int64_t _valSec, int64_t _valNano) {
m_data = std11::chrono::duration_cast<std11::chrono::nanoseconds>(std11::chrono::seconds(_valSec));
m_data += std11::chrono::nanoseconds(_valNano);
}
audio::Duration::Duration(int64_t _val) {
m_data = std11::chrono::nanoseconds(_val);
}
audio::Duration::Duration(double _val) {
m_data = std11::chrono::duration_cast<std11::chrono::nanoseconds>(std11::chrono::seconds(int64_t(_val)));
m_data += std11::chrono::nanoseconds(int64_t(((_val - int64_t(_val)) * 1000000000.0)));
}
audio::Duration::Duration(std11::chrono::nanoseconds _val) {
m_data = std11::chrono::duration_cast<std11::chrono::nanoseconds>(_val);
}
audio::Duration::Duration(std11::chrono::microseconds _val) {
m_data = std11::chrono::duration_cast<std11::chrono::nanoseconds>(_val);
}
audio::Duration::Duration(std11::chrono::milliseconds _val) {
m_data = std11::chrono::duration_cast<std11::chrono::nanoseconds>(_val);
}
audio::Duration::Duration(std11::chrono::seconds _val) {
m_data = std11::chrono::duration_cast<std11::chrono::nanoseconds>(_val);
}
int64_t audio::Duration::count() const {
return m_data.count();
}
const audio::Duration& audio::Duration::operator= (const audio::Duration& _obj) {
m_data = _obj.m_data;
return *this;
}
bool audio::Duration::operator== (const audio::Duration& _obj) const {
return _obj.m_data == m_data;
}
bool audio::Duration::operator!= (const audio::Duration& _obj) const {
return _obj.m_data != m_data;
}
bool audio::Duration::operator< (const audio::Duration& _obj) const {
return m_data < _obj.m_data;
}
bool audio::Duration::operator<= (const audio::Duration& _obj) const {
return m_data <= _obj.m_data;
}
bool audio::Duration::operator> (const audio::Duration& _obj) const {
return m_data > _obj.m_data;
}
bool audio::Duration::operator>= (const audio::Duration& _obj) const {
return m_data >= _obj.m_data;
}
const audio::Duration& audio::Duration::operator+= (const audio::Duration& _obj) {
m_data += _obj.m_data;
return *this;
}
audio::Duration audio::Duration::operator+ (const audio::Duration& _obj) const {
audio::Duration tmpp(m_data);
tmpp.m_data += _obj.m_data;
return tmpp;
}
const audio::Duration& audio::Duration::operator-= (const audio::Duration& _obj) {
m_data -= _obj.m_data;
return *this;
}
audio::Duration audio::Duration::operator- (const audio::Duration& _obj) const {
audio::Duration tmpp(m_data);
tmpp.m_data -= _obj.m_data;
return tmpp;
}
audio::Duration& audio::Duration::operator++() {
m_data += std11::chrono::nanoseconds(1);
return *this;
}
audio::Duration audio::Duration::operator++(int _unused) {
audio::Duration result(m_data);
m_data += std11::chrono::nanoseconds(1);
return result;
}
audio::Duration& audio::Duration::operator--() {
m_data -= std11::chrono::nanoseconds(1);
return *this;
}
audio::Duration audio::Duration::operator--(int _unused) {
m_data -= std11::chrono::nanoseconds(1);
audio::Duration result(m_data);
return result;
}
std::ostream& audio::operator <<(std::ostream& _os, const audio::Duration& _obj) {
int64_t totalSecond = _obj.count()/1000000000;
int64_t millisecond = (_obj.count()%1000000000)/1000000;
int64_t microsecond = (_obj.count()%1000000)/1000;
int64_t nanosecond = _obj.count()%1000;
//_os << totalSecond << "s " << millisecond << "ms " << microsecond << "µs " << nanosecond << "ns";
int32_t second = totalSecond % 60;
int32_t minute = (totalSecond/60)%60;
int32_t hour = (totalSecond/3600)%24;
int32_t day = (totalSecond/(24*3600))%365;
int32_t year = totalSecond/(24*3600*365);
bool start = false;
if (year != 0) {
start = true;
_os << year << "y ";
}
if (day != 0 || start == true) {
start = true;
_os << day << "d ";
}
if (hour != 0 || start == true) {
start = true;
_os << hour << "h ";
}
if (minute != 0 || start == true) {
start = true;
_os << minute << ":";
}
if (second != 0 || start == true) {
start = true;
_os << second << "s ";
}
if (millisecond != 0 || start == true) {
start = true;
_os << millisecond << "ms ";
}
if (microsecond != 0 || start == true) {
start = true;
_os << microsecond << "us ";
}
_os << nanosecond << "ns ";
return _os;
}

51
audio/Duration.h Normal file
View File

@ -0,0 +1,51 @@
/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#ifndef __AUDIO_DURATION_H__
#define __AUDIO_DURATION_H__
#include <etk/types.h>
#include <etk/chrono.h>
namespace audio {
class Duration {
private:
std11::chrono::nanoseconds m_data;
public:
Duration() { };
Duration(int _val); //value in nanosecond
Duration(int64_t _val); //value in nanosecond
Duration(int64_t _valSec, int64_t _valNano); //value in second and nanosecond
Duration(double _val); //value in second
Duration(std11::chrono::nanoseconds _val);
Duration(std11::chrono::microseconds _val);
Duration(std11::chrono::milliseconds _val);
Duration(std11::chrono::seconds _val);
~Duration() { };
int64_t count() const;
const std11::chrono::nanoseconds& get() const {
return m_data;
}
const Duration& operator= (const Duration& _obj);
bool operator== (const Duration& _obj) const;
bool operator!= (const Duration& _obj) const;
bool operator< (const audio::Duration& _obj) const;
bool operator<= (const audio::Duration& _obj) const;
bool operator> (const audio::Duration& _obj) const;
bool operator>= (const audio::Duration& _obj) const;
const Duration& operator+= (const Duration& _obj);
Duration operator+ (const Duration& _obj) const;
const Duration& operator-= (const Duration& _obj);
Duration operator- (const Duration& _obj) const;
Duration& operator++();
Duration operator++(int _unused);
Duration& operator--();
Duration operator--(int _unused);
};
std::ostream& operator <<(std::ostream& _os, const audio::Duration& _obj);
}
#endif

136
audio/Time.cpp Normal file
View File

@ -0,0 +1,136 @@
/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#include <audio/Time.h>
#include <audio/Duration.h>
#include <audio/debug.h>
audio::Time::Time(int64_t _valSec, int64_t _valNano) {
m_data = std11::chrono::steady_clock::time_point(std11::chrono::seconds(_valSec));
m_data += std11::chrono::nanoseconds(_valNano);
}
audio::Time::Time(const std11::chrono::steady_clock::time_point& _val) {
m_data = _val;
}
audio::Time audio::Time::now() {
return audio::Time(std11::chrono::steady_clock::now());
}
const audio::Time& audio::Time::operator= (const audio::Time& _obj) {
m_data = _obj.m_data;
return *this;
}
bool audio::Time::operator== (const audio::Time& _obj) const {
return m_data == _obj.m_data;
}
bool audio::Time::operator!= (const audio::Time& _obj) const {
return m_data != _obj.m_data;
}
bool audio::Time::operator< (const audio::Time& _obj) const {
return m_data < _obj.m_data;
}
bool audio::Time::operator<= (const audio::Time& _obj) const {
return m_data <= _obj.m_data;
}
bool audio::Time::operator> (const audio::Time& _obj) const {
return m_data > _obj.m_data;
}
bool audio::Time::operator>= (const audio::Time& _obj) const {
return m_data >= _obj.m_data;
}
const audio::Time& audio::Time::operator+= (const audio::Duration& _obj) {
#if defined(__TARGET_OS__MacOs) || defined(__TARGET_OS__IOs)
std11::chrono::microseconds ms = std11::chrono::duration_cast<std11::chrono::microseconds>(_obj.get());
m_data += ms;
#else
m_data += _obj.get();
#endif
return *this;
}
audio::Time audio::Time::operator+ (const audio::Duration& _obj) const {
audio::Time time(m_data);
time += _obj;
return time;
}
const audio::Time& audio::Time::operator-= (const audio::Duration& _obj) {
#if defined(__TARGET_OS__MacOs) || defined(__TARGET_OS__IOs)
std11::chrono::microseconds ms = std11::chrono::duration_cast<std11::chrono::microseconds>(_obj.get());
m_data -= ms;
#else
m_data -= _obj.get();
#endif
return *this;
}
audio::Time audio::Time::operator- (const audio::Duration& _obj) const {
audio::Time time(m_data);
time -= _obj;
return time;
}
audio::Duration audio::Time::operator- (const audio::Time& _obj) const {
std11::chrono::nanoseconds ns = std11::chrono::duration_cast<std11::chrono::nanoseconds>(m_data.time_since_epoch());
std11::chrono::nanoseconds ns2 = std11::chrono::duration_cast<std11::chrono::nanoseconds>(_obj.m_data.time_since_epoch());
audio::Duration duration(ns);
audio::Duration duration2(ns2);
return duration - duration2;
}
std::ostream& audio::operator <<(std::ostream& _os, const audio::Time& _obj) {
std11::chrono::nanoseconds ns = std11::chrono::duration_cast<std11::chrono::nanoseconds>(_obj.get().time_since_epoch());
int64_t totalSecond = ns.count()/1000000000;
int64_t millisecond = (ns.count()%1000000000)/1000000;
int64_t microsecond = (ns.count()%1000000)/1000;
int64_t nanosecond = ns.count()%1000;
//_os << totalSecond << "s " << millisecond << "ms " << microsecond << "µs " << nanosecond << "ns";
int32_t second = totalSecond % 60;
int32_t minute = (totalSecond/60)%60;
int32_t hour = (totalSecond/3600)%24;
int32_t day = (totalSecond/(24*3600))%365;
int32_t year = totalSecond/(24*3600*365);
bool start = false;
if (year != 0) {
start = true;
_os << year << "y ";
}
if (day != 0 || start == true) {
start = true;
_os << day << "d ";
}
if (hour != 0 || start == true) {
start = true;
_os << hour << "h ";
}
if (minute != 0 || start == true) {
start = true;
_os << minute << ":";
}
if (second != 0 || start == true) {
start = true;
_os << second << "s ";
}
if (millisecond != 0 || start == true) {
start = true;
_os << millisecond << "ms ";
}
if (microsecond != 0 || start == true) {
start = true;
_os << microsecond << "us ";
}
_os << nanosecond << "ns ";
return _os;
}

46
audio/Time.h Normal file
View File

@ -0,0 +1,46 @@
/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#ifndef __AUDIO_TIME_H__
#define __AUDIO_TIME_H__
#include <string>
#include <etk/types.h>
#include <etk/chrono.h>
namespace audio {
class Duration;
class Time {
private:
std11::chrono::steady_clock::time_point m_data;
public:
Time() {};
//Time(const audio::Duration& _val) {}; //value in second
Time(int64_t _valSec, int64_t _valNano);
Time(const std11::chrono::steady_clock::time_point& _val);
~Time() {};
const std11::chrono::steady_clock::time_point& get() const {
return m_data;
}
static Time now();
const Time& operator= (const audio::Time& _obj );
bool operator== (const audio::Time& _obj) const;
bool operator!= (const audio::Time& _obj) const;
bool operator< (const audio::Time& _obj) const;
bool operator<= (const audio::Time& _obj) const;
bool operator> (const audio::Time& _obj) const;
bool operator>= (const audio::Time& _obj) const;
const Time& operator+= (const audio::Duration& _obj);
Time operator+ (const audio::Duration& _obj) const;
const Time& operator-= (const audio::Duration& _obj);
Time operator- (const audio::Duration& _obj) const;
Duration operator- (const audio::Time& _obj) const;
};
std::ostream& operator <<(std::ostream& _os, const audio::Time& _obj);
}
#endif

View File

@ -13,6 +13,8 @@ def create(target):
'audio/debug.cpp',
'audio/channel.cpp',
'audio/format.cpp',
'audio/Time.cpp',
'audio/Duration.cpp',
'audio/int8_8_t.cpp',
'audio/int8_16_t.cpp',
'audio/int16_16_t.cpp',