-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
652 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.