Skip to content

Commit

Permalink
Merge v2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-thompson committed May 23, 2023
2 parents 05d8acd + 18b2150 commit 2123bc4
Show file tree
Hide file tree
Showing 31 changed files with 307 additions and 659 deletions.
23 changes: 7 additions & 16 deletions cli/Benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ const auto* kConsoleShimScript = R"script(
)script";

template <typename FloatType>
void runBenchmark(std::string const& name, std::string const& inputFile) {
void runBenchmark(std::string const& name, std::string const& inputFileName, std::function<void(elem::Runtime<FloatType>&)>&& initCallback) {
elem::Runtime<FloatType> runtime(44100.0, 512);

// Allow additional user initialization
initCallback(runtime);

auto ctx = choc::javascript::createQuickJSContext();

ctx.registerFunction("__postNativeMessage__", [&](choc::javascript::ArgumentList args) {
Expand All @@ -51,6 +54,7 @@ void runBenchmark(std::string const& name, std::string const& inputFile) {
(void) ctx.evaluate(kConsoleShimScript);

// Execute the input file
auto inputFile = choc::file::loadFileAsString(inputFileName);
auto rv = ctx.evaluate(inputFile);

// Now we benchmark the realtime processing step
Expand Down Expand Up @@ -107,18 +111,5 @@ void runBenchmark(std::string const& name, std::string const& inputFile) {
std::cout << "Done" << std::endl << std::endl;
}


int main(int argc, char** argv) {
// Read the input file from disk
if (argc < 2) {
std::cout << "Missing argument: what file do you want to run?" << std::endl;
return 1;
}

auto inputFile = choc::file::loadFileAsString(argv[1]);

runBenchmark<float>("Float", inputFile);
runBenchmark<double>("Double", inputFile);

return 0;
}
template void runBenchmark<float>(std::string const& name, std::string const& inputFileName, std::function<void(elem::Runtime<float>&)>&& initCallback);
template void runBenchmark<double>(std::string const& name, std::string const& inputFileName, std::function<void(elem::Runtime<double>&)>&& initCallback);
14 changes: 14 additions & 0 deletions cli/Benchmark.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <functional>

#include "Runtime.h"

/*
* Your main can call this function to run a complete benchmark test for either
* float or double processing. Before the benchmark starts, your initCallback
* will be called with a reference to the runtime for additional initialization,
* like adding a custom node type or filling the shared resource map.
*/
template <typename FloatType>
void runBenchmark(std::string const& name, std::string const& inputFileName, std::function<void(elem::Runtime<FloatType>&)>&& initCallback);
21 changes: 21 additions & 0 deletions cli/BenchmarkMain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <string>

#include "Benchmark.h"


int main(int argc, char **argv)
{
// Read the input file from disk
if (argc < 2) {
std::cout << "Missing argument: what file do you want to run?" << std::endl;
return 1;
}

auto inputFileName = std::string(argv[1]);

runBenchmark<float>("Float", inputFileName, [](auto&) {});
runBenchmark<double>("Double", inputFileName, [](auto&) {});

return 0;
}
52 changes: 32 additions & 20 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
cmake_minimum_required(VERSION 3.15)
project(cli VERSION 0.11.0)

add_executable(elemcli Realtime.cpp)
add_executable(elembench Benchmark.cpp)

foreach(TARGET_NAME elemcli elembench)
target_include_directories(${TARGET_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/choc/javascript
${CMAKE_CURRENT_SOURCE_DIR}/choc/text)

target_compile_features(${TARGET_NAME} PRIVATE
cxx_std_17)

target_link_libraries(${TARGET_NAME} PRIVATE
runtime)

if(MSVC)
target_compile_options(${TARGET_NAME} PRIVATE /W4)
else()
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra)
endif()
endforeach()
add_library(elemcli_core STATIC Realtime.cpp Benchmark.cpp)

target_include_directories(elemcli_core PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/choc/javascript
${CMAKE_CURRENT_SOURCE_DIR}/choc/text)

target_compile_features(elemcli_core PUBLIC
cxx_std_17)

target_link_libraries(elemcli_core PUBLIC
runtime)

if(MSVC)
target_compile_options(elemcli_core PRIVATE /W4)
else()
target_compile_options(elemcli_core PRIVATE -Wall -Wextra)
endif()

add_executable(elemcli RealtimeMain.cpp)
add_executable(elembench BenchmarkMain.cpp)

target_link_libraries(elemcli PRIVATE elemcli_core)
target_link_libraries(elembench PRIVATE elemcli_core)

if(UNIX AND NOT APPLE)
find_package(Threads REQUIRED)
target_link_libraries(elemcli PRIVATE
Threads::Threads
${CMAKE_DL_LIBS})
endif()

6 changes: 4 additions & 2 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ imports into a single bundle before trying to run it. To do that, we have a simp
`esbuild` workflow inside the `examples/` directory.

```bash
cd examples/
cd cli/examples/
npm install
npm run build
```
Expand All @@ -49,5 +49,7 @@ After these few steps, you'll see bundled files in `examples/dist/`. These files
then be run with the command line to hear them:

```bash
./elemcli examples/dist/00_HelloSine.js
# Your path to the elemcli binary might be different depending on your build
# directory structure
./build/cli/Debug/elemcli examples/dist/00_HelloSine.js
```
6 changes: 5 additions & 1 deletion cli/Realtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <Runtime.h>

#include "Realtime.h"

#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"

Expand Down Expand Up @@ -80,7 +82,7 @@ void audioCallback(ma_device* pDevice, void* pOutput, const void* /* pInput */,
proxy->process(static_cast<float*>(pOutput), numChannels, numFrames);
}

int main(int argc, char** argv) {
int RealtimeMain(int argc, char** argv, std::function<void(elem::Runtime<float>&)> initCallback) {
// First, initialize our audio device
ma_result result;

Expand Down Expand Up @@ -124,6 +126,8 @@ int main(int argc, char** argv) {
return choc::value::Value();
});

initCallback(proxy->runtime);

// Shim the js environment for console logging
(void) ctx.evaluate(kConsoleShimScript);

Expand Down
15 changes: 15 additions & 0 deletions cli/Realtime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <functional>

#include "Runtime.h"

/*
* Your main can call this function to execute the realtime command line loop. Before audio playback
* starts, your initCallback will be called with a reference to the elem::Runtime<float> instance
* for your own initialization needs (i.e. registering new node types or adding shared resources).
*
* The Realtime loop runs only on float runtimes for now, so to avoid a template explosion
* use a specialized runtime float in this callback.
*/
extern int RealtimeMain(int argc, char **argv, std::function<void(elem::Runtime<float> &)> initCallback);
7 changes: 7 additions & 0 deletions cli/RealtimeMain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "Realtime.h"


int main(int argc, char **argv)
{
return RealtimeMain(argc, argv, [](auto&) {});
}
14 changes: 7 additions & 7 deletions cli/examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"build": "esbuild *.js --bundle --outdir=dist"
},
"dependencies": {
"@elemaudio/core": "*",
"@elemaudio/core": "^2.0.0",
"esbuild": "^0.17.8"
}
}
Loading

0 comments on commit 2123bc4

Please sign in to comment.