私は現在、コンピュータサイエンスの学士号の一部としてアプリケーションに取り組んでいます。このアプリケーションは、iPhoneハードウェア(加速度計、GPS)からのデータと再生中の音楽を相互に関連付けます。
このプロジェクトはまだ始まったばかりで、2か月しか取り組んでいません。
私が今、助けが必要な瞬間は、iTunesライブラリの曲からPCMサンプルを読み取り、オーディオユニットを使用して再生しています。現在、私が作業したい実装は次のことを行います。iTunesからランダムな曲を選択し、必要に応じてそこからサンプルを読み取り、バッファに保存して、sampleBufferと呼びます。後でコンシューマーモデルで、オーディオユニット(ミキサーとremoteIO出力を備えた)にコールバックがあり、必要な数のサンプルをsampleBufferからコールバックで指定されたバッファーにコピーするだけです。その後、スピーカーから聞こえるのは、私が期待していることとはまったく異なるものです。曲を再生していることはわかりますが、デコードが間違っているようで、ノイズが多いようです。最初の約0.5秒(24576サンプル@ 44.1kHz)を示す画像を添付しました。これは通常の出力とは似ていません。リストに入る前に、ファイルが破損していないことを確認しました。同様に、バッファーのテストケースを作成しました(バッファーがサンプルを変更しないことを知っています)。ただし、これは最善の方法ではない可能性があります。 (オーディオキュールートに行くと主張する人もいます)、サンプルのさまざまな操作を実行したり、終了する前に曲を変更したり、再生する曲を再配置したりしたいです。さらに、オーディオの設定が間違っている可能性がありますただし、ユニットを表示するグラフ(サンプルが正しくデコードされていないことを示す)はバッファから直接取得されるため、ディスクからの読み取りとデコードが正しく機能しない理由を解決するために今探しているだけです。今、私は単に仕事を通して遊びをしたいです。http://i.stack.imgur.com/RHjlv.jpg
リスト:
ここで、AVAssetReaderAudioMixOutputに使用されるaudioReadSettignsを設定します
// Set the read settings
audioReadSettings = [[NSMutableDictionary alloc] init];
[audioReadSettings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM]
forKey:AVFormatIDKey];
[audioReadSettings setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[audioReadSettings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[audioReadSettings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
[audioReadSettings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsNonInterleaved];
[audioReadSettings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
次のコードリストは、曲のpersistant_idを持つNSStringを受け取るメソッドです。
-(BOOL)setNextSongID:(NSString*)persistand_id {
assert(persistand_id != nil);
MPMediaItem *song = [self getMediaItemForPersistantID:persistand_id];
NSURL *assetUrl = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetUrl
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:AVURLAssetPreferPreciseDurationAndTimingKey]];
NSError *assetError = nil;
assetReader = [[AVAssetReader assetReaderWithAsset:songAsset error:&assetError] retain];
if (assetError) {
NSLog(@"error: %@", assetError);
return NO;
}
CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, songAsset.duration);
[assetReader setTimeRange:timeRange];
track = [[songAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
assetReaderOutput = [AVAssetReaderAudioMixOutput assetReaderAudioMixOutputWithAudioTracks:[NSArray arrayWithObject:track]
audioSettings:audioReadSettings];
if (![assetReader canAddOutput:assetReaderOutput]) {
NSLog(@"cant add reader output... die!");
return NO;
}
[assetReader addOutput:assetReaderOutput];
[assetReader startReading];
// just getting some basic information about the track to print
NSArray *formatDesc = ((AVAssetTrack*)[[assetReaderOutput audioTracks] objectAtIndex:0]).formatDescriptions;
for (unsigned int i = 0; i < [formatDesc count]; ++i) {
CMAudioFormatDescriptionRef item = (CMAudioFormatDescriptionRef)[formatDesc objectAtIndex:i];
const CAStreamBasicDescription *asDesc = (CAStreamBasicDescription*)CMAudioFormatDescriptionGetStreamBasicDescription(item);
if (asDesc) {
// get data
numChannels = asDesc->mChannelsPerFrame;
sampleRate = asDesc->mSampleRate;
asDesc->Print();
}
}
[self copyEnoughSamplesToBufferForLength:24000];
return YES;
}
次に、関数-(void)copyEnoughSamplesToBufferForLengthを示します。
-(void)copyEnoughSamplesToBufferForLength:(UInt32)samples_count {
[w_lock lock];
int stillToCopy = 0;
if (sampleBuffer->numSamples() < samples_count) {
stillToCopy = samples_count;
}
NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
CMSampleBufferRef sampleBufferRef;
SInt16 *dataBuffer = (SInt16*)malloc(8192 * sizeof(SInt16));
int a = 0;
while (stillToCopy > 0) {
sampleBufferRef = [assetReaderOutput copyNextSampleBuffer];
if (!sampleBufferRef) {
// end of song or no more samples
return;
}
CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBufferRef);
CMItemCount numSamplesInBuffer = CMSampleBufferGetNumSamples(sampleBufferRef);
AudioBufferList audioBufferList;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBufferRef,
NULL,
&audioBufferList,
sizeof(audioBufferList),
NULL,
NULL,
0,
&blockBuffer);
int data_length = floorf(numSamplesInBuffer * 1.0f);
int j = 0;
for (int bufferCount=0; bufferCount < audioBufferList.mNumberBuffers; bufferCount++) {
SInt16* samples = (SInt16 *)audioBufferList.mBuffers[bufferCount].mData;
for (int i=0; i < numSamplesInBuffer; i++) {
dataBuffer[j] = samples[i];
j++;
}
}
CFRelease(sampleBufferRef);
sampleBuffer->putSamples(dataBuffer, j);
stillToCopy = stillToCopy - data_length;
}
free(dataBuffer);
[w_lock unlock];
[apool release];
}
これで、sampleBufferは誤ってサンプルをデコードします。なぜこれがそうなのか誰かが私を助けることができますか?これは、iTunesライブラリ上のさまざまなファイル(mp3、aac、wavなど)で発生します。さらに、私のコードの他のリスト、またはおそらく出力がどのように聞こえるかが必要な場合は、リクエストごとに添付します。私はこの1週間、デバッグを試みていましたが、オンラインで助けが見つかりませんでした。誰もが私のやり方でそれを支配しているようですが、この問題を抱えているのは私だけのようです。
助けてくれてありがとう!
ピーター