SmoothReversal - New Ugen - Also, new C++ buffer interface?


Follow this thread Back and forth playback of a sample --> avoiding singularity started by @moscardo, I made a Ugen the reverses playback of a buffer when the derivative is small to reduce (or eliminate) clicking at the expense of latency.


It looks like this…

s.waitForBoot {
	~buffer = ~buffer ?? {, Platform.resourceDir +/+ "sounds/a11wlk01.wav")};
	try {}{};
	x = {
			bufnum: ~buffer, 
			switchDirectionTrigger: > 0.5, 
			threshold: -15.dbamp			

C++ buffer interface

As a side thing that might needs its own thread. I was reading the implementation of BufRd - a lot of tears were shed - but I think I’ve come up with a nicer way of using buffers in C++. Its nicer because:
there are no exposed macros, the locks are done in raii, if it fails it returns an optional, you can lerp with a member function.

This is what the usage looks like to use…

//.. try to create a buffer factory - will also update if buf num changed
const auto buffer_factory = scoped_snd_buf_manager.try_to_load_snd_buf_factory(
		static_cast<uint32_t>(in0(0)), // first arg
 		mNumOutputs,  // checks channels match
  		mDone == 0
 if (!buffer_factory) {
    // it failed one of the checks (bufnum invalid, channel mismatch...etc)
 	ClearUnitOutputs(this, num_samples);

// acquire raii locked buffer
auto buf{buffer_factory->buf()};

buf.frames(); // samples(), sampleRate().... along with the other SndBuf things

// it also supports lerping with none of that dumb LOOP_BODY_4 macro injecting vars around...
buf.cubic_lerp(phase, 0); // phase is double, 0 is channel number

I needed three classes in the end:

  • ScopedSndBuf RAII locked buffer, provides nice interface.
  • ScopedSndBufFactory manages changing bufnums, and making the above class
  • ScopedSndBufManager tries to make a factory if the args are valid.

I don’t know if this might be of interest? All classes are very lightweight see : smoothreversal/SmoothReversal.hpp at main · JordanHendersonMusic/smoothreversal · GitHub

I also don’t know if this already exists, seems like it should if not!