我在Xcode 9編程 - SWIFT 4 - MacOS的不IOS斯威夫特OSX音頻直通,同時記錄
下面我有一個基本的錄音音頻設置用於記錄與耳機和麥克風的歌手。我希望它能像邏輯X一樣反應。
在邏輯X中,您將要錄製的音軌放入錄音準備就緒,而發生此情況時,麥克風會路由至耳機,因此主唱可以聽到自己的聲音。在播放時,準備好的錄音靜音,主唱可以在這裏錄製。並且如果在回放期間按下了錄音按鈕,則主唱聽到正在錄製的人聲。
我遇到的問題是將音頻直接路由到音頻輸出。我不確定什麼音頻系統我應該使用AVAudioSession顯然是iOS的 。我必須去Core Audio嗎?我想堅持與SWIFT 4
任何意見,將不勝感激
這裏是我迄今爲止
import Cocoa
import AVFoundation
enum RecordingPreset: Int {
case Low = 0
case Medium
case High
func settings() -> Dictionary<String, Int> {
switch self {
case .Low:
return [AVLinearPCMBitDepthKey: 16, AVNumberOfChannelsKey : 1, AVSampleRateKey : 12_000, AVLinearPCMIsBigEndianKey : 0, AVLinearPCMIsFloatKey : 0]
case .Medium:
return [AVLinearPCMBitDepthKey: 16, AVNumberOfChannelsKey : 1, AVSampleRateKey : 24_000, AVLinearPCMIsBigEndianKey : 0, AVLinearPCMIsFloatKey : 0]
case .High:
return [AVLinearPCMBitDepthKey: 16, AVNumberOfChannelsKey : 1, AVSampleRateKey : 48_000, AVLinearPCMIsBigEndianKey : 0, AVLinearPCMIsFloatKey : 0]
}
}
func exportSettings() -> Dictionary <String, Int> {
var recordingSetting = self.settings()
recordingSetting[AVFormatIDKey] = Int(kAudioFormatLinearPCM)
recordingSetting[AVLinearPCMIsNonInterleaved] = 0
return recordingSetting
}
}//eo RecordingPreset
extension Array {
func firstObject() -> Element? {
var firstObject: Element?
if self.count > 0 {
firstObject = self[0]
}
return firstObject
}
func lastObject() -> Element? {
var lastObject: Element?
if self.count > 0 {
lastObject = self[self.endIndex - 1]
}
return lastObject
}
}
class MainWindowController: NSWindowController {
var audioEngine:AVAudioEngine!
var player:AVAudioPlayer!
var recorder: AVAudioRecorder?
override func windowDidLoad() {
super.windowDidLoad()
}//EO Overide
func createRecorder() ->() {
var initialisedRecorder: AVAudioRecorder?
let currentDate = NSDate()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yy HHmmss"
let fileName = "/RECORDING/Audio/Recording on " + dateFormatter.string(from: currentDate as Date) + ".wav"
let filePaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
if let firstPath = filePaths.firstObject() {
let recordingPath = firstPath + fileName
let url = NSURL(fileURLWithPath: recordingPath)
let selectedPreset = RecordingPreset.High
do {
initialisedRecorder = try AVAudioRecorder(url: url as URL, settings: selectedPreset.settings()
)
}catch {
print("nope")
}
initialisedRecorder!.isMeteringEnabled = true
initialisedRecorder!.prepareToRecord()
}
recorder = initialisedRecorder
}//eo createRecorder
@IBAction func myRecord(_ sender: Any) {
print("RECORD")
createRecorder()
recorder?.record()
}
@IBAction func myStop(_ sender: Any) {
print("STOP")
recorder?.stop()
recorder = nil
}
}//EnD oF thE wORld