ios - NaN 会导致这个核心音频 iOS 应用程序偶尔崩溃吗?
<p><p>我的第一个应用合成了<a href="https://github.com/hollance/AudioBufferPlayer" rel="noreferrer noopener nofollow">music audio from a sine look-up table</a>使用自 iOS 6 以来不推荐使用的方法。我刚刚对其进行了修改,以解决关于 <code>AudioSession</code> 的警告,由 <a href="https://github.com/software-mariodiana/AudioBufferPlayer/wiki/Replacing-C-functions-deprecated-in-iOS-7" rel="noreferrer noopener nofollow">this blog</a> 提供帮助以及关于 AVFoundationFramework 的 Apple 指南。 Audio Session 警告现已得到解决,该应用程序会像以前一样产生音频。它目前在 iOS 9 下运行。</p>
<p>但是,该应用偶尔会无缘无故地崩溃。我 checkout<a href="https://stackoverflow.com/questions/4299419/determine-number-of-frames-in-a-core-audio-audiobuffer" rel="noreferrer noopener nofollow">this SO post</a>但它似乎处理访问而不是生成原始音频数据,所以它可能没有处理时间问题。我怀疑存在缓冲问题,但在更改或微调代码中的任何内容之前,我需要了解这可能是什么。 </p>
<p>我有一个将修改后的应用程序提供给用户的最后期限,因此我非常感谢处理过类似问题的人的来信。 </p>
<p>这就是问题所在。该应用程序在模拟器报告中进入调试:</p>
<pre><code>com.apple.coreaudio.AQClient (8):EXC_BAD_ACCESS (code=1, address=0xffffffff10626000)
</code></pre>
<p>在调试导航器的线程 8 (<code>com.apple.coreaudio.AQClient (8)</code>) 中,它报告:</p>
<pre><code> 0 -
1 -
2 playCallback
</code></pre>
<p>fillBuffer中这行代码高亮显示</p>
<pre><code> float sineValue = (1.0f - b)*sine + b*sine;
</code></pre>
<p>...audioBufferPlayer 中的这行代码也是如此</p>
<pre><code> int packetsWritten = ;
</code></pre>
<p>... 和 playCallBack</p>
<pre><code> ;
</code></pre>
<p>这里是 audioBufferPlayer 的代码(委托(delegate),与 <a href="https://github.com/hollance/AudioBufferPlayer" rel="noreferrer noopener nofollow">above</a> 中的演示基本相同)。 </p>
<pre><code> - (void)audioBufferPlayer:(AudioBufferPlayer*)audioBufferPlayer fillBuffer:(AudioQueueBufferRef)buffer format:(AudioStreamBasicDescription)audioFormat
{
;
int packetsPerBuffer = buffer->mAudioDataBytesCapacity / audioFormat.mBytesPerPacket;
int packetsWritten = ;
buffer->mAudioDataByteSize = packetsWritten * audioFormat.mBytesPerPacket;
;
}
</code></pre>
<p>...(在 myViewController 中初始化)</p>
<pre><code>- (id)init
{
if ((self = )) {
// The audio buffer is managed (filled up etc.) within its own thread (Audio Queue thread)
// Since we are also responding to changes from the GUI, we need a lock so both threads
// do not attempt to change the same value independently.
synthLock = [ init];
// Synth and the AudioBufferPlayer must use the same sample rate.
float sampleRate = 44100.0f;
// Initialise synth to fill the audio buffer with audio samples.
synth = [ initWithSampleRate:sampleRate];
// Initialise note buttons
buttons = [ init];
// Initialise the audio buffer.
player = [ initWithSampleRate:sampleRate channels:1 bitsPerChannel:16 packetsPerBuffer:1024];
player.delegate = self;
player.gain = 0.9f;
[ setActive:YES error:nil];
}
return self;
} // initialisation
</code></pre>
<p>... 以及用于 playCallback</p>
<pre><code>static void playCallback( void* inUserData, AudioQueueRef inAudioQueue, AudioQueueBufferRef inBuffer)
{
AudioBufferPlayer* player = (AudioBufferPlayer*) inUserData;
if (player.playing){
;
AudioQueueEnqueueBuffer(inAudioQueue, inBuffer, 0, NULL);
}
}
</code></pre>
<p>...这里是用于合成音频的 fillBuffer 代码</p>
<pre><code>- (int)fillBuffer:(void*)buffer frames:(int)frames
{
SInt16* p = (SInt16*)buffer;
//Loop through the frames (or "block size"), then consider each sample for each tone.
for (int f = 0; f < frames; ++f)
{
float m = 0.0f;// the mixed value for this frame
for (int n = 0; n < MAX_TONE_EVENTS; ++n)
{
if (tones.state == STATE_INACTIVE) // only active tones
continue;
// recalculate a 30sec envelope and place in a look-up table
// Longer notes need to interpolate through the envelope
int a = (int)tones.envStep; // integer part(like a floored float)
float b = tones.envStep - a; // decimal part(like doing a modulo)
// c allows us to calculate if we need to wrap around
int c = a + 1; // (like a ceiling of integer part)
if (c >= envLength) c = a; // don't wrap around
/////////////// LOOK UP ENVELOPE TABLE /////////////////
//uses table look-up with interpolation for both level and pitch envelopes
//'b' is a value interpolated between 2 successive samples 'a' and 'c')
//first, read values for the level envelope
float envValue = (1.0f - b)*tones.levelEnvelope + b*tones.levelEnvelope;
//then the pitch envelope
float pitchFactorValue = (1.0f - b)*tones.pitchEnvelope + b*tones.pitchEnvelope;
//Advance envelope pointer one step
tones.envStep += tones.envDelta;
//Turn note off at the end of the envelope.
if (((int)tones.envStep) >= envLength){
tones.state = STATE_INACTIVE;
continue;
}
//Precalculated Sine look-up table
a = (int)tones.phase; // integer part
b = tones.phase - a; // decimal part
c = a + 1;
if (c >= sineLength) c -= sineLength; // wrap around
///////////////// LOOK UP OF SINE TABLE ///////////////////
float sineValue = (1.0f - b)*sine + b*sine;
// Wrap round when we get to the end of the sine look-up table.
tones.phase += (tones.frequency * pitchFactorValue); // calculate frequency for each point in the pitch envelope
if (((int)tones.phase) >= sineLength)
tones.phase -= sineLength;
////////////////// RAMP NOTE OFF IF IT HAS BEEN UNPRESSED
if (tones.state == STATE_UNPRESSED) {
tones.gain -= 0.0001;
if ( tones.gain <= 0 ) {
tones.state = STATE_INACTIVE;
}
}
//////////////// FINAL SAMPLE VALUE ///////////////////
float s = sineValue * envValue * gain * tones.gain;
// Clip the signal, if needed.
if (s > 1.0f) s = 1.0f;
else if (s < -1.0f) s = -1.0f;
// Add the sample to the out-going signal
m += s;
}
// Write the sample mix to the buffer as a 16-bit word.
p = (SInt16)(m * 0x7FFF);
}
return frames;
}
</code></pre>
<p>我不确定这是否是一个红鲱鱼,但我在几个调试寄存器中遇到了 NaN。它似乎发生在计算 <code>fillBuffer</code> 中正弦查找的相位增量时(见上文)。该计算以 44.1 kHz 的采样率对每个样本进行多达十几个分音,并在 iPhone 4 上的 iOS 4 中运行。我在 iOS 9 的模拟器上运行。我所做的唯一更改在这篇文章中进行了描述! </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>结果证明我的 NaN 问题与 Core Audio 没有直接关系。这是由我的代码的另一个区域的更改引入的边缘条件引起的。真正的问题是在实时计算声音包络的持续时间时尝试除以零。</p>
<p>但是,在尝试确定该问题的原因时,我确信我的 iOS 7 之前的 Audio Session 已被基于 AVFoundation 的工作设置所取代。感谢我的初始代码的来源 <a href="https://github.com/hollance/AudioBufferPlayer" rel="noreferrer noopener nofollow">Matthijs Hollemans</a>以及 <a href="https://github.com/software-mariodiana/AudioBufferPlayer/wiki/Replacing-C-functions-deprecated-in-iOS-7" rel="noreferrer noopener nofollow">Mario Diana</a>他的博客解释了所需的更改。 </p>
<p>起初,我的 iPhone 上的音量明显低于模拟器上的音量,已解决的问题 <a href="https://stackoverflow.com/questions/18807157/how-do-i-route-audio-to-speaker-without-using-audiosessionsetproperty/18808124#18808124" rel="noreferrer noopener nofollow">here</a>由类型转换厂。我发现有必要通过替换 Mario 的 </p> 来包含这些改进
<pre><code> - (BOOL)setUpAudioSession
</code></pre>
<p>代工的</p>
<pre><code> - (void)configureAVAudioSession
</code></pre>
<p>希望这可能对其他人有所帮助。</p></p>
<p style="font-size: 20px;">关于ios - NaN 会导致这个核心音频 iOS 应用程序偶尔崩溃吗?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/37692276/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/37692276/
</a>
</p>
页:
[1]