开发者

Audio Toolbox playback only plays part of output buffers

开发者 https://www.devze.com 2022-12-19 09:25 出处:网络
I\'m working on a project which is using Audio Toolbox for recording and playback of PCM data, and I\'m having trouble with playback. In the simulator, I can record and play audio just fine, using a c

I'm working on a project which is using Audio Toolbox for recording and playback of PCM data, and I'm having trouble with playback. In the simulator, I can record and play audio just fine, using a custom class to handle storing and sourcing PCM bytes for the recording and playback buffers as needed. On device (iPhone (3.0.1) and iPod 2G (3.1.2)) recording works fine, the audio files produced are correct, but in-app playback stutters, like it's only playing part of each playback buffer. My buffers are one second long, and I've got 3 buffers, which are preloaded before playback starts; stuttering occurs during those first 3 seconds as well, which I think rules out a latency problem.

I've written Audio Toolbox code before that worked, and I'm not doing anything strange here except that I'm using my own class to source PCM data instead of AudioFileReadBytes()

I know the data that comes out of my source is good, because it plays right in the sim, and it writes to disk as a correct audio file

I've played around 开发者_运维技巧with sample rates a bit; I'm normally using 11025Hz sampling to cut down on file size (it's all voice, so it sounds fine). at 44100Hz, but with the same size of buffers, I get the same stuttering problem, but the audio segments come a lot faster, about 4 times faster. That's why I think it's only playing part of each buffer.

The only reason I can conceive that it would only play part of each buffer is a latency problem... like the audio toolbox code is running out of full buffers while I'm still filling an empty one. But that would cause it to play the preloaded buffers correctly, and then start stuttering, and that doesn't happen, it stutters the whole way through

I've tried humongous buffers, like 10MB buffers, and I just get silence and a single stutter of audio at the end of playback. I've also tried preloading more buffers than normal, like 10 seconds worth of audio, and it behaves the same. The audio session is being set with AVAudioSession, not the Audio Toolbox calls, and it's being set to the Playback category for playback

I have no idea how to try and attack this problem, it makes no sense to me that it works fine on the simulator but not the device.

Code for the playing callback and the set up for the audio queue services: http://pastebin.com/mfaa546c


It turns out that the use of NSData's GetBytes:length: was causing the problem. The buffer filled with that method was playing incorrectly. However, doing a memcpy from that buffer to another buffer would prevent the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消