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 #include "os/log.h"
26 
27 namespace bluetooth {
28 namespace testing {
29 
WavReader(const char * filename)30 WavReader::WavReader(const char* filename) {
31   if (os::FileExists(filename)) {
32     wavFile_.open(filename, std::ios::in | std::ios::binary);
33     wavFile_.read((char*)&header_, kWavHeaderSize);
34     ReadSamples();
35   } else {
36     log::fatal("File {} does not exist!", filename);
37   }
38 }
39 
~WavReader()40 WavReader::~WavReader() {
41   if (wavFile_.is_open()) {
42     wavFile_.close();
43   }
44 }
45 
GetHeader() const46 WavHeader WavReader::GetHeader() const { return header_; }
47 
ReadSamples()48 void WavReader::ReadSamples() {
49   std::istreambuf_iterator<char> start{wavFile_}, end;
50   samples_ = std::vector<uint8_t>(start, end);
51 }
52 
GetSamples()53 uint8_t* WavReader::GetSamples() { return &samples_[0]; }
54 
GetSampleCount()55 size_t WavReader::GetSampleCount() { return samples_.size(); }
56 
57 }  // namespace testing
58 }  // namespace bluetooth
59