📄 exstream.dpr
字号:
{*
* Example program for the Allegro library, by Shawn Hargreaves.
*
* This program shows how to use the audio stream functions to
* transfer large blocks of sample data to the soundcard. In
* this case, the sample data is generated during runtime,
* and the resulting sound reminds of a car engine when you
* are accelerating.
*}
program exStream;
uses
SysUtils, Allegro, SnesAPU;
const
BUFFER_SIZE = 4096;
procedure allegro_message(const Msg: String; const Params: array of const);
begin
allegro.allegro_message(PChar(Format(Msg, Params)));
end;
procedure textprintf_centre_ex(bmp: P_BITMAP; const f: P_FONT; x, y, color, bg: sint32; const fmt: String; const Params: array of const);
begin
allegro.textprintf_centre_ex(bmp, f, x, y, color, bg, PChar(Format(fmt, Params)));
end;
label
the_End;
var
stream : P_AUDIOSTREAM;
p : p_sint8s;
updates : sint32 = 0;
i, c : sint32;
spc : array[0..70000] of Byte;
fp : File;
begin
assign(fp, 'mmx3-04.spc');
reset(fp, 1);
blockread(fp, spc, 66088);
close(fp);
{ TODO -oUser -cConsole Main : Insert code here }
if allegro_init <> 0 then
Exit;
install_keyboard();
install_timer();
if set_gfx_mode(GFX_GDI, 320, 200, 0, 0) <> 0 then
begin
if set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) <> 0 then
begin
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message('Unable to set any graphic mode'#$D#$A'%s',
[allegro_error]);
Exit;
end;
end;
allegro.set_display_switch_mode(SWITCH_BACKGROUND);
set_palette(desktop_palette^);
clear_to_color(screen^, makecol(255, 255, 255));
//install a digital sound driver
if install_sound(DIGI_AUTODETECT, MIDI_NONE, nil) <> 0 then
begin
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message('Error initialising sound driver'#$D#$A'%s',
[allegro_error]);
Exit;
end;
// we want a _real_ sound driver
if digi_card^ = DIGI_NONE then
begin
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message('Unable to find a sound driver'#$D#$A'%s',
[allegro_error]);
Exit;
end;
// create an audio stream
APU.SetAPUOpt(MIX_INT, 2, 16, 44100, INT_LINEAR, DSP_NOECHO);
APU.SetDSPAmp(96);
APU.LoadSPCFile(@spc);
stream := play_audio_stream(BUFFER_SIZE, 16, true, 44100, 255, 128);
if stream = nil then
begin
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message('Error creating audio stream!', []);
Exit;
end;
textprintf_centre_ex(screen^, the_font^, SCREEN_W div 2, SCREEN_H div 3-24,
makecol(0, 0, 0), makecol(255, 255, 255),
'Audio stream is now playing...', []);
textprintf_centre_ex(screen^, the_font^, SCREEN_W div 2, SCREEN_H div 3,
makecol(0, 0, 0), makecol(255, 255, 255),
'Driver: %s', [the_digi_driver^^.name]);
textprintf_centre_ex(screen^, the_font^, SCREEN_W div 2, SCREEN_H div 3+24,
makecol(0, 0, 0), makecol(255, 255, 255),
'Press [space] to stop/resume', []);
while True do
begin
if keypressed then
begin
c := readkey();
if ((c shr 8) = KEY_SPACE) then
begin
voice_stop(stream^.voice);
while (TRUE) do
begin
c := readkey();
if ((c shr 8) = KEY_ESC) then
goto the_End
else if ((c shr 8) = KEY_SPACE) then
break;
end;
voice_start(stream^.voice);
end else
if ((c shr 8) = KEY_ESC) then
break;
end;
// does the stream need any more data yet?
p := get_audio_stream_buffer(stream);
if p <> nil then
begin
// if so, generate a bit more of our waveform...
textprintf_centre_ex(screen^, the_font^, SCREEN_W div 2, SCREEN_H*2 div 3,
makecol(0, 0, 0), makecol(255, 255, 255),
'update #%d', [updates]);
inc(updates);
Apu.EmuAPU(p, BUFFER_SIZE, true);
for i := 0 to BUFFER_SIZE - 1 do
begin
{
w := @p[i * 4 + 0];
w^ := w^ - 32768;
p[i * 4 + 0] := w^;
p[i * 4 + 1] := w^ shr 8;
w := @p[i * 4 + 2];
w^ := w^ - 32768;
p[i * 4 + 2] := w^;
p[i * 4 + 3] := w^ shr 8;
}
// p[i * 4 + 0] := p[i * 4 + 0] - 128;
p[i * 4 + 1] := p[i * 4 + 1] - 128;
// p[i * 4 + 2] := p[i * 4 + 2] - 128;
p[i * 4 + 3] := p[i * 4 + 3] - 128;
end;
free_audio_stream_buffer(stream);
end;
sleep(1);
end;
the_End:
stop_audio_stream(stream);
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -