Skip to content

Commit bdbce08

Browse files
committed
Audio monitor
1 parent 5ee3680 commit bdbce08

33 files changed

Lines changed: 292 additions & 3520 deletions

notch-prompter/AudioMonitor.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class AudioMonitor: ObservableObject {
1717
private var timer: Timer?
1818
private let smoothingFactor: Float = 0.5
1919
private var lastPublishTime = Date.timeIntervalSinceReferenceDate
20-
20+
2121
@Published var rmsLevel: Float = 0
22-
22+
2323

2424
init() {
2525
audioEngine = AVAudioEngine()
@@ -28,12 +28,12 @@ class AudioMonitor: ObservableObject {
2828

2929
func startMonitoring() {
3030
let format = inputNode!.outputFormat(forBus: 0)
31-
31+
3232
inputNode!.installTap(onBus: 0, bufferSize: 256, format: format) { [weak self] buffer, time in
3333
guard let self = self else { return }
3434

3535
let rms = self.rms(buffer: buffer)
36-
let smoothedRms = (self.rmsLevel * (1 - self.smoothingFactor)) + (rms * self.smoothingFactor)
36+
let smoothedRms = (self.rmsLevel * self.smoothingFactor) + (rms * self.smoothingFactor) * -1
3737
DispatchQueue.main.async {
3838
self.rmsLevel = smoothedRms
3939
}
@@ -54,10 +54,10 @@ class AudioMonitor: ObservableObject {
5454
private func rms(buffer: AVAudioPCMBuffer) -> Float {
5555
guard let channelData = buffer.floatChannelData?[0] else { return 0 }
5656
let frameLength = vDSP_Length(buffer.frameLength)
57-
57+
5858
var rmsValue: Float = 0
5959
vDSP_rmsqv(channelData, 1, &rmsValue, frameLength)
60-
60+
6161
return rmsValue
6262
}
6363
}

notch-prompter/FontDesignExtension.swift

Lines changed: 0 additions & 28 deletions
This file was deleted.

notch-prompter/HighlightingTextEditor.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ struct HighlightingTextEditor: NSViewRepresentable {
1212
@Binding var text: String
1313
var font: NSFont = .systemFont(ofSize: 15, weight: .regular)
1414
var isFocused: FocusState<Bool>.Binding?
15-
15+
1616
func makeCoordinator() -> Coordinator {
1717
Coordinator(self)
1818
}
19-
19+
2020
func makeNSView(context: Context) -> NSScrollView {
2121
let scrollView = NSScrollView()
2222
scrollView.hasVerticalScroller = true
2323
scrollView.hasHorizontalScroller = false
2424
scrollView.autohidesScrollers = true
2525
scrollView.borderType = .noBorder
2626
scrollView.drawsBackground = false
27-
27+
2828
let textView = NSTextView()
2929
textView.isEditable = true
3030
textView.isSelectable = true
@@ -41,79 +41,79 @@ struct HighlightingTextEditor: NSViewRepresentable {
4141
textView.isVerticallyResizable = true
4242
textView.isHorizontallyResizable = false
4343
textView.autoresizingMask = [.width]
44-
44+
4545
scrollView.documentView = textView
4646
context.coordinator.textView = textView
47-
47+
4848
// Set initial text and apply highlighting
4949
textView.string = text
5050
context.coordinator.applyHighlighting(textView)
51-
51+
5252
return scrollView
5353
}
54-
54+
5555
func updateNSView(_ scrollView: NSScrollView, context: Context) {
5656
guard let textView = scrollView.documentView as? NSTextView else { return }
57-
57+
5858
if textView.string != text {
5959
let selectedRanges = textView.selectedRanges
6060
textView.string = text
6161
textView.selectedRanges = selectedRanges
6262
context.coordinator.applyHighlighting(textView)
6363
}
6464
}
65-
65+
6666
class Coordinator: NSObject, NSTextViewDelegate {
6767
var parent: HighlightingTextEditor
6868
weak var textView: NSTextView?
69-
69+
7070
private static let annotationPattern = try! NSRegularExpression(
7171
pattern: "\\[[^\\]]+\\]",
7272
options: []
7373
)
74-
74+
7575
init(_ parent: HighlightingTextEditor) {
7676
self.parent = parent
7777
}
78-
78+
7979
func textDidChange(_ notification: Notification) {
8080
guard let textView = notification.object as? NSTextView else { return }
8181
parent.text = textView.string
8282
applyHighlighting(textView)
8383
}
84-
84+
8585
func applyHighlighting(_ textView: NSTextView) {
8686
guard let textStorage = textView.textStorage else { return }
8787
let fullRange = NSRange(location: 0, length: textStorage.length)
8888
let text = textStorage.string
89-
89+
9090
// Preserve selection
9191
let selectedRanges = textView.selectedRanges
92-
92+
9393
textStorage.beginEditing()
94-
94+
9595
// Reset to default style
9696
let defaultAttributes: [NSAttributedString.Key: Any] = [
9797
.font: parent.font,
9898
.foregroundColor: NSColor.labelColor
9999
]
100100
textStorage.setAttributes(defaultAttributes, range: fullRange)
101-
101+
102102
// Highlight [bracket] annotations
103103
let matches = Self.annotationPattern.matches(in: text, options: [], range: fullRange)
104104
for match in matches {
105105
let annotationAttributes: [NSAttributedString.Key: Any] = [
106106
.font: NSFontManager.shared.convert(parent.font, toHaveTrait: .italicFontMask),
107107
.foregroundColor: NSColor.secondaryLabelColor,
108-
.backgroundColor: NSColor.secondaryLabelColor.withAlphaComponent(0.08)
108+
.backgroundColor: NSColor.secondaryLabelColor.withAlphaComponent(1.08)
109109
]
110110
textStorage.addAttributes(annotationAttributes, range: match.range)
111111
}
112-
112+
113113
textStorage.endEditing()
114-
114+
115115
// Restore selection
116116
textView.selectedRanges = selectedRanges
117117
}
118118
}
119-
}
119+
}

notch-prompter/NotchPrompterApp.swift

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
4848

4949
struct MenuContent: View {
5050
@ObservedObject var viewModel: PrompterViewModel
51-
5251

53-
52+
53+
5454
var body: some View {
5555
Button {
5656
viewModel.isPrompterVisible.toggle()
@@ -59,9 +59,9 @@ struct MenuContent: View {
5959
systemImage: viewModel.isPrompterVisible ? "eye.slash" : "eye")
6060
}
6161
.keyboardShortcut("h", modifiers: [.command, .option])
62-
62+
6363
Divider()
64-
64+
6565
Button {
6666
if viewModel.isPlaying {
6767
viewModel.pause()
@@ -82,13 +82,13 @@ struct MenuContent: View {
8282
Label("Reset", systemImage: "arrow.counterclockwise")
8383
}
8484

85-
85+
8686
Divider()
8787

8888
SettingsLink {
8989
Label("Settings", systemImage: "gearshape")
9090
}
91-
91+
9292
.keyboardShortcut(",", modifiers: [.command])
9393

9494
Divider()
@@ -98,26 +98,20 @@ struct MenuContent: View {
9898
NSWorkspace.shared.open(url)
9999
}
100100
}
101-
101+
102102
Button("Project page") {
103103
if let url = URL(string: "https://notchprompter.com") {
104104
NSWorkspace.shared.open(url)
105105
}
106106
}
107-
108-
// Button("Help translate") {
109-
// if let url = URL(string: "https://simplelocalize.io/suggestions/?id=f1f11f9305dc44a2872b6a154dea6edc") {
110-
// NSWorkspace.shared.open(url)
111-
// }
112-
// }
113-
// #if !APP_STORE_VERSION
114-
Button("Sponsor the project") {
115-
if let url = URL(string: "https://jpomykala.gumroad.com/l/notchprompter") {
116-
NSWorkspace.shared.open(url)
117-
}
118-
}
119-
// #endif
120-
107+
108+
Button("Help translate") {
109+
if let url = URL(string: "https://simplelocalize.io/suggestions/?id=f1f11f9305dc44a2872b6a154dea6edc") {
110+
NSWorkspace.shared.open(url)
111+
}
112+
}
113+
114+
121115
Divider()
122116

123117
Button(role: .destructive) {

0 commit comments

Comments
 (0)