delphi – Am I loading this Song correctly with Bass.dll?

Question:

Based on Bass documentation, I'm trying to load a regular ogg file, with the following code:

var
  FFile : string;
  Music: HSAMPLE; 
  ch: HCHANNEL;
  OpenDialog1 : TOpenDialog;
begin
  Dynamic_Bass.Load_BASSDLL('Library/Bass.dll');
  Dynamic_Bass.BASS_Init(1,44000,Bass_DEVICE_SPEAKERS,0,nil);  
  OpenDialog1 := TOpenDialog.Create(nil);
  if not OpenDialog1.Execute then
    Exit;
  ffile := OpenDialog1.FileName;
  Music := BASS_SampleLoad(FALSE, PChar(ffile), 0, 0, 3, BASS_SAMPLE_OVER_POS);
  ch := BASS_SampleGetChannel(Music, False);
  BASS_ChannelSetAttribute(ch, BASS_ATTRIB_PAN, 0);
  BASS_ChannelSetAttribute(ch, BASS_ATTRIB_VOL, 1);
  BASS_ChannelPlay(ch, False);
  ShowMessage(IntToStr(BASS_ErrorGetCode));
end;

It shows the number 5 which according to the documentation is an error in the handle , in this case my music variable. If I comment out these lines below BASS_SampleLoad , the code changes to 2, which means the file cannot be loaded. This is a regular file and it's not corrupted, so my question: am I doing something wrong?

Answer:

I don't speak Portuguese natively, I used Google Translate to create my answer.

Question Original English

Your code appears to be correct, just be sure to check each call for errors.

Corrected code:

var
  ffile : string;
  music: HSAMPLE; 
  ch: HCHANNEL;
  opendialog1 : topendialog;

begin 
 if not Dynamic_Bass.Load_BASSDLL('Library/Bass.dll') then
  Exit;
 // change device to -1 which is the default device
 if Dynamic_Bass.BASS_Init(-1,44000,Bass_DEVICE_SPEAKERS,0,nil) then
  begin
   opendialog1 := topendialog.Create(nil);
   try
    if OpenDialog1.Execute then
     begin
      ffile := OpenDialog1.FileName;
      music := BASS_SampleLoad(FALSE, PChar(ffile), 0, 0, 3, BASS_SAMPLE_OVER_POS);
      if music <> 0 then
       begin
        ch := BASS_SampleGetChannel(music, False);
        if ch <> 0 then
         begin
          // omitted, see if the basics work   
          // BASS_ChannelSetAttribute(ch, BASS_ATTRIB_PAN, 0);
          // BASS_ChannelSetAttribute(ch, BASS_ATTRIB_VOL, 1);
          if not BASS_ChannelPlay(ch, False) then
           ShowMessage(inttostr(bass_errorgetcode));
         end
        else
         ShowMessage(inttostr(bass_errorgetcode));
       end
      else
       ShowMessage(inttostr(bass_errorgetcode));
     end;
   finally  
    OpenDialog1.Free;
   end;
  end
 else
  ShowMessage(inttostr(bass_errorgetcode));
end;

UPDATE

I should have seen this before, your fault code because BASS_SampleLoad expects the filename to be in ANSI format, the documentation clearly mentions this , since you use Delphi XE3 and the Strings are Unicode, you must provide the BASS_UNICODE flag to indicate this.

So the Bass_SampleLoad line becomes:

music := BASS_SampleLoad(FALSE, PChar(ffile), 0, 0, 3, BASS_SAMPLE_OVER_POS 
          or BASS_UNICODE);
Scroll to Top