home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / ModBass / Delphi / StreamTest / STMain.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-02-19  |  4.3 KB  |  169 lines

  1. unit STMain;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, BASS;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     Button2: TButton;
  13.     Button3: TButton;
  14.     GroupBox1: TGroupBox;
  15.     ScrollBar1: TScrollBar;
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     ScrollBar2: TScrollBar;
  19.     Label3: TLabel;
  20.     Label4: TLabel;
  21.     Label5: TLabel;
  22.     Label6: TLabel;
  23.     Button4: TButton;
  24.     Label7: TLabel;
  25.     Label8: TLabel;
  26.     procedure Button4Click(Sender: TObject);
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure Button1Click(Sender: TObject);
  29.     procedure Button2Click(Sender: TObject);
  30.     procedure Button3Click(Sender: TObject);
  31.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  32.     procedure ScrollBar1Change(Sender: TObject);
  33.     procedure ScrollBar2Change(Sender: TObject);
  34.   private
  35.     { Private-Deklarationen }
  36.     SineStream: HSTREAM;
  37.     procedure Error(msg: string);
  38.   public
  39.     { Public-Deklarationen }
  40.   end;
  41.  
  42. var
  43.   Form1: TForm1;
  44.   SineCount, Frequency, Amplitude: Real;
  45.  
  46. implementation
  47.  
  48. {$R *.DFM}
  49.  
  50. function MakeSine(handle: HSTREAM; buffer: Pointer; length: DWORD; user: DWORD): DWORD; stdcall;
  51. var
  52.   buf: ^WORD;
  53.   i, len: Integer;
  54. begin
  55.   buf := buffer;
  56.   len := length div 2;
  57.   // write the sine function to the output stream
  58.   for i := 0 to len - 1 do begin
  59.     buf^ := Trunc(Sin(SineCount * PI) * Amplitude);
  60.     Inc(buf);
  61.     SineCount := SineCount + (Frequency / 44100);
  62.   end;
  63.   Result := length;
  64. end;
  65.  
  66. procedure TForm1.Error(msg: string);
  67. var
  68.   s: string;
  69. begin
  70.   // add the error code to the output string
  71.   s := msg + #13#10 + '(error code: ' + IntToStr(BASS_ErrorGetCode) + ')';
  72.   MessageBox(handle, PChar(s), 'BASS Error', MB_OK or MB_ICONERROR);
  73. end;
  74.  
  75. procedure TForm1.Button4Click(Sender: TObject);
  76. begin
  77.   Close;
  78. end;
  79.  
  80. procedure TForm1.FormCreate(Sender: TObject);
  81. begin
  82.   // enable the BASS Init button
  83.   Button1.Enabled := TRUE;
  84. end;
  85.  
  86. procedure TForm1.Button1Click(Sender: TObject);
  87. begin
  88.   // do we have the right BASS version?
  89.   if BASS_GetVersion <> MAKELONG(0,8) then begin
  90.     Error('BASS version 0.8 was not loaded');
  91.     Exit;
  92.   end;
  93.   // Initialize BASS with the default device
  94.   if not BASS_Init(-1, 44100, 0, handle) then begin
  95.     Error('Could not initialize BASS');
  96.     Exit;
  97.   end;
  98.   // start BASS
  99.   if not BASS_Start then begin
  100.     Error('Could not start the BASS output');
  101.     Exit;
  102.   end;
  103.   // if all successful, enable the create stream button
  104.   Button1.Enabled := FALSE;
  105.   Button2.Enabled := TRUE;
  106. end;
  107.  
  108. procedure TForm1.Button2Click(Sender: TObject);
  109. begin
  110.   (*
  111.     create a stream with a sample rate of 44100Hz
  112.     the max. output rate is sample rate / 2
  113.     i.e. we have a 22050Hz stream!
  114.     however, we'll set the max. output frequency
  115.     of the sine wave lower becouse the human
  116.     ear isn't able to hear waves above 16KHz...
  117.   *)
  118.   SineStream := BASS_StreamCreate(44100, 0, MakeSine, 0);
  119.   if (SineStream = NULL) then begin
  120.     Error('Could not create user stream');
  121.     Exit;
  122.   end;
  123.   // if successfully called, enable the play stream button
  124.   Button2.Enabled := FALSE;
  125.   Button3.Enabled := TRUE;
  126. end;
  127.  
  128. procedure TForm1.Button3Click(Sender: TObject);
  129. begin
  130.   // reset the sine counter
  131.   SineCount := 0;
  132.   // initialize the amplitude and the frequency
  133.   Frequency := ScrollBar1.Position;
  134.   Amplitude := ScrollBar2.Position;
  135.   if not BASS_StreamPlay(SineStream, FALSE, 0) then begin
  136.     Error('Could not start stream playback');
  137.     Exit;
  138.   end;
  139.   // enable the potentiometers
  140.   Button3.Enabled := FALSE;
  141.   GroupBox1.Enabled := TRUE;
  142. end;
  143.  
  144. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  145. begin
  146.   // stop BASS...
  147.   BASS_Stop;
  148.   // and release it
  149.   // the stream will be released automatically
  150.   BASS_Free;
  151. end;
  152.  
  153. procedure TForm1.ScrollBar1Change(Sender: TObject);
  154. begin
  155.   // update the output frequency
  156.   Frequency := ScrollBar1.Position;
  157.   Label7.Caption := IntToStr(ScrollBar1.Position) + 'Hz';
  158. end;
  159.  
  160. procedure TForm1.ScrollBar2Change(Sender: TObject);
  161. begin
  162.   // update the output amplitude
  163.   Amplitude := ScrollBar2.Position;
  164.   Label8.Caption := IntToStr(ScrollBar2.Position * 100 div 32767) + '%';
  165. end;
  166.  
  167.  
  168. end.
  169.