-
-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace AVAudioEngine with Audio Units; Temporarily add file audio pl…
…ayer
- Loading branch information
Showing
8 changed files
with
351 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import AVFoundation | ||
import CoreMedia | ||
import CoreAudio | ||
|
||
protocol AudioFilePlayerDelegate: AnyObject { | ||
func didReceiveAudioSample(_ buffer: AVAudioPCMBuffer, time: AVAudioTime) | ||
} | ||
|
||
class AudioFilePlayer: NSObject { | ||
private var audioFile: AVAudioFile? | ||
private var audioEngine: AVAudioEngine = AVAudioEngine() | ||
private var audioPlayerNode: AVAudioPlayerNode = AVAudioPlayerNode() | ||
private var format: AVAudioFormat? | ||
weak var delegate: (any AudioFilePlayerDelegate)? | ||
|
||
override init() { | ||
super.init() | ||
loadAudioFile() | ||
setupAudioEngine() | ||
} | ||
|
||
private func loadAudioFile() { | ||
guard let fileURL = Bundle.main.url(forResource: "audio", withExtension: "wav") else { | ||
print("Audio file not found in bundle") | ||
return | ||
} | ||
|
||
do { | ||
let file = try AVAudioFile(forReading: fileURL) | ||
self.audioFile = file | ||
self.format = file.processingFormat | ||
} catch { | ||
print("Error loading audio file: \(error)") | ||
} | ||
} | ||
|
||
private func setupAudioEngine() { | ||
guard let format = self.format else { | ||
print("Audio format is nil") | ||
return | ||
} | ||
|
||
audioEngine.attach(audioPlayerNode) | ||
audioEngine.connect(audioPlayerNode, to: audioEngine.mainMixerNode, format: format) | ||
|
||
audioPlayerNode.volume = 0 | ||
|
||
do { | ||
try audioEngine.start() | ||
} catch { | ||
print("Error starting audio engine: \(error)") | ||
} | ||
} | ||
|
||
func play() { | ||
guard let audioFile = self.audioFile, let format = self.format else { | ||
print("Audio file or format is nil") | ||
return | ||
} | ||
|
||
audioPlayerNode.scheduleFile(audioFile, at: nil) { | ||
// File finished playing | ||
} | ||
|
||
audioPlayerNode.installTap(onBus: 0, bufferSize: 1024, format: format) { [weak self] (buffer, time) in | ||
self?.delegate?.didReceiveAudioSample(buffer, time: time) | ||
} | ||
|
||
audioPlayerNode.play() | ||
} | ||
} |
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
Binary file not shown.
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.