audio engine에서 inputNode를 다룰 때 알아야 할 개념은 AVAudioPCMBuffer, audioFormat, frameCount, sampleRate, frameCapacity 등...
여기서 sample은 오디오 data를 다루는 가장 작은 단위이고, channel이 mono라면 frameCount와 sampleRate이 같지만, channel이 2개 이상이 되는 경우 frameCount는 sampleRate * channelCount 가 된다.
가장 궁금한 것은 audio engine에서 inputNode를 installTap할 때, channel이 1개이고 bufferSize를 samplerate와 같은 48kHz로 했는데도 timeInterval이 1초보다 빨리 진행되느냐 였다.
원한 결과는 1초에 48000개에 데이터가 모이면 installTap closure에 정의한 코드들이 실행되는 것이었는데, 약 0.4초마다 데이터가 모였고, 처음에 바로 약 90000개이상의 데이터가 모인 걸 확인할 수 있었다. channel이 1개인데.. 이유가 궁금했다.
audioFormat에 답이 있을 것 같다. audioFormat을 description했을 때, 위와 같은 정보가 나오는데, sample rate은 같으니 됐고, BytesPerPacket, FramesPerPacket, BytesPerFrame, ChannelsPerFrame, BitsPerChannel에 따라 time이 달라진다고 가정하고 다음 변수들이 installTap했을 때 어떤 영향을 주는지 알아봤다.(channel data를 float32로 해서 자동으로 설정된 format같다.)
mFramesPerPacket : For uncompressed audio, the value is 1. For variable bit-rate formats, the value is a larger fixed number, such as 1024 for AAC. For formats with a variable number of frames per packet, such as Ogg Vorbis, set this field to 0.
mBytesPerPacket : To indicate variable packet size, set this field to 0. For a format that uses variable packet size, specify the size of each packet using an AudioStreamPacketDescription structure.
mBytesPerFrame : For an audio buffer containing noninterleaved (monophonic) data, also using AudioSampleType samples, calculate the value for this field as follows:
mBytesPerFrame = sizeof (AudioSampleType)
아무리 봐도 그냥 floatchannel에 따른 당연한 개념이고, 시간에 영향을 주지 않을 것 같다. installTap을 분석하는 게 빠를 것 같다.
...
installTap이 open-source가 아니다.
...
installTap이 동작하는 방식을 보면 buffer size를 sample rate로 나눠서 second를 구한다.
아..이제 보니 installtap 자체가 지원되는 범위가 400ms까지였다...어쩐지 buffer size를 계속 크게 바꿔줘도 0.4초씩 호출이 되더라니...
그러면 bufferList를 정의해서 이를 통해 관리하는 것이 낫겠다.