[英]AudioFileReadPackets gives strange data
我正在嘗試將音頻記錄到一個文件(正在工作),然后對該文件中的數據進行采樣(給出奇怪的結果)。 僅供參考,我大致遵循此代碼... 從iPhone上的Linear PCM提取幅度數據
我注意到了一些不同的結果。 為簡單起見,假設記錄時間固定為1秒。
當以每秒8,000個樣本的速度采樣時,可變數組(請參見代碼)將列出8,000個條目,但是只有前4,000個具有真實數據,最后4,000個點是相同的數字值(確切的數字值從運行到-跑)。
與問題1有關。 當采樣速度超過8,000個樣本/秒時,樣本的前半部分(例如10,000個樣本/秒的10,0000個樣本集中的5,000個,持續1秒)看起來像真實數據,而集合的后半部分的值將固定為某個值(同樣,該精確值因運行而異)。 從我的調試窗口中看到以下代碼片段,第一個數字是packetIndex,第二個數字是緩沖區值。
4996:-137
4997:1043
4998:-405
4999:-641
5000:195請注意,對於10k樣本文件,從5k的隨機數據切換到恆定值
5001:195
5002:195
5003:195
5004:195
3。 當麥克風讓揚聲器聽近距離播放1kHz正弦波的揚聲器並以每秒40,000個樣本的速度采樣此音頻時,在電子表格中繪制時所得的數據顯示信號約為2kHz或兩倍。
有什么想法我可能在這里做錯了嗎?
這是我錄制麥克風音頻的設置工作...
-(void) initAudioSession {
// setup av session
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error: nil];
NSLog(@"audio session initiated");
// settings for the recorded file
NSDictionary *recordSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat:SAMPLERATE],AVSampleRateKey,
[NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
[NSNumber numberWithInt:1],AVNumberOfChannelsKey,
[NSNumber numberWithInt:16],AVEncoderBitRateKey,
[NSNumber numberWithInt:AVAudioQualityMax],AVEncoderAudioQualityKey, nil];
// setup file name and location
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
fileURL = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:@"input.caf"]];//caf or aif?
// initialize my new audio recorder
newAudioRecorder = [[AVAudioRecorder alloc] initWithURL:fileURL settings:recordSettings error:nil];
// show file location so i can check it with some player
NSLog(@"file path = %@",fileURL);
// check if the recorder exists, if so prepare the recorder, if not tell me in debug window
if (newAudioRecorder) {
[newAudioRecorder setDelegate:self];
[newAudioRecorder prepareToRecord];
[self.setupStatus setText:[NSString stringWithFormat:@"recorder ready"]];
}else{
NSLog(@"error setting up recorder");
}
}
這是我的代碼,用於加載記錄的文件和獲取數據...
//loads file and go thru values, converts data to be put into an NSMutableArray
-(void)readingRainbow{
// get audio file and put into a file ID
AudioFileID fileID;
AudioFileOpenURL((__bridge CFURLRef)fileURL, kAudioFileReadPermission, kAudioFileCAFType /*kAudioFileAIFFType*/ , &fileID);
// get number of packets of audio contained in file
// instead of getting packets, i just set them to the duration times the sample rate i set
// not sure if this is a valid approach
UInt64 totalPacketCount = SAMPLERATE*timer;
// get size of each packet, is this valid?
UInt32 maxPacketSizeInBytes = sizeof(SInt32);
// setup to extract audio data
UInt32 totPack32 = SAMPLERATE*timer;
UInt32 ioNumBytes = totPack32*maxPacketSizeInBytes;
SInt16 *outBuffer = malloc(ioNumBytes);
memset(outBuffer, 0, ioNumBytes);
// setup array to put buffer samples in
readArray = [[NSMutableArray alloc] initWithObjects: nil];
NSNumber *arrayData;
SInt16 data;
int data2;
// this may be where i need help as well....
// process every packet
for (SInt64 packetIndex = 0; packetIndex<totalPacketCount; packetIndex++) {
// method description for reference..
// AudioFileReadPackets(<#AudioFileID inAudioFile#>, <#Boolean inUseCache#>, <#UInt32 *outNumBytes#>,
// <#AudioStreamPacketDescription *outPacketDescriptions#>, <#SInt64 inStartingPacket#>,
// <#UInt32 *ioNumPackets#>, <#void *outBuffer#>)
// extract packet data, not sure if i'm setting this up properly
AudioFileReadPackets(fileID, false, &ioNumBytes, NULL, packetIndex, &totPack32, outBuffer);
// get buffer data and pass into mutable array
data = outBuffer[packetIndex];
data2=data;
arrayData = [[NSNumber alloc] initWithInt:data2];
[readArray addObject:arrayData];
// printf("%lld:%d\n",packetIndex,data);
printf("%d,",data);
}
另外,我正在使用這種方法來啟動記錄器...
[newAudioRecorder recordForDuration:timer];
Thots? 我是菜鳥,因此非常感謝您提供任何信息!
您可能正在記錄16位樣本,但是嘗試從文件數據中讀取32位樣本,因此只能找到一半的樣本(其余樣本可能是垃圾)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.