From 02753c516362a31364bfee87aa823e78da5e05c7 Mon Sep 17 00:00:00 2001
From: Nodeful <romankisilo@gmail.com>
Date: Mon, 4 Oct 2021 18:53:59 +0300
Subject: [PATCH] fxies to the driver cleanup routine

---
 .../driver/Driver.xcodeproj/project.pbxproj   |  2 +
 native/driver/Source/EQMDevice.swift          | 43 +++++++++++--------
 2 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/native/driver/Driver.xcodeproj/project.pbxproj b/native/driver/Driver.xcodeproj/project.pbxproj
index 16ebd543..db308ac0 100644
--- a/native/driver/Driver.xcodeproj/project.pbxproj
+++ b/native/driver/Driver.xcodeproj/project.pbxproj
@@ -461,6 +461,7 @@
 					"$(inherited)",
 					"@executable_path/../Frameworks",
 					"@loader_path/../Frameworks",
+					"/Library/Audio/Plug-Ins/HAL/eqMac.driver/Contents/Frameworks",
 				);
 				MACOSX_DEPLOYMENT_TARGET = 10.10;
 				MARKETING_VERSION = 1.3.0;
@@ -533,6 +534,7 @@
 					"$(inherited)",
 					"@executable_path/../Frameworks",
 					"@loader_path/../Frameworks",
+					"/Library/Audio/Plug-Ins/HAL/eqMac.driver/Contents/Frameworks",
 				);
 				MACOSX_DEPLOYMENT_TARGET = 10.10;
 				MARKETING_VERSION = 1.3.0;
diff --git a/native/driver/Source/EQMDevice.swift b/native/driver/Source/EQMDevice.swift
index a9da6fe7..f0962fd4 100644
--- a/native/driver/Source/EQMDevice.swift
+++ b/native/driver/Source/EQMDevice.swift
@@ -592,6 +592,10 @@ class EQMDevice: EQMObject {
 
   static func doIO (client: EQMClient?, operationID: UInt32, sample: UnsafeMutablePointer<Float32>, cycleInfo: AudioServerPlugInIOCycleInfo, frameSize: UInt32) -> OSStatus {
 
+    guard let buffer = ringBuffer else {
+      return noErr
+    }
+
     ioMutex.lock()
 
     switch operationID {
@@ -601,21 +605,22 @@ class EQMDevice: EQMObject {
       for frame in 0 ..< frameSize {
         for channel in 0 ..< kChannelCount {
           let readFrame = Int(frame * kChannelCount + channel)
+
           if EQMControl.muted {
             // Muted
             sample[readFrame] = 0
-          } else {
-            let nextSampleTime = sampleTime + Int(frame)
-            let remainder = nextSampleTime % Int(ringBufferSize)
-            let writeFrame = remainder * Int(kChannelCount) + Int(channel)
-            ringBuffer![writeFrame] += sample[readFrame]
           }
 
+          let writePosition = sampleTime + Int(frame)
+          let writeRemainder = writePosition % Int(ringBufferSize)
+          let writeFrame = writeRemainder * Int(kChannelCount) + Int(channel)
+          buffer[writeFrame] += sample[readFrame]
+
           // Clean up buffer
-          let cleanFromFrame = sampleTime + Int(frame) + 8192
-          let remainder = cleanFromFrame % Int(ringBufferSize)
-          let cleanFrame = remainder * Int(kChannelCount) + Int(channel)
-          ringBuffer![cleanFrame] = 0
+          let cleanCleanPosition = sampleTime + Int(frame) + 8192
+          let cleanRemainder = cleanCleanPosition % Int(ringBufferSize)
+          let cleanFrame = cleanRemainder * Int(kChannelCount) + Int(channel)
+          buffer[cleanFrame] = 0
         }
       }
 
@@ -629,20 +634,24 @@ class EQMDevice: EQMObject {
       for frame in 0 ..< frameSize {
         for channel in 0 ..< kChannelCount {
           let writeFrame = Int(frame * kChannelCount + channel)
+
           if EQMControl.muted {
             sample[writeFrame] = 0
           } else {
-            let nextSampleTime = sampleTime + Int(frame)
-            let remainder = nextSampleTime % Int(ringBufferSize)
-            let readFrame = remainder * Int(kChannelCount) + Int(channel)
-            sample[writeFrame] = ringBuffer![readFrame]
+            let readPosition = sampleTime + Int(frame)
+            let readRemainder = readPosition % Int(ringBufferSize)
+            let readFrame = readRemainder * Int(kChannelCount) + Int(channel)
+            sample[writeFrame] = buffer[readFrame]
           }
 
           // Clean up buffer
-          let cleanFromFrame = sampleTime + Int(frame) - Int(ringBufferSize)
-          let remainder = cleanFromFrame % Int(ringBufferSize)
-          let cleanFrame = remainder * Int(kChannelCount) + Int(channel)
-          ringBuffer![cleanFrame] = 0
+          let cleanPosition = sampleTime + Int(frame) - Int(ringBufferSize)
+          if (cleanPosition > 0) {
+            let cleanRemainder = cleanPosition % Int(ringBufferSize)
+            let cleanFrame = cleanRemainder * Int(kChannelCount) + Int(channel)
+            buffer[cleanFrame] = 0
+          }
+
         }
       }
       break