简体   繁体   English

AudioFileReadPackets提供了奇怪的数据

[英]AudioFileReadPackets gives strange data

I'm trying to record audio to a file (working) and then sample the data in that file (giving strange results). 我正在尝试将音频记录到一个文件(正在工作),然后对该文件中的数据进行采样(给出奇怪的结果)。 FYI, I am roughly following this code... Extracting Amplitude Data from Linear PCM on the iPhone 仅供参考,我大致遵循此代码... 从iPhone上的Linear PCM提取幅度数据

I have noticed a few different results. 我注意到了一些不同的结果。 For simplicity, assume the record time is fixed at 1 second. 为简单起见,假设记录时间固定为1秒。

  1. when sampling up to 8,000 samples/sec, the mutable array (see code) will list 8,000 entries but only the first 4,000 have real-looking data, the last 4,000 points are the same number value (the exact number value varies from run-to-run). 当以每秒8,000个样本的速度采样时,可变数组(请参见代码)将列出8,000个条目,但是只有前4,000个具有真实数据,最后4,000个点是相同的数字值(确切的数字值从运行到-跑)。

  2. somewhat related to issue #1. 与问题1有关。 when sampling above 8,000 samples/second, the first half of the samples (ex. 5,000 of a 10,0000 sample set from 10,000 samples/sec for 1 second) will look like real data, while the values of the second half of the set will be fixed to some value (again this exact value varies run to run). 当采样速度超过8,000个样本/秒时,样本的前半部分(例如10,000个样本/秒的10,0000个样本集中的5,000个,持续1秒)看起来像真实数据,而集合的后半部分的值将固定为某个值(同样,该精确值因运行而异)。 See below snippet from my debug window, first number is packetIndex, second number is buffer value. 从我的调试窗口中看到以下代码片段,第一个数字是packetIndex,第二个数字是缓冲区值。

4996:-137 4996:-137

4997:1043 4997:1043

4998:-405 4998:-405

4999:-641 4999:-641

5000:195 notice the switch from random data to constant value at 5k, for 10k sample file 5000:195请注意,对于10k样本文件,从5k的随机数据切换到恒定值

5001:195 5001:195

5002:195 5002:195

5003:195 5003:195

5004:195 5004:195

3 . 3。 when having the mic listen to a speaker playing a 1kHz sinusoidal tone in close proximity and sampling this tone at 40,000 samples per second, the resulting data when plotted in a spreadsheet shows the signal at about 2kHz, or double. 当麦克风让扬声器听近距离播放1kHz正弦波的扬声器并以每秒40,000个样本的速度采样此音频时,在电子表格中绘制时所得的数据显示信号约为2kHz或两倍。

Any ideas what I may be doing wrong here? 有什么想法我可能在这里做错了吗?

Here is my setup work to record the audio from the mic... 这是我录制麦克风音频的设置工作...

-(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");
    }
}

Here is my code for loading the recorded file and grabbing the data... 这是我的代码,用于加载记录的文件和获取数据...

//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);

    }

Also, I'm using this method to start the recorder... 另外,我正在使用这种方法来启动记录器...

[newAudioRecorder recordForDuration:timer];

Thots? Thots? I'm a noob, so any info is greatly appreciated! 我是菜鸟,因此非常感谢您提供任何信息!

您可能正在记录16位样本,但是尝试从文件数据中读取32位样本,因此只能找到一半的样本(其余样本可能是垃圾)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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