Skip to content

Commit

Permalink
Working Jai compiler on MacOS and fallback for ARM CPUs
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 committed Jul 24, 2023
1 parent f3ab856 commit 1f1cf2f
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 14 deletions.
17 changes: 17 additions & 0 deletions modules/cmake/FindJaiCompiler.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
if(NOT IOS)
if(WIN32)
set(JAI_COMPILER_EXE "jai.exe")
elseif(APPLE)
set(JAI_COMPILER_EXE "jai-macos")
else()
set(JAI_COMPILER_EXE "jai-linux")
endif()

find_program(JAI_COMPILER
NAMES ${JAI_COMPILER_EXE}
HINTS ${CMAKE_SOURCE_DIR}/../jai/bin ${CMAKE_SOURCE_DIR}/../../Research/jai/bin
)
message(STATUS "Jai compiler: ${JAI_COMPILER}")
else()
message(STATUS "Skipping Jai checks on this platform")
endif()
7 changes: 1 addition & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,7 @@ target_sources(BYOD PRIVATE
)

# Jai files
find_program(JAI_COMPILER
NAMES jai jai.exe jai-linux jai-macos
HINTS ${CMAKE_SOURCE_DIR}/../jai/bin ${CMAKE_SOURCE_DIR}/../../Research/jai/bin
)

message(STATUS "Jai compiler: ${JAI_COMPILER}")
include(${CMAKE_SOURCE_DIR}/modules/cmake/FindJaiCompiler.cmake)
if (NOT(${JAI_COMPILER} STREQUAL "JAI_COMPILER-NOTFOUND"))
message(STATUS "Configuring Jai compilation!")
add_subdirectory(jai)
Expand Down
2 changes: 1 addition & 1 deletion src/jai/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ target_link_libraries(BYOD PRIVATE byod_jai_lib)
target_sources(BYOD PRIVATE SharedJaiContext.cpp stb_sprintf.cpp)
target_compile_definitions(BYOD PRIVATE STB_SPRINTF_IMPLEMENTATION=1)

if((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
if((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
string(JOIN " " STB_CXX_FLAGS
"-Wno-language-extension-token"
"-Wno-zero-as-null-pointer-constant"
Expand Down
3 changes: 1 addition & 2 deletions src/jai/krusher/lofi_downsampler.jai
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ krusher_process_lofi_resample :: (source_buffer: **float32,
overshoot_samples: *float64)
{
// simple S&H lofi resampler
grab_index : s32 = -1;
for channel: 0..num_channels-1 {
source_data := source_buffer[channel];
dest_data := dest_buffer[channel];

for i: 0..num_samples_dest-1 {
grab_index = cast(s32) (cast(float64) i * resample_factor + (<<overshoot_samples));
grab_index := cast(s32) (cast(float64) i * resample_factor + (<<overshoot_samples));
dest_data[i] = source_data[min(grab_index, num_samples_source - 1)];
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/processors/other/krusher/Krusher.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

#include "processors/BaseProcessor.h"

#include "jai/byod_jai_lib.h"
#define KRUSHER_USE_JAI_IMPL ! (JUCE_ARM || JUCE_IOS)

//#include "BRRHelpers.h"
//#include "LoFiDownsampler.h"
#if KRUSHER_USE_JAI_IMPL
#include "jai/byod_jai_lib.h"
#else
#include "krusher_fallback_impl.h"
#endif

class Krusher : public BaseProcessor
{
Expand All @@ -31,11 +34,14 @@ class Krusher : public BaseProcessor
chowdsp::EllipticFilter<8> aiFilter;
float hostFs = 48000.0f;

#if KRUSHER_USE_JAI_IMPL
SharedJaiContext jai_context;
jai_Krusher_Lofi_Resample_State resample_state {};
#else
std::unique_ptr<chowdsp::NullType> jai_context;
Krusher_Lofi_Resample_State resample_state {};
#endif

// LoFiDownsampler loFiDownsampler;
//
// std::array<brr_helpers::BRRFilterState, 2> brrFilterStates {};

chowdsp::FirstOrderHPF<float> dcBlocker;
Expand Down
53 changes: 53 additions & 0 deletions src/processors/other/krusher/krusher_fallback_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

struct Krusher_Lofi_Resample_State
{
double upsample_overshoot = 0.0;
double downsample_overshoot = 0.0;
};

inline void krusher_init_lofi_resample (Krusher_Lofi_Resample_State* state)
{
state->upsample_overshoot = 0.0;
state->downsample_overshoot = 0.0;
}

inline void krusher_process_lofi_resample (float** source_buffer,
float** dest_buffer,
int num_channels,
int num_samples_source,
int num_samples_dest,
double resample_factor,
double& overshoot_samples)
{
// simple S&H lofi resampler
for (int channel = 0; channel < num_channels; ++channel)
{
const auto* source_data = source_buffer[channel];
auto* dest_data = dest_buffer[channel];

for (int i = 0; i < num_samples_dest; ++i)
{
const auto grab_index = (int) ((double) i * resample_factor + overshoot_samples);
dest_data[i] = source_data[std::min (grab_index, num_samples_source - 1)];
}
}

overshoot_samples = (double) num_samples_dest * resample_factor - std::floor ((double) num_samples_dest * resample_factor);
}

inline void krusher_process_lofi_downsample ([[maybe_unused]] void* ctx,
Krusher_Lofi_Resample_State* state,
float** buffer,
int num_channels,
int num_samples,
double resample_factor)
{
const auto ds_buffer_size = (int) std::ceil ((double) num_samples / resample_factor);

auto* temp_data = (float*) alloca (2 * (size_t) ds_buffer_size * sizeof (float));
float* ds_buffer[2] = { temp_data, temp_data + ds_buffer_size };

krusher_process_lofi_resample (buffer, ds_buffer, num_channels, num_samples, ds_buffer_size, resample_factor, state->downsample_overshoot);
krusher_process_lofi_resample (ds_buffer, buffer, num_channels, ds_buffer_size, num_samples, 1.0 / resample_factor, state->upsample_overshoot);
}

0 comments on commit 1f1cf2f

Please sign in to comment.