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

SmoothReversal

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.

Quarks.install("https://github.com/JordanHendersonMusic/smoothreversal.git")

It looks like this…

(
s.waitForBoot {
	~buffer = ~buffer ?? {Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav")};
	s.sync;
	
	try {x.free}{};
	
	x = {
		SmoothReversal.ar(
			bufnum: ~buffer, 
			playbackRate: K2A.ar(BufRateScale.kr(~buffer)), 
			switchDirectionTrigger: Dust.ar(1) > 0.5, 
			threshold: -15.dbamp			
		)
	}.play;	
}
)

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
 		*mWorld,
 		mNumOutputs,  // checks channels match
  		mDone == 0
);
 if (!buffer_factory) {
    // it failed one of the checks (bufnum invalid, channel mismatch...etc)
 	ClearUnitOutputs(this, num_samples);
	return;
 }

// 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!

6 Likes