Spaces:
Sleeping
Sleeping
File size: 15,246 Bytes
9f5b176 |
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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 |
/*
Copyright 2015 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "core/track.h"
#include <math.h>
#include <memory>
#include <stdlib.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#if !defined(INVALID_LOG)
#define INVALID_LOG -1.0E+10F
#endif // INVALID_LOG
namespace {
int ToInt(const std::string &s) {
char *p = 0;
int32_t r = strtol(s.c_str(), &p, 10);
return static_cast<int>(r);
}
std::string ToString(float n, int p) {
char buf[1000];
buf[0] = 0;
snprintf(buf, sizeof(buf), "%.*f", p, n);
return buf;
}
std::string ToString(uint32_t n) {
char buf[1000];
buf[0] = 0;
snprintf(buf, sizeof(buf), "%" PRIu32, n);
return buf;
}
double ToDouble(const std::string &s) {
char *p = 0;
return strtod(s.c_str(), &p);
}
float ToFloat(const std::string &s) {
return static_cast<float>(ToDouble(s));
}
//
// Default size of character I/O buffers:
//
const uint32_t kMaxCharBufSize = 8192; // 8k
std::string GetToken(FileResource *fr) {
char buff[kMaxCharBufSize];
uint32_t i = 0;
bool foundData = false;
do {
buff[i] = fgetc(fr->fp());
if ((iscntrl((unsigned char)buff[i]) || isspace((unsigned char)buff[i])) &&
!foundData) {
continue;
}
foundData = true;
if (iscntrl((unsigned char)buff[i]) || isspace((unsigned char)buff[i])) {
break;
}
i++;
} while (i < kMaxCharBufSize && !fr->eof());
buff[i] = 0;
return std::string(buff);
}
// Find the first whitespace/newline delimited token in the character
// array c. Return the stripped token in s. return the position of
// the first character following the token in tok_end. If the end of
// c is found during the search, return true, else return false. If no
// token is found, return 0 in tok_end.
bool GetTokenFromChars(const char *c, std::string *s, int32_t *tok_end) {
int32_t i = 0;
s->clear();
*tok_end = 0;
// skip initial whitespace
while (c[i] && ((c[i] == ' ') || (c[i] == '\t') || (c[i] == '\n'))) {
i++;
}
if (!c[i]) {
return true;
}
int32_t start = i;
while (c[i] && (c[i] != ' ') && (c[i] != '\t') && (c[i] != '\n')) {
i++;
}
*tok_end = i;
if (c[i-1] == '\r') {
--i;
}
s->assign(c + start, i - start);
return c[i] == 0;
}
} // namespace
Track::Track() {
num_frames_ = 0;
num_channels_ = 0;
shift_ = 0.0F;
voicing_enabled_ = false;
}
void Track::FillFrom(const Track &t) {
num_frames_ = t.num_frames_;
num_channels_ = t.num_channels_;
shift_ = t.shift_;
data_ = t.data_;
val_ = t.val_;
voicing_enabled_ = t.voicing_enabled_;
}
Track::~Track() {
Clear();
}
bool Track::Save(FileResource *fr) const {
fprintf(fr->fp(), "EST_File Track\n");
fprintf(fr->fp(), "DataType binary2\n");
fprintf(fr->fp(), "NumFrames %d\n", num_frames());
fprintf(fr->fp(), "NumChannels %d\n", num_channels());
fprintf(fr->fp(), "FrameShift %f\n", shift());
if (voicing_enabled_) {
fprintf(fr->fp(), "VoicingEnabled true\n");
} else {
fprintf(fr->fp(), "VoicingEnabled false\n");
}
fprintf(fr->fp(), "EST_Header_End\n");
// write time information
for (int i = 0; i < num_frames(); ++i) {
float f = t(i);
if (fwrite(&f, sizeof(f), 1, fr->fp()) != 1) {
fprintf(stderr, "Failed to write to track file");
return false;
}
}
// write voicing information
if (voicing_enabled_) {
for (int i = 0; i < num_frames(); ++i) {
char vu = v(i) ? 1 : 0;
if (fwrite(&vu, sizeof(vu), 1, fr->fp()) != 1) {
fprintf(stderr, "Failed to write to track file");
return false;
}
}
}
// write coefficient information
for (int i = 0; i < num_frames(); ++i) {
for (int j = 0; j < num_channels(); ++j) {
float f = a(i, j);
if (fwrite(&f, sizeof(f), 1, fr->fp()) != 1) {
fprintf(stderr, "Failed to write to track file");
return false;
}
}
}
return true;
}
int Track::Index(float x) const {
if (num_frames() > 1) { // if single frame, return that index (0)
int bst, bmid, bend;
bst = 1;
bend = num_frames();
if (x < t(bst)) {
bmid = bst;
}
if (x >= t(bend - 1)) {
bmid = bend - 1;
} else {
while (1) {
bmid = bst + (bend - bst) / 2;
if (bst == bmid) {
break;
} else if (x < t(bmid)) {
if (x >= t(bmid - 1)) {
break;
}
bend = bmid;
} else {
bst = bmid;
}
}
}
if (fabs(x - t(bmid)) < fabs(x - t(bmid - 1))) {
return bmid;
} else {
return bmid - 1;
}
}
return num_frames() - 1;
}
int Track::IndexBelow(float x) const {
for (int i = 1; i < num_frames(); ++i) {
if (x <= t(i)) {
return i - 1;
}
}
return num_frames() - 1;
}
int Track::IndexAbove(float x) const {
for (int i = 0; i < num_frames(); ++i) {
if (x <= t(i)) {
return i;
}
}
return num_frames() - 1;
}
std::string Track::HeaderToString() const {
std::string ret;
ret = "EST_File Track\n";
ret += "DataType ascii\n";
ret += "NumFrames " + ::ToString(num_frames()) + "\n";
ret += "NumChannels " + ::ToString(num_channels()) + "\n";
ret += "FrameShift " + ::ToString(shift(), 5) + "\n";
ret += "VoicingEnabled ";
if (voicing_enabled_) {
ret += "true\n";
} else {
ret += "false\n";
}
ret += "EST_Header_End\n";
return ret;
}
std::string Track::ToString(uint32_t precision) const {
std::string ret;
ret = HeaderToString();
char buf[1024];
for (int i = 0; i < num_frames(); ++i) {
snprintf(buf, 1024, "%f %d", t(i), v(i) ? 1 : 0);
ret += buf;
for (int j = 0; j < num_channels(); ++j) {
ret += " ";
ret += ::ToString(a(i, j), precision);
}
ret += std::string("\n");
}
return ret;
}
std::string Track::ToString() const {
const uint32_t kPrintOutPrecision = 6;
return ToString(kPrintOutPrecision);
}
float Track::shift() const {
return shift_;
}
void Track::FillTime(float frame_shift, int start) {
shift_ = frame_shift;
for (int i = 0; i < num_frames_; ++i) {
data_.Set(0, i, frame_shift * (i + start));
}
}
void Track::FillTime(const Track &t) {
for (int i = 0; i < num_frames_; ++i) {
data_.Set(0, i, t.t(i));
}
}
void Track::SetTimes(float *times, int length) {
if (length != num_frames_) {
fprintf(stderr, "Track::SetTimes: input `times` has different number "
"of frames (%d != %d)", length, num_frames_);
}
for (int i = 0; i < num_frames_; ++i) {
data_.Set(0, i, times[i]);
}
}
bool Track::SetVoicing(const std::vector<bool> &vuv) {
if (vuv.size() != num_frames_) {
fprintf(stderr, "Track::SetVoicing: input has different number "
"of frames (%zu != %d)", vuv.size(), num_frames_);
return false;
}
val_ = vuv;
return true;
}
void Track::FrameOut(std::vector<float> *fv, int n) const {
fv->resize(num_channels_);
for (int i = 0; i < num_channels_; i++) {
(*fv)[i] = a(n, i);
}
}
void Track::FrameOut(std::vector<double> *fv, int n) const {
fv->resize(num_channels_);
for (int i = 0; i < num_channels_; i++) {
(*fv)[i] = a(n, i);
}
}
void Track::FrameIn(const float &f, int n) {
a(n) = f;
}
void Track::FrameIn(const std::vector<float> &fv, int n) {
for (int i = 0; i < num_channels_; i++) {
a(n, i) = fv[i];
}
}
void Track::FrameIn(const std::vector<double> &fv, int n) {
for (int i = 0; i < num_channels_; i++) {
a(n, i) = fv[i];
}
}
Track *Track::GetSubTrack(float start,
float end,
int ch_offset,
int ch_size) const {
int start_frame = -1;
int end_frame = -1;
int n_sub_frames;
int i, j;
for (i = 0; i < this->num_frames(); ++i) {
if ((start_frame == -1) && (this->t(i) > start)) {
start_frame = i - 1;
}
if ((end_frame == -1) && (this->t(i) > end)) {
end_frame = i;
}
}
if (start_frame == -1) {
start_frame = this->num_frames() - 1;
}
if (end_frame == -1) {
end_frame = this->num_frames() - 1;
}
n_sub_frames = end_frame - start_frame;
if (n_sub_frames < 1) {
// make sure we have at least one frame
n_sub_frames = 1;
}
if (ch_size == -1 && ch_offset == -1) {
ch_offset = 0;
ch_size = this->num_channels();
} else {
if (ch_size > this->num_channels()) {
fprintf(stderr, "Incorrect number of channels for sub track");
return NULL;
}
}
Track *sub_track = new Track;
sub_track->resize(n_sub_frames, ch_size);
for (i = 0; i < n_sub_frames; ++i) {
sub_track->t(i) = this->t(i + start_frame);
for (j = ch_offset; j < ch_size; ++j) {
sub_track->a(i, j) = this->a(i + start_frame, j);
}
}
return sub_track;
}
Track *Track::GetSubTrack(int start_frame_index,
int end_frame_index,
int start_channel_index,
int end_channel_index) const {
if (start_frame_index > end_frame_index ||
start_frame_index < 0 || start_frame_index >= this->num_frames() ||
end_frame_index < 0 || end_frame_index >= this->num_frames()) {
fprintf(stderr, "Incorrect frame indices for sub-track 0<%d<=%d<%d\n",
start_frame_index,
end_frame_index,
this->num_frames());
return NULL;
}
if (start_channel_index > end_channel_index ||
start_channel_index < 0 || start_channel_index >= this->num_channels() ||
end_channel_index < 0 || end_channel_index >= this->num_channels()) {
fprintf(stderr, "Incorrect channel indices for sub-track 0<%d<=%d<%d\n",
start_channel_index,
end_channel_index,
this->num_channels());
return NULL;
}
Track *sub_track = new Track;
int number_frames = end_frame_index - start_frame_index + 1;
int number_channels = end_channel_index - start_channel_index + 1;
sub_track->resize(number_frames, number_channels);
for (int f = 0; f < number_frames; f++) {
for (int p = 0; p < number_channels; p++) {
sub_track->a(f, p) = this->a(start_frame_index + f,
start_channel_index + p);
}
sub_track->t(f) = this->t(start_frame_index + f);
sub_track->set_v(f, this->v(start_frame_index + f));
}
sub_track->set_shift(this->shift());
return sub_track;
}
void Track::resize(int n, int c) {
if (n != num_frames_) {
val_.resize(n, 1);
}
data_.resize(c + 1, n);
num_frames_ = n;
num_channels_ = c;
}
void Track::Clear() {
num_frames_ = 0;
num_channels_ = 0;
shift_ = 0.0F;
data_.clear();
val_.clear();
}
void Track::CopyFrom(const char *v, int32_t num_frames, int32_t num_channels) {
resize(num_frames, num_channels);
const float* source = reinterpret_cast<const float*>(v);
for (int32_t i = 0; i < num_frames; ++i) {
for (int32_t j = 0; j < num_channels; ++j) {
a(i, j) = *source++;
}
}
}
// Sets current track to be combination of two tracks
bool Track::SetCombinedTrack(const Track &track_a, const Track &track_b) {
if (track_a.num_frames() != track_b.num_frames()) {
fprintf(stderr, "Mismatching number of frames: %d vs. %d",
track_a.num_frames(),
track_b.num_frames());
return false;
}
const int n_frames = track_a.num_frames();
const int n_channels_a = track_a.num_channels();
const int n_channels_b = track_b.num_channels();
resize(n_frames, n_channels_a + n_channels_b);
for (int i = 0; i < n_frames; i++) {
for (int j = 0; j < n_channels_a; j++) {
a(i, j) = track_a.a(i, j);
}
for (int j = 0; j < n_channels_b; j++) {
a(i, n_channels_a + j) = track_b.a(i, j);
}
set_v(i, track_a.v(i) && track_b.v(i));
}
return true;
}
bool Track::SetCombinedTrack(const Track &track_a,
const Track &track_b,
const Track &track_c) {
Track track_ab;
if (!track_ab.SetCombinedTrack(track_a, track_b)) {
return false;
}
return SetCombinedTrack(track_ab, track_c);
}
// Pads a track with <num_pads> frames:
bool Track::Pad(int num_pads) {
if (num_pads <= 0) {
return false;
}
const int num_original_frames = num_frames();
const int num_original_channels = num_channels();
Track new_track;
new_track.resize(num_original_frames + num_pads, num_original_channels);
for (int i = 0; i < num_original_frames; ++i) {
for (int j = 0; j < num_original_channels; ++j) {
new_track.a(i, j) = a(i, j);
}
new_track.set_v(i, v(i));
}
for (int i = 0; i < num_pads; ++i) {
for (int j = 0; j < num_original_channels; ++j) {
new_track.a(num_original_frames + i, j) = a(num_original_frames - 1, j);
}
new_track.set_v(i, v(num_original_frames - 1));
}
const int num_new_frames = new_track.num_frames();
Clear();
resize(num_new_frames, num_original_channels);
for (int i = 0; i < num_new_frames; ++i) {
for (int j = 0; j < num_original_channels; ++j) {
a(i, j) = new_track.a(i, j);
}
set_v(i, new_track.v(i));
}
return true;
}
// Forces the two tracks to have the same size, padding if necessary.
void Track::MakeSameSize(Track *track) {
int max_frames = num_frames();
if (track->num_frames() > max_frames) {
max_frames = track->num_frames();
}
Pad(max_frames - num_frames());
track->Pad(max_frames - track->num_frames());
}
// Forces the three tracks to have same size, padding if necessary.
void Track::MakeSameSize(Track *track_a, Track *track_b) {
int max_frames = num_frames();
if (track_a->num_frames() > max_frames) {
max_frames = track_a->num_frames();
}
if (track_b->num_frames() > max_frames) {
max_frames = track_b->num_frames();
}
Pad(max_frames - num_frames());
track_a->Pad(max_frames - track_a->num_frames());
track_b->Pad(max_frames - track_b->num_frames());
}
//------------------------------------------------------------------------------
// Utils.
//------------------------------------------------------------------------------
void ConvertToLogarithmic(Track *t) {
for (int32_t i = 0; i < t->num_frames(); ++i) {
for (int32_t j = 0; j < t->num_channels(); ++j) {
if (t->a(i, j) <= 0.0F) {
t->a(i, j) = INVALID_LOG;
} else {
t->a(i, j) = log(t->a(i, j));
}
}
}
}
float *TrackToFloatPointer(const Track &track, int *num_samples) {
*num_samples = track.num_channels() * track.num_frames();
float *array = new float[*num_samples];
uint32_t k = 0;
for (int i = 0; i < track.num_frames(); ++i) {
for (int j = 0; j < track.num_channels(); ++j) {
array[k++] = track.a(i, j);
}
}
return array;
}
|