@@ -2,125 +2,59 @@ import AVFoundation
22import PlaygroundSupport
33import AMCoreAudio
44
5- var engine : AVAudioEngine ?
5+ var engine = AVAudioEngine ( )
6+ // File
7+ let path = Bundle . main. path ( forResource: " track " , ofType: " mp3 " ) !
8+ let url = URL ( fileURLWithPath: path)
9+ let file = try ! AVAudioFile ( forReading: url)
10+ let fileFormat = file. processingFormat
11+ let frameCount = UInt32 ( file. length)
612
7- func stopEngine ( ) {
8- if ( engine != nil ) {
9- print ( " pausing " )
10- engine!. pause ( )
11- print ( " paused, stopping " )
12- engine!. stop ( )
13- print ( " stopped " )
14- // engine = nil
15- }
16- }
17- func createEngine ( ) {
18- stopEngine ( )
19- engine = AVAudioEngine ( )
20- // File
21- let path = Bundle . main. path ( forResource: " track " , ofType: " mp3 " ) !
22- let url = URL ( fileURLWithPath: path)
23- let file = try ! AVAudioFile ( forReading: url)
24- let fileFormat = file. processingFormat
25- let frameCount = UInt32 ( file. length)
26-
27- let buffer = AVAudioPCMBuffer ( pcmFormat: fileFormat, frameCapacity: frameCount) !
28- try ! file. read ( into: buffer, frameCount: frameCount)
29-
30- let player = AVAudioPlayerNode ( )
31-
32- let eq = AVAudioUnitEQ ( numberOfBands: 3 )
33- eq. bypass = false
34- //eq.globalGain = -24
35- let gain = Float32 ( 10 )
36- let band1 = eq. bands [ 0 ]
37- band1. filterType = . parametric
38- band1. bandwidth = 0.5
39- band1. gain = gain
40- band1. frequency = 32.0
41- band1. bypass = false
42-
43- let band2 = eq. bands [ 1 ]
44- band2. filterType = . parametric
45- band2. bandwidth = 0.5
46- band2. gain = gain
47- band2. frequency = 64.0
48- band2. bypass = false
49-
50- let band3 = eq. bands [ 2 ]
51- band3. filterType = . parametric
52- band3. bandwidth = 0.5
53- band3. gain = gain
54- band3. frequency = 125.0
55- band3. bypass = false
56-
57- var varispeed = AVAudioUnitVarispeed ( )
58- engine!. attach ( varispeed)
59- engine!. attach ( player)
60- engine!. attach ( eq)
61- varispeed. rate = 0.1
62- engine!. connect ( player, to: varispeed, format: fileFormat)
63- engine!. connect ( varispeed, to: eq, format: fileFormat)
64- engine!. connect ( eq, to: engine!. mainMixerNode, format: fileFormat)
65-
66- var asbd = AudioStreamBasicDescription ( )
67- var propSize = UInt32 ( MemoryLayout< AudioStreamBasicDescription> . size)
68- AudioUnitGetProperty ( engine!. outputNode. audioUnit!, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0 , & asbd, & propSize)
69-
70- print ( asbd)
71- print ( AVAudioFormat ( streamDescription: & asbd) )
72-
73- try ! engine!. outputNode. auAudioUnit. setDeviceID ( AudioDevice . defaultOutputDevice ( ) !. id)
74- engine!. prepare ( )
75- try ! engine!. start ( )
76- print ( engine!)
77-
78- player. play ( )
79- player. scheduleBuffer ( buffer, at: AVAudioTime ( hostTime: 0 ) , options: . loops, completionHandler: nil )
80- }
13+ let buffer = AVAudioPCMBuffer ( pcmFormat: fileFormat, frameCapacity: frameCount) !
14+ try ! file. read ( into: buffer, frameCount: frameCount)
8115
82- class EventListener : EventSubscriber {
83- var hashValue : Int = 1
84- init ( ) {
85- NotificationCenter . defaultCenter. subscribe (
86- self ,
87- eventType: AudioHardwareEvent . self,
88- dispatchQueue: DispatchQueue . main
89- )
90- NotificationCenter . defaultCenter. subscribe (
91- self ,
92- eventType: AudioDeviceEvent . self,
93- dispatchQueue: DispatchQueue . main
94- )
95- }
96-
97- internal func eventReceiver( _ event: AMCoreAudio . Event ) {
98- switch event {
99- case let event as AudioHardwareEvent :
100- switch event {
101- case . deviceListChanged( let added, let removed) :
102- print ( added, removed)
103- if removed. count > 0 && !removed[ 0 ] . name. contains ( " CADefaultDeviceAggregate " ) || added. count > 0 && !added[ 0 ] . name. contains ( " CADefaultDeviceAggregate " ) {
104- stopEngine ( )
105- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 1000 / 1000 ) {
106- print ( " Trying to start again " )
107- createEngine ( )
108- }
109- }
110-
111- default : return
112- }
113- case let event as AudioDeviceEvent :
114- switch event {
115- case . isAliveDidChange( let device) :
116- print ( " isAliveDidChange " , device)
117- default :
118- return
119- }
120- default : return
121- }
122- }
123- }
16+ let player = AVAudioPlayerNode ( )
12417
125- createEngine ( )
126- let listener = EventListener ( )
18+ let eq = AVAudioUnitEQ ( numberOfBands: 3 )
19+ eq. bypass = false
20+ eq. globalGain = - 16
21+ let gain = Float32 ( 24 )
22+ let band1 = eq. bands [ 0 ]
23+ band1. filterType = . parametric
24+ band1. bandwidth = 0.5
25+ band1. gain = gain
26+ band1. frequency = 32.0
27+ band1. bypass = false
28+
29+ let band2 = eq. bands [ 1 ]
30+ band2. filterType = . parametric
31+ band2. bandwidth = 0.5
32+ band2. gain = gain
33+ band2. frequency = 64.0
34+ band2. bypass = false
35+
36+ let band3 = eq. bands [ 2 ]
37+ band3. filterType = . parametric
38+ band3. bandwidth = 0.5
39+ band3. gain = gain
40+ band3. frequency = 125.0
41+ band3. bypass = false
42+
43+ var varispeed = AVAudioUnitVarispeed ( )
44+
45+ engine. attach ( varispeed)
46+ engine. attach ( player)
47+ engine. attach ( eq)
48+
49+
50+ engine. connect ( player, to: varispeed, format: fileFormat)
51+ engine. connect ( varispeed, to: eq, format: fileFormat)
52+ engine. connect ( eq, to: engine. mainMixerNode, format: fileFormat)
53+
54+ try ! engine. outputNode. auAudioUnit. setDeviceID ( AudioDevice . defaultOutputDevice ( ) !. id)
55+ engine. prepare ( )
56+ try ! engine. start ( )
57+ print ( engine)
58+
59+ player. play ( )
60+ player. scheduleBuffer ( buffer, at: AVAudioTime ( hostTime: 0 ) , options: . loops, completionHandler: nil )
0 commit comments