|
1 | 1 | import AVFoundation |
2 | 2 | import PlaygroundSupport |
3 | | -import AMCoreAudio |
4 | 3 | import Foundation |
5 | | -import STPrivilegedTask |
6 | 4 |
|
7 | | -Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in |
8 | | - let randomNumber = Int.random(in: 1...20) |
9 | | - print("Number: \(randomNumber)") |
10 | | -} |
11 | 5 |
|
12 | | -PlaygroundPage.current.needsIndefiniteExecution = true |
| 6 | +var engine = AVAudioEngine() |
| 7 | +// File |
| 8 | +let path = Bundle.main.path(forResource: "track", ofType: "mp3")! |
| 9 | +let url = URL(fileURLWithPath: path) |
| 10 | +let file = try! AVAudioFile(forReading: url) |
| 11 | +let fileFormat = file.processingFormat |
| 12 | +let frameCount = UInt32(file.length) |
13 | 13 |
|
14 | | -// |
15 | | -//var engine = AVAudioEngine() |
16 | | -//// File |
17 | | -//let path = Bundle.main.path(forResource: "track", ofType: "mp3")! |
18 | | -//let url = URL(fileURLWithPath: path) |
19 | | -//let file = try! AVAudioFile(forReading: url) |
20 | | -//let fileFormat = file.processingFormat |
21 | | -//let frameCount = UInt32(file.length) |
22 | | -// |
23 | | -//let buffer = AVAudioPCMBuffer(pcmFormat: fileFormat, frameCapacity: frameCount)! |
24 | | -//try! file.read(into: buffer, frameCount: frameCount) |
25 | | -// |
26 | | -//let player = AVAudioPlayerNode() |
27 | | -// |
28 | | -//let eq = AVAudioUnitEQ(numberOfBands: 3) |
29 | | -//eq.bypass = false |
30 | | -//eq.globalGain = -16 |
31 | | -//let gain = Float32(24) |
32 | | -//let band1 = eq.bands[0] |
33 | | -//band1.filterType = .parametric |
34 | | -//band1.bandwidth = 0.5 |
35 | | -//band1.gain = gain |
36 | | -//band1.frequency = 32.0 |
37 | | -//band1.bypass = false |
38 | | -// |
39 | | -//let band2 = eq.bands[1] |
40 | | -//band2.filterType = .parametric |
41 | | -//band2.bandwidth = 0.5 |
42 | | -//band2.gain = gain |
43 | | -//band2.frequency = 64.0 |
44 | | -//band2.bypass = false |
45 | | -// |
46 | | -//let band3 = eq.bands[2] |
47 | | -//band3.filterType = .parametric |
48 | | -//band3.bandwidth = 0.5 |
49 | | -//band3.gain = gain |
50 | | -//band3.frequency = 125.0 |
51 | | -//band3.bypass = false |
52 | | -// |
53 | | -//var varispeed = AVAudioUnitVarispeed() |
54 | | -// |
55 | | -//engine.attach(varispeed) |
56 | | -//engine.attach(player) |
57 | | -//engine.attach(eq) |
58 | | -// |
59 | | -// |
60 | | -//engine.connect(player, to: varispeed, format: fileFormat) |
61 | | -//engine.connect(varispeed, to: eq, format: fileFormat) |
62 | | -//engine.connect(eq, to: engine.mainMixerNode, format: fileFormat) |
63 | | -// |
64 | | -//try! engine.outputNode.auAudioUnit.setDeviceID(AudioDevice.defaultOutputDevice()!.id) |
65 | | -//engine.prepare() |
66 | | -//try! engine.start() |
67 | | -//print(engine) |
68 | | -// |
69 | | -//player.play() |
70 | | -//player.scheduleBuffer(buffer, at: AVAudioTime(hostTime: 0), options:.loops, completionHandler: nil) |
| 14 | +let buffer = AVAudioPCMBuffer(pcmFormat: fileFormat, frameCapacity: frameCount)! |
| 15 | +try! file.read(into: buffer, frameCount: frameCount) |
| 16 | + |
| 17 | +let player = AVAudioPlayerNode() |
| 18 | +let mixer = AVAudioMixerNode() |
| 19 | +let eq = AVAudioUnitEQ() |
| 20 | + |
| 21 | +engine.attach(player) |
| 22 | +engine.attach(mixer) |
| 23 | +engine.attach(eq) |
| 24 | +engine.connect(player, to: mixer, format: fileFormat) |
| 25 | +engine.connect(mixer, to: eq, format: fileFormat) |
| 26 | + |
| 27 | +engine.connect(eq, to: engine.mainMixerNode, format: fileFormat) |
| 28 | + |
| 29 | +engine.prepare() |
| 30 | + |
| 31 | +try! engine.start() |
| 32 | +print(engine) |
| 33 | +engine.mainMixerNode.outputVolume = 0.01 |
| 34 | + |
| 35 | + |
| 36 | +player.play() |
| 37 | +player.scheduleBuffer(buffer, at: AVAudioTime(hostTime: 0), options:.loops, completionHandler: nil) |
| 38 | + |
| 39 | +DispatchQueue.main.asyncAfter(deadline: .now() + 5, execute: { |
| 40 | + mixer.pan = -1 |
| 41 | +}) |
0 commit comments