2012-12-21 2 views

답변

3

, 당신은 AudioToolbox 프레임 워크, AudioFile.h에서 특히 API를 원하는 (또는 ExtAudioFile.h 당신이 읽기에 다른 형식으로 오디오 데이터를 변환해야하는 경우입니다.)

예를 들어,

여기
#include <AudioToolbox/AudioFile.h> 

... 

AudioFileID audioFile; 
OSStatus err = AudioFileOpenURL(fileURL, kAudioFileReadPermission, 0, &audioFile); 
// get the number of audio data bytes 
UInt64 numBytes = 0; 
UInt32 dataSize = sizeof(numBytes); 
err = AudioFileGetProperty(audioFile, kAudioFilePropertyAudioDataByteCount, &dataSize, &numBytes); 

unsigned char *audioBuffer = (unsigned char *)malloc(numBytes); 

UInt32 toRead = numBytes; 
UInt64 offset = 0; 
unsigned char *pBuffer = audioBuffer; 
while(true) { 
    err = AudioFileReadBytes(audioFile, true, offset, &toRead, &pBuffer); 
    if (kAudioFileEndOfFileError == err) { 
     // cool, we're at the end of the file 
     break; 
    } else if (noErr != err) { 
     // uh-oh, some error other than eof 
     break; 
    } 
    // advance the next read offset 
    offset += toRead; 
    // advance the read buffer's pointer 
    pBuffer += toRead; 
    toRead = numBytes - offset; 
    if (0 == toRead) { 
     // got to the end of file but no eof err 
     break; 
    } 
} 

// Process audioBuffer ... 

free(audioBuffer); 
+0

업데이트됩니다 파일에? 그 크기로 audioBuffer []를 정의하고 싶으므로 한 번의 호출로 사용 가능한 모든 데이터를 추출 할 수 있습니다. – MusiGenesis

+0

오디오 데이터 바이트 수를 쿼리하는 방법을 보여주는'AudioFileGetProperty'를 호출하여 예제 (테스트되지 않은 코드이지만 컴파일 됨)를 업데이트했습니다. 또한 실세계에 가까운 읽기 루프를 추가했습니다. –

+0

감사합니다. 마지막에 도달 할 때까지 4096 크기의 청크를 계속 읽도록 수정 한 원본을 사용하여 종료되었습니다. NSMutableData 객체에 이러한 바이트를 추가하는 것이므로 그렇게하기가 어렵지 않았습니다. – MusiGenesis

1

내가 데이터의 바이트 수 availab 어떻게 확인합니까 어떻게 Getting NSData out of music file in iPhone에서 훔친 뭔가 ARC

- (NSData *)readSoundFileSamples:(NSString *)filePath 
{ 

    // Get raw PCM data from the track 
    NSURL *assetURL = [NSURL fileURLWithPath:filePath]; 
    NSMutableData *data = [[NSMutableData alloc] init]; 

    const uint32_t sampleRate = 16000; // 16k sample/sec 
    const uint16_t bitDepth = 16; // 16 bit/sample/channel 
    const uint16_t channels = 2; // 2 channel/sample (stereo) 

    NSDictionary *opts = [NSDictionary dictionary]; 
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:assetURL options:opts]; 
    AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:asset error:NULL]; 
    NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey, 
           [NSNumber numberWithFloat:(float)sampleRate], AVSampleRateKey, 
           [NSNumber numberWithInt:bitDepth], AVLinearPCMBitDepthKey, 
           [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved, 
           [NSNumber numberWithBool:NO], AVLinearPCMIsFloatKey, 
           [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey, nil]; 

    AVAssetReaderTrackOutput *output = [[AVAssetReaderTrackOutput alloc] initWithTrack:[[asset tracks] objectAtIndex:0] outputSettings:settings]; 
    [reader addOutput:output]; 
    [reader startReading]; 

    // read the samples from the asset and append them subsequently 
    while ([reader status] != AVAssetReaderStatusCompleted) { 
     CMSampleBufferRef buffer = [output copyNextSampleBuffer]; 
     if (buffer == NULL) continue; 

     CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(buffer); 
     size_t size = CMBlockBufferGetDataLength(blockBuffer); 
     uint8_t *outBytes = malloc(size); 
     CMBlockBufferCopyDataBytes(blockBuffer, 0, size, outBytes); 
     CMSampleBufferInvalidate(buffer); 
     CFRelease(buffer); 
     [data appendBytes:outBytes length:size]; 
     free(outBytes); 
    } 

    return data; 

} 
관련 문제