Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split identity sliding FFT into a fourier bank library. #57

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ add_subdirectory(third_party/highway EXCLUDE_FROM_ALL)
add_executable(driver_model driver_model/driver_model.cc)
target_link_libraries(driver_model PkgConfig::SndFile)

add_library(fourier_bank speaker_experiments/fourier_bank.h speaker_experiments/fourier_bank.cc)
target_link_libraries(fourier_bank
PkgConfig::SndFile
absl::flags
absl::flags_parse
absl::log
absl::log_internal_check_impl
)

foreach (experiment IN ITEMS angular emphasizer revolve spectrum_similarity two_to_three virtual_speakers identity_sliding_fft audio_diff)
add_executable(${experiment} speaker_experiments/${experiment}.cc)
target_link_libraries(${experiment} PkgConfig::SndFile absl::flags absl::flags_parse absl::log absl::log_internal_check_impl)
target_link_libraries(${experiment} PkgConfig::SndFile absl::flags absl::flags_parse absl::log absl::log_internal_check_impl fourier_bank)
endforeach ()


target_link_libraries(angular PkgConfig::FFTW3)
target_link_libraries(spectrum_similarity PkgConfig::FFTW3)
target_link_libraries(two_to_three PkgConfig::FFTW3)
Expand Down
16 changes: 10 additions & 6 deletions speaker_experiments/angular.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,18 @@ void Process(
std::greater<>()) -
speaker_to_ratio_table.begin();

// amp-kludge to make borders louder -- it is a virtual line array where the
// borders will be further away in rendering, so let's compensate for it here.
// amp-kludge to make borders louder -- it is a virtual line array where
// the borders will be further away in rendering, so let's compensate for
// it here.

float distance_from_center = (subspeaker_index - 0.5 * (output_channels - 1));
float distance_from_center =
(subspeaker_index - 0.5 * (output_channels - 1));
float assumed_distance_to_line = 0.75 * (output_channels - 1);
float distance_to_virtual = sqrt(distance_from_center * distance_from_center +
assumed_distance_to_line * assumed_distance_to_line);
float dist_ratio = distance_to_virtual * (1.0f / assumed_distance_to_line);
float distance_to_virtual =
sqrt(distance_from_center * distance_from_center +
assumed_distance_to_line * assumed_distance_to_line);
float dist_ratio =
distance_to_virtual * (1.0f / assumed_distance_to_line);
float amp = dist_ratio * dist_ratio;

const float index =
Expand Down
24 changes: 12 additions & 12 deletions speaker_experiments/audio_closed_loop
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env python
#!/ usr / bin / env python

# Copyright 2024 Google LLC
#Copyright 2024 Google LLC
#
# 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
#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
#
# https://www.apache.org/licenses/LICENSE-2.0
#https: // 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.
#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.

"""Usage: ./audio_closed_loop input.wav recorded.wav [<beep peak in dBFS> [<playback device> [<recording device>]]]
"""
Expand Down Expand Up @@ -44,7 +44,7 @@ def detect_offset(samples: np.ndarray, sample_rate: int) -> int:
kernel = (-2.) ** -np.abs(np.arange(-24, 24 + 1))
samples = np.convolve(samples, kernel, mode='same')

# Require the beep to be at least 10dB above the background noise in amplitude
#Require the beep to be at least 10dB above the background noise in amplitude
min_ratio = 10**(10/20)
max_background = max(1e-7, np.max(np.abs(samples[:int(0.002 * sample_rate)])))
max_above = None
Expand Down
25 changes: 11 additions & 14 deletions speaker_experiments/audio_diff.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
namespace {

int FindMedian3xLeaker(double window) {
return static_cast<int>(-2.32/log(window)); // Approximate filter delay.
return static_cast<int>(-2.32 / log(window)); // Approximate filter delay.
/*
double windowM1 = 1.0 - window;
double half_way_to_total_sum = 1e99;
Expand Down Expand Up @@ -60,7 +60,7 @@ constexpr int64_t kNumRotators = 128;

struct Rotator {
std::complex<double> rot[4] = {{1, 0}, 0};
double window = std::pow(0.9996, 128.0/kNumRotators); // at 40 Hz.
double window = std::pow(0.9996, 128.0 / kNumRotators); // at 40 Hz.
double windowM1 = 1 - window;
std::complex<double> exp_mia;
int advance = 0;
Expand Down Expand Up @@ -110,14 +110,13 @@ static const int kHistoryMask = kHistorySize - 1;

class TaskExecutor {
public:
TaskExecutor(size_t num_threads)
: thread_outputs_(num_threads) {
}
TaskExecutor(size_t num_threads) : thread_outputs_(num_threads) {}
double error_ = 0;

void Execute(size_t num_tasks,
size_t read, size_t total, const double* history, Rotator* rot_left, Rotator* rot_right,
size_t read2, size_t total2, const double* history2, Rotator* rot_left2, Rotator* rot_right2) {
void Execute(size_t num_tasks, size_t read, size_t total,
const double* history, Rotator* rot_left, Rotator* rot_right,
size_t read2, size_t total2, const double* history2,
Rotator* rot_left2, Rotator* rot_right2) {
read_ = read;
total_ = total;
history_ = history;
Expand Down Expand Up @@ -184,9 +183,7 @@ class TaskExecutor {
};

template <typename In>
void Process(
In& input_stream, In& input_stream2,
double *error) {
void Process(In& input_stream, In& input_stream2, double* error) {
std::vector<double> history(input_stream.channels() * kHistorySize);
std::vector<double> input(input_stream.channels() * kBlockSize);

Expand Down Expand Up @@ -231,9 +228,9 @@ void Process(
if (read == 0) break;
if (read2 == 0) break;

pool.Execute(kNumRotators,
read, total, history.data(), rot_left.data(), rot_right.data(),
read2, total, history2.data(), rot_left2.data(), rot_right2.data());
pool.Execute(kNumRotators, read, total, history.data(), rot_left.data(),
rot_right.data(), read2, total, history2.data(),
rot_left2.data(), rot_right2.data());

total += read;
}
Expand Down
9 changes: 4 additions & 5 deletions speaker_experiments/emphasizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ double ActualLeftToRightRatio(const double squared_left,
}

double FindMedian3xLeaker(double window) {
return static_cast<int>(-2.32/log(window)); // Approximate filter delay.
return static_cast<int>(-2.32 / log(window)); // Approximate filter delay.
}

double CalcReverbRatio(double frequency) {
Expand Down Expand Up @@ -239,8 +239,7 @@ class TaskExecutor {

template <typename In, typename Out>
void Process(
const int output_channels,
In& input_stream, Out& output_stream,
const int output_channels, In& input_stream, Out& output_stream,
const std::function<void()>& start_progress = [] {},
const std::function<void(int64_t)>& set_progress = [](int64_t written) {}) {
std::vector<double> history(input_stream.channels() * kHistorySize);
Expand Down Expand Up @@ -308,6 +307,6 @@ int main(int argc, char** argv) {
/*channels=*/output_channels, /*samplerate=*/input_file.samplerate());

Process(
output_channels, input_file, output_file,
[] {}, [](const int64_t written) {});
output_channels, input_file, output_file, [] {},
[](const int64_t written) {});
}
Loading