我正在使用Oboe C++ 库在我的 android 应用程序中播放声音。我想改变我的音频样本的音高。所以,我开始创建“ mPos ”浮点值来保存当前播放的帧,并在每一步添加“ mPitch ”值。
似乎使用新的音高可以正确播放音频,但是当音高高(例如 1.2)并发出奇怪的噪音以及音高低(例如 0.212)时,它是自身的两倍。
这是我第一次进行音频编程,在发布这个问题之前我做了很多研究。我什至直接向“双簧管”支持发送消息但没有响应。有谁知道如何正确实施 Pitch?
流长度始终为192
channelCount总是2
代码:
void Player::renderAudio(float *stream, int32_t streamLength){
const int32_t channelCount = mSound->getChannelCount();
if (mIsPlaying){
float framesToRenderFromData = streamLength ;
float totalSourceFrames = mSound->getTotalFrames()/mPitch;
const float *data = mSound->getData();
// Check whether we're about to reach the end of the recording
if (mPos + streamLength >= totalSourceFrames ){
framesToRenderFromData = (totalSourceFrames - mPos);
mIsPlaying = false;
}
for (int i = 0; i < framesToRenderFromData; ++i) {
for (int j = 0; j < channelCount; ++j) {
if(j % 2 == 0){
stream[(i*channelCount)+j] = (data[((size_t)mPos * channelCount)) + j] * mLeftVol) * mVol;
}else{
stream[(i*channelCount)+j] = (data[((size_t)mPos * channelCount)) + j] * mRightVol) * mVol;
}
}
mPos += mPitch;
if(mPos >= totalSourceFrames){
mPos = 0;
}
}
if (framesToRenderFromData < streamLength){
renderSilence(&stream[(size_t)framesToRenderFromData], streamLength * channelCount);
}
} else {
renderSilence(stream, streamLength * channelCount);
}
}
void Player::renderSilence(float *start, int32_t numSamples){
for (int i = 0; i < numSamples; ++i) {
start[i] = 0;
}
}
void Player::setPitch(float pitchData){
mPitch = pitchData;
};