summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/output/audioTime.cpp
blob: f4ed2e9c96d8def2e3d7ab70ad068659f7fe2853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
  defines the format of an audio stream
  Copyright (C) 1999  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */


#include "audioTime.h"

#include <iostream>

using namespace std;


AudioTime::AudioTime(){
  time=0.0;
}


AudioTime::~AudioTime() {
}


void AudioTime::setFormat(int stereo,int samplesize,int speed,int sign, int bigendian) {
  setStereo(stereo);
  setSampleSize(samplesize);
  setSpeed(speed);
  setSign(sign);
  setBigendian(bigendian);
}


int AudioTime::getStereo() {
  return stereo;
}


int AudioTime::getSampleSize() {
  return samplesize;
}


int AudioTime::getSpeed() {
  return speed;
}

int AudioTime::getSign() {
  return _sign;
}

int AudioTime::getBigendian() {
  return _bigendian;
}

void AudioTime::setStereo(int stereo) {
  this->stereo=stereo;
}


void AudioTime::setSampleSize(int samplesize) {
  this->samplesize=samplesize;
}


void AudioTime::setSpeed(int speed) {
  this->speed=speed;
}

void AudioTime::setSign(int sign) {
  this->_sign=sign;
}

void AudioTime::setBigendian(int bigendian) {
  this->_bigendian = bigendian;
}

float AudioTime::getTime() {
  return time;
}


void AudioTime::setTime(float time) {
  this->time=time;
}


void AudioTime::forwardTime(int bytes){
  time+=calculateTime(bytes);
}


float AudioTime::calculateTime(int bytes) {
  float back=0;

  bytes=bytes/(samplesize/8);
  if (stereo==1) {
    bytes=bytes/2;
  }
  if (speed != 0) {
    back=(float)bytes/(float)speed;
  }
  return back;
}


/**
   How much byte we need for time usecs?
*/
int AudioTime::calculateBytes(float time) {
  float back=time;

  if (speed != 0) {
    back=back*(float)speed;
  }
  back=back*(float)(samplesize/8);
  if (stereo==1) {
    back=back*2.0;
  }
  return (int)back;
}



void AudioTime::sleepWrite(int size) {
  timeval_t time;
  float timeLength=calculateTime(size);
  time.tv_sec=(long)timeLength;
  time.tv_usec=(long)(1000000*(timeLength-time.tv_sec));
  TimeWrapper::usleep(&time);
}


void AudioTime::print() {
  cout << "AudioTime-begin-"<<endl;
  cout << "stereo:"<<getStereo()<<" sampleSize:"<<getSampleSize()
       << " speed: "<<getSpeed()<<endl;

  cout << "AudioTime-end-"<<endl;
 
}


void AudioTime::copyTo(AudioTime* dest) {
  dest->setStereo(getStereo());
  dest->setSampleSize(getSampleSize());
  dest->setSpeed(getSpeed());
}