Skip to content

Commit

Permalink
feat: macos fixes, dsp fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
feelfreelinux committed Jan 9, 2024
1 parent 3017f8d commit e9f1b55
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .clangd
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

CompileFlags:
CompilationDatabase: build/ # Search build/ directory for compile_commands.json
CompilationDatabase: examples/dsp/build # Search build/ directory for compile_commands.json
6 changes: 3 additions & 3 deletions main/audio-dsp/Biquad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Biquad::Biquad() {

void Biquad::sampleRateChanged(uint32_t sampleRate) {
this->sampleRate = sampleRate;
//this->configure(this->type, this->currentConfig);
this->configure(this->type, this->currentConfig);
}

void Biquad::configure(Type type, std::map<std::string, float>& newConf) {
Expand Down Expand Up @@ -417,11 +417,11 @@ std::unique_ptr<StreamInfo> Biquad::process(
std::unique_ptr<StreamInfo> stream) {
std::scoped_lock lock(accessMutex);

auto input = stream->data->at(this->channel);
auto& input = stream->data->at(this->channel);
auto numSamples = stream->numSamples;

#ifdef ESP_PLATFORM
dsps_biquad_f32_ae32(input, input, numSamples, coeffs, w);
dsps_biquad_f32_ae32(input.data(), input.data(), numSamples, coeffs, w);
#else
// Apply the set coefficients
for (int i = 0; i < numSamples; i++) {
Expand Down
2 changes: 2 additions & 0 deletions main/audio-dsp/include/AudioMixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class AudioMixer : public bell::AudioTransform {

void reconfigure() override {}

void sampleRateChanged(uint32_t sampleRate) override {}

void fromJSON(cJSON* json) {
cJSON* mappedChannels = cJSON_GetObjectItem(json, "mapped_channels");

Expand Down
5 changes: 4 additions & 1 deletion main/audio-dsp/include/Biquad.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ class Biquad : public bell::AudioTransform {
float slope = config->getFloat("slope", false, invalid);
float gain = config->getFloat("gain", false, invalid);
float frequency = config->getFloat("frequency", false, invalid);

float q = config->getFloat("q", false, invalid);
std::cout << type << " " << gain << " " << frequency << " " << q << std::endl;
std::cout << config->getChannels()[0] << std::endl;

if (currentConfig["bandwidth"] == bandwidth &&
currentConfig["slope"] == slope && currentConfig["gain"] == gain &&
Expand Down Expand Up @@ -122,7 +125,7 @@ class Biquad : public bell::AudioTransform {
float coeffs[5];
float w[2] = {1.0, 1.0};

float sampleRate = 44100;
float sampleRate = 48000;

// Generator methods for different filter types
void highPassCoEffs(float f, float q);
Expand Down
3 changes: 3 additions & 0 deletions main/audio-dsp/include/Gain.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ class Gain : public bell::AudioTransform {

void configure(std::vector<int> channels, float gainDB);

void sampleRateChanged(uint32_t sampleRate) override {}

std::unique_ptr<StreamInfo> process(
std::unique_ptr<StreamInfo> data) override;

void reconfigure() override {
std::scoped_lock lock(this->accessMutex);
float gain = config->getFloat("gain");
this->channels = config->getChannels();
std::cout << "the gain " << gain << std::endl;

if (gainDb == gain) {
return;
Expand Down
144 changes: 144 additions & 0 deletions main/audio-dsp/include/NlohmannJSONTransformConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#pragma once

#include <memory>
#include "AudioPipeline.h"
#ifndef BELL_ONLY_CJSON

// dsp filters
#include "AudioMixer.h"
#include "Biquad.h"
#include "BiquadCombo.h"
#include "Compressor.h"
#include "Gain.h"

#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include "TransformConfig.h"

namespace bell {
class NlohmannJSONTransformConfig : public bell::TransformConfig {
private:
nlohmann::json json;

public:
NlohmannJSONTransformConfig(nlohmann::json& body) { this->json = body; };
~NlohmannJSONTransformConfig(){};

static std::shared_ptr<AudioPipeline> parsePipeline(
nlohmann::json& dspPipeline) {
auto pipeline = std::make_shared<bell::AudioPipeline>();

for (auto& transform : dspPipeline) {
if (!transform.contains("type"))
continue;

std::string type = transform["type"];
if (type == "gain") {
auto filter = std::make_shared<bell::Gain>();
filter->config =
std::make_unique<bell::NlohmannJSONTransformConfig>(transform);
filter->reconfigure();
pipeline->addTransform(filter);
std::cout << "got gain" << std::endl;
} else if (type == "compressor") {
auto filter = std::make_shared<bell::Compressor>();
filter->config =
std::make_unique<bell::NlohmannJSONTransformConfig>(transform);
filter->reconfigure();
pipeline->addTransform(filter);
} else if (type == "mixer") {
auto filter = std::make_shared<bell::AudioMixer>();
filter->config =
std::make_unique<bell::NlohmannJSONTransformConfig>(transform);
filter->reconfigure();
pipeline->addTransform(filter);
} else if (type == "biquad") {
auto filter = std::make_shared<bell::Biquad>();
filter->config =
std::make_unique<bell::NlohmannJSONTransformConfig>(transform);
filter->reconfigure();
pipeline->addTransform(filter);
} else if (type == "biquad_combo") {
auto filter = std::make_shared<bell::BiquadCombo>();
filter->config =
std::make_unique<bell::NlohmannJSONTransformConfig>(transform);
filter->reconfigure();
pipeline->addTransform(filter);
} else {
throw std::invalid_argument(
"No filter type found for configuration field " + type);
}
}

pipeline->volumeUpdated(50);
return pipeline;
}

std::string rawGetString(const std::string& field) override {
if (json.contains(field) && json[field].is_string()) {
return json[field];
}

return "invalid";
}

std::vector<int> rawGetIntArray(const std::string& field) override {
std::vector<int> result;

if (json.contains(field) && json[field].is_array()) {
for (auto& field : json[field]) {
if (!field.is_number_integer()) {
continue;
}

result.push_back(field);
}
}

return result;
}

std::vector<float> rawGetFloatArray(const std::string& field) override {
std::vector<float> result;

if (json.contains(field) && json[field].is_array()) {
for (auto& field : json[field]) {
if (!field.is_number_float() && !field.is_number()) {
continue;
}

result.push_back(field);
}
}

return result;
}

int rawGetInt(const std::string& field) override {
if (json.contains(field) && json[field].is_number_integer()) {
return json[field];
}

return invalidInt;
}

bool isArray(const std::string& field) override {
if (json.contains(field) && json[field].is_array()) {
return true;
}

return false;
}

float rawGetFloat(const std::string& field) override {
if (json.contains(field) &&
(json[field].is_number_float() || json[field].is_number())) {
return json[field];
}

return invalidInt;
}
};
} // namespace bell

#endif
14 changes: 9 additions & 5 deletions main/platform/apple/MDNSBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class implMDNSBrowser : public MDNSBrowser {
private:
DNSServiceRef service;
std::vector<DNSServiceRef> addrResolveRefs;
std::vector<DiscoveredRecord> lastPublishedRecords;

int socketFd = -1;
std::atomic<bool> runDiscovery = false;
Expand All @@ -38,8 +39,9 @@ class implMDNSBrowser : public MDNSBrowser {
}
}

if (fullyDiscovered.size() > 0 && recordsCallback) {
if (recordsCallback && (lastPublishedRecords != fullyDiscovered)) {
recordsCallback(fullyDiscovered);
lastPublishedRecords = fullyDiscovered;
}
}

Expand Down Expand Up @@ -81,6 +83,7 @@ class implMDNSBrowser : public MDNSBrowser {
errorCode, hostname, address, ttl);
}

DNSServiceRefDeallocate(ref);
// Not passed anywhere further
delete ctx;
}
Expand Down Expand Up @@ -120,9 +123,8 @@ class implMDNSBrowser : public MDNSBrowser {
ctx->parentPtr->handleServiceResolveReply(ctx, ref, flags, interfaceIndex,
errorCode, fullName, hostTarget,
opaqueport, txtLen, txtRecord);
} else {
delete ctx;
}
DNSServiceRefDeallocate(ref);
}

/// Handle DNS-SD responses
Expand Down Expand Up @@ -155,7 +157,6 @@ class implMDNSBrowser : public MDNSBrowser {
addrResolvCtx->type = record.type;
addrResolvCtx->name = record.name;
addrResolvCtx->parentPtr = this;

DNSServiceErrorType err =
DNSServiceResolve(&refCopy, kDNSServiceFlagsShareConnection,
interfaceIndex, serviceName, regType, replyDomain,
Expand All @@ -179,7 +180,10 @@ class implMDNSBrowser : public MDNSBrowser {
publishDiscovered();
}

if (!(flags & kDNSServiceFlagsMoreComing) && recordsCallback) {}
if (!(flags & kDNSServiceFlagsMoreComing) &&
(flags & kDNSServiceFlagsAdd)) {
publishDiscovered();
}
}

/// Thin shim passing dns-sd C-style callback to a member function
Expand Down

0 comments on commit e9f1b55

Please sign in to comment.