1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#include "dsp/sh_hrir_creator.h"
18
19#include "third_party/SADIE_hrtf_database/generated/hrtf_assets.h"
20#include "ambisonics/utils.h"
21#include "base/logging.h"
22#include "dsp/resampler.h"
23#include "utils/planar_interleaved_conversion.h"
24
25namespace vraudio {
26
27std::unique_ptr<AudioBuffer> CreateShHrirsFromWav(const Wav& wav,
28 int target_sample_rate_hz,
29 Resampler* resampler) {
30 DCHECK(resampler);
31 const size_t num_channels = wav.GetNumChannels();
32 CHECK(IsValidAmbisonicOrder(num_channels));
33
34 const size_t sh_hrir_length = wav.interleaved_samples().size() / num_channels;
35 std::unique_ptr<AudioBuffer> sh_hrirs(
36 new AudioBuffer(num_channels, sh_hrir_length));
37 FillAudioBuffer(interleaved_buffer: wav.interleaved_samples(), num_input_channels: num_channels, output: sh_hrirs.get());
38
39 const int wav_sample_rate_hz = wav.GetSampleRateHz();
40 CHECK_GT(wav_sample_rate_hz, 0);
41 CHECK_GT(target_sample_rate_hz, 0);
42 if (wav_sample_rate_hz != target_sample_rate_hz) {
43 if (!Resampler::AreSampleRatesSupported(source: wav_sample_rate_hz,
44 destination: target_sample_rate_hz)) {
45 LOG(FATAL) << "Unsupported sampling rates for loading HRIRs: "
46 << wav_sample_rate_hz << ", " << target_sample_rate_hz;
47 }
48 resampler->ResetState();
49 // Resample the SH HRIRs if necessary.
50 resampler->SetRateAndNumChannels(source_frequency: wav_sample_rate_hz, destination_frequency: target_sample_rate_hz,
51 num_channels);
52 std::unique_ptr<AudioBuffer> resampled_sh_hrirs(new AudioBuffer(
53 num_channels, resampler->GetNextOutputLength(input_length: sh_hrir_length)));
54 resampler->Process(input: *sh_hrirs, output: resampled_sh_hrirs.get());
55 return resampled_sh_hrirs;
56 }
57 return sh_hrirs;
58}
59
60std::unique_ptr<AudioBuffer> CreateShHrirsFromAssets(
61 const std::string& filename, int target_sample_rate_hz,
62 Resampler* resampler) {
63 // Read SH HRIR from asset store.
64 sadie::HrtfAssets hrtf_assets;
65 std::unique_ptr<std::string> sh_hrir_data = hrtf_assets.GetFile(filename);
66 CHECK_NOTNULL(sh_hrir_data.get());
67 std::istringstream wav_data_stream(*sh_hrir_data);
68 std::unique_ptr<const Wav> wav = Wav::CreateOrNull(binary_stream: &wav_data_stream);
69 return CreateShHrirsFromWav(wav: *wav, target_sample_rate_hz, resampler);
70}
71
72} // namespace vraudio
73

source code of qtmultimedia/src/3rdparty/resonance-audio/resonance_audio/dsp/sh_hrir_creator.cc