Take this simple C++ ring buffer implementation:
#define count 10*512
class CRingBuffer {
float samples[count];
int readPos = 0;
int writePos = 0;
void put(float sample) {
samples[writePos++ % count] = sample;
}
float get() {
return samples[readPos++ % count];
}
};
If you naïvely convert it to swift:
class RingBuffer {
static let count = 10*512
var buffer: [Float]
var writePos = 0
var readPos = 0
init() {
buffer = [Float](repeating: 0, count: Self.count)
}
func put(_ v: Float) {
buffer[writePos % Self.count] = v
writePos += 1
}
func get() -> Float {
let v = buffer[readPos % Self.count]
readPos += 1
return v
}
}
you will have an implementation which has retain/release in its get/put methods (which can take a mutex lock) -- not good for use in real time code: see old but good links: link1 (49:12 -- 50:50), link2 and link3. That's the kind of minefields I am talking about. If you are extra careful (switch to unsafe pointers and verify the resulting asm (which can change from one swift version to another)) - yes, you can link4 (29:00 -- 32:40, 37:50 -- 42:25).