私のアプリは基本的に次々とノートを再生し、毎回どのノートが再生されているかを表示したいと考えています。各音符は UI の四角形であり、再生中の音符に向かって左から右に移動する 1 ピクセルのビューがあります。
問題は、これら 2 つのことを同時に行う方法がわからないことです。サンプル レンダリング関数からメッセージを送信できません。これは、速度が遅くなり、オーディオ再生に不具合が生じる可能性があるためです。再生されているものを反映するように UI を更新する方法を提案する人はいますか?
私の音楽再生コードはこの例に基づいており、RenderTone メソッドは別のスレッドで実行されていると思います。1000 個のサンプルが再生されるたびに 1 ピクセルのビューを 1 ピクセル移動したいのですが (一例にすぎませんが、もっと少なくすることもできます)、UI にメッセージを送信し、サンプル数の更新を送信する方法がわかりません。再生されました。
したがって、以下のコードのバリエーションが実行されている間に、何らかの方法で UI を更新する必要があります。
OSStatus RenderTone(
void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
// Fixed amplitude is good enough for our purposes
const double amplitude = 0.25;
// Get the tone parameters out of the view controller
ToneGeneratorViewController *viewController =
(ToneGeneratorViewController *)inRefCon;
double theta = viewController->theta;
double theta_increment =
2.0 * M_PI * viewController->frequency / viewController->sampleRate;
// This is a mono tone generator so we only need the first buffer
const int channel = 0;
Float32 *buffer = (Float32 *)ioData->mBuffers[channel].mData;
// Generate the samples
for (UInt32 frame = 0; frame < inNumberFrames; frame++)
{
buffer[frame] = sin(theta) * amplitude;
theta += theta_increment;
if (theta > 2.0 * M_PI)
{
theta -= 2.0 * M_PI;
}
}
// Store the updated theta back in the view controller
viewController->theta = theta;
return noErr;
}