• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
20 #include <iterator>
21 
22 #include "gd/os/files.h"
23 #include "os/log.h"
24 
25 namespace bluetooth {
26 namespace testing {
27 
WavReader(const char * filename)28 WavReader::WavReader(const char* filename) {
29   if (os::FileExists(filename)) {
30     wavFile_.open(filename, std::ios::in | std::ios::binary);
31     wavFile_.read((char*)&header_, kWavHeaderSize);
32     ReadSamples();
33   } else {
34     ASSERT_LOG(false, "File %s does not exist!", filename);
35   }
36 }
37 
~WavReader()38 WavReader::~WavReader() {
39   if (wavFile_.is_open()) {
40     wavFile_.close();
41   }
42 }
43 
GetHeader() const44 WavHeader WavReader::GetHeader() const { return header_; }
45 
ReadSamples()46 void WavReader::ReadSamples() {
47   std::istreambuf_iterator<char> start{wavFile_}, end;
48   samples_ = std::vector<uint8_t>(start, end);
49 }
50 
GetSamples()51 uint8_t* WavReader::GetSamples() { return &samples_[0]; }
52 
GetSampleCount()53 size_t WavReader::GetSampleCount() { return samples_.size(); }
54 
55 }  // namespace testing
56 }  // namespace bluetooth
57