Jump to content

  • Log In with Google      Sign In   
  • Create Account

CruzWoman

Member Since 07 Jul 2012
Offline Last Active Feb 16 2013 05:19 AM

Topics I've Started

3D sound with SDL_Mixer and convolution with a HRTF

06 November 2012 - 10:13 AM

Hello,

I'm new to SDL and also new to audioprocessing and have problems by generating 3D sound. I'using SDL_Mixer for the sound output and I'm trying to make 3D sound by convolution of a Mix_Chunk with a head related transfer function (HRTF). I tried it by registering an effect and make the convolution with the hrtf in the callback function of the effect. But I got no 3D sound, just the originally sound. Then I tried to make the convolution before playing the sound. I make the convolution of the HRTF directly with the Mix_Chunk->abuf. But when I play the sound, there is only noise...?

Is there a possibility to make 3D sound with my approach at all? I've loaded my wav file in Matlab and compared it with my loaded wav file by using Mix_LoadWav and wrote it in a .txt file. The data is not the same...?

Here's my convolution of the second approach (Mix_Chunk with HRTF):
Mix_Chunk* PlaySDLAudio::makeConvolution(LeftRightHRTF myHRTFs, Mix_Chunk* audioChunk){
short* Y;
short* HL;
short* HR;
int m = audioChunk->alen/2;// ? alen in byte, so /2 because short 16 bit = 2 byte ?
int n = myHRTFs.nTaps;
short* X = (short*)audioChunk->abuf;
HL = new short[n+m];
HR = new short[n+m];
Y = new short[n+m-1];
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;
}
//convolution
for ( int i = 0; i < (n+m-1)/2; i++ ) {
  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];
	Y[i*2+1] = Y[i*2+1] + X[j*2+1] * HR[i-j];
   }
  }
}

//load result in new Mix_Chunk
Mix_Chunk *wave_chunk;
if(!(wave_chunk=Mix_QuickLoad_RAW((Uint8*)Y,m+n))) {
	printf("Mix_QuickLoad_WAV: %s\n", Mix_GetError());// handle error
}
return wave_chunk;
}

I hope that someone has an idea where's my mistake or knows whether my first or second approach is a dead-end road...
Thanks a lot.
Greetings.

Drawing diagonal lines with SDL

07 July 2012 - 12:00 PM

Hello,
I tried to draw a diagonal line with SDL, but I only get a dashed horizontal line. I have no idea what's the problem. I tried a simple algorithm (commented out) and then I found some soucecode for the bresenham algorithm. Both draw only dashed horizontal lines (the first and half of the second line of the surface)
[source lang="cpp"]void ProcessSDL::DrawLine(SDL_Surface *screen, int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b){ //simple algorithm /*double x = x2 - x1; double y = y2 - y1; double length = sqrt( x*x + y*y ); double addx = x / length; double addy = y / length; x = x1; y = y1; if( SDL_MUSTLOCK( m_pScreen ) ) { SDL_LockSurface( m_pScreen ); } for(double i = 0; i < length; i += 1) { setSdlPixel( screen, (int)x, (int)y, c_green); x += addx; y += addy; } if( SDL_MUSTLOCK( m_pScreen ) ) { SDL_UnlockSurface( m_pScreen ); } SDL_Flip(screen);*/ bresenham_line(screen, x1, y1, x2, y2, c_green); SDL_Flip(screen);}void ProcessSDL::bresenham_line(SDL_Surface *screen, Uint32 x1, Uint32 y1, Uint32 x2, Uint32 y2, Uint32 color){ int lg_delta; int sh_delta; int cycle; int lg_step; int sh_step; lg_delta = x2 - x1; sh_delta = y2 - y1; lg_step = SGN(lg_delta); lg_delta = ABS(lg_delta); sh_step = SGN(sh_delta); sh_delta = ABS(sh_delta); if (sh_delta < lg_delta) { cycle = lg_delta >> 1; while (x1 != x2) { setSdlPixel(screen, x1, y1, color); cycle += sh_delta; if (cycle > lg_delta) { cycle -= lg_delta; y1 += sh_step; } x1 += lg_step; } setSdlPixel(screen, x1, y1, color); } cycle = sh_delta >> 1; while (y1 != y2) { setSdlPixel(screen, x1, y1, color); cycle += lg_delta; if (cycle > sh_delta) { cycle -= sh_delta; x1 += lg_step; } y1 += sh_step; } setSdlPixel(screen, x1, y1, color);}void ProcessSDL::setSdlPixel(SDL_Surface *screen, int x, int y, Uint32 color){ Uint32 *pixmem32; Uint32 colour; //colour = SDL_MapRGB( screen->format, r, g, b ); pixmem32 = (Uint32*) screen->pixels + y + x; *pixmem32 = color;}[/source]

My init_SDL looks like this. Perhaps here's the problem:
[source lang="cpp"]bool ProcessSDL::initSDL() { //Initialize all SDL subsystems if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false; } // atexit(SDL_Quit); // init screen SDL_putenv("SDL_VIDEO_CENTERED=center"); SDL_WM_SetCaption("Ear Tracker", NULL); m_pScreen = SDL_SetVideoMode( 1280,720, 32, SDL_SWSURFACE); if ( m_pScreen == NULL ) { return false; } //init colours c_white = SDL_MapRGB(m_pScreen->format, 255,255,255); c_gray = SDL_MapRGB(m_pScreen->format, 200,200,200); c_dgray= SDL_MapRGB(m_pScreen->format, 64,64,64); c_cyan = SDL_MapRGB(m_pScreen->format, 32,255,255); c_magenta = SDL_MapRGB(m_pScreen->format, 255,0,255); c_yellow = SDL_MapRGB(m_pScreen->format, 255,255,0); c_black = SDL_MapRGB(m_pScreen->format, 0,0,0); c_red = SDL_MapRGB(m_pScreen->format, 255,0,0); c_green = SDL_MapRGB(m_pScreen->format, 0,255,0); c_blue = SDL_MapRGB(m_pScreen->format, 0,0,255); //If everything initialized fine return true;}[/source]
Could anyone help me to find out whats wrong?
Thank you very much.
Greetings.

PARTNERS