Board index » delphi » Sound Blaster Music

Sound Blaster Music

Yeah, I'm kinda new to the coding sceen and I was woundering if someone
could tell me how to do Turbo Pascal Sound Blaster Music, or even Turbo
Assembler would be great! I'm not taking a school class or anything, just
need some help with some music I want to play :) Thanx a million, Either
post on the newsgroup or mail me at WDKxDel...@aol.com
Later

 

Re:Sound Blaster Music


Quote
WDKxDelphi wrote:

> Yeah, I'm kinda new to the coding sceen and I was woundering if someone
> could tell me how to do Turbo Pascal Sound Blaster Music, or even Turbo
> Assembler would be great! I'm not taking a school class or anything, just
> need some help with some music I want to play :) Thanx a million, Either
> post on the newsgroup or mail me at WDKxDel...@aol.com
> Later

Here is a Unit to play voc-files with. it can only handle 8-bit sounds.
I have only tested it on a Soundblaster 16, But I guess it should work
on any SB clone. I have included a test program. Hope you'll find it of
any use.

{---CUT HERE-------------------------------------------CUT HERE------}

unit play;

interface
function reset_dsp(base : word) : boolean; {1=210h 2=220h}
procedure write_dac(level : byte);
function read_dac : byte;
function speaker_on: byte;
function speaker_off: byte;
procedure dma_pause;
procedure dma_continue;
procedure playback(sound : pointer; size : word; freq : word);
procedure loadvoc(filename : string; buf : pointer;var size,freq:word);
function playingvoc : boolean;

implementation
uses crt;

var dsp_reset        : word;
    dsp_read_data    : word;
    dsp_write_data   : word;
    dsp_write_status : word;
    dsp_data_avail   : word;
    since_midnight   : longint absolute $40:$6C;
    playing_till     : longint;

function reset_dsp(base : word) : boolean;

begin
 base := base * $10;
 dsp_reset        := base + $206;
 dsp_read_data    := base + $20a;
 dsp_write_data   := base + $20c;
 dsp_write_status := base + $20c;
 dsp_data_avail   := base + $20e;
 port[dsp_reset] := 1; delay(10);
 port[dsp_reset] := 0; delay(10);
 reset_dsp := (port[dsp_data_avail] and $80 = $80) and
 (port[dsp_read_data] = $aa);
end;

procedure write_dsp(value : byte);

begin
 while port[dsp_write_status] and $80 <> 0 do;
 port[dsp_write_data] := value;
end;

function read_dsp : byte;

begin
 while port[dsp_data_avail] and $80 = 0 do;
 read_dsp := port[dsp_read_data];
end;

procedure write_dac(level : byte);

begin
 write_dsp($10);
 write_dsp(level);
end;

function read_dac : byte;

begin
 write_dsp($20);
 read_dac := read_dsp;
end;

function speaker_on: byte;

begin
 write_dsp($d1);
end;

function speaker_off: byte;
begin
 write_dsp($d3);
end;

procedure dma_continue;
begin
 playing_till := since_midnight + playing_till;
 write_dsp($d4);
end;

procedure dma_pause;
begin
 playing_till := playing_till - since_midnight;
 write_dsp($d0);
end;

procedure playback(sound : pointer; size : word; freq : word);
 var time_constant : word;
     page          : word;
     offset        : word;
begin
 playing_till := since_midnight + round(size / freq * 18.20648193);
 speaker_on; size := size - 1;
 offset := seg(sound^) shl 4 + ofs(sound^);
 page := (seg(sound^) + ofs(sound^) shr 4) shr 12;
 port[$0a] := 5; port[$0c] := 0;
 port[$0b] := $49; port[$02] := lo(offset);
 port[$02] := hi(offset); port[$83] := page;
 port[$03] := lo(size); port[$03] := hi(size);
 port[$0a] := 1;
 time_constant := 256 - 1000000 div freq;
 write_dsp($40); write_dsp(time_constant);
 write_dsp($14); {8-bit}
 write_dsp(lo(size)); write_dsp(hi(size));
end;

procedure loadvoc(filename : string; buf : pointer;var size,freq:word);
var f : file;
    header : record
     signature  : array[1..20] of char;
     data_start : word;
     version    : integer;
     id         : integer;
    end;
    data : record
     id   : byte;
     len  : array[1..3] of byte;
     sr   : byte;
     pack : byte;
    end;

begin
 if pos('.', filename) = 0 then filename := filename + '.voc';
 assign(f, filename);
 reset(f, 1);
 blockread(f, header, 26);
 blockread(f, data, 6);
 freq:= round(1000000 / (256 - data.sr));
 size:= ord(data.len[3]) + ord(data.len[2]) * 256 + ord(data.len[1]) *
256 * 256;
 blockread(f, buf^, size);
 close(f);
end;

function playingvoc : boolean;

begin
  playingvoc := since_midnight > playing_till;
end;

begin
 if not reset_dsp(2) then
  begin
   writeln('SoundBlaster not found at 220h');
   halt(1);
  end;
end.

{---CUT HERE-------------------------------------------CUT HERE------}

program testplay;
uses crt, play;
var voc  : pointer;
    size,freq: word;
begin
 getmem(voc, 65535);
        {name without ext.!!!}
 loadvoc('name', voc,size,freq);
repeat
 playback(voc,size,freq);
  repeat until playingvoc;
until keypressed;
 freemem(voc, 65535);
end.

Other Threads