DirectSound Sound-Synthesis, Streaming, best way?

Started by
-1 comments, last by mineralaktiv 15 years, 5 months ago
hello, i have a question with streaming sounds by directsound. i'm coding an sound game and it is almost finish. you can have a look here:
">
">
">
all the sounds are generated in realtime (FM-Synthesis) and streamed via directSound. my problem/question is that my streamingbuffer is round about 100ms big and because there are so many oscillators to calculate, every 100ms the framerate of my game is breaking. that happens because i'm checking in every frame if the buffer is ready to fill. when the buffer is ready all the soundcalculations for the next 100ms are done and so this single frame will take much more time than the others. so the calculation of the framerate is going to be strange (i use the framerate for framerateundependent programming of the movements of all the graphical objects). usally the framrate is 60 (the rate of my monitor) but every half second the framerate is rising, which is strange because this rising of the framerate depends on the soundcalculations (when i disable the soundcalculations the framerate is stable). i think the framerate should fall when a frame is taken more time because of the soundcalculations. i wonder where my mistake is. when i use an external framecounter (fraps) it shows allways stable 60 frames even when the sound is on. so there must be an error in my code. but i cant find it.


void TicTimer(void) {
	double static FPS, tmpFPS;
	double static AvarageFPSStartTime;
	LONGLONG static lastFrame;
	LONGLONG CurrentFrame;
	double static AvarageFPSCounter;

	if (freq > 0) {
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentFrame);
		elapsed = CurrentFrame - lastFrame;
		if(elapsed > 0) FPS = (double)((double)freq / (double)elapsed);
		else FPS = 1;
	} else {
		timeBeginPeriod(1);
		CurrentFrame = timeGetTime();
		timeEndPeriod(1);
		elapsed = CurrentFrame - lastFrame;
		if(elapsed > 0) FPS = 1000.0/elapsed;
		else FPS = 1;
	}
	
	if ((AvarageFPSStartTime + 500)>=(double)GetTickCount()) {
		AvarageFPSCounter++;
		tmpFPS+=FPS;
	} else {
		if(AvarageFPSCounter > 0.0) AverageFPS=tmpFPS/AvarageFPSCounter;
		else AverageFPS = 60.0;

		fFrameRate = AverageFPS;
		AvarageFPSCounter=0.0;
		tmpFPS=0.0;
		AvarageFPSStartTime=GetTickCount();
		fFrameFactor = 60.0f/fFrameRate;
	}

	lastFrame = CurrentFrame;
}
i hope this was comprehensible. and i would be very pleased if anybode could help me because the release of the game will be in two weeks. greets, andi general question: what is the best way for realtime sound synthesis. should i put the stremingbuffer-ready check and soundcalculations in a seperate thread?

This topic is closed to new replies.

Advertisement