aboutsummaryrefslogtreecommitdiffstats
path: root/medley_new/src/media/UAudioDecoder_Bass.pas
blob: d6d2425af16f90a099f8a699e89f8e59e013f2dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
{* UltraStar Deluxe - Karaoke Game
 *
 * UltraStar Deluxe is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *}

unit UAudioDecoder_Bass;

interface

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}

{$I switches.inc}

implementation

uses
  Classes,
  SysUtils,
  bass,
  UMain,
  UMusic,
  UAudioCore_Bass,
  ULog,
  UPath;

type
  TBassDecodeStream = class(TAudioDecodeStream)
    private
      Handle: HSTREAM;
      FormatInfo : TAudioFormatInfo;
      Error: boolean;
    public
      constructor Create(Handle: HSTREAM);
      destructor Destroy(); override;

      procedure Close();                     override;

      function GetLength(): real;            override;
      function GetAudioFormatInfo(): TAudioFormatInfo; override;
      function GetPosition: real;            override;
      procedure SetPosition(Time: real);     override;
      function GetLoop(): boolean;           override;
      procedure SetLoop(Enabled: boolean);   override;
      function IsEOF(): boolean;             override;
      function IsError(): boolean;           override;

      function ReadData(Buffer: PByteArray; BufSize: integer): integer; override;
  end;

type
  TAudioDecoder_Bass = class( TInterfacedObject, IAudioDecoder )
    public
      function GetName: string;

      function InitializeDecoder(): boolean;
      function FinalizeDecoder(): boolean;
      function Open(const Filename: IPath): TAudioDecodeStream;
  end;

var
  BassCore: TAudioCore_Bass;


{ TBassDecodeStream }

constructor TBassDecodeStream.Create(Handle: HSTREAM);
var
  ChannelInfo: BASS_CHANNELINFO;
  Format: TAudioSampleFormat;
begin
  inherited Create();
  Self.Handle := Handle;

  // setup format info
  if (not BASS_ChannelGetInfo(Handle, ChannelInfo)) then
  begin
    raise Exception.Create('Failed to open decode-stream');
  end;
  BassCore.ConvertBASSFlagsToAudioFormat(ChannelInfo.flags, Format);
  FormatInfo := TAudioFormatInfo.Create(ChannelInfo.chans, ChannelInfo.freq, format);

  Error := false;
end;

destructor TBassDecodeStream.Destroy();
begin
  Close();
  inherited;
end;

procedure TBassDecodeStream.Close();
begin
  if (Handle <> 0) then
  begin
    BASS_StreamFree(Handle);
    Handle := 0;
  end;
  PerformOnClose();
  FreeAndNil(FormatInfo);
  Error := false;
end;

function TBassDecodeStream.GetAudioFormatInfo(): TAudioFormatInfo;
begin
  Result := FormatInfo;
end;

function TBassDecodeStream.GetLength(): real;
var
  bytes: QWORD;
begin
  bytes  := BASS_ChannelGetLength(Handle, BASS_POS_BYTE);
  Result := BASS_ChannelBytes2Seconds(Handle, bytes);
end;

function TBassDecodeStream.GetPosition: real;
var
  bytes: QWORD;
begin
  bytes  := BASS_ChannelGetPosition(Handle, BASS_POS_BYTE);
  Result := BASS_ChannelBytes2Seconds(Handle, bytes);
end;

procedure TBassDecodeStream.SetPosition(Time: real);
var
  bytes: QWORD;
begin
  bytes := BASS_ChannelSeconds2Bytes(Handle, Time);
  BASS_ChannelSetPosition(Handle, bytes, BASS_POS_BYTE);
end;

function TBassDecodeStream.GetLoop(): boolean;
var
  flags: DWORD;
begin
  // retrieve channel flags
  flags := BASS_ChannelFlags(Handle, 0, 0);
  if (flags = DWORD(-1)) then
  begin
    Log.LogError('BASS_ChannelFlags: ' + BassCore.ErrorGetString(), 'TBassDecodeStream.GetLoop');
    Result := false;
    Exit;
  end;
  Result := (flags and BASS_SAMPLE_LOOP) <> 0;
end;

procedure TBassDecodeStream.SetLoop(Enabled: boolean);
var
  flags: DWORD;
begin
  // set/unset loop-flag
  if (Enabled) then
    flags := BASS_SAMPLE_LOOP
  else
    flags := 0;

  // set new flag-bits
  if (BASS_ChannelFlags(Handle, flags, BASS_SAMPLE_LOOP) = DWORD(-1)) then
  begin
    Log.LogError('BASS_ChannelFlags: ' + BassCore.ErrorGetString(), 'TBassDecodeStream.SetLoop');
    Exit;
  end;
end;

function TBassDecodeStream.IsEOF(): boolean;
begin
  Result := (BASS_ChannelIsActive(Handle) = BASS_ACTIVE_STOPPED);
end;

function TBassDecodeStream.IsError(): boolean;
begin
  Result := Error;
end;

function TBassDecodeStream.ReadData(Buffer: PByteArray; BufSize: integer): integer;
begin
  Result := BASS_ChannelGetData(Handle, Buffer, BufSize);
  // check error state (do not handle EOF as error)
  if ((Result = -1) and (BASS_ErrorGetCode() <> BASS_ERROR_ENDED)) then
    Error := true
  else
    Error := false;
end;


{ TAudioDecoder_Bass }

function TAudioDecoder_Bass.GetName: String;
begin
  result := 'BASS_Decoder';
end;

function TAudioDecoder_Bass.InitializeDecoder(): boolean;
begin
  Result := false;
  BassCore := TAudioCore_Bass.GetInstance();
  if not BassCore.CheckVersion then
    Exit;
  Result := true;
end;

function TAudioDecoder_Bass.FinalizeDecoder(): boolean;
begin
  Result := true;
end;

function TAudioDecoder_Bass.Open(const Filename: IPath): TAudioDecodeStream;
var
  Stream: HSTREAM;
  ChannelInfo: BASS_CHANNELINFO;
  FileExt: string;
begin
  Result := nil;

  // check if BASS was initialized
  // in case the decoder is not used with BASS playback, init the NO_SOUND device
  if ((integer(BASS_GetDevice) = -1) and (BASS_ErrorGetCode() = BASS_ERROR_INIT)) then
    BASS_Init(0, 44100, 0, 0, nil);

  // TODO: use BASS_STREAM_PRESCAN for accurate seeking in VBR-files?
  //       disadvantage: seeking will slow down.
  
  {$IFDEF MSWINDOWS}
  // Windows: Use UTF-16 version
  Stream := BASS_StreamCreateFile(False, PWideChar(Filename.ToWide), 0, 0, BASS_STREAM_DECODE or BASS_UNICODE);
  {$ELSE}
  // Mac OS X: Use UTF8/ANSI version
  Stream := BASS_StreamCreateFile(False, PAnsiChar(Filename.ToNative), 0, 0, BASS_STREAM_DECODE);
  {$ENDIF}
  if (Stream = 0) then
  begin
    //Log.LogError(BassCore.ErrorGetString(), 'TAudioDecoder_Bass.Open');
    Exit;
  end;

  // check if BASS opened some erroneously recognized file-formats
  if BASS_ChannelGetInfo(Stream, channelInfo) then
  begin
    fileExt := Filename.GetExtension.ToUTF8;
    // BASS opens FLV-files (maybe others too) although it cannot handle them.
    // Setting BASS_CONFIG_VERIFY to the max. value (100000) does not help.
    if ((fileExt = '.flv') and (channelInfo.ctype = BASS_CTYPE_STREAM_MP1)) then
    begin
      BASS_StreamFree(Stream);
      Exit;
    end;
  end;

  Result := TBassDecodeStream.Create(Stream);
end;


initialization
  MediaManager.Add(TAudioDecoder_Bass.Create);

end.