-5

Based on Bass's documentation, I'm trying to load an ogg file with the code that follows:

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;

ShowMessage returns 5. Acording to documentation this is Handle error, what means that the error is in the music var. If I comment the lines below BASS_SampleLoad I get error 2. Means the file could not be loaded. So it is an ordinary OGG file. So my question: Am I doing something wrong?

Thanks in advance.

17
  • You don't appear to be checking the return values of the functions that you call Feb 20, 2014 at 21:01
  • 3
    You have to use the library properly. I for one won't attempt to help until you implement proper error checking. Chuck Norris can get away with not checking return values. The rest of us? Not so much. Feb 20, 2014 at 21:04
  • 3
    Please use a try..finally together with your open dialog!! Feb 20, 2014 at 21:19
  • 1
    What's the deal with the cross posting? pt.stackoverflow.com/questions/6778/… Feb 21, 2014 at 6:38
  • 2
    @whosrdaddy I just read this by Stack Team: "we expect almost every question asked on the Portuguese site to also be asked (and answered) on the English site" Maybe I interpreted wrong? About offending, in my opinion, people would get offended in the same way if I choose your answer instead of theirs.
    – Guill
    Feb 21, 2014 at 16:27

1 Answer 1

8

Your code seems to be correct, just make sure you check for errors on each call.

Fixed 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 sooner, your code fails because BASS_SampleLoad expects the filename in ANSI format, the documentation clearly mentions this, since you have Delphi XE3 and Strings are Unicode, you must supply 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);

UPDATE2

Per Sir Rufo's request: I added an exception based error checking routine to make the code cleaner and debugging more straightforward.

function BASS_ErrorToString(BASS_ErrorCode : Integer) : String;

begin

 case BASS_ErrorCode of :
  0: Result := 'BASS_OK';
  1: Result := 'BASS_ERROR_MEM';
  2: Result := 'BASS_ERROR_FILEOPEN';
  3: Result := 'BASS_ERROR_DRIVER';
  4: Result := 'BASS_ERROR_BUFLOST';
  5: Result := 'BASS_ERROR_HANDLE';
  6: Result := 'BASS_ERROR_FORMAT';
  7: Result := 'BASS_ERROR_POSITION';
  8: Result := 'BASS_ERROR_INIT';
  9: Result := 'BASS_ERROR_START';
  14: Result := 'BASS_ERROR_ALREADY';
  18: Result := 'BASS_ERROR_NOCHAN';
  19: Result := 'BASS_ERROR_ILLTYPE';
  20: Result := 'BASS_ERROR_ILLPARAM';
  21: Result := 'BASS_ERROR_NO3D';
  22: Result := 'BASS_ERROR_NOEAX';
  23: Result := 'BASS_ERROR_DEVICE';
  24: Result := 'BASS_ERROR_NOPLAY';
  25: Result := 'BASS_ERROR_FREQ';
  27: Result := 'BASS_ERROR_NOTFILE';
  29: Result := 'BASS_ERROR_NOHW';
  31: Result := 'BASS_ERROR_EMPTY';
  32: Result := 'BASS_ERROR_NONET';
  33: Result := 'BASS_ERROR_CREATE';
  34: Result := 'BASS_ERROR_NOFX';
  37: Result := 'BASS_ERROR_NOTAVAIL';
  38: Result := 'BASS_ERROR_DECODE';
  39: Result := 'BASS_ERROR_DX';
  40: Result := 'BASS_ERROR_TIMEOUT';
  41: Result := 'BASS_ERROR_FILEFORM';
  42: Result := 'BASS_ERROR_SPEAKER';
  43: Result := 'BASS_ERROR_VERSION';
  44: Result := 'BASS_ERROR_CODEC';
  45: Result := 'BASS_ERROR_ENDED';
  46: Result := 'BASS_ERROR_BUSY';
 else
  Result := 'BASS_ERROR_UNKNOWN';
 end;
end;

procedure BassResultCheck(ResultCode : Integer);
begin
 if ResultCode = 0 then
  begin
   ResultCode := BASS_ErrorGetCode;
   raise Exception.CreateFmt('BASS error : %s(%d)', [BASS_ErrorToString(ResultCode), ResultCode]);
  end;
end;

procedure BassBoolCheck(const BoolResult : Boolean);
begin
 BassResultCheck(Integer(BoolResult));
end;

procedure PlaySample(const SampleFilename : String);

var
  Sample : HSAMPLE; 
  Channel: HCHANNEL;


begin 
 if not Dynamic_Bass.Load_BASSDLL('Library/Bass.dll') then
  raise Exception.Create('Could not load BASS DLL');
 // change device to -1 which is the default device
 BassBoolCheck(Dynamic_Bass.BASS_Init(-1,44000,Bass_DEVICE_SPEAKERS,0,nil);
 Sample := BASS_SampleLoad(FALSE, PChar(SampleFilename), 0, 0, 3, BASS_SAMPLE_OVER_POS OR BASS_UNICODE);
 BassResultCheck(Sample);
 Channel := BASS_SampleGetChannel(Sample, False);
 BassResultCheck(Channel);
 BassBoolCheck(BASS_ChannelPlay(Channel, False));
end;
13
  • Output 2, as in the question anyway. At music <> 0 condition.
    – Guill
    Feb 21, 2014 at 4:35
  • 1
    Just did it! Offcourse I did not put the code into GT, that would be a mess :)
    – whosrdaddy
    Feb 21, 2014 at 6:35
  • 1
    I love that feeling to get 100 pontos for nothing :-) You got my voto even there.
    – TLama
    Feb 21, 2014 at 7:42
  • 1
    I would upvote again, but I already had - the question was already well answered in first place. Now it is a gem :)
    – Sir Rufo
    Feb 21, 2014 at 8:52
  • 1
    @Guil no need to apologize here! One golden rule, no one is never too old to learn something new each day ;)
    – whosrdaddy
    Feb 21, 2014 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.