Spaces:
Sleeping
Sleeping
File size: 9,712 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 |
/*
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.
*/
#ifndef _TRACK_H_
#define _TRACK_H_
#include <stdint.h>
#include <string>
#include <vector>
#include "core/file_resource.h"
#include "core/float_matrix.h"
class Track {
public:
Track();
~Track();
void FillFrom(const Track &track);
// Returns the number of frames contained within this track instance.
int num_frames() const;
// Returns the number of channels contained within this track instance.
int num_channels() const;
// Returns whether the voicing is enabled on this track instance.
bool voicing_enabled() const;
// Sets the voicing enable flag on this track instance.
void set_voicing_enabled(bool b);
// Gets values for frame "f" and channel "c".
float &a(int f, int c = 0);
// Gets values for frame "f" and channel "c".
float a(int f, int c = 0) const;
// Gets voiced flags for frame "f".
void set_v(int f, bool value);
// Gets voiced flags for frame "f".
bool v(int f) const;
// Gets time instant for frame "f".
float &t(int f);
// Gets time instant for frame "f".
float t(int f) const;
// Sets the interval between frames in seconds.
void set_shift(float shift);
// Returns the interval between frames in seconds.
float shift() const;
// Modifies this track instances frame times with that given, from the
// given frame index.
void FillTime(float frame_shift, int start = 1);
// Modifies this track instances frame times with the value corresponding
// to its frame index.
void FillTime(const Track &t);
// Modifies the times of this track instance with the given values.
// NOTE: the length must be equal to the number of frames in the track.
void SetTimes(float *times, int length);
// Modifies the voicing flag of this track instance with the given values.
bool SetVoicing(const std::vector<bool> &vuv);
// Sets the track from the given time and value vectors.
// All frames are set to voiced.
template <typename ValueType>
void Set(const std::vector<ValueType> ×, const std::vector<ValueType> &values);
// Sets the track from the given time and value vectors.
// All frames are set to voiced.
template <typename ValueType>
void Set(float time_shift, const std::vector<ValueType> &values);
// Populates the given vector with the values for all channels for
// the given frame index.
void FrameOut(std::vector<float> *fv, int n) const;
// Populates the given vector with the values for all channels for
// the given frame index.
void FrameOut(std::vector<double> *fv, int n) const;
// Updates the values for all channels for the given frame index with
// the values given.
void FrameIn(const float &f, int n);
// Updates the values for all channels for the given frame index with
// the values given.
void FrameIn(const std::vector<float> &fv, int n);
// Updates the values for all channels for the given frame index with
// the values given.
void FrameIn(const std::vector<double> &fv, int n);
// Returns the index of the frame at the given time index.
int Index(float x) const;
// Returns the index of the first frame below that at the given time index, x.
int IndexBelow(float x) const;
// Returns the index of the first frame above that at the given time index, x.
int IndexAbove(float x) const;
// Resizes the data and voicing flags to the given value, n, and the
// number of channels to that given, c.
void resize(int n, int c = 1);
// Resets all the data associated with this track instance.
void Clear();
// Returns a portion of this track instance as defined by the input
// start and end times along with the channels required.
// Note: Ownership of the returned track is required to prevent
// memory leaks downstream.
Track *GetSubTrack(float start,
float end,
int ch_offset = -1,
int ch_size = -1) const;
// Returns a portion of this track instance as defined by the input
// start and end frame indices along with the channels required.
// Note: Ownership of the returned track is required to prevent
// memory leaks downstream.
Track *GetSubTrack(int start_frame_index,
int end_frame_index,
int start_channel_index,
int end_channel_index) const;
// Returns a std::string representation of the header of this track:
// The number of frames in this track
// The number of channels in this track
// The interval between frames
// Whether voicing is enabled or not
std::string HeaderToString() const;
// Returns a std::string representation of this track instance containing:
// The number of frames in this track
// The number of channels in this track
// The interval between frames
// Whether voicing is enabled or not
// values
// If precision is specified, float numbers are printed out up to
// the specified number of decimal places.
std::string ToString(uint32_t precision) const;
std::string ToString() const;
// Saves this track instance to the given FileResource.
bool Save(FileResource *fr) const;
// Saves this track instance to the given FileResource.
bool Save(const std::string &filename, bool ascii) const;
const FloatMatrix &data() const;
// Resizes the track to the number of frames and channels given,
// copying the given data into the track.
void CopyFrom(const char *v, int32_t num_frames, int32_t num_channels);
// Sets this track to be a combination of two input tracks
bool SetCombinedTrack(const Track &track_a,
const Track &track_b);
// Sets this track to be combination of three input tracks
bool SetCombinedTrack(const Track &track_a,
const Track &track_b,
const Track &track_c);
// Pads a track by repeating the last frame num_pads times.
bool Pad(int num_pads);
// Makes the current track and ref track equal lengths, use the longest length
// and zero pad the shorter track if the are different lengths.
void MakeSameSize(Track *track);
// Makes the current track and ref track equal lengths, use the longest length
// and zero pad the shorter tracks if any are shorter than the longest.
void MakeSameSize(Track *track_a,
Track *track_b);
private:
// Holds the number of frames of this track instance.
int num_frames_;
// Holds the number of channels of this track instance.
int num_channels_;
// Holds the frames values and times of this track instance.
FloatMatrix data_;
// Holds the voicing flags of this track instance.
std::vector<bool> val_;
// Holds whether this track instance has voicing enabled.
bool voicing_enabled_;
// The frame interval in seconds.
float shift_;
};
// Applies logarithm to all data values. Invalid values are set to INVALID_LOG.
void ConvertToLogarithmic(Track *t);
// Returns a new vector with the track data as a single sequence of
// values. The caller takes ownership fo the pointer.
float *TrackToFloatPointer(const Track &track, int *num_samples);
inline int Track::num_frames() const {
return num_frames_;
}
inline int Track::num_channels() const {
return num_channels_;
}
inline float &Track::a(int f, int c) {
return data_.Get(c + 1, f);
}
inline float Track::a(int f, int c) const {
return data_.Get(c + 1, f);
}
inline void Track::set_v(int f, bool value) {
// If a writable reference is used, force the track into
// voicing enabled mode.
voicing_enabled_ = true;
val_[f] = value;
}
inline bool Track::v(int f) const {
return val_[f];
}
inline float &Track::t(int f) {
return data_.Get(0, f);
}
inline float Track::t(int f) const {
return data_.Get(0, f);
}
inline void Track::set_shift(float shift) {
shift_ = shift;
}
inline const FloatMatrix &Track::data() const {
return data_;
}
inline bool Track::voicing_enabled() const {
return voicing_enabled_;
}
inline void Track::set_voicing_enabled(bool b) {
voicing_enabled_ = b;
}
inline bool Track::Save(const std::string &filename, bool ascii) const {
FileResource fr(filename, "wb");
if (!fr.Get()) {
fprintf(stderr, "Failed to write '%s'", filename.c_str());
return false;
}
if (!ascii) {
return Save(&fr);
}
const std::string data = ToString();
if (fprintf(fr.fp(), "%s", data.c_str()) != data.size()) {
return false;
}
return true;
}
template <typename ValueType>
void Track::Set(const std::vector<ValueType> ×,
const std::vector<ValueType> &values) {
if (times.size() != values.size()) {
fprintf(stderr, "Length of time and value vectors should equal (%d != %d)",
times.size(), values.size());
return;
}
resize(times.size());
for (int n = 0; n < times.size(); ++n) {
t(n) = times[n];
a(n) = values[n];
set_v(n, true);
}
}
template <typename ValueType>
void Track::Set(float time_shift, const std::vector<ValueType> &values) {
resize(values.size());
FillTime(time_shift, 0);
for (int n = 0; n < values.size(); ++n) {
a(n) = values[n];
set_v(n, true);
}
}
#endif // _TRACK_H_
|