File: eca-audio-time.cpp

package info (click to toggle)
ecasound 2.9.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,292 kB
  • sloc: cpp: 39,475; sh: 4,335; lisp: 1,918; ansic: 1,883; makefile: 888; python: 617; ruby: 202
file content (254 lines) | stat: -rw-r--r-- 6,424 bytes parent folder | download | duplicates (6)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// ------------------------------------------------------------------------
// eca-audio-time.cpp: Generic class for representing time in audio 
//                     environment.
// Copyright (C) 2000,2007,2008 Kai Vehmanen
//
// Attributes:
//     eca-style-version: 3
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
// ------------------------------------------------------------------------

#include <cstdlib>
#include <cstdio>
#include <cmath>

#include <kvu_numtostr.h>
#include <kvu_dbc.h>

#include "eca-audio-time.h"

/**
 * FIXME notes  (last update 2008-03-09)
 *
 *  - Add variant that allows specifying both sample position and
 *    sampling rate to set_time_string(). E.g. "1234@44100sa" or
 *    something similar.
 */

using SAMPLE_SPECS::sample_pos_t;
using SAMPLE_SPECS::sample_rate_t;

ECA_AUDIO_TIME::ECA_AUDIO_TIME(sample_pos_t samples, 
			       sample_rate_t sample_rate)
{
  set_samples_per_second(sample_rate);
  set_samples(samples);
  rate_set_rep = true;
}

ECA_AUDIO_TIME::ECA_AUDIO_TIME(double time_in_seconds)
  : samples_rep(0),
    sample_rate_rep(ECA_AUDIO_TIME::invalid_srate),
    rate_set_rep(false)
{
  set_seconds(time_in_seconds);
  rate_set_rep = true;
}


ECA_AUDIO_TIME::ECA_AUDIO_TIME(format_type type, 
			       const std::string& time)
  : samples_rep(0),
    sample_rate_rep(ECA_AUDIO_TIME::invalid_srate),
    rate_set_rep(false)
{
  set(type, time);
}

ECA_AUDIO_TIME::ECA_AUDIO_TIME(const std::string& time) 
  : samples_rep(0),
    sample_rate_rep(ECA_AUDIO_TIME::invalid_srate),
    rate_set_rep(false)
{
  set_time_string(time);
}

ECA_AUDIO_TIME::ECA_AUDIO_TIME(void) 
  : samples_rep(0),
    sample_rate_rep(ECA_AUDIO_TIME::invalid_srate),
    rate_set_rep(false)
{
}

/**
 * Sets time based on 'type', 'time' and 'srate'.
 *
 * @param sample_rate a value of zero will be ignored.
 */
void ECA_AUDIO_TIME::set(format_type type, const std::string& time)
{
  switch(type) 
    {
      /* FIXME: not implemented! */
    case format_hour_min_sec: { DBC_CHECK(false); break; }
      /* FIXME: not implemented! */
    case format_min_sec: { DBC_CHECK(false); break; }
    case format_seconds:
      {
	double seconds = atof(time.c_str());
	set_seconds(seconds);
	break;
      }
    case format_samples:
      {
	samples_rep = atol(time.c_str());
	break;
      }
    default: { }
    }
}

/**
 * Sets time expressed in seconds. Additionally sample_rate is given
 * to express the timing accuracy.
 *
 * @param sample_rate a value of zero will be ignored.
 */
void ECA_AUDIO_TIME::set_seconds(double seconds)
{
  if (sample_rate_rep ==
      ECA_AUDIO_TIME::invalid_srate) {
    sample_rate_rep = ECA_AUDIO_TIME::default_srate;
    rate_set_rep = true;
  }

  samples_rep = static_cast<SAMPLE_SPECS::sample_pos_t>(seconds * sample_rate_rep);
}

/**
 * Sets time based on string 'time'. Additionally sample_rate is given
 * to express the timing accuracy.
 *
 * The time string is by default interpreted as seconds (need not 
 * be an integer but can be given as a decimal number, e.g. "1.05"). 
 * However, if the string contains an integer number and has a postfix 
 * of "sa" (e.g. "44100sa"), it is interpreted as time expressed as 
 * samples (in case of a multichannel stream, time in sample frames).
 *
 * @param sample_rate a value of zero will be ignored.
 */
void ECA_AUDIO_TIME::set_time_string(const std::string& time)
{
  if (time.size() > 2 &&
      time.find("sa") != std::string::npos)
    ECA_AUDIO_TIME::set(format_samples, std::string(time, 0, time.size() - 2));
  else
    ECA_AUDIO_TIME::set(format_seconds, time);
}

/**
 * Sets the sample count.
 *
 * Note, this can change the value of seconds().
 */
void ECA_AUDIO_TIME::set_samples(SAMPLE_SPECS::sample_pos_t samples)
{
  samples_rep = samples;
}

/**
 * Sets samples per second. Additionally sample_rate is given
 * to express the timing accuracy.
 *
 * Note, this can change the value of seconds().
 */
void ECA_AUDIO_TIME::set_samples_per_second(SAMPLE_SPECS::sample_rate_t srate)
{
  if (srate > 0) {
    sample_rate_rep = srate;
    rate_set_rep = true;
  }
  else {
    rate_set_rep = false;
    sample_rate_rep = ECA_AUDIO_TIME::invalid_srate;
  }
}

/**
 * Sets samples per second.
 *
 * Note, if sampling rate has been set earlier, this function 
 * does NOT change the value of seconds() like 
 * set_samples_per_second() potentially does.
 */
void ECA_AUDIO_TIME::set_samples_per_second_keeptime(sample_rate_t srate)
{
  if (srate > 0 &&
      sample_rate_rep != srate) {
    if (rate_set_rep == true) {
      /* only needed if sampling rate has been set */
      double time_secs = seconds();
      set_samples_per_second(srate);
      set_seconds(time_secs);
    }
    else {
      set_samples_per_second(srate);
    }
  }
}

void ECA_AUDIO_TIME::mark_as_invalid(void)
{
  set_samples_per_second(ECA_AUDIO_TIME::invalid_srate);
}

std::string ECA_AUDIO_TIME::to_string(format_type type) const
{
  /* FIXME: not implemented */

  switch(type) 
    {
    case format_hour_min_sec: 
      { 
	return "";
      }
    case format_min_sec: 
      {
	return "";
      }
    case format_seconds: { return kvu_numtostr(seconds(), 6); }
    case format_samples: { return kvu_numtostr(samples_rep); }

    default: { }
    }

  return "";
}

double ECA_AUDIO_TIME::seconds(void) const
{
  if (rate_set_rep != true) {
    sample_rate_rep = ECA_AUDIO_TIME::default_srate;
    rate_set_rep = true;
  }

  return static_cast<double>(samples_rep) / sample_rate_rep;
}

SAMPLE_SPECS::sample_rate_t ECA_AUDIO_TIME::samples_per_second(void) const
{
  return sample_rate_rep;
}

SAMPLE_SPECS::sample_pos_t ECA_AUDIO_TIME::samples(void) const
{
  return samples_rep;
}

bool ECA_AUDIO_TIME::valid(void) const
{
  return rate_set_rep;
}