質問編集履歴
4
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
3
参考リンク追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
[Swift]
|
1
|
+
[Swift]ExtAudioFileOpenURL実行時のエラーを解決したい
|
test
CHANGED
@@ -18,6 +18,8 @@
|
|
18
18
|
|
19
19
|
[audio - iOS Code to Convert m4a to WAV - Stack Overflow](https://stackoverflow.com/questions/35738133/ios-code-to-convert-m4a-to-wav/36001262#36001262)
|
20
20
|
|
21
|
+
[avAudioPlayer OSStatus error 2003334207 | Apple Developer Forums](https://forums.developer.apple.com/thread/5278)
|
22
|
+
|
21
23
|
|
22
24
|
|
23
25
|
###実現したいこと
|
@@ -112,23 +114,13 @@
|
|
112
114
|
|
113
115
|
|
114
116
|
|
115
|
-
/// MARK: 変換先wavファイルをひらく
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
// 変換先のoutput.wavを指すURL
|
120
|
-
|
121
|
-
let toUrl = URL(fileURLWithPath: (NSTemporaryDirectory() as NSString).appendingPathComponent("output.wav"))
|
122
|
-
|
123
|
-
|
124
|
-
|
125
117
|
// wavファイル情報
|
126
118
|
|
127
119
|
var outASBDSize = UInt32(MemoryLayout<AudioStreamBasicDescription>.size)
|
128
120
|
|
129
121
|
var wavFormat = AudioStreamBasicDescription()
|
130
122
|
|
131
|
-
wavFormat.mFormatID = kAudioFormat
|
123
|
+
wavFormat.mFormatID = kAudioFormatLinearPCM
|
132
124
|
|
133
125
|
wavFormat.mSampleRate = 44100.0
|
134
126
|
|
@@ -142,15 +134,15 @@
|
|
142
134
|
|
143
135
|
status = ExtAudioFileCreateWithURL(outFileUrl as CFURL,
|
144
136
|
|
145
|
-
kAudioFi
|
137
|
+
kAudioFormatLinearPCM,
|
146
|
-
|
138
|
+
|
147
|
-
&wavFormat,
|
139
|
+
&wavFormat,
|
148
|
-
|
140
|
+
|
149
|
-
nil,
|
141
|
+
nil,
|
150
|
-
|
142
|
+
|
151
|
-
AudioFileFlags.eraseFile.rawValue,
|
143
|
+
AudioFileFlags.eraseFile.rawValue,
|
152
|
-
|
144
|
+
|
153
|
-
&outFile)
|
145
|
+
&outFile)
|
154
146
|
|
155
147
|
print("---> 04")
|
156
148
|
|
@@ -172,11 +164,11 @@
|
|
172
164
|
|
173
165
|
status = ExtAudioFileSetProperty(outFile!,
|
174
166
|
|
175
|
-
kExtAudioFileProperty_ClientDataFormat,
|
167
|
+
kExtAudioFileProperty_ClientDataFormat,
|
176
|
-
|
168
|
+
|
177
|
-
inASBDSize,
|
169
|
+
inASBDSize,
|
178
|
-
|
170
|
+
|
179
|
-
&inFormat)
|
171
|
+
&inFormat)
|
180
172
|
|
181
173
|
print("---> 05")
|
182
174
|
|
2
参考URLの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,9 +10,13 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
+
以下の記事を参考にしています。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
[Swift3でCore Audioを使用した音声ファイル変換 - Voicy Tech Blog](http://voicetech.hatenablog.com/entry/2017/06/07/225127)
|
18
|
+
|
13
|
-
[Stack Overflow
|
19
|
+
[audio - iOS Code to Convert m4a to WAV - Stack Overflow](https://stackoverflow.com/questions/35738133/ios-code-to-convert-m4a-to-wav/36001262#36001262)
|
14
|
-
|
15
|
-
|
16
20
|
|
17
21
|
|
18
22
|
|
1
モジュールの追加、ソースコード修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,6 +16,262 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
+
###実現したいこと
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
`convertAudio`メソッドを上手く動作させて、音声変換を行いたいです。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
###該当のソースコード
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
- 音声変換メソッド
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
```swift
|
38
|
+
|
39
|
+
import AVKit
|
40
|
+
|
41
|
+
import AVFoundation
|
42
|
+
|
43
|
+
import AudioToolbox
|
44
|
+
|
45
|
+
import CoreAudioKit
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
/* m4a -> wav の音声変換 */
|
50
|
+
|
51
|
+
func convertAudio(_ inFileUrl: URL, _ outFileUrl: URL) {
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
// ステータス
|
56
|
+
|
57
|
+
var status: OSStatus = noErr
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
// 変換元のm4aファイルを開く
|
62
|
+
|
63
|
+
var inFile: ExtAudioFileRef?
|
64
|
+
|
65
|
+
status = ExtAudioFileOpenURL(inFileUrl as CFURL, &inFile)
|
66
|
+
|
67
|
+
print("---> 01")
|
68
|
+
|
69
|
+
print(status)
|
70
|
+
|
71
|
+
print("<--- 01")
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
// 変換先のwavファイルを開く
|
76
|
+
|
77
|
+
var outFile: ExtAudioFileRef?
|
78
|
+
|
79
|
+
status = ExtAudioFileOpenURL(outFileUrl as CFURL, &outFile)
|
80
|
+
|
81
|
+
print("---> 02")
|
82
|
+
|
83
|
+
print(status)
|
84
|
+
|
85
|
+
print("<--- 02")
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
// 出力するwavファイルの情報を取得
|
90
|
+
|
91
|
+
var inASBDSize = UInt32(MemoryLayout<AudioStreamBasicDescription>.size)
|
92
|
+
|
93
|
+
var inFormat = AudioStreamBasicDescription()
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
/* *** ERROR!! *** */
|
98
|
+
|
99
|
+
status = ExtAudioFileGetProperty(inFile!, kExtAudioFileProperty_FileDataFormat, &inASBDSize, &inFormat)
|
100
|
+
|
101
|
+
print("---> 03")
|
102
|
+
|
103
|
+
print(status)
|
104
|
+
|
105
|
+
print("<--- 03")
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
/// MARK: 変換先wavファイルをひらく
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
// 変換先のoutput.wavを指すURL
|
116
|
+
|
117
|
+
let toUrl = URL(fileURLWithPath: (NSTemporaryDirectory() as NSString).appendingPathComponent("output.wav"))
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
// wavファイル情報
|
122
|
+
|
123
|
+
var outASBDSize = UInt32(MemoryLayout<AudioStreamBasicDescription>.size)
|
124
|
+
|
125
|
+
var wavFormat = AudioStreamBasicDescription()
|
126
|
+
|
127
|
+
wavFormat.mFormatID = kAudioFormatMPEG4AAC
|
128
|
+
|
129
|
+
wavFormat.mSampleRate = 44100.0
|
130
|
+
|
131
|
+
wavFormat.mChannelsPerFrame = 1
|
132
|
+
|
133
|
+
AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, nil, &outASBDSize, &wavFormat)
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
// wavファイルファイルを開く
|
138
|
+
|
139
|
+
status = ExtAudioFileCreateWithURL(outFileUrl as CFURL,
|
140
|
+
|
141
|
+
kAudioFileM4AType,
|
142
|
+
|
143
|
+
&wavFormat,
|
144
|
+
|
145
|
+
nil,
|
146
|
+
|
147
|
+
AudioFileFlags.eraseFile.rawValue,
|
148
|
+
|
149
|
+
&outFile)
|
150
|
+
|
151
|
+
print("---> 04")
|
152
|
+
|
153
|
+
print(status)
|
154
|
+
|
155
|
+
print("<--- 04")
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
/// MARK: 変換ルールの指定
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
// 書き込みプロパティ設定
|
168
|
+
|
169
|
+
status = ExtAudioFileSetProperty(outFile!,
|
170
|
+
|
171
|
+
kExtAudioFileProperty_ClientDataFormat,
|
172
|
+
|
173
|
+
inASBDSize,
|
174
|
+
|
175
|
+
&inFormat)
|
176
|
+
|
177
|
+
print("---> 05")
|
178
|
+
|
179
|
+
print(status)
|
180
|
+
|
181
|
+
print("<--- 05")
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
/// MARK: 変換処理
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
// バッファ作成
|
192
|
+
|
193
|
+
var readFrameSize: UInt32 = 1024 // 一度に読み込むフレーム数
|
194
|
+
|
195
|
+
var bufferSize = readFrameSize * inFormat.mBytesPerPacket
|
196
|
+
|
197
|
+
var buffer: UnsafeMutableRawPointer = malloc(Int(bufferSize))
|
198
|
+
|
199
|
+
defer { free(buffer) }
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
var audioBuffer = AudioBufferList()
|
204
|
+
|
205
|
+
audioBuffer.mNumberBuffers = 1
|
206
|
+
|
207
|
+
audioBuffer.mBuffers.mNumberChannels = inFormat.mChannelsPerFrame
|
208
|
+
|
209
|
+
audioBuffer.mBuffers.mDataByteSize = bufferSize
|
210
|
+
|
211
|
+
audioBuffer.mBuffers.mData = buffer
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
// ファイルを読み込んで出力
|
216
|
+
|
217
|
+
while (true) {
|
218
|
+
|
219
|
+
// WAVファイル読み込み
|
220
|
+
|
221
|
+
ExtAudioFileRead(inFile!, &readFrameSize, &audioBuffer)
|
222
|
+
|
223
|
+
// 読み込むデータがなくなれば終了
|
224
|
+
|
225
|
+
if readFrameSize <= 0 { break }
|
226
|
+
|
227
|
+
// AACファイル書き込み
|
228
|
+
|
229
|
+
ExtAudioFileWrite(outFile!, readFrameSize, &audioBuffer)
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
/// MARK: ファイルを閉じる
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
ExtAudioFileDispose(inFile!)
|
242
|
+
|
243
|
+
ExtAudioFileDispose(outFile!)
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
```
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
- 呼び出し元
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
```swift
|
256
|
+
|
257
|
+
//audioM4aURL: URL? // result: file:///Users/Polaris/Library/Developer/CoreSimulator/Devices/7A5A0C5B-AB28-4582-86D8-652072416B1F/data/Containers/Data/Application/62BAB05E-D169-4886-9327-7F8B041E397A/Documents/2017-11-10-13-45-28.m4a
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
// audioWavURL: URL? // result: "file:///Users/Polaris/Library/Developer/CoreSimulator/Devices/7A5A0C5B-AB28-4582-86D8-652072416B1F/data/Containers/Data/Application/63026CA3-FABA-474B-8FF6-10C819A00CBC/Documents/2017-11-10-15-45-45.wav"
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
convertAudio(audioM4aURL!, audioWavURL!)
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
```
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
|
274
|
+
|
19
275
|
###発生している問題・エラーメッセージ
|
20
276
|
|
21
277
|
|
@@ -38,215 +294,29 @@
|
|
38
294
|
|
39
295
|
|
40
296
|
|
41
|
-
###実現したいこと
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
`convertAudio`メソッドを上手く動作させて、音声変換を行いたいです。
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
###該当のソースコード
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
-
|
297
|
+
- コンソール
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
298
|
+
|
60
|
-
|
61
|
-
|
299
|
+
|
62
|
-
|
63
|
-
var error : OSStatus = noErr
|
64
|
-
|
65
|
-
var outFile: ExtAudioFileRef? = nil
|
66
|
-
|
67
|
-
var inFile : ExtAudioFileRef? = nil
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
var srcFormat : AudioStreamBasicDescription = AudioStreamBasicDescription()
|
72
|
-
|
73
|
-
var dstFormat : AudioStreamBasicDescription = AudioStreamBasicDescription()
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
ExtAudioFileOpenURL(url as CFURL, &inFile)
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
var thePropertySize: UInt32 = UInt32(MemoryLayout.stride(ofValue: srcFormat))
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
/* *** ERROR HERE!! *** */
|
86
|
-
|
87
|
-
ExtAudioFileGetProperty(inFile!, kExtAudioFileProperty_FileDataFormat, &thePropertySize, &srcFormat)
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
dstFormat.mSampleRate = 44100 //Set sample rate
|
92
|
-
|
93
|
-
dstFormat.mFormatID = kAudioFormatLinearPCM
|
94
|
-
|
95
|
-
dstFormat.mChannelsPerFrame = 1
|
96
|
-
|
97
|
-
dstFormat.mBitsPerChannel = 16
|
98
|
-
|
99
|
-
dstFormat.mBytesPerPacket = 2 * dstFormat.mChannelsPerFrame
|
100
|
-
|
101
|
-
dstFormat.mBytesPerFrame = 2 * dstFormat.mChannelsPerFrame
|
102
|
-
|
103
|
-
dstFormat.mFramesPerPacket = 1
|
104
|
-
|
105
|
-
dstFormat.mFormatFlags = kLinearPCMFormatFlagIsPacked |
|
106
|
-
|
107
|
-
kAudioFormatFlagIsSignedInteger
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
// Create destination file
|
112
|
-
|
113
|
-
error = ExtAudioFileCreateWithURL(
|
114
|
-
|
115
|
-
outputURL as CFURL,
|
116
|
-
|
117
|
-
kAudioFileWAVEType,
|
118
|
-
|
119
|
-
&dstFormat,
|
120
|
-
|
121
|
-
nil,
|
122
|
-
|
123
|
-
AudioFileFlags.eraseFile.rawValue,
|
124
|
-
|
125
|
-
&outFile)
|
126
|
-
|
127
|
-
print("Error 1 in convertAudio: (error.description)")
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
error = ExtAudioFileSetProperty(inFile!,
|
132
|
-
|
133
|
-
kExtAudioFileProperty_ClientDataFormat,
|
134
|
-
|
135
|
-
thePropertySize,
|
136
|
-
|
137
|
-
&dstFormat)
|
138
|
-
|
139
|
-
print("Error 2 in convertAudio: (error.description)")
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
error = ExtAudioFileSetProperty(outFile!,
|
144
|
-
|
145
|
-
kExtAudioFileProperty_ClientDataFormat,
|
146
|
-
|
147
|
-
thePropertySize,
|
148
|
-
|
149
|
-
&dstFormat)
|
150
|
-
|
151
|
-
print("Error 3 in convertAudio: (error.description)")
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
let bufferByteSize : UInt32 = 32768
|
156
|
-
|
157
|
-
var srcBuffer = [UInt8](repeating: 0, count: 32768)
|
158
|
-
|
159
|
-
var sourceFrameOffset : ULONG = 0
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
while(true){
|
164
|
-
|
165
|
-
var fillBufList = AudioBufferList(
|
166
|
-
|
167
|
-
mNumberBuffers: 1,
|
168
|
-
|
169
|
-
mBuffers: AudioBuffer(
|
170
|
-
|
171
|
-
mNumberChannels: 2,
|
172
|
-
|
173
|
-
mDataByteSize: UInt32(srcBuffer.count),
|
174
|
-
|
175
|
-
mData: &srcBuffer
|
176
|
-
|
177
|
-
)
|
178
|
-
|
179
|
-
)
|
180
|
-
|
181
|
-
var numFrames : UInt32 = 0
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
if(dstFormat.mBytesPerFrame > 0){
|
186
|
-
|
187
|
-
numFrames = bufferByteSize / dstFormat.mBytesPerFrame
|
188
|
-
|
189
|
-
}
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
error = ExtAudioFileRead(inFile!, &numFrames, &fillBufList)
|
194
|
-
|
195
|
-
print("Error 4 in convertAudio: (error.description)")
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
if(numFrames == 0){
|
200
|
-
|
201
|
-
error = noErr;
|
202
|
-
|
203
|
-
break;
|
204
|
-
|
205
|
-
}
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
sourceFrameOffset += numFrames
|
210
|
-
|
211
|
-
error = ExtAudioFileWrite(outFile!, numFrames, &fillBufList)
|
212
|
-
|
213
|
-
print("Error 5 in convertAudio: (error.description)")
|
214
|
-
|
215
|
-
}
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
error = ExtAudioFileDispose(outFile!)
|
220
|
-
|
221
|
-
print("Error 6 in convertAudio: (error.description)")
|
222
|
-
|
223
|
-
error = ExtAudioFileDispose(inFile!)
|
224
|
-
|
225
|
-
print("Error 7 in convertAudio: (error.description)")
|
226
|
-
|
227
|
-
}
|
228
300
|
|
229
301
|
```
|
230
302
|
|
231
|
-
|
232
|
-
|
233
|
-
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
303
|
+
2017-11-11 01:17:00.088640+0900 enPiT2SUProduct[34488:655917] 231: EXCEPTION (2003334207): "open audio file"
|
304
|
+
|
305
|
+
---> 01
|
306
|
+
|
307
|
+
2003334207
|
308
|
+
|
309
|
+
<--- 01
|
310
|
+
|
311
|
+
2017-11-11 01:17:00.089532+0900 enPiT2SUProduct[34488:655917] 231: EXCEPTION (2003334207): "open audio file"
|
312
|
+
|
313
|
+
---> 02
|
314
|
+
|
315
|
+
2003334207
|
316
|
+
|
317
|
+
<--- 02
|
318
|
+
|
319
|
+
// ここでアプリが落ちる
|
250
320
|
|
251
321
|
```
|
252
322
|
|
@@ -254,6 +324,10 @@
|
|
254
324
|
|
255
325
|
|
256
326
|
|
327
|
+
|
328
|
+
|
329
|
+
|
330
|
+
|
257
331
|
###考えたこと
|
258
332
|
|
259
333
|
音声変換の途中で、 `ExtAudioFileOpenURL(url as CFURL, &inFile)`という処理を行ったときに、`url`が指す音声ファイルを`inFile`にセットすれば、とりあえず、次の処理に進めるのではないかと考えました。
|