Commit d5c3663

Nick committed on
Add proper LFSR noise channel stuff, change max/min to EnsureRange calls
commit d5c36635518066257c947e1b0ff05d2f8ce7484b parent a4d0661
3 changed files +35−12
ModifiedSound/sound.pas +27−1
@@ -5,6 +5,8 @@
5 | Stand: 15.12.2000 5 | Stand: 15.12.2000
6 | 6 |
7 | http://www.tu-ilmenau.de/~hackbart 7 | http://www.tu-ilmenau.de/~hackbart
8 |
9 | Translated from VGBC's Sound.cpp, written by Rusty Wagner
8 +----------------------------------------------------------------------------+} 10 +----------------------------------------------------------------------------+}
9 11
10 unit sound; 12 unit sound;
@@ -277,6 +279,30 @@ const
277 279
278 var 280 var
279 swpClk, envClk, lenClk, freqClk, freq4Clk: longint; 281 swpClk, envClk, lenClk, freqClk, freq4Clk: longint;
282 lfsr: Integer = 0;
283
284 // Yanked from SameBoy: https://github.com/LIJI32/SameBoy/blob/master/Core/apu.c#L489
285 // carrying on the tradition of translating existing C audio code
286 function NextLFSRBit(Narrow: Boolean): Integer;
287 var
288 HighBitMask: Integer;
289 NewHighBit: Integer;
290 begin
291 if Narrow then
292 HighBitMask := $4040
293 else
294 HighBitMask := $4000;
295
296 NewHighBit := ((lfsr xor (lfsr shr 1)) xor 1) and 1;
297 lfsr := lfsr shr 1;
298
299 if NewHighBit <> 0 then
300 lfsr := lfsr or HighBitMask
301 else
302 lfsr := lfsr and not HighBitMask;
303
304 Result := (lfsr and 1);
305 end;
280 306
281 procedure SoundUpdate(cycles: integer); 307 procedure SoundUpdate(cycles: integer);
282 var 308 var
@@ -582,7 +608,7 @@ begin
582 if (freq4Clk >= snd[4].Freq) then 608 if (freq4Clk >= snd[4].Freq) then
583 begin 609 begin
584 freq4Clk := freq4Clk mod snd[4].Freq; 610 freq4Clk := freq4Clk mod snd[4].Freq;
585 snd[4].Bit := random(255) and 1; // white noise 611 snd[4].Bit := NextLFSRBit((m_iram[$FF22] and %1000) <> 0);// random(255) and 1; // white noise
586 end; 612 end;
587 end; 613 end;
588 if (m_iram[$FF25] and 8) > 0 then 614 if (m_iram[$FF25] and 8) > 0 then
Modifiedtracker.pas +1−4
@@ -1279,10 +1279,7 @@ var
1279 begin 1279 begin
1280 if DrawingWave then begin 1280 if DrawingWave then begin
1281 Idx := Trunc((X / WaveEditPaintBox.Width)*32); 1281 Idx := Trunc((X / WaveEditPaintBox.Width)*32);
1282 CurrentWave^[Idx] := 1282 CurrentWave^[Idx] := EnsureRange(Trunc((Y / WaveEditPaintBox.Height)*$F), 0, $F);
1283 Min($F,
1284 Max(0,
1285 Trunc((Y / WaveEditPaintBox.Height)*$F)));
1286 WaveEditPaintBox.Invalidate; 1283 WaveEditPaintBox.Invalidate;
1287 end; 1284 end;
1288 end; 1285 end;
Modifiedtrackergrid.pas +7−7
@@ -576,10 +576,10 @@ end;
576 576
577 procedure TTrackerGrid.ClampCursors; 577 procedure TTrackerGrid.ClampCursors;
578 begin 578 begin
579 Cursor.Y := Min(High(TPattern), Max(Low(TPattern), Cursor.Y)); 579 Cursor.Y := EnsureRange(Cursor.Y, Low(TPattern), High(TPattern));
580 Cursor.X := Min(High(Patterns), Max(Low(Patterns), Cursor.X)); 580 Cursor.X := EnsureRange(Cursor.X, Low(TPattern), High(TPattern));
581 Other.Y := Min(High(TPattern), Max(Low(TPattern), Other.Y)); 581 Other.Y := EnsureRange(Other.Y, Low(TPattern), High(TPattern));
582 Other.X := Min(High(Patterns), Max(Low(Patterns), Other.X)); 582 Other.X := EnsureRange(Other.X, Low(TPattern), High(TPattern));
583 end; 583 end;
584 584
585 procedure TTrackerGrid.NormalizeCursors; 585 procedure TTrackerGrid.NormalizeCursors;
@@ -616,7 +616,7 @@ begin
616 for C := Cursor.X to Other.X do 616 for C := Cursor.X to Other.X do
617 if Patterns[C]^[R].Note <> NO_NOTE then 617 if Patterns[C]^[R].Note <> NO_NOTE then
618 Patterns[C]^[R].Note := 618 Patterns[C]^[R].Note :=
619 Max(LOWEST_NOTE, Min(HIGHEST_NOTE, Patterns[C]^[R].Note + Semitones)); 619 EnsureRange(Patterns[C]^[R].Note + Semitones, LOWEST_NOTE, HIGHEST_NOTE);
620 620
621 Invalidate; 621 Invalidate;
622 SaveUndoState; 622 SaveUndoState;
@@ -894,8 +894,8 @@ end;
894 894
895 function TTrackerGrid.MousePosToSelection(X, Y: Integer): TSelectionPos; 895 function TTrackerGrid.MousePosToSelection(X, Y: Integer): TSelectionPos;
896 begin 896 begin
897 X := Max(0, Min(Width-1, X)); 897 X := EnsureRange(Width-1, 0, X);
898 Y := Max(0, Min(Height-1, Y)); 898 Y := EnsureRange(Height-1, 0, Y);
899 899
900 Result.X := Trunc((X/Width)*NUM_COLUMNS); 900 Result.X := Trunc((X/Width)*NUM_COLUMNS);
901 Result.Y := Trunc((Y/Height)*NUM_ROWS); 901 Result.Y := Trunc((Y/Height)*NUM_ROWS);