-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.swift
More file actions
78 lines (61 loc) · 1.9 KB
/
Copy pathmain.swift
File metadata and controls
78 lines (61 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import Foundation
import AVFoundation
class AudioRecorder: NSObject, AVAudioRecorderDelegate {
var recorder: AVAudioRecorder?
var outputURL: URL
init(outputPath: String) {
self.outputURL = URL(fileURLWithPath: outputPath)
super.init()
}
func startRecording() {
let settings: [String: Any] = [
AVFormatIDKey: Int(kAudioFormatLinearPCM),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 1,
AVLinearPCMBitDepthKey: 16,
AVLinearPCMIsBigEndianKey: false,
AVLinearPCMIsFloatKey: false
]
do {
recorder = try AVAudioRecorder(url: outputURL, settings: settings)
recorder?.delegate = self
recorder?.prepareToRecord()
recorder?.record()
print("🎙️ Recording started at \(outputURL.path)")
} catch {
print("❌ Failed to start recording: \(error.localizedDescription)")
exit(1)
}
}
func stopRecording() {
recorder?.stop()
print("✅ Recording stopped. File saved to \(outputURL.path)")
}
}
// Entry point
let args = CommandLine.arguments
guard args.count >= 3 else {
print("Usage: AudioRecorderHelper start --output /path/to/file.wav")
exit(1)
}
let command = args[1]
let outputFlagIndex = args.firstIndex(of: "--output")
guard let outputIndex = outputFlagIndex, outputIndex + 1 < args.count else {
print("Error: Missing --output argument")
exit(1)
}
let outputPath = args[outputIndex + 1]
let recorder = AudioRecorder(outputPath: outputPath)
switch command {
case "start":
recorder.startRecording()
signal(SIGINT) { _ in
recorder.stopRecording()
exit(0)
}
// Keep alive until manually stopped (e.g., via Ctrl+C)
RunLoop.current.run()
default:
print("Unknown command: \(command)")
exit(1)
}