8

LPCM RAW ファイルからチャネル オーディオを抽出したいと思います。つまり、ステレオ LPCM ファイルの左右のチャネルを抽出します。LPCM は 16 ビット深度、インターリーブ、2 チャネル、リトルエンディアンです。私が収集したものから、バイトの順序は {LeftChannel,RightChannel,LeftChannel,RightChannel...} であり、16 ビット深度であるため、各チャンネルのサンプルは 2 バイトですよね?

私の質問は、左チャネルを抽出したい場合は、0,2,4,6...n*2 アドレスのバイトを取得しますか? 一方、右チャンネルは 1,3,4,...(n*2+1) になります。

また、オーディオ チャンネルを抽出した後、抽出したチャンネルのフォーマットを 16 ビット深度、1 チャンネルに設定する必要がありますか?

前もって感謝します

これは、AssetReader から PCM オーディオを抽出するために現在使用しているコードです。このコードは、チャネルを抽出せずに音楽ファイルを書き込むことで正常に動作するため、フォーマットまたは何かが原因である可能性があります...

    NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey, 
                                [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
                                [NSNumber numberWithInt:2], AVNumberOfChannelsKey,
                            //  [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey,
                                [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
                                [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
                                [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                                [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
                                nil];
NSError *assetError = nil;
AVAssetReader *assetReader = [[AVAssetReader assetReaderWithAsset:songAsset
                                                            error:&assetError]
                              retain];
if (assetError) {
    NSLog (@"error: %@", assetError);
    return;
}

AVAssetReaderOutput *assetReaderOutput = [[AVAssetReaderAudioMixOutput 
                                           assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks
                                           audioSettings: outputSettings]
                                          retain];
if (! [assetReader canAddOutput: assetReaderOutput]) {
    NSLog (@"can't add reader output... die!");
    return;
}
[assetReader addOutput: assetReaderOutput];


NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [dirs objectAtIndex:0];

//CODE TO SPLIT STEREO
[self setupAudioWithFormatMono:kAudioFormatLinearPCM];
NSString *splitExportPath = [[documentsDirectoryPath stringByAppendingPathComponent:@"monoleft.caf"] retain];
if ([[NSFileManager defaultManager] fileExistsAtPath:splitExportPath]) {
    [[NSFileManager defaultManager] removeItemAtPath:splitExportPath error:nil];
}

AudioFileID mRecordFile;
NSURL *splitExportURL = [NSURL fileURLWithPath:splitExportPath];


OSStatus status =  AudioFileCreateWithURL(splitExportURL, kAudioFileCAFType, &_streamFormat, kAudioFileFlags_EraseFile,
                                          &mRecordFile);

NSLog(@"status os %d",status);

[assetReader startReading];

CMSampleBufferRef sampBuffer = [assetReaderOutput copyNextSampleBuffer];
UInt32 countsamp= CMSampleBufferGetNumSamples(sampBuffer);
NSLog(@"number of samples %d",countsamp);

SInt64 countByteBuf = 0;
SInt64 countPacketBuf = 0;
UInt32 numBytesIO = 0;
UInt32 numPacketsIO = 0;
NSMutableData * bufferMono = [NSMutableData new];
while (sampBuffer) {


    AudioBufferList  audioBufferList;
    CMBlockBufferRef blockBuffer;
    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampBuffer, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
    for (int y=0; y<audioBufferList.mNumberBuffers; y++) {
        AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
        //frames = audioBuffer.mData;
        NSLog(@"the number of channel for buffer number %d is %d",y,audioBuffer.mNumberChannels);
        NSLog(@"The buffer size is %d",audioBuffer.mDataByteSize);






        //Append mono left to buffer data
        for (int i=0; i<audioBuffer.mDataByteSize; i= i+4) {
            [bufferMono appendBytes:(audioBuffer.mData+i) length:2];
        }

        //the number of bytes in the mutable data containing mono audio file
        numBytesIO = [bufferMono length];
        numPacketsIO = numBytesIO/2;
        NSLog(@"numpacketsIO %d",numPacketsIO);
        status = AudioFileWritePackets(mRecordFile, NO, numBytesIO, &_packetFormat, countPacketBuf, &numPacketsIO, audioBuffer.mData);
        NSLog(@"status for writebyte %d, packets written %d",status,numPacketsIO);
        if(numPacketsIO != (numBytesIO/2)){
            NSLog(@"Something wrong");
            assert(0);
        }


        countPacketBuf = countPacketBuf + numPacketsIO;
        [bufferMono setLength:0];


    }

    sampBuffer = [assetReaderOutput copyNextSampleBuffer];
    countsamp= CMSampleBufferGetNumSamples(sampBuffer);
    NSLog(@"number of samples %d",countsamp);
}
AudioFileClose(mRecordFile);
[assetReader cancelReading];
[self performSelectorOnMainThread:@selector(updateCompletedSizeLabel:)
                       withObject:0
                    waitUntilDone:NO];

audiofileservices を使用した出力形式は次のとおりです。

        _streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
    _streamFormat.mBitsPerChannel = 16;
    _streamFormat.mChannelsPerFrame = 1;
    _streamFormat.mBytesPerPacket = 2;
    _streamFormat.mBytesPerFrame = 2;// (_streamFormat.mBitsPerChannel / 8) * _streamFormat.mChannelsPerFrame;
    _streamFormat.mFramesPerPacket = 1;
    _streamFormat.mSampleRate = 44100.0;

    _packetFormat.mStartOffset = 0;
    _packetFormat.mVariableFramesInPacket = 0;
    _packetFormat.mDataByteSize = 2;
4

3 に答える 3

4

ほぼ正しいように聞こえます-16ビットの深さがあるため、各サンプルは2バイトを取ることを意味します. つまり、左チャンネルのデータはバイト {0,1}、{4,5}、{8,9} などになります。インターリーブとは、バイトではなくサンプルがインターリーブされることを意味します。それ以外は、試してみて、コードに問題がないかどうかを確認します。

また、オーディオ チャンネルを抽出した後、抽出したチャンネルのフォーマットを 16 ビット深度、1 チャンネルに設定する必要がありますか?

抽出後、2 つのチャンネルのうち 1 つだけが残っているので、これは正しいです。

于 2011-01-07T23:22:30.907 に答える
1

オーディオのサウンドが「遅い」という同様のエラーが発生しました。これは、mChannelsPerFrame を 1 に指定したのに、デュアル チャネル サウンドを使用しているためです。2 に設定すると、再生が高速化されます。また、これを行った後、出力が正しく「聞こえる」かどうかも教えてください... :)

于 2012-02-18T21:44:57.587 に答える
0

ステレオ オーディオを 2 つのモノラル ファイルに分割しようとしています ( iOS では、ステレオ オーディオをモノラル ストリームに分割します)。私はあなたのコードを使用してきましたが、それを機能させることができないようです。setupAudioWithFormatMono メソッドの内容は何ですか?

于 2015-01-12T23:12:53.060 に答える