質問編集履歴
2
コードの方を追記したい
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,8 +6,234 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
|
10
|
-
|
11
9
|
※追記
|
12
10
|
|
13
11
|
私がやろうとしているのはマイクから取得したデータをリアルタイムにFFTしたいと考えています
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
import UIKit
|
18
|
+
|
19
|
+
import AudioToolbox
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
private func AudioQueueInputCallback(
|
24
|
+
|
25
|
+
_ inUserData: UnsafeMutableRawPointer?,
|
26
|
+
|
27
|
+
inAQ: AudioQueueRef,
|
28
|
+
|
29
|
+
inBuffer: AudioQueueBufferRef,
|
30
|
+
|
31
|
+
inStartTime: UnsafePointer<AudioTimeStamp>,
|
32
|
+
|
33
|
+
inNumberPacketDescriptions: UInt32,
|
34
|
+
|
35
|
+
inPacketDescs: UnsafePointer<AudioStreamPacketDescription>?)
|
36
|
+
|
37
|
+
{
|
38
|
+
|
39
|
+
print(inBuffer) //出力されない
|
40
|
+
|
41
|
+
//ここが音声データ? 出力されない
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
class AudioViewController: UIViewController {
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
@IBOutlet weak var loudLabel: UILabel!
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
@IBOutlet weak var peakTextField: UITextField!
|
56
|
+
|
57
|
+
@IBOutlet weak var averageTextField: UITextField!
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
var queue: AudioQueueRef!
|
62
|
+
|
63
|
+
var timer: Timer!
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
override func viewDidLoad() {
|
68
|
+
|
69
|
+
super.viewDidLoad()
|
70
|
+
|
71
|
+
self.startUpdatingVolume()
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
override func viewDidDisappear(_ animated: Bool) {
|
78
|
+
|
79
|
+
super.viewDidDisappear(animated)
|
80
|
+
|
81
|
+
self.stopUpdatingVolume()
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
override func didReceiveMemoryWarning() {
|
88
|
+
|
89
|
+
super.didReceiveMemoryWarning()
|
90
|
+
|
91
|
+
// Dispose of any resources that can be recreated.
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
// MARK: - Internal methods
|
98
|
+
|
99
|
+
func startUpdatingVolume() {
|
100
|
+
|
101
|
+
// Set data format
|
102
|
+
|
103
|
+
var dataFormat = AudioStreamBasicDescription(
|
104
|
+
|
105
|
+
mSampleRate: 44100.0,
|
106
|
+
|
107
|
+
mFormatID: kAudioFormatLinearPCM,
|
108
|
+
|
109
|
+
mFormatFlags: AudioFormatFlags(kLinearPCMFormatFlagIsBigEndian | kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked),
|
110
|
+
|
111
|
+
mBytesPerPacket: 2,
|
112
|
+
|
113
|
+
mFramesPerPacket: 1,
|
114
|
+
|
115
|
+
mBytesPerFrame: 2,
|
116
|
+
|
117
|
+
mChannelsPerFrame: 1,
|
118
|
+
|
119
|
+
mBitsPerChannel: 16,
|
120
|
+
|
121
|
+
mReserved: 0)
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
// Observe input level
|
126
|
+
|
127
|
+
var audioQueue: AudioQueueRef? = nil
|
128
|
+
|
129
|
+
var error = noErr
|
130
|
+
|
131
|
+
error = AudioQueueNewInput(
|
132
|
+
|
133
|
+
&dataFormat,
|
134
|
+
|
135
|
+
AudioQueueInputCallback,//充填完了時に呼ばれる?
|
136
|
+
|
137
|
+
UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()),
|
138
|
+
|
139
|
+
.none,
|
140
|
+
|
141
|
+
.none,
|
142
|
+
|
143
|
+
0,
|
144
|
+
|
145
|
+
&audioQueue)
|
146
|
+
|
147
|
+
if error == noErr {
|
148
|
+
|
149
|
+
self.queue = audioQueue
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
AudioQueueStart(self.queue, nil)
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
// Enable level meter
|
158
|
+
|
159
|
+
var enabledLevelMeter: UInt32 = 1
|
160
|
+
|
161
|
+
AudioQueueSetProperty(self.queue, kAudioQueueProperty_EnableLevelMetering, &enabledLevelMeter, UInt32(MemoryLayout<UInt32>.size))
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
self.timer = Timer.scheduledTimer(timeInterval: 0.5,
|
166
|
+
|
167
|
+
target: self,
|
168
|
+
|
169
|
+
selector: #selector(AudioViewController.detectVolume(_:)),
|
170
|
+
|
171
|
+
userInfo: nil,
|
172
|
+
|
173
|
+
repeats: true)
|
174
|
+
|
175
|
+
self.timer?.fire()
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
func stopUpdatingVolume()
|
182
|
+
|
183
|
+
{
|
184
|
+
|
185
|
+
// Finish observation
|
186
|
+
|
187
|
+
self.timer.invalidate()
|
188
|
+
|
189
|
+
self.timer = nil
|
190
|
+
|
191
|
+
AudioQueueFlush(self.queue)
|
192
|
+
|
193
|
+
AudioQueueStop(self.queue, false)
|
194
|
+
|
195
|
+
AudioQueueDispose(self.queue, true)
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
func detectVolume(_ timer: Timer)
|
202
|
+
|
203
|
+
{
|
204
|
+
|
205
|
+
// Get level
|
206
|
+
|
207
|
+
var levelMeter = AudioQueueLevelMeterState()
|
208
|
+
|
209
|
+
var propertySize = UInt32(MemoryLayout<AudioQueueLevelMeterState>.size)
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
AudioQueueGetProperty(
|
214
|
+
|
215
|
+
self.queue,
|
216
|
+
|
217
|
+
kAudioQueueProperty_CurrentLevelMeterDB,
|
218
|
+
|
219
|
+
&levelMeter,
|
220
|
+
|
221
|
+
&propertySize)
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
// Show the audio channel's peak and average RMS power.
|
226
|
+
|
227
|
+
self.peakTextField.text = "".appendingFormat("%.2f", levelMeter.mPeakPower)
|
228
|
+
|
229
|
+
self.averageTextField.text = "".appendingFormat("%.2f", levelMeter.mAveragePower)
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
// Show "LOUD!!" if mPeakPower is larger than -1.0
|
234
|
+
|
235
|
+
self.loudLabel.isHidden = (levelMeter.mPeakPower >= -1.0) ? false : true
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
}
|
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -3,3 +3,11 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
現在私がやっているのはAudioToolboxを使って、マイクからデータを拾うもので波型データを得る方法が調べてもSwift3で使えそうなサンプルが見つかりませんでした。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
※追記
|
12
|
+
|
13
|
+
私がやろうとしているのはマイクから取得したデータをリアルタイムにFFTしたいと考えています
|