/music.pas
unit Music;
interface

type PsgData = record
        data: PChar;
        len: Word;
    end;

procedure LoadMusic(const filename: String);
procedure SfxTakeoff;

var currentSong: PsgData;

implementation

uses Util;

const takeoff: array[0..113] of Byte = (
    $04, $e7, $c0, $03, $fd, $84, $03, $c7, $03, $fb, $84, $03, $c0, $04, $f9, $84,
    $03, $c7, $04, $f7, $84, $03, $c0, $05, $f5, $84, $03, $c7, $05, $f3, $84, $03,
    $c0, $06, $f1, $84, $03, $c7, $06, $f0, $84, $02, $c0, $07, $84, $02, $c7, $07,
    $84, $02, $c0, $08, $84, $02, $c7, $08, $8d, $02, $ca, $08, $8d, $03, $cd, $08,
    $f1, $8d, $03, $c0, $09, $f3, $8d, $03, $c3, $09, $f5, $8d, $03, $c6, $09, $f7,
    $8d, $03, $c9, $09, $f8, $8d, $03, $cc, $09, $f9, $8d, $03, $cf, $09, $fa, $8d,
    $03, $c2, $0a, $fb, $8d, $03, $c5, $0a, $fc, $8d, $03, $c8, $0a, $fd, $8d, $01,
    $ff, $ff
);

procedure LoadMusic(const filename: String);
var i, actualLen: Word;
    f: File;
begin
    { clean up existing song }
    if currentSong.data <> nil then
        FreeMem(currentSong.data, currentSong.len);

    { load new song }
    Assign(f, filename);
    Reset(f, 1);
    currentSong.len := FileSize(f);
    GetMem(currentSong.data, currentSong.len);
    BlockRead(f, currentSong.data^, currentSong.len, actualLen);
    Close(f);
    if actualLen < currentSong.len then
        Die('could not load all of ' + filename);
    musicStartPtr := currentSong.data;
    musicDataPtr := currentSong.data;
    musicDelayCount := 0;
    musicPlaying := false;
    musicEndPtr := musicStartPtr + currentSong.len;
end;

procedure SfxTakeoff;
begin
    musicStartPtr := @takeoff;
    musicDataPtr := musicStartPtr;
    musicEndPtr := musicStartPtr + SizeOf(takeoff);
    musicDelayCount := 0;
    musicPlaying := true;
end;

end.