###発生している問題・エラーメッセージ
AVAssetReaderを使って44100hzの音声データを取得し、1秒間に44100個の音声データを取得したいのですが、なぜか16384個のデータしか取得できません。と書きましたが、おそらく音声データ自体は読み込めていると思われるのですが、それをうまく取り出す方法がわかっていないという状態です。
最後の方でarraySamples.append(samples.advancedBy(i).memory)というふうに格納しているのですが、これではまずいのでしょうか?
###該当のソースコード
Swift
1//音声データURLを取得 2let sineURL = NSBundle.mainBundle().URLForResource("memorize", withExtension: "mp3")! 3 let asset = AVAsset(URL: sineURL) 4 var assetReader:AVAssetReader 5 6 do{ 7 assetReader = try AVAssetReader(asset: asset) 8 }catch{ 9 fatalError("Unable to read Asset: \(error) : \(#function).") 10 } 11//取得する音声データ形式を設定 12 let track = asset.tracksWithMediaType(AVMediaTypeAudio).first 13 let outputSettings: [String:Int] = 14 [ AVFormatIDKey: Int(kAudioFormatLinearPCM), 15 AVLinearPCMIsBigEndianKey: 0, 16 AVLinearPCMIsFloatKey: 0, 17 AVLinearPCMBitDepthKey: 16, 18 AVLinearPCMIsNonInterleaved: 0] 19 20 21 let trackOutput = AVAssetReaderTrackOutput(track: track!, outputSettings: outputSettings) 22 23 assetReader.addOutput(trackOutput) 24 25 assetReader.startReading() 26 27 let sampleData = NSMutableData() 28 29 //サンプリングしたデータを取り出した後、格納する配列 30 var arraySamples = [Int16]() 31 var sampleCount = 0 32//音声データの読み込み、配列への格納 33 while assetReader.status == AVAssetReaderStatus.Reading { 34 if let sampleBufferRef = trackOutput.copyNextSampleBuffer() { 35 //読み込んだ音声データの形式を表示 36 print(sampleBufferRef) 37 38 if let blockBufferRef = CMSampleBufferGetDataBuffer(sampleBufferRef) { 39 40 let bufferLength = CMBlockBufferGetDataLength(blockBufferRef) 41 let data = NSMutableData(length: bufferLength) 42 CMBlockBufferCopyDataBytes(blockBufferRef, 0, bufferLength, data!.mutableBytes) 43 let samples = UnsafeMutablePointer<Int16>(data!.mutableBytes) 44 sampleData.appendBytes(samples, length: bufferLength) 45 CMSampleBufferInvalidate(sampleBufferRef) 46 //ここで16834個分のデータを格納。ここで本来は44100個にしたいが、16384個目を超えたところでデータ値はすべて0になってしまう。 47 for i in 0..<32768/2{ 48 arraySamples.append(samples.advancedBy(i).memory) 49 print(i,arraySamples[i]) 50 } 51 countSample += 1 52 } 53 } 54 } 55 } 56
###試したこと
最初の1秒分の出力結果は以下の通りです。
CMSampleBuffer 0x7fbbbb7247f0 retainCount: 6 allocator: 0x10ed90a40
invalid = NO
dataReady = YES
makeDataReadyCallback = 0x0
makeDataReadyRefcon = 0x0
formatDescription = <CMAudioFormatDescription 0x7fbbbb6124d0 [0x10ed90a40]> {
mediaType:'soun'
mediaSubType:'lpcm'
mediaSpecific: {
ASBD: {
mSampleRate: 44100.000000
mFormatID: 'lpcm'
mFormatFlags: 0xc
mBytesPerPacket: 4
mFramesPerPacket: 1
mBytesPerFrame: 4
mChannelsPerFrame: 2
mBitsPerChannel: 16 }
cookie: {(null)}
ACL: {(null)}
}
extensions: {(null)}
}
sbufToTrackReadiness = 0x0
numSamples = 8192
sampleTimingArray[1] = {
{PTS = {0/44100 = 0.000}, DTS = {INVALID}, duration = {1/44100 = 0.000}},
}
sampleSizeArray[1] = {
sampleSize = 4,
}
dataBuffer = 0x7fbbbb7047f0
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
・・・
省略(上で値が0になっているのは、まだ曲が始まってすぐだから?)
・・・
16356 6
16357 -37
16358 -13
16359 -37
16360 26
16361 -6
16362 2
16363 -22
16364 21
16365 -27
16366 -6
16367 -17
16368 16
16369 -13
16370 -7
16371 26
16372 -19
16373 21
16374 -16
16375 1
16376 16
16377 22
16378 -23
16379 -21
16380 5
16381 -9
16382 8
16383 -38
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/10 03:27
2016/06/10 03:57
2016/06/10 03:59