繁体   English   中英

AudioFileReadPackets提供了奇怪的数据

[英]AudioFileReadPackets gives strange data

我正在尝试将音频记录到一个文件(正在工作),然后对该文件中的数据进行采样(给出奇怪的结果)。 仅供参考,我大致遵循此代码... 从iPhone上的Linear PCM提取幅度数据

我注意到了一些不同的结果。 为简单起见,假设记录时间固定为1秒。

  1. 当以每秒8,000个样本的速度采样时,可变数组(请参见代码)将列出8,000个条目,但是只有前4,000个具有真实数据,最后4,000个点是相同的数字值(确切的数字值从运行到-跑)。

  2. 与问题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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM