2

我正在尝试通过将音频流与 HRTF 进行卷积来使用 SDL_Mixer lib 制作 3D 声音(我分别使用 MIT HRTF 数据库和基于紧凑数据集的 mit_hrtf_lib)。

我使用 MIX_CHANNEL_POST 上的回调函数注册了一个效果。然后,在我的效果的回调函数中,我将音频流与 HRTF 进行卷积。

但我没有 3D 声音。我想播放的音频流听起来像是自己叠加的两倍甚至三倍,并且没有 3D 效果。

这就是我正在做的事情:

首先加载 wav,加载 HRTF 并开始音频:

if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 16384 ) == -1 ) {
    fprintf(stderr, "Couldn't open SDL_mixer", SDL_GetError()); return 0; }

myChunk = Mix_LoadWAV( "bombsBitrate176400Hz.wav" ); //load wav

if( myChunk == NULL) { 
    fprintf(stderr, "Couldn't load wav file", Mix_GetError()); return 0; 
}

myLoadHRTF = new LoadHRTF;
myHRTFs = myLoadHRTF->getHRTF(90,0,44100);//load hrtf => works

Mix_RegisterEffect(MIX_CHANNEL_POST, HRTFEffect, NULL, &myHRTFs);

while(1){
    if (canPlay){//virtual source is set and azimuth is calculated => I can start audio
        playAudio();
        canPlay = false;
    }
}

播放音频():

void PlaySDLAudio::playAudio(){
int channel;
channel = Mix_PlayChannel(-1,myChunk,0); //play audio once}

我注册效果的回调:

void HRTFEffect(int chan, void* stream, int len, void* udata){
Mix_Pause(-1);

short* Y;
short* X = (short*) stream;
LeftRightHRTF* myHRTFs = (LeftRightHRTF*) udata;
short* HL;
short* HR;

int m = len/2;//size= len/2 beause we use 2 bytes values (16 bit audio format => 2 byte), len is the size of stream in bytes
int n = myHRTFs->nTaps;

HL = new short[n+m];
HR = new short[n+m];
Y = new short[n+m-1];

for (int i = m/2; i < (m+n)/2; i++){// fill with 0 
    X[i*2] = 0;
    X[i*2+1] = 0;
}
for( int i = 0 ; i < n; i++ ){
    HL[i] = *myHRTFs->leftHRTF;
    myHRTFs->leftHRTF++;
    HR[i] = *myHRTFs->rightHRTF;
    myHRTFs->rightHRTF++;
}
for ( int i = n; i < n+m; i++ ){//fill rest with 0
    HL[i] = 0;
    HR[i] = 0;
}

for ( int i = 0; i < (n+m)/2-1; i++ ) { // convolution
    Y[i*2] = 0;
    Y[i*2+1] = 0;
    for ( int j = 0; j < m/2; j++ ) {
        if (i-j>=0){
            Y[i*2] = Y[i*2] + X[j*2] * HL[i-j];//stream for left speaker
            Y[i*2+1] = Y[i*2+1] + X[j*2+1] * HR[i-j];//stream for right speaker
        }
    }
}

stream = Y; //set result of convolution to stream

Mix_Resume(-1);

delete[] Y;
Y = 0;
delete[] HL;
HL = 0;
delete[] HR;
HR = 0;}

我猜只是说 stream = Y 是有问题的,因为我的卷积结果 Y 比原始流长(m+n-1,原始流 m)。

4

0 回答 0