Commit def7dda

Nick committed on
Fixing bug where sound buffer would grow too fast and start lagging
commit def7dda98832c21679dec97dbb3c8e83f4985801 parent a7cf88a
3 changed files +13−17
ModifiedSound/sound.pas +10−14
@@ -131,6 +131,8 @@ procedure DisableSound;
131 procedure SoundUpdate(cycles: integer); 131 procedure SoundUpdate(cycles: integer);
132 procedure SoundSetCycles(n: integer); 132 procedure SoundSetCycles(n: integer);
133 133
134 function SoundBufferTooFull: Boolean;
135
134 var 136 var
135 soundEnable: boolean; 137 soundEnable: boolean;
136 sndRegChange: boolean; 138 sndRegChange: boolean;
@@ -154,13 +156,20 @@ uses vars, bass;
154 156
155 const 157 const
156 sampleCycles: longint = (8192 * 1024) div 22050; 158 sampleCycles: longint = (8192 * 1024) div 22050;
159 TooFullThreshold: DWORD = 5120; // (0.2s)
157 160
158 var 161 var
159 playStream: HStream; 162 playStream: HStream;
160 ready: integer;
161 bufPos, bufCycles, bufLVal, bufRVal: integer; 163 bufPos, bufCycles, bufLVal, bufRVal: integer;
162 buf: array[0..2047] of byte; 164 buf: array[0..2047] of byte;
163 165
166 // HACK: This is kind of a hack. We should probably figure out a way to
167 // tie playback rate to the emulation function instead of this, but it works.
168 function SoundBufferTooFull: Boolean;
169 begin
170 Result := BASS_ChannelGetData(PlayStream, nil, BASS_DATA_AVAILABLE) > TooFullThreshold;
171 end;
172
164 procedure EnableSound; 173 procedure EnableSound;
165 begin 174 begin
166 if soundEnable then 175 if soundEnable then
@@ -176,8 +185,6 @@ begin
176 BASS_ChannelPlay(PlayStream, True); 185 BASS_ChannelPlay(PlayStream, True);
177 186
178 soundEnable := True; 187 soundEnable := True;
179 ready := 3;
180 bufPos := 0;
181 bufCycles := 0; 188 bufCycles := 0;
182 bufLVal := 0; 189 bufLVal := 0;
183 bufRVal := 0; 190 bufRVal := 0;
@@ -209,7 +216,6 @@ begin
209 if bufPos >= 2048 then 216 if bufPos >= 2048 then
210 begin 217 begin
211 BASS_StreamPutData(PlayStream, @buf, 2048); 218 BASS_StreamPutData(PlayStream, @buf, 2048);
212 Dec(ready);
213 bufPos := 0; 219 bufPos := 0;
214 end; 220 end;
215 end; 221 end;
@@ -549,16 +555,6 @@ begin
549 Dec(r, vol[snd[4].Vol]); 555 Dec(r, vol[snd[4].Vol]);
550 end; 556 end;
551 557
552 {l:=l shr 2;
553 if (shortint(l)<-128) then l:=-128;
554 if (shortint(l)>127) then l:=127;
555 inc(l,$80);
556
557 r:=r shr 2;
558 if (shortint(r)<-128) then r:=-128;
559 if (shortint(r)>127) then r:=127;
560 inc(r,$80);}
561
562 l := shortint(l shr 2) + 128; 558 l := shortint(l shr 2) + 128;
563 r := shortint(r shr 2) + 128; 559 r := shortint(r shr 2) + 128;
564 SoundOutBits(l, r, cycles) 560 SoundOutBits(l, r, cycles)
ModifiedUnitMain.lfm +2−2
@@ -1,7 +1,7 @@
1 object frmGameboy: TfrmGameboy 1 object frmGameboy: TfrmGameboy
2 Left = 572 2 Left = 1200
3 Height = 354 3 Height = 354
4 Top = 127 4 Top = 83
5 Width = 318 5 Width = 318
6 Caption = 'Gameboy' 6 Caption = 'Gameboy'
7 ClientHeight = 334 7 ClientHeight = 334
ModifiedUnitMain.pas +1−1
@@ -244,7 +244,7 @@ begin
244 frameEnd := li.QuadPart; 244 frameEnd := li.QuadPart;
245 245
246 frameElapsedInSec := (frameEnd - frameStart) / tickFreq; 246 frameElapsedInSec := (frameEnd - frameStart) / tickFreq;
247 until frameElapsedInSec > TimePerFrame; 247 until (frameElapsedInSec > TimePerFrame) and (not SoundBufferTooFull);
248 248
249 frameStart := frameEnd; 249 frameStart := frameEnd;
250 until application.Terminated; 250 until application.Terminated;