Skip to content

Commit b429b4a

Browse files
author
Stanislas Chevallier
committed
Started work on SpectralView
1 parent 2ce2662 commit b429b4a

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

Subtitler.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
97924982270351FA00248C3D /* SubtitlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97924981270351FA00248C3D /* SubtitlerTests.swift */; };
2525
97924989270374C700248C3D /* PlayerControlsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97924988270374C700248C3D /* PlayerControlsView.swift */; };
2626
9792498B2703802100248C3D /* Codable+SY.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9792498A2703802100248C3D /* Codable+SY.swift */; };
27+
97A49D9827130B1A007844C8 /* SpectralView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97A49D9727130B1A007844C8 /* SpectralView.swift */; };
2728
97DF38302703F8FE002342CA /* SubtitleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97DF382F2703F8FE002342CA /* SubtitleView.swift */; };
2829
97DF3834270407F7002342CA /* SizingTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97DF3833270407F7002342CA /* SizingTextField.swift */; };
2930
97FAA54F270326DE00684B3F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 974FAA7B2703223C008B539B /* Main.storyboard */; };
@@ -60,6 +61,7 @@
6061
97924981270351FA00248C3D /* SubtitlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubtitlerTests.swift; sourceTree = "<group>"; };
6162
97924988270374C700248C3D /* PlayerControlsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerControlsView.swift; sourceTree = "<group>"; };
6263
9792498A2703802100248C3D /* Codable+SY.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Codable+SY.swift"; sourceTree = "<group>"; };
64+
97A49D9727130B1A007844C8 /* SpectralView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpectralView.swift; sourceTree = "<group>"; };
6365
97DF382F2703F8FE002342CA /* SubtitleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubtitleView.swift; sourceTree = "<group>"; };
6466
97DF3833270407F7002342CA /* SizingTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SizingTextField.swift; sourceTree = "<group>"; };
6567
/* End PBXFileReference section */
@@ -118,6 +120,7 @@
118120
97924988270374C700248C3D /* PlayerControlsView.swift */,
119121
97027088270339EC001B5785 /* PlayerView.swift */,
120122
97DF3833270407F7002342CA /* SizingTextField.swift */,
123+
97A49D9727130B1A007844C8 /* SpectralView.swift */,
121124
97DF382F2703F8FE002342CA /* SubtitleView.swift */,
122125
972CAB6E27032591005E9C3D /* TimingButton.swift */,
123126
);
@@ -315,6 +318,7 @@
315318
972CAB7127032591005E9C3D /* NSTableView+SY.swift in Sources */,
316319
97DF38302703F8FE002342CA /* SubtitleView.swift in Sources */,
317320
9702708727033502001B5785 /* DocumentController.swift in Sources */,
321+
97A49D9827130B1A007844C8 /* SpectralView.swift in Sources */,
318322
97027089270339EC001B5785 /* PlayerView.swift in Sources */,
319323
972CAB7327032591005E9C3D /* TimingButton.swift in Sources */,
320324
97920DAA2711C8D100D9F678 /* NSView+SY.swift in Sources */,

Subtitler/Views/SpectralView.swift

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// SpectralView.swift
3+
// Subtitler
4+
//
5+
// Created by Stanislas Chevallier on 10/10/2021.
6+
//
7+
8+
import Cocoa
9+
import AVFAudio
10+
import AVFoundation
11+
12+
// https://betterprogramming.pub/audio-visualization-in-swift-using-metal-accelerate-part-1-390965c095d7
13+
class SpectralView: NSView {
14+
15+
// MARK: Init
16+
override init(frame frameRect: NSRect) {
17+
super.init(frame: frameRect)
18+
setup()
19+
}
20+
21+
required init?(coder aDecoder: NSCoder) {
22+
super.init(coder: aDecoder)
23+
setup()
24+
}
25+
26+
private func setup() {
27+
/*
28+
_ = engine.mainMixerNode
29+
engine.prepare()
30+
31+
do {
32+
try engine.start()
33+
} catch {
34+
print(error)
35+
}
36+
37+
//now we need to create our player node
38+
let player = AVAudioPlayerNode()
39+
40+
do {
41+
//player nodes have a few ways to play-back music, the easiest way is from an AVAudioFile
42+
let audioFile = try AVAudioFile(forReading: url)
43+
44+
//audio always has a format, lets keep track of what the format is as an AVAudioFormat
45+
let format = audioFile.processingFormat
46+
print(format)
47+
48+
//we now need to connect add the node to our engine. This part is a little weird but we first need
49+
//to attach it to the engine itself before connecting it to the mainMixerNode. Recall that the
50+
//mainMixerNode connects to the default outputNode, so now we'll have a complete playback path from
51+
//our file to the outputNode!
52+
engine.attach(player)
53+
engine.connect(player, to: engine.mainMixerNode, format: format)
54+
55+
//let's play the file!
56+
//note: player must be attached first before scheduling a file to play
57+
player.scheduleFile(audioFile, at: nil, completionHandler: nil)
58+
} catch let error {
59+
print(error.localizedDescription)
60+
}
61+
62+
//tap it to get the buffer data at playtime
63+
engine.mainMixerNode.installTap(onBus: 0, bufferSize: 1024, format: nil) { (buffer, time) in
64+
65+
}
66+
67+
//start playing the music!
68+
player.play()
69+
*/
70+
}
71+
72+
// MARK: Properties
73+
private let engine = AVAudioEngine()
74+
75+
// MARK: Content
76+
/*
77+
func load(url: URL) {
78+
let node = AVAudioPlayerNode()
79+
node.
80+
engine.attach()
81+
}
82+
83+
func
84+
*/
85+
}

0 commit comments

Comments
 (0)