Spaces:
Sleeping
Sleeping
File size: 3,126 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 |
/*
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 "wave/wave_io.h"
#include <limits>
#include "core/file_resource.h"
WaveIO::WaveIO(void) {
Initialize(PCM16);
}
WaveIO::WaveIO(WaveCodingType coding_type) {
Initialize(coding_type);
}
bool WaveIO::ReadAudioData(int32_t wave_start,
int32_t num_samples,
std::vector<int16_t> *samples,
FileResource *fr) {
switch (coding_type_) {
case PCM16:
case PCM8:
case ULAW8:
return codec_riff_.ReadAudioData(wave_start, num_samples, samples, fr);
default:
fprintf(stderr, "WaveIO:ReadAudioData: unknown coding type %d",
coding_type_);
}
return false;
}
bool WaveIO::ReadAudioContainer(int container_size_in_bytes,
std::vector<int16_t> *samples,
FileResource *fr) {
switch (coding_type_) {
case PCM16:
case PCM8:
case ULAW8:
return codec_riff_.ReadAudioContainer(container_size_in_bytes,
samples, fr);
fprintf(stderr, "WaveIO:ReadAudioData: unknown coding type %d",
coding_type_);
default:
fprintf(stderr, "WaveIO: coding type not supported %d", coding_type_);
return false;
}
return false;
}
bool WaveIO::ReadHeader(FileResource *fr,
int32_t *num_samples_per_channel,
int32_t *sampling_rate) {
switch (coding_type_) {
case PCM16:
case PCM8:
case ULAW8: {
bool status = codec_riff_.ReadHeader(fr);
if (!status) {
return false;
}
*num_samples_per_channel = codec_riff_.get_num_samples_per_channel();
*sampling_rate = codec_riff_.get_sampling_rate();
return true;
}
default:
fprintf(stderr, "WaveIO:ReadHeader: unknown coding type %d",
coding_type_);
}
return false;
}
bool WaveIO::Load(FileResource *fr,
std::vector<int16_t> *samples,
int32_t *sample_rate) {
int32_t num_samples;
if (!ReadHeader(fr, &num_samples, sample_rate)) {
return false;
}
samples->resize(num_samples);
return ReadAudioData(0, num_samples, samples, fr);
}
bool WaveIO::Initialize(WaveCodingType coding_type) {
coding_type_ = coding_type;
switch (coding_type_) {
case PCM16:
case PCM8:
case ULAW8:
return codec_riff_.Initialize(coding_type);
default:
fprintf(stderr, "WaveIO:WaveIO: unknown coding type %d", coding_type_);
return false;
}
}
|