home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 November / CDVD1105.ISO / Software / Freeware / programare / bass / Delphi / Speakers / Unit1.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2005-09-15  |  4.5 KB  |  180 lines

  1. unit Unit1;
  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.     GroupBox1: TGroupBox;
  12.     Button1: TButton;
  13.     GroupBox2: TGroupBox;
  14.     Button2: TButton;
  15.     GroupBox3: TGroupBox;
  16.     Button3: TButton;
  17.     GroupBox4: TGroupBox;
  18.     Button4: TButton;
  19.     OpenDialog1: TOpenDialog;
  20.     Button5: TButton;
  21.     Button6: TButton;
  22.     Button7: TButton;
  23.     procedure FormCreate(Sender: TObject);
  24.     procedure FormDestroy(Sender: TObject);
  25.     procedure Button1Click(Sender: TObject);
  26.     procedure Button5Click(Sender: TObject);
  27.   private
  28.     { Private declarations }
  29.   public
  30.     { Public declarations }
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36.   flags: array[0..3] of DWORD = (BASS_SPEAKER_FRONT, BASS_SPEAKER_REAR, BASS_SPEAKER_CENLFE, BASS_SPEAKER_REAR2);
  37.   chan: array[0..3] of HSTREAM;
  38.  
  39. implementation
  40.  
  41. {$R *.dfm}
  42.  
  43. procedure TForm1.FormCreate(Sender: TObject);
  44. var
  45.   i: BASS_INFO;
  46. begin
  47.   // Check that BASS 2.2 was loaded
  48.   if (BASS_GetVersion <> DWORD(MAKELONG(2,2))) then
  49.   begin
  50.     MessageBox(0, 'BASS version 2.2 was not loaded', 'Incorrect BASS.DLL', 0);
  51.     Halt;
  52.   end;
  53.  
  54.   // initialize BASS - default device
  55.   if (not BASS_Init(-1, 44100, 0, Handle, nil)) then
  56.   begin
  57.     MessageBox(0, 'Can''t initialize device', 'Error', 0);
  58.     Halt;
  59.   end;
  60.  
  61.   // check how many speakers the device supports
  62.   BASS_GetInfo(i);
  63.   if (i.speakers < 4) then // no extra speakers detected, enable them anyway?
  64.   begin
  65.     if (MessageBox(0, 'Do you wish to enable "speaker assignment" anyway?', 'No extra speakers detected', MB_ICONQUESTION or MB_YESNO) = IDYES) then
  66.     begin
  67.       // reinitialize BASS - forcing speaker assignment
  68.       BASS_Free;
  69.       if (not BASS_Init(-1, 44100, BASS_DEVICE_SPEAKERS, Handle, nil)) then
  70.       begin
  71.         MessageBox(0, 'Can''t initialize device', 'Error', 0);
  72.         Halt;
  73.       end;
  74.       BASS_GetInfo(i); // get info again
  75.     end;
  76.   end;
  77.  
  78.   if (i.speakers < 8) then
  79.   begin
  80.     Button4.Enabled := False;
  81.     Button7.Enabled := False;
  82.   end;
  83.  
  84.   if (i.speakers < 6) then
  85.   begin
  86.     Button3.Enabled := False;
  87.     Button6.Enabled := False;
  88.   end;
  89.  
  90.   if (i.speakers < 4) then
  91.   begin
  92.     Button2.Enabled := False;
  93.     Button5.Enabled := False;
  94.     // no multi-speaker support, so remove speaker flag for normal stereo output
  95.     flags[0] := 0;
  96.   end;
  97. end;
  98.  
  99. procedure TForm1.FormDestroy(Sender: TObject);
  100. begin
  101.   BASS_Free;
  102. end;
  103.  
  104. procedure TForm1.Button1Click(Sender: TObject);
  105. var
  106.   speaker: Integer;
  107. begin
  108.   speaker := TButton(Sender).Tag;
  109.   if (not OpenDialog1.Execute) then
  110.     Exit;
  111.  
  112.   BASS_StreamFree(chan[speaker]); // free old stream before opening new
  113.   chan[speaker] := BASS_StreamCreateFile(False, PChar(OpenDialog1.FileName), 0, 0, flags[speaker] or BASS_SAMPLE_LOOP);
  114.   if (chan[speaker] = 0) then
  115.   begin
  116.     TButton(Sender).Caption := 'click here to open a file...';
  117.     MessageBox(0, 'Can''t play the file', 'Error', 0);
  118.     Exit;
  119.   end;
  120.  
  121.   TButton(Sender).Caption := OpenDialog1.FileName;
  122.   BASS_ChannelPlay(chan[speaker], False);
  123. end;
  124.  
  125. procedure TForm1.Button5Click(Sender: TObject);
  126. var
  127.   i: BASS_CHANNELINFO;
  128.   speaker: Integer;
  129.   temp: HSTREAM;
  130.   temp1, temp2: String;
  131. begin
  132.   speaker := TButton(Sender).Tag;
  133.  
  134.   // swap handles
  135.   temp := chan[speaker];
  136.   chan[speaker] := chan[speaker+1];
  137.   chan[speaker+1] := temp;
  138.  
  139.   // swap text
  140.   case speaker of
  141.     0: // swap 1 and 2
  142.     begin
  143.       temp1 := Button1.Caption;
  144.       temp2 := Button2.Caption;
  145.       Button1.Caption := temp2;
  146.       Button2.Caption := temp1;
  147.     end;
  148.     1: // swap 2 and 3
  149.     begin
  150.       temp1 := Button2.Caption;
  151.       temp2 := Button3.Caption;
  152.       Button2.Caption := temp2;
  153.       Button3.Caption := temp1;
  154.     end;
  155.     2: // swap 3 and 4
  156.     begin
  157.       temp1 := Button3.Caption;
  158.       temp2 := Button4.Caption;
  159.       Button3.Caption := temp2;
  160.       Button4.Caption := temp1;
  161.     end;
  162.   end;
  163.  
  164.   // update speaker flags
  165.   if (BASS_ChannelGetInfo(chan[speaker],i)) then // get the flags
  166.   begin
  167.     i.flags := i.flags and (not $3F000000); // clear all speaker flags
  168.     i.flags := i.flags or flags[speaker];
  169.     BASS_ChannelSetFlags(chan[speaker], i.flags); // update flags
  170.   end;
  171.   if (BASS_ChannelGetInfo(chan[speaker+1],i)) then
  172.   begin
  173.     i.flags := i.flags and (not $3F000000);
  174.     i.flags := i.flags or flags[speaker+1];
  175.     BASS_ChannelSetFlags(chan[speaker+1], i.flags);
  176.   end;
  177. end;
  178.  
  179. end.
  180.