1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "wav_reader.h"
18 
19 #include <bluetooth/log.h>
20 
21 #include <iostream>
22 #include <iterator>
23 
24 #include "os/files.h"
25 
26 namespace bluetooth {
27 namespace testing {
28 
WavReader(const char * filename)29 WavReader::WavReader(const char* filename) {
30   if (os::FileExists(filename)) {
31     wavFile_.open(filename, std::ios::in | std::ios::binary);
32     wavFile_.read((char*)&header_, kWavHeaderSize);
33     ReadSamples();
34   } else {
35     log::fatal("File {} does not exist!", filename);
36   }
37 }
38 
~WavReader()39 WavReader::~WavReader() {
40   if (wavFile_.is_open()) {
41     wavFile_.close();
42   }
43 }
44 
GetHeader() const45 WavHeader WavReader::GetHeader() const { return header_; }
46 
ReadSamples()47 void WavReader::ReadSamples() {
48   std::istreambuf_iterator<char> start{wavFile_}, end;
49   samples_ = std::vector<uint8_t>(start, end);
50 }
51 
GetSamples()52 uint8_t* WavReader::GetSamples() { return &samples_[0]; }
53 
GetSampleCount()54 size_t WavReader::GetSampleCount() { return samples_.size(); }
55 
56 }  // namespace testing
57 }  // namespace bluetooth
58