프로그래밍 방식으로 WAV (또는 CAF) 파일을 읽고 샘플 (오디오) 데이터를 바이트 배열로 추출 할 수 있어야합니다. 가장 쉬운 방법은 무엇입니까? 당신이 iOS 또는 OS의 X에있어 가정WAV/CAF 파일의 샘플 데이터를 바이트 배열로 변환하는 방법은 무엇입니까?
1
A
답변
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);
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;
}
관련 문제
- 1. 정수를 바이트 배열로 변환하는 방법은 무엇입니까?
- 2. YAML 라이브러리를 사용하여 문자열 데이터를 바이트 배열로 변환하는 방법은 무엇입니까?
- 3. 대리자를 바이트 배열로 변환하는 방법은 무엇입니까?
- 4. Silverlight4에서 BitmapImage를 바이트 배열로 변환하는 방법은 무엇입니까?
- 5. UTF8 문자열을 바이트 배열로 변환하는 방법은 무엇입니까?
- 6. .NET에서 문자열을 바이트 배열로 변환하는 방법은 무엇입니까?
- 7. 긴 배열을 바이트 배열로 변환하는 방법은 무엇입니까?
- 8. C에서 바이트 배열로 구조를 변환하는 방법은 무엇입니까?
- 9. PNG 이미지를 바이트 배열로 변환하는 방법은 무엇입니까?
- 10. PdfContentBytes를 바이트 배열로 변환하는 방법
- 11. csv 바이트 문자열을 Java에서 바이트 배열로 변환하는 방법은 무엇입니까?
- 12. 문자열을 바이트 배열로 변환하는 방법
- 13. Ext.Store에서 배열로 데이터를 변환하는 방법은 무엇입니까?
- 14. 문자열을 base64 바이트 배열로 변환하는 방법은 유효할까요?
- 15. C++에서 바이트를 포함하는 문자열을 바이트 배열로 변환하는 방법은 무엇입니까?
- 16. 24 비트 정수를 3 바이트 배열로 변환하는 방법은 무엇입니까?
- 17. pdfstamper를 바이트 배열로 변환하는 방법
- 18. IEnumerable을 바이트 배열로 변환하는 방법
- 19. NameValueCollection을 바이트 배열로 변환하는 방법
- 20. System.Drawing.Image를 바이트 배열로 변환하는 방법?
- 21. 간단한 문자열을 VBA의 바이트 배열로 변환하는 방법은 무엇입니까?
- 22. MD5를 계산하기 위해 request.body를 바이트 배열로 변환하는 방법은 무엇입니까?
- 23. Java에서 이진 문자열을 이진 바이트 배열로 효과적으로 변환하는 방법은 무엇입니까?
- 24. 기본 64 문자열을 데이터 손실없이 바이트 배열로 변환하는 방법은 무엇입니까?
- 25. 모든 유형의 파일을 바이트 배열로 변환하는 방법은 무엇입니까?
- 26. 16 진수 문자열을 바이트 배열로 직접 변환하는 방법은 무엇입니까?
- 27. XMLSS (XML SpreadSheet)를 바이트 배열로 변환하는 방법은 무엇입니까?
- 28. Crystal에서 16 진수 문자열을 바이트 배열로 변환하는 방법은 무엇입니까?
- 29. 비 직렬화 가능 객체를 바이트 배열로 변환하는 방법은 무엇입니까?
- 30. 3x3 픽셀 .bmp를 3x3 바이트 배열로 변환하는 방법은 무엇입니까?
업데이트됩니다 파일에? 그 크기로 audioBuffer []를 정의하고 싶으므로 한 번의 호출로 사용 가능한 모든 데이터를 추출 할 수 있습니다. – MusiGenesis
오디오 데이터 바이트 수를 쿼리하는 방법을 보여주는'AudioFileGetProperty'를 호출하여 예제 (테스트되지 않은 코드이지만 컴파일 됨)를 업데이트했습니다. 또한 실세계에 가까운 읽기 루프를 추가했습니다. –
감사합니다. 마지막에 도달 할 때까지 4096 크기의 청크를 계속 읽도록 수정 한 원본을 사용하여 종료되었습니다. NSMutableData 객체에 이러한 바이트를 추가하는 것이므로 그렇게하기가 어렵지 않았습니다. – MusiGenesis