2012-04-03 9 views
3

iOSのシンセサイザーを作成中です。周りを遊んでコアオーディオを習得しようとすると、頭がおかしくないという問題が発生しました。私の正弦波は一定の間隔でクリックノイズを発生させますが、Imの推測は位相に関連しています。私はそのテーマに関するいくつかのガイドと本を見てきましたが、私はそれを正しくやっていることを示唆しています。iOSサイン波生成 - 可聴クリック

誰かが私のコードを見て親切であれば、大変感謝します。

static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) 
{ 


    // Get a reference to the object that was passed with the callback 
    // In this case, the AudioController passed itself so 
    // that you can access its data. 
    AudioController *THIS = (AudioController*)inRefCon; 

    // Get a pointer to the dataBuffer of the AudioBufferList 
    AudioSampleType *outA = (AudioSampleType *)ioData->mBuffers[0].mData; 

    float freq = THIS->Frequency; 
    float phase = THIS->sinPhase; 

    float envValue; 
    float sinSignal; 

    // The amount the phase changes in single sample 
    double phaseIncrement = 2 * M_PI * freq/kGraphSampleRate; 

    // Loop through the callback buffer, generating samples 
    for (UInt32 i = 0; i < inNumberFrames; ++i) {  

     sinSignal = sin(phase); 

     envValue = THIS->env.tick(); 

     // Put the sample into the buffer 
     // Scale the -1 to 1 values float to 
     // -32767 to 32767 and then cast to an integer 
     outA[i] = (SInt16)(((sinSignal * 32767.0f)/2) * envValue); 

     phase = phase + phaseIncrement; 

     if (phase >= (2 * M_PI * freq)) { 
      phase = phase - (2 * M_PI * freq); 
     } 
    } 

    // Store the phase for the next callback. 
    THIS->sinPhase = phase; 

    return noErr; 
} 

答えて

5

相が間違ってポイント

にオーバーフローすることができ、これを置き換えます

if (phase >= (2 * M_PI * freq)) { 
    phase = phase - (2 * M_PI * freq); 
} 

if (phase >= (2 * M_PI)) { 
    phase = phase - (2 * M_PI); 
} 
+0

ありがとう、これは動作します! – Mordapps

2

であなたの頻度は、このラインを正確に整数値ではない場合:

phase = phase - (2 * M_PI * freq); 

は、2piと等しくない量だけ位相を調整して回転させるため、不連続性が生じます。

+0

どうもありがとうhotpaw2 – Mordapps

1

これらのタイプの問題をデバッグするには、オシロスコープまたはウェーブエディタでオーディオを確認することが重要です。正確にクリックが発生する時期と頻度を確認すると、通常、クリックが発生している理由がわかります。

エンベロープジェネレータなどの「オーディオ」信号ではない信号でも、同じテクニックを使用できます。スピーカーを必ずオフにしてください。

関連する問題