AVRecorderまたはリアルタイムIOオーディオユニットのようなより低いものを使用できます。
「ボリューム」の概念はかなりあいまいです。ピーク値とRMS値の計算の違いを確認し、特定の時間(たとえば、VUメーターが使用する300ms)でRMS値を統合する方法を理解することをお勧めします。
基本的に、値のすべての2乗を合計します。平方根を取り、10 * log10f(sqrt(sum / num_samples))を使用してdBFSに変換しますが、20 * log10f(sum / num_samples)を使用すると、sqrtなしで1ステップで変換できます。
希望どおりに動作させるには、積分時間としきい値を大幅に調整する必要があります。
ピッチシフトについては、OpenALがトリックを実行すると思います。その背後にある手法は、帯域制限補間と呼ばれます-https://ccrma.stanford.edu/~jos/resample/Theory_Ideal_Bandlimited_Interpolation.html
この例は、移動平均としてのrms計算を示しています。循環バッファは二乗の履歴を維持し、操作ごとに二乗を合計する必要をなくします。私はそれを実行していないので、それを擬似コードとして扱います;)
例:
class VUMeter
{
protected:
// samples per second
float _sampleRate;
// the integration time in seconds (vu meter is 300ms)
float _integrationTime;
// these maintain a circular buffer which contains
// the 'squares' of the audio samples
int _integrationBufferLength;
float *_integrationBuffer;
float *_integrationBufferEnd;
float *_cursor;
// this is a sort of accumulator to make a running
// average more efficient
float _sum;
public:
VUMeter()
: _sampleRate(48000.0f)
, _integrationTime(0.3f)
, _sum(0.)
{
// create a buffer of values to be integrated
// e.g 300ms @ 48khz is 14400 samples
_integrationBufferLength = (int) (_integrationTime * _sampleRate);
_integrationBuffer = new float[_integrationBufferLength + 1];
bzero(_integrationBuffer, _integrationBufferLength);
// set the pointers for our ciruclar buffer
_integrationBufferEnd = _integrationBuffer + _integrationBufferLength;
_cursor = _integrationBuffer;
}
~VUMeter()
{
delete _integrationBuffer;
}
float getRms(float *audio, int samples)
{
// process the samples
// this part accumulates the 'squares'
for (int i = 0; i < samples; ++i)
{
// get the input sample
float s = audio[i];
// remove the oldest value from the sum
_sum -= *_cursor;
// calculate the square and write it into the buffer
double square = s * s;
*_cursor = square;
// add it to the sum
_sum += square;
// increment the buffer cursor and wrap
++_cursor;
if (_cursor == _integrationBufferEnd)
_cursor = _integrationBuffer;
}
// now calculate the 'root mean' value in db
return 20 * log10f(_sum / _integrationBufferLength);
}
};