How to get array of float audio data from AudioQueueRef in iOS?

前端 未结 2 1847
梦毁少年i
梦毁少年i 2020-12-14 04:09

I\'m working on getting audio into the iPhone in a form where I can pass it to a (C++) analysis algorithm. There are, of course, many options: the AudioQueue tutorial at tra

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 04:17

    The above solution did not work for me, I was getting the wrong sample data itself.(an endian issue) If incase someone is getting wrong sample data in future, I hope this helps you :

    -(void)feedSamplesToEngine:(UInt32)audioDataBytesCapacity audioData:(void *)audioData { int sampleCount = audioDataBytesCapacity / sizeof(SAMPLE_TYPE);

    SAMPLE_TYPE *samples = (SAMPLE_TYPE*)audioData;
    //SAMPLE_TYPE *sample_le = (SAMPLE_TYPE *)malloc(sizeof(SAMPLE_TYPE)*sampleCount );//for swapping endians
    
    std::string shorts;
    double power = pow(2,10);
    for(int i = 0; i < sampleCount; i++)
    {
        SAMPLE_TYPE sample_le =  (0xff00 & (samples[i] << 8)) | (0x00ff & (samples[i] >> 8)) ; //Endianess issue
        char dataInterim[30];
        sprintf(dataInterim,"%f ", sample_le/power); // normalize it.
        shorts.append(dataInterim);
    }
    

提交回复
热议问题