File size: 4,357 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
/*
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 _CODEC_API_INL_H_
#define _CODEC_API_INL_H_

#include "core/file_resource.h"


template <class CodecImplementation>
inline bool CodecApi<CodecImplementation>::SetHeaderInfo(
    int sampling_rate, int num_samples_per_channel) {
  bool status = set_sampling_rate(sampling_rate);
  status &= set_num_samples_per_channel(num_samples_per_channel);
  return status;
}

template <class CodecImplementation>
inline bool CodecApi<CodecImplementation>::GetHeaderInfo(
    int *sampling_rate, int *num_samples_per_channel) {
  *sampling_rate = sampling_rate_;
  *num_samples_per_channel = num_samples_per_channel_;
  return true;
}

template <class CodecImplementation>
inline bool CodecApi<CodecImplementation>::ReadHeader(FileResource *fr) {
  return codec_.ReadHeader(fr, &num_samples_per_channel_, &sampling_rate_);
}

template <class CodecImplementation>
bool CodecApi<CodecImplementation>::ReadAudioData(
    int wave_start, int num_samples,
    std::vector<int16_t> *samples, FileResource *fr) {
  if (samples == NULL) {
    fprintf(stderr, "CodecApi::ReadAudioData: empty pointer was given");
    return true;
  }
  int64_t offset_audio_container = ftell(fr->fp());
  bool status = codec_.ReadAudioData(wave_start, num_samples, samples, fr);
  // Reset the file resource pointer back to the beginning of the
  // audio container.
  if (fseek(fr->fp(), offset_audio_container, SEEK_SET) != 0) {
    fprintf(stderr, "CodecApi::ReadAudioData: error seeking the beginning of the "
            "audio container");
    return false;
  }
  return status;
}

template <class CodecImplementation>
bool CodecApi<CodecImplementation>::ReadAudioContainer(
    int container_size_in_bytes,
    std::vector<int16_t> *samples,
    FileResource *fr) {
  int64_t offset_audio_container = ftell(fr->fp());
  bool status = codec_.ReadAudioContainer(container_size_in_bytes, samples, fr);
  // Reset the FileResource pointer back to the beginning of the audio container
  if (fseek(fr->fp(), offset_audio_container, SEEK_SET) != 0) {
    fprintf(stderr, "CodecApi::ReadAudioData: error seeking the beginning of the "
            "audio container");
    return false;
  }
  return status;
}

template <class CodecImplementation>
inline int CodecApi<CodecImplementation>::get_num_samples_per_channel() const {
  return num_samples_per_channel_;
}

template <class CodecImplementation>
inline int CodecApi<CodecImplementation>::get_sampling_rate() const {
  return sampling_rate_;
}

template <class CodecImplementation>
inline
bool CodecApi<CodecImplementation>::set_num_samples_per_channel(int value) {
  num_samples_per_channel_ = value;
  return true;
}

template <class CodecImplementation>
inline
bool CodecApi<CodecImplementation>::set_sampling_rate(int value) {
  sampling_rate_ = value;
  return true;
}

template <class CodecImplementation>
bool CodecApi<CodecImplementation>::Load(
    FileResource *fr, std::vector<int16_t> *samples) {
  if (!codec_.ReadHeader(fr, &num_samples_per_channel_, &sampling_rate_)) {
    return false;
  }
  samples->resize(num_samples_per_channel_);
  return codec_.ReadAudioData(
      0, num_samples_per_channel_, PCM16, samples, fr);
}

template <class CodecImplementation>
template <class FileResourceType>
bool CodecApi<CodecImplementation>::Load(
    const std::string &filename, std::vector<int16_t> *samples) {
  FileResource *fr = FileResourceType::Open(filename);
  if (fr != NULL) {
    return Load(fr, samples);
  } else {
    fprintf(stderr, "CodecApi::Load: Failed to open \"%s\"", filename.c_str());
    return false;
  }
}

template <class CodecImplementation>
bool CodecApi<CodecImplementation>::Initialize(
    WaveCodingType coding_type) {
  return codec_.Initialize(coding_type);
}


#endif  // SPEECH_PATTS_LIBS_IO_CODEC_API_INL_H_