Last active
September 14, 2022 16:21
-
-
Save johndpope/059f8aaaafab14b28756fa47558102e5 to your computer and use it in GitHub Desktop.
SongProcessor AudioKit - hack to play multiple files at once
This file contains hidden or 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
// | |
// Mixer.swift | |
// SongProcessor | |
// | |
// Created by Pope, John on 8/10/17. | |
// Copyright © 2017 AudioKit. All rights reserved. | |
// | |
import Foundation | |
import AudioKit | |
extension AKAudioPlayer{ | |
func isMuted()->Bool{ | |
if (self.volume > 0){ | |
return false | |
}else{ | |
return true | |
} | |
} | |
func unmute(){ | |
self.volume = 1 | |
} | |
func mute(){ | |
self.volume = 0 | |
} | |
} | |
class JPMixer{ | |
static let shared = JPMixer() | |
var audioFiles: [AKAudioFile] = [] | |
var audioPlayers: [AKAudioPlayer] = [] | |
var mixer:AKMixer! | |
var audioFilePlayer: [AKAudioPlayer] = [] | |
func build()->AKMixer?{ | |
if let files = try? FileManager.default.contentsOfDirectory(atPath: Bundle.main.bundlePath ){ | |
for file in files { | |
if file.hasSuffix("caf"){ | |
if let audioFile = try? AKAudioFile(readFileName:file, baseDir: .resources){ | |
audioFiles.append(audioFile) | |
if let audioFilePlayer = try? AKAudioPlayer(file: audioFile) | |
{ | |
audioFilePlayer.looping = true | |
audioPlayers.append(audioFilePlayer) | |
}else{ | |
print("FAILED to add fileplayer") | |
} | |
}else{ | |
print("FAILED to find file") | |
} | |
} | |
} | |
} | |
mixer = AKMixer(audioPlayers) | |
mixer.stop() | |
return mixer | |
} | |
func playMix(){ | |
mixer.stop() | |
print("isStarted:",mixer.isStarted) | |
print("isPlaying:",mixer.isPlaying) | |
for players in audioPlayers{ | |
players.play() | |
} | |
// failed attempt to have all players play at same time. | |
for players in audioPlayers{ | |
players.startTime = 0 | |
} | |
print("isStarted:",mixer.isStarted) | |
print("isPlaying:",mixer.isPlaying) | |
mixer.start() | |
} | |
func playFile(_ fileName:String){ | |
for players in audioPlayers{ | |
let fullname = "\(fileName).caf" | |
if (players.audioFile.fileName == fileName){ | |
players.unmute() | |
}else if (players.audioFile.fileName == fullname){ | |
players.unmute() | |
} | |
} | |
} | |
} |
This file contains hidden or 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
// | |
// SongProcessor.swift | |
// SongProcessor | |
// | |
// Created by Aurelius Prochazka on 6/22/16. | |
// Copyright © 2016 AudioKit. All rights reserved. | |
// | |
import UIKit | |
import AudioKit | |
import MediaPlayer | |
class SongProcessor { | |
static let sharedInstance = SongProcessor() | |
var audioFile: AKAudioFile! | |
var mixer:AKMixer! | |
var audioFilePlayer: AKAudioPlayer! | |
var variableDelay: AKVariableDelay! | |
var delayMixer: AKDryWetMixer! | |
var moogLadder: AKMoogLadder! | |
var filterMixer: AKDryWetMixer! | |
var reverb: AKCostelloReverb! | |
var reverbMixer: AKDryWetMixer! | |
var pitchShifter: AKPitchShifter! | |
var pitchMixer: AKDryWetMixer! | |
var bitCrusher: AKBitCrusher! | |
var bitCrushMixer: AKDryWetMixer! | |
var playerBooster: AKBooster! | |
var currentSong: MPMediaItem? | |
var isPlaying: Bool? | |
init() { | |
audioFile = try? AKAudioFile(readFileName: "Bass.caf", baseDir: .resources) | |
audioFilePlayer = try? AKAudioPlayer(file: audioFile) | |
audioFilePlayer?.looping = true | |
startVariableDelay() | |
startMoogLadder() | |
startCostelloReverb() | |
startPitchShifting() | |
startBitCrushing() | |
//Booster for Volume | |
//playerBooster = AKBooster(bitCrushMixer, gain: 0.5) | |
// Mixer | |
if let jpMixer = JPMixer.shared.build(){ | |
mixer = jpMixer | |
} | |
AudioKit.output = mixer | |
AudioKit.start() | |
} | |
func startVariableDelay() { | |
variableDelay = AKVariableDelay(audioFilePlayer) | |
variableDelay?.rampTime = 0.2 | |
delayMixer = AKDryWetMixer(audioFilePlayer, variableDelay, balance: 0) | |
} | |
func startMoogLadder() { | |
moogLadder = AKMoogLadder(delayMixer) | |
filterMixer = AKDryWetMixer(delayMixer, moogLadder, balance: 0) | |
} | |
func startCostelloReverb() { | |
reverb = AKCostelloReverb(filterMixer) | |
reverbMixer = AKDryWetMixer(filterMixer, reverb, balance: 0) | |
} | |
func startPitchShifting() { | |
pitchShifter = AKPitchShifter(reverbMixer) | |
pitchMixer = AKDryWetMixer(reverbMixer, pitchShifter, balance: 0) | |
} | |
func startBitCrushing() { | |
bitCrusher = AKBitCrusher(pitchMixer) | |
bitCrusher?.bitDepth = 16 | |
bitCrusher?.sampleRate = 3_333 | |
bitCrushMixer = AKDryWetMixer(pitchMixer, bitCrusher, balance: 0) | |
} | |
} |
try the develop branch of audiokit
https://github.com/AudioKit/AudioKit/tree/develop/Examples/iOS/SongProcessor/SongProcessor
I got distracted with work haven't had a chance to progress things.
I do know that the AmazingAudioEngine worked flawlessly in obj-c / I could load > 300mb of caf files no problem.
Tell me that this works for you. if you find a bug - log an issue - audiokit team are actively working on this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi John, I think we're both trying to achieve similar functionality right now.
Is
play(from: to: avtime:)
suitable for your needs?