浏览代码

Merge pull request #762 from ftk/pitchfix

Fix audio pitch
Ray 6 年之前
父节点
当前提交
854e5d2f7e
共有 1 个文件被更改,包括 4 次插入2 次删除
  1. 4 2
      src/raudio.c

+ 4 - 2
src/raudio.c

@@ -712,11 +712,13 @@ void SetAudioBufferPitch(AudioBuffer *audioBuffer, float pitch)
         return;
         return;
     }
     }
 
 
-    audioBuffer->pitch = pitch;
+    float pitchMul = pitch / audioBuffer->pitch;
 
 
     // Pitching is just an adjustment of the sample rate. Note that this changes the duration of the sound - higher pitches
     // Pitching is just an adjustment of the sample rate. Note that this changes the duration of the sound - higher pitches
     // will make the sound faster; lower pitches make it slower.
     // will make the sound faster; lower pitches make it slower.
-    mal_uint32 newOutputSampleRate = (mal_uint32)((((float)audioBuffer->dsp.src.config.sampleRateOut / (float)audioBuffer->dsp.src.config.sampleRateIn) / pitch) * audioBuffer->dsp.src.config.sampleRateIn);
+    mal_uint32 newOutputSampleRate = (mal_uint32)((float)audioBuffer->dsp.src.config.sampleRateOut / pitchMul);
+    audioBuffer->pitch *= (float)audioBuffer->dsp.src.config.sampleRateOut / newOutputSampleRate;
+
     mal_dsp_set_output_sample_rate(&audioBuffer->dsp, newOutputSampleRate);
     mal_dsp_set_output_sample_rate(&audioBuffer->dsp, newOutputSampleRate);
 }
 }