使用HRTF(头部相关传输函数)使用SDL_Mixer进行3D声音

时间:2012-11-05 13:02:05

标签: sdl mixer

我正在尝试通过使用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;
    }
}

playAudio():

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)长。

0 个答案:

没有答案