Skip to content

Commit b8c057f

Browse files
tremblapweefuzzytedmoore
authored
Example files corrections (#145)
* WIP: RT-alloc in RT clients (#129) * Update Realtime to pass RT allocator to clients * added link to examples folder in the guide * Wrapper: Allocatorize Part 1 * (Buf)MFCC.sc: Handle maxNumBands * (Buf)MFCC.sc: Handle maxNumBands (#130) * typo * Remove CondVar (#132) * removed from FluidWaveform * typo * Wrapper: allocatorize * Remove redundant old help files * Wrapper: Use `fmt` insetad of `std::to_chars` (STL function needs macOS >= 10.15) * CMake: Set PIC globally * ensure PIC for all libs * Readme: Correct C++ version Co-authored-by: Ted Moore <[email protected]> * correction in example code of the new NN interface * fixed example: 'Neural Network Predicts FM Params from Audio Analysis' * Feature/peaks (#143) * working frankenstein freq only * removed all the unused arguments * now with mag out * now with the buffer version * change the interface to singular like other bufSTFT * added logFreq and linMag * change of interface (sortBy to order) * last SC commit - object overview added * corrected interface and simplified some examples Co-authored-by: Owen Green <[email protected]> Co-authored-by: Ted Moore <[email protected]>
1 parent 401cc00 commit b8c057f

30 files changed

+495
-907
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ set(CMAKE_CXX_STANDARD 17)
1111
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1212
set(CMAKE_CXX_EXTENSIONS OFF)
1313
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
14-
14+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
1515
################################################################################
1616
# Paths
1717
set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/install" CACHE PATH "")
@@ -89,7 +89,7 @@ include(flucoma-buildtools)
8989
include(flucoma-buildtype)
9090
# endif()
9191

92-
option(DOCS "Generate scdocs" OFF)
92+
option(DOCS "Generate scdocs" ON)
9393
set(FLUID_DOCS_PATH "" CACHE PATH "Optional path to flucoma-docs (needed for docs); will download if absent")
9494

9595
if(DOCS)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Note that on macOS you may need to [dequarantine](https://learn.flucoma.org/inst
1010
## Pre-requisites
1111

1212

13-
* C++14 compliant compiler (clang, GCC or MSVC)
13+
* C++17 compliant compiler (clang, GCC or MSVC)
1414
* cmake
1515
* make (or Ninja or XCode or VisualStudio)
1616
* git

include/FluidSCWrapper.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,17 @@ class FluidSCWrapper : public impl::FluidSCWrapperBase<C>
7676

7777
}
7878

79-
static auto& setParams(Unit* x, ParamSetType& p,
80-
FloatControlsIter& inputs, bool constrain = false, bool initialized = true)
79+
static auto& setParams(Unit* x, ParamSetType& p, FloatControlsIter& inputs,
80+
Allocator& alloc, bool constrain = false,
81+
bool initialized = true)
8182
{
8283
bool verbose = x->mWorld->mVerbosity > 0;
8384

8485
using Reportage = decltype(static_cast<FluidSCWrapper*>(x)->mReportage);
8586

8687
Reportage* reportage = initialized ? &(static_cast<FluidSCWrapper*>(x)->mReportage) : new Reportage();
8788

88-
p.template setParameterValuesRT<ControlSetter>(verbose ? reportage: nullptr , x, inputs);
89+
p.template setParameterValuesRT<ControlSetter>(verbose ? reportage: nullptr , x, inputs, alloc);
8990
if (constrain) p.constrainParameterValuesRT(verbose ? reportage : nullptr);
9091
if(verbose)
9192
{

include/clients/rt/FluidDataSetWr.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ class DataSetWriterClient : public FluidBaseClient, OfflineIn, OfflineOut {
4646

4747
static constexpr auto &getParameterDescriptors() { return DataSetWrParams; }
4848

49-
DataSetWriterClient(ParamSetViewType &p) : mParams(p) {}
49+
DataSetWriterClient(ParamSetViewType &p, FluidContext&) : mParams(p) {}
5050

5151
template <typename T> Result process(FluidContext &) {
5252
auto dataset = get<kDataSet>().get();
5353
if (auto datasetPtr = dataset.lock()) {
54-
std::string &idPrefix = get<kIDPrefix>();
54+
std::string idPrefix = std::string(get<kIDPrefix>());
5555
auto &idNumberArr = get<kIDNumber>();
5656
if (idNumberArr.size() != 2)
5757
return {Result::Status::kError, "ID number malformed"};

include/wrapper/ArgsFromClient.hpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

33
#include "Meta.hpp"
4+
#include <data/FluidMemory.hpp>
5+
#include <fmt/format.h>
46

57
namespace fluid {
68
namespace client {
@@ -40,7 +42,11 @@ struct ParamReader<impl::FloatControlsIter>
4042

4143
using Controls = impl::FloatControlsIter;
4244

43-
static auto fromArgs(Unit* /*x*/, Controls& args, std::string, int)
45+
/// todo: fix std::string to use a specialisation with RT alloc
46+
template <typename Alloc>
47+
static auto
48+
fromArgs(Unit* /*x*/, Controls& args,
49+
std::basic_string<char, std::char_traits<char>, Alloc> const&, int)
4450
{
4551
// first is string size, then chars
4652
index size = static_cast<index>(args.next());
@@ -50,14 +56,15 @@ struct ParamReader<impl::FloatControlsIter>
5056
res[asUnsigned(i)] = static_cast<char>(args.next());
5157
return res;
5258
}
53-
59+
5460
static auto fromArgs(Unit*, Controls& args,typename LongArrayT::type&, int)
5561
{
5662
//first is array size, then items
5763
using Container = typename LongArrayT::type;
5864
using Value = typename Container::type;
5965
index size = static_cast<index>(args.next());
60-
Container res(size);
66+
/// todo: fix allocator
67+
Container res(size, FluidDefaultAllocator());
6168
for (index i = 0; i < size; ++i)
6269
res[i] = static_cast<Value>(args.next());
6370
return res;
@@ -225,7 +232,8 @@ struct ParamReader<sc_msg_iter>
225232
return argTypeOK(T{},tag);
226233
}
227234

228-
static auto fromArgs(World*, sc_msg_iter& args, std::string, int)
235+
template<typename Alloc>
236+
static auto fromArgs(World*, sc_msg_iter& args, std::basic_string<char,std::char_traits<char>,Alloc> const&, int)
229237
{
230238
const char* recv = args.gets("");
231239

@@ -285,7 +293,7 @@ struct ParamReader<sc_msg_iter>
285293
using Container = typename LongArrayT::type;
286294
using Value = typename Container::type;
287295
index size = static_cast<index>(args.geti());
288-
Container res(size);
296+
Container res(size, FluidDefaultAllocator());
289297
for (index i = 0; i < size; ++i)
290298
res[i] = static_cast<Value>(args.geti());
291299
return res;
@@ -325,14 +333,14 @@ struct ClientParams{
325333

326334
template<typename Context, typename Client = typename Wrapper::Client, size_t Number = N>
327335
std::enable_if_t<!impl::IsNamedShared_v<Client> || Number!=0, typename T::type>
328-
operator()(Context* x, ArgType& args)
336+
operator()(Context* x, ArgType& args, Allocator& alloc)
329337
{
330338
// Just return default if there's nothing left to grab
331339
if (args.remain() == 0)
332340
{
333341
std::cout << "WARNING: " << Wrapper::getName()
334342
<< " received fewer parameters than expected\n";
335-
return Wrapper::Client::getParameterDescriptors().template makeValue<N>();
343+
return Wrapper::Client::getParameterDescriptors().template makeValue<N>(alloc);
336344
}
337345

338346
ParamLiteralConvertor<T, argSize> a;
@@ -348,18 +356,25 @@ struct ClientParams{
348356

349357
template<typename Context, typename Client = typename Wrapper::Client, size_t Number = N>
350358
std::enable_if_t<impl::IsNamedShared_v<Client> && Number==0, typename T::type>
351-
operator()(Context* x, ArgType& args)
359+
operator()(Context* x, ArgType& args, Allocator& alloc)
352360
{
353361
// Just return default if there's nothing left to grab
354362
if (args.remain() == 0)
355363
{
356364
std::cout << "WARNING: " << Wrapper::getName()
357365
<< " received fewer parameters than expected\n";
358-
return Wrapper::Client::getParameterDescriptors().template makeValue<N>();
366+
return Wrapper::Client::getParameterDescriptors().template makeValue<N>(alloc);
359367
}
360368

361369
index id = ParamReader<ArgType>::fromArgs(x,args,index{},0);
362-
return std::to_string(id);
370+
using StdAlloc = foonathan::memory::std_allocator<char, Allocator>;
371+
using fmt_memory_buffer =
372+
fmt::basic_memory_buffer<char, fmt::inline_buffer_size, StdAlloc>;
373+
auto buf = fmt_memory_buffer(alloc);
374+
std::string_view fmt_string("{}");
375+
fmt::vformat_to(std::back_inserter(buf), fmt_string,
376+
fmt::make_format_args(id));
377+
return rt::string(buf.data(), buf.size(), alloc);
363378
}
364379
};
365380

include/wrapper/ArgsToClient.hpp

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <data/FluidMemory.hpp>
4+
35
namespace fluid {
46
namespace client {
57

@@ -15,20 +17,20 @@ namespace client {
1517
return 1;
1618
}
1719

18-
static index allocSize(std::string s)
20+
static index allocSize(std::string const& s)
1921
{
2022
return asSigned(s.size()) + 1;
2123
} // put null char at end when we send
2224

23-
static index allocSize(FluidTensor<std::string, 1> s)
25+
static index allocSize(FluidTensor<std::string, 1> const& s)
2426
{
2527
index count = 0;
2628
for (auto& str : s) count += (str.size() + 1);
2729
return count;
2830
}
2931

3032
template <typename T>
31-
static index allocSize(FluidTensor<T, 1> s)
33+
static index allocSize(FluidTensor<T, 1> const& s)
3234
{
3335
return s.size();
3436
}
@@ -68,12 +70,12 @@ namespace client {
6870
f[0] = static_cast<float>(x);
6971
}
7072

71-
static void convert(float* f, std::string s)
73+
static void convert(float* f, std::string const& s)
7274
{
7375
std::copy(s.begin(), s.end(), f);
7476
f[s.size()] = 0; // terminate
7577
}
76-
static void convert(float* f, FluidTensor<std::string, 1> s)
78+
static void convert(float* f, FluidTensor<std::string, 1> const& s)
7779
{
7880
for (auto& str : s)
7981
{
@@ -83,7 +85,7 @@ namespace client {
8385
}
8486
}
8587
template <typename T>
86-
static void convert(float* f, FluidTensor<T, 1> s)
88+
static void convert(float* f, FluidTensor<T, 1> const& s)
8789
{
8890
static_assert(std::is_convertible<T, float>::value,
8991
"Can't convert this to float output");
@@ -114,19 +116,24 @@ namespace client {
114116
return 1;
115117
}
116118

117-
static index numTags(std::string)
119+
static index numTags(rt::string const&)
120+
{
121+
return 1;;
122+
}
123+
124+
static index numTags(std::string const&)
118125
{
119126
return 1;;
120127
}
121128

122129
template <typename T>
123-
static index numTags(FluidTensor<T, 1> s)
130+
static index numTags(FluidTensor<T, 1> const& s)
124131
{
125132
return s.size();
126133
}
127134

128135
template <typename... Ts>
129-
static index numTags(std::tuple<Ts...>&& t)
136+
static index numTags(std::tuple<Ts...> const& t)
130137
{
131138
index count = 0;
132139
ForEach(t,[&count](auto& x){ count += numTags(x);});
@@ -143,18 +150,19 @@ namespace client {
143150
static std::enable_if_t<std::is_floating_point<std::decay_t<T>>::value>
144151
getTag(Packet& p, T&&) { p.addtag('f'); }
145152

146-
static void getTag (Packet& p, std::string) { p.addtag('s'); }
153+
static void getTag (Packet& p, std::string const&) { p.addtag('s'); }
154+
static void getTag (Packet& p, rt::string const&) { p.addtag('s'); }
147155

148156
template <typename T>
149-
static void getTag(Packet& p, FluidTensor<T, 1> x)
157+
static void getTag(Packet& p, FluidTensor<T, 1> const& x)
150158
{
151159
T dummy{};
152160
for (int i = 0; i < x.rows(); i++)
153161
getTag(p, dummy);
154162
}
155163

156164
template <typename... Ts>
157-
static void getTag(Packet& p, std::tuple<Ts...>&& t)
165+
static void getTag(Packet& p, std::tuple<Ts...> const& t)
158166
{
159167
ForEach(t,[&p](auto& x){getTag(p,x);});
160168
}
@@ -179,19 +187,24 @@ namespace client {
179187
p.addf(static_cast<float>(x));
180188
}
181189

182-
static void convert(Packet& p, std::string s)
190+
static void convert(Packet& p, std::string const& s)
191+
{
192+
p.adds(s.c_str());
193+
}
194+
195+
static void convert(Packet& p, rt::string const& s)
183196
{
184197
p.adds(s.c_str());
185198
}
186199

187200
template <typename T>
188-
static void convert(Packet& p, FluidTensor<T, 1> s)
201+
static void convert(Packet& p, FluidTensor<T, 1> const& s)
189202
{
190203
for(auto& x: s) convert(p,x);
191204
}
192205

193206
template <typename... Ts>
194-
static void convert(Packet& p, std::tuple<Ts...>&& t)
207+
static void convert(Packet& p, std::tuple<Ts...> const& t)
195208
{
196209
ForEach(t,[&p](auto& x){ convert(p,x);});
197210
}

include/wrapper/Messaging.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ struct FluidSCMessaging{
154154

155155
static void refreshParams(Params& p, MessageResult<ParamValues>& r)
156156
{
157-
p.fromTuple(ParamValues(r));
157+
p.fromTuple(r.value());
158158
}
159159

160160
template<typename T>
@@ -253,7 +253,7 @@ struct FluidSCMessaging{
253253
template <typename T> // call from RT
254254
static void messageOutput(const std::string& s, index id, MessageResult<T>& result, void* replyAddr)
255255
{
256-
index numTags = ToOSCTypes<small_scpacket>::numTags(static_cast<T>(result));
256+
index numTags = ToOSCTypes<small_scpacket>::numTags(result.value());
257257
if(numTags > 2048)
258258
{
259259
std::cout << "ERROR: Message response too big to send (" << asUnsigned(numTags) * sizeof(float) << " bytes)." << std::endl;
@@ -290,9 +290,7 @@ struct FluidSCMessaging{
290290
template <typename... Ts>
291291
static void messageOutput(const std::string& s, index id, MessageResult<std::tuple<Ts...>>& result, void* replyAddr)
292292
{
293-
using T = std::tuple<Ts...>;
294-
295-
index numTags = ToOSCTypes<small_scpacket>::numTags(static_cast<T>(result));
293+
index numTags = ToOSCTypes<small_scpacket>::numTags(result.value());
296294
if(numTags > 2048)
297295
{
298296
std::cout << "ERROR: Message response too big to send (" << asUnsigned(numTags) * sizeof(float) << " bytes)." << std::endl;
@@ -304,10 +302,10 @@ struct FluidSCMessaging{
304302
packet.maketags(static_cast<int>(numTags + 2));
305303
packet.addtag(',');
306304
packet.addtag('i');
307-
ToOSCTypes<small_scpacket>::getTag(packet,static_cast<T>(result));
305+
ToOSCTypes<small_scpacket>::getTag(packet,result.value());
308306

309307
packet.addi(static_cast<int>(id));
310-
ToOSCTypes<small_scpacket>::convert(packet, static_cast<T>(result));
308+
ToOSCTypes<small_scpacket>::convert(packet, result.value());
311309

312310
if(replyAddr)
313311
SendReply(replyAddr,packet.data(),static_cast<int>(packet.size()));

0 commit comments

Comments
 (0)