Skip to content

Commit

Permalink
[add] BandPass, TanhDistortion
Browse files Browse the repository at this point in the history
  • Loading branch information
iarata committed Jul 26, 2021
1 parent bd0230e commit e5328d2
Show file tree
Hide file tree
Showing 18 changed files with 652 additions and 238 deletions.
54 changes: 37 additions & 17 deletions DSP Playground.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
BB20190726ACC415000F2753 /* FilterModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB20190626ACC415000F2753 /* FilterModel.swift */; };
BB20190926AD0A77000F2753 /* LowPassFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB20190826AD0A77000F2753 /* LowPassFilter.swift */; };
BB20190B26AD0CB1000F2753 /* Reverb.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB20190A26AD0CB1000F2753 /* Reverb.swift */; };
BB20190F26AE268E000F2753 /* TanhDistortion.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB20190E26AE268E000F2753 /* TanhDistortion.swift */; };
BB20191126AE26C4000F2753 /* BandPassButterworthFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB20191026AE26C4000F2753 /* BandPassButterworthFilter.swift */; };
BB20191326AE2A91000F2753 /* Conductor.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB20191226AE2A91000F2753 /* Conductor.swift */; };
BB24F41226A3396300CF3AD7 /* DSPObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB24F41126A3396300CF3AD7 /* DSPObject.swift */; };
BB24F41426A34B5700CF3AD7 /* DSPNotification.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB24F41326A34B5700CF3AD7 /* DSPNotification.swift */; };
BB24F41626A3692700CF3AD7 /* Extentions.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB24F41526A3692700CF3AD7 /* Extentions.swift */; };
Expand All @@ -39,7 +42,6 @@
BB24F42D26A46E6100CF3AD7 /* AudioInspect.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB24F42C26A46E6100CF3AD7 /* AudioInspect.swift */; };
BB24F43526A55CE200CF3AD7 /* AudioModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB24F43426A55CE200CF3AD7 /* AudioModel.swift */; };
BB24F43A26A5B98100CF3AD7 /* PythonPlot.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB24F43926A5B98100CF3AD7 /* PythonPlot.swift */; };
BB24F43D26A5BB8800CF3AD7 /* PythonKit in Frameworks */ = {isa = PBXBuildFile; productRef = BB24F43C26A5BB8800CF3AD7 /* PythonKit */; };
BB24F44026A5BC0E00CF3AD7 /* PythonKitTest.py in Resources */ = {isa = PBXBuildFile; fileRef = BB24F43F26A5BC0E00CF3AD7 /* PythonKitTest.py */; };
BB24F44326A5FFE200CF3AD7 /* VerticalBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB24F44226A5FFE200CF3AD7 /* VerticalBar.swift */; };
BB24F44526A600C700CF3AD7 /* AmplitudeVisualizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB24F44426A600C700CF3AD7 /* AmplitudeVisualizer.swift */; };
Expand Down Expand Up @@ -92,6 +94,9 @@
BB20190626ACC415000F2753 /* FilterModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterModel.swift; sourceTree = "<group>"; };
BB20190826AD0A77000F2753 /* LowPassFilter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LowPassFilter.swift; sourceTree = "<group>"; };
BB20190A26AD0CB1000F2753 /* Reverb.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Reverb.swift; sourceTree = "<group>"; };
BB20190E26AE268E000F2753 /* TanhDistortion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TanhDistortion.swift; sourceTree = "<group>"; };
BB20191026AE26C4000F2753 /* BandPassButterworthFilter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BandPassButterworthFilter.swift; sourceTree = "<group>"; };
BB20191226AE2A91000F2753 /* Conductor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Conductor.swift; sourceTree = "<group>"; };
BB24F41126A3396300CF3AD7 /* DSPObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DSPObject.swift; sourceTree = "<group>"; };
BB24F41326A34B5700CF3AD7 /* DSPNotification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DSPNotification.swift; sourceTree = "<group>"; };
BB24F41526A3692700CF3AD7 /* Extentions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extentions.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -140,7 +145,6 @@
BB2018EC26AC5164000F2753 /* AudioKitUI in Frameworks */,
BB2018F626AC65EF000F2753 /* AudioKit in Frameworks */,
BB2018F326AC6558000F2753 /* SoundpipeAudioKit in Frameworks */,
BB24F43D26A5BB8800CF3AD7 /* PythonKit in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -168,6 +172,8 @@
BB2018ED26AC6357000F2753 /* FilterInspect.swift */,
BB2018EF26AC6517000F2753 /* HighPassFilter.swift */,
BB20190826AD0A77000F2753 /* LowPassFilter.swift */,
BB20191026AE26C4000F2753 /* BandPassButterworthFilter.swift */,
BB20190E26AE268E000F2753 /* TanhDistortion.swift */,
BB20190A26AD0CB1000F2753 /* Reverb.swift */,
);
path = Filter;
Expand Down Expand Up @@ -203,6 +209,7 @@
BB24F43426A55CE200CF3AD7 /* AudioModel.swift */,
BB20190626ACC415000F2753 /* FilterModel.swift */,
BB16D81526A9E597004370C1 /* SampleConductor.swift */,
BB20191226AE2A91000F2753 /* Conductor.swift */,
);
path = Audio;
sourceTree = "<group>";
Expand Down Expand Up @@ -381,14 +388,14 @@
BB2726AC26A1C371003BA560 /* Sources */,
BB2726AD26A1C371003BA560 /* Frameworks */,
BB2726AE26A1C371003BA560 /* Resources */,
BB20191426AE4E61000F2753 /* ShellScript */,
);
buildRules = (
);
dependencies = (
);
name = "DSP Playground";
packageProductDependencies = (
BB24F43C26A5BB8800CF3AD7 /* PythonKit */,
BB20189326AB23AE000F2753 /* Charts */,
BB2018EB26AC5164000F2753 /* AudioKitUI */,
BB2018F226AC6558000F2753 /* SoundpipeAudioKit */,
Expand Down Expand Up @@ -466,7 +473,6 @@
);
mainGroup = BB2726A726A1C371003BA560;
packageReferences = (
BB24F43B26A5BB8800CF3AD7 /* XCRemoteSwiftPackageReference "PythonKit" */,
BB20189226AB23AE000F2753 /* XCRemoteSwiftPackageReference "swiftui-charts" */,
BB2018EA26AC5164000F2753 /* XCRemoteSwiftPackageReference "AudioKitUI" */,
BB2018F126AC6558000F2753 /* XCRemoteSwiftPackageReference "SoundpipeAudioKit" */,
Expand Down Expand Up @@ -510,6 +516,26 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
BB20191426AE4E61000F2753 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "buildNumber=$(/usr/libexec/PlistBuddy -c \"Print CFBundleVersion\" \"${PROJECT_DIR}/${INFOPLIST_FILE}\")\nbuildNumber=$(($buildNumber + 1))\n/usr/libexec/PlistBuddy -c \"Set :CFBundleVersion $buildNumber\" \"${PROJECT_DIR}/${INFOPLIST_FILE}\"\n";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
BB2726AC26A1C371003BA560 /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand All @@ -527,10 +553,12 @@
BB24F41A26A36BD700CF3AD7 /* OutputDeviceOV.swift in Sources */,
BB20190526AC9E21000F2753 /* VisualEffectBlur.swift in Sources */,
BB2018FF26AC67FB000F2753 /* Cookbook.swift in Sources */,
BB20191126AE26C4000F2753 /* BandPassButterworthFilter.swift in Sources */,
BB24F42D26A46E6100CF3AD7 /* AudioInspect.swift in Sources */,
BB2726B626A1C371003BA560 /* ContentView.swift in Sources */,
BB16D81626A9E597004370C1 /* SampleConductor.swift in Sources */,
BB20190726ACC415000F2753 /* FilterModel.swift in Sources */,
BB20191326AE2A91000F2753 /* Conductor.swift in Sources */,
BB20189126AB0798000F2753 /* MetalTest.swift in Sources */,
BB24F41826A36A8E00CF3AD7 /* FilterOV.swift in Sources */,
BB24F42A26A46C2B00CF3AD7 /* TimeDomain.swift in Sources */,
Expand All @@ -545,6 +573,7 @@
BB2018FE26AC67FB000F2753 /* ParameterSlider.swift in Sources */,
BB2018F026AC6517000F2753 /* HighPassFilter.swift in Sources */,
BB24F42726A4390600CF3AD7 /* Inspector.swift in Sources */,
BB20190F26AE268E000F2753 /* TanhDistortion.swift in Sources */,
BB2726F026A2E776003BA560 /* ExtraTEST.swift in Sources */,
BB24F43A26A5B98100CF3AD7 /* PythonPlot.swift in Sources */,
BB20190326AC67FB000F2753 /* ColorManager.swift in Sources */,
Expand Down Expand Up @@ -713,6 +742,7 @@
CODE_SIGN_ENTITLEMENTS = "DSP Playground/DSP_Playground.entitlements";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"DSP Playground/Preview Content\"";
DEVELOPMENT_TEAM = 9RU52AC58E;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -723,6 +753,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 11.0;
MARKETING_VERSION = 1.0.6;
PRODUCT_BUNDLE_IDENTIFIER = studio.arata.DSPPlayground;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -737,6 +768,7 @@
CODE_SIGN_ENTITLEMENTS = "DSP Playground/DSP_Playground.entitlements";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"DSP Playground/Preview Content\"";
DEVELOPMENT_TEAM = 9RU52AC58E;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -747,6 +779,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 11.0;
MARKETING_VERSION = 1.0.6;
PRODUCT_BUNDLE_IDENTIFIER = studio.arata.DSPPlayground;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -911,14 +944,6 @@
minimumVersion = 5.2.0;
};
};
BB24F43B26A5BB8800CF3AD7 /* XCRemoteSwiftPackageReference "PythonKit" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/pvieito/PythonKit";
requirement = {
branch = master;
kind = branch;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand All @@ -942,11 +967,6 @@
package = BB2018F426AC65EF000F2753 /* XCRemoteSwiftPackageReference "AudioKit" */;
productName = AudioKit;
};
BB24F43C26A5BB8800CF3AD7 /* PythonKit */ = {
isa = XCSwiftPackageProductDependency;
package = BB24F43B26A5BB8800CF3AD7 /* XCRemoteSwiftPackageReference "PythonKit" */;
productName = PythonKit;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = BB2726A826A1C371003BA560 /* Project object */;
Expand Down
14 changes: 13 additions & 1 deletion DSP Playground/DSP_Playground.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.cs.disable-library-validation</key>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.assets.movies.read-write</key>
<true/>
<key>com.apple.security.assets.music.read-write</key>
<true/>
<key>com.apple.security.assets.pictures.read-write</key>
<true/>
<key>com.apple.security.device.audio-input</key>
<true/>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<key>com.apple.security.files.downloads.read-write</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
</dict>
</plist>
4 changes: 2 additions & 2 deletions DSP Playground/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>861</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
</dict>
Expand Down
36 changes: 28 additions & 8 deletions DSP Playground/Presenters/Audio/AudioModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,42 @@ class AudioModelManager: ObservableObject {
DSPNotification().inspectUpdate(object: self)
}

func add(models: [AudioModel]) {
let data = models.map { try? JSONEncoder().encode($0) }
UserDefaults.standard.set(data, forKey: keyForAudioModels)
self.audioModels = getAll()
DSPNotification().inspectUpdate(object: self)
}

func updateD(modelID: UUID, duration: Double) {
let prevData = getAll()
var newData = [AudioModel]()
for model in prevData {
if model.id == modelID {
newData.append(AudioModel(objectID: model.objectID, path: model.path, duration: duration))
} else {
newData.append(model)
}
}
self.add(models: newData)
DSPNotification().inspectUpdate(object: self)
}

// MARK: Get All Audio Models List
func getAll() -> [AudioModel] {
guard let encodedData = UserDefaults.standard.array(forKey: keyForAudioModels) as? [Data] else { return [] }
return encodedData.map { try! JSONDecoder().decode(AudioModel.self, from: $0)}
}

// MARK: Get an AudioModel
func get(id: UUID) -> AudioModel {
func get(id: UUID) -> AudioModel? {
let data = getAll()
var result = AudioModel(objectID: UUID(), path: "", duration: 0.0)
for model in data {
if model.id == id {
result = model
}
let result = data.filter { $0.id == id }
if result.isNotEmpty {
return result.first
} else {
return nil
}

return result
}
func get(dspID: UUID) -> AudioModel {
let data = getAll()
Expand All @@ -82,5 +101,6 @@ class AudioModelManager: ObservableObject {
}
}

self.add(models: newData)
}
}
86 changes: 86 additions & 0 deletions DSP Playground/Presenters/Audio/Conductor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// Conductor.swift
// DSP Playground
//
// Created by Alireza Hajebrahimi on 2021/07/26.
//

import AudioKit
import AudioKitUI
import AudioKitEX
import AVFoundation
import SoundpipeAudioKit
import SwiftUI


struct ConductorData {
var isPlaying = false
var isPaused = false
var volume: AUValue = 1
var balance: AUValue = 0.5
var gain: AUValue = 10.0
}

class Conductor: ObservableObject, ProcessesPlayerInput {

var engine = AudioEngine()
var player = AudioPlayer()
var buffer: AVAudioPCMBuffer?
var filter: Fader?
var dryWetMixer: DryWetMixer?

@Published var data = ConductorData() {
didSet {
data.isPlaying ? player.play() : player.stop()
data.isPaused = player.isPaused
player.volume = data.volume
filter!.gain = data.gain
dryWetMixer!.balance = data.balance
}
}

var audioFileURL: String?


func initFrom(audioFile: String) {
audioFileURL = audioFile
buffer = Cookbook().sourceBuffer(url: audioFileURL!)
player.buffer = buffer
player.file = try! AVAudioFile(forReading: URL(string: audioFileURL!)!)
player.isLooping = true
self.filter = Fader(player)
self.dryWetMixer = DryWetMixer(player, filter!)
engine.output = dryWetMixer
}

func start() {
do {
try engine.start()
} catch let err {
Log(err)
}
}

func selectFile() {
let panel = NSOpenPanel()
panel.allowsMultipleSelection = false
panel.canChooseDirectories = false
panel.title = "Select an audio file"
panel.allowedFileTypes = ["mp3", "aac", "wav", "flac", "alac", "dsd"]
if panel.runModal() == .OK {
self.audioFileURL = panel.url!.absoluteString
}
}

func play() {
player.play()
}

func pause() {
player.pause()
}

func stop() {
engine.stop()
}
}
12 changes: 12 additions & 0 deletions DSP Playground/Presenters/Audio/FilterModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ enum FilterType: String, Codable {
case highPass = "HighPass"
case lowPass = "LowPass"
case reverb = "Reverb"
case BandPass = "BandPass"
case TanhDistortion = "TanhDistortion"
}


Expand Down Expand Up @@ -116,4 +118,14 @@ class FilterModelManager: ObservableObject {
}

}

func remove(id: UUID) {
let prevData = getAll()
var newData = [FilterModel]()
for model in prevData {
if model.id != id {
newData.append(model)
}
}
}
}
1 change: 1 addition & 0 deletions DSP Playground/Presenters/Objects/ObjectManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class ObjectManager: ObservableObject {
}

saveObject(newData)
DSPNotification().update(object: self)
}

// MARK: Delete One
Expand Down
Loading

0 comments on commit e5328d2

Please sign in to comment.