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
|
{* 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 UAudioConverter_SDL;
interface
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
{$I switches.inc}
uses
UMusic,
ULog,
UMediaPlugin,
UMediaCore_SDL,
sdl,
SysUtils,
Math;
type
TAudioConverter_SDL = class(TInterfacedObject, IMediaInterface, IAudioConverter)
public
function GetName(): string;
function GetPriority(): integer;
function Init(): boolean;
function Finalize(): boolean;
function Open(SrcFormatInfo: TAudioFormatInfo;
DstFormatInfo: TAudioFormatInfo): TAudioConvertStream;
end;
TAudioConvertStream_SDL = class(TAudioConvertStream)
private
cvt: TSDL_AudioCVT;
function Init(): boolean;
protected
constructor Create(SrcFormatInfo: TAudioFormatInfo;
DstFormatInfo: TAudioFormatInfo);
public
destructor Destroy(); override;
class function Open(SrcFormatInfo: TAudioFormatInfo;
DstFormatInfo: TAudioFormatInfo): TAudioConvertStream_SDL;
function Convert(InputBuffer: PByteArray; OutputBuffer: PByteArray; var InputSize: integer): integer; override;
function GetOutputBufferSize(InputSize: integer): integer; override;
function GetRatio(): double; override;
end;
implementation
{ TAudioConverter_SDL }
function TAudioConverter_SDL.GetName(): string;
begin
Result := 'AudioConverter_SDL';
end;
function TAudioConverter_SDL.GetPriority(): integer;
begin
{* Notes:
* - 44.1kHz to 48kHz conversion or vice versa is not supported
* by SDL 1.2 (will be introduced in 1.3).
* No conversion takes place in this cases.
* This is because SDL just converts differences in powers of 2.
* So the result might not be that accurate.
* This IS audible (voice to high/low) and it needs good synchronization
* with the video or the lyrics timer.
* - float<->int16 conversion is not supported (will be part of 1.3) and
* SDL (<1.3) is not capable of handling floats at all.
* -> Using FFmpeg or libsamplerate for resampling is preferred.
* Use SDL for channel and format conversion only.
*}
Result := 0;
end;
function TAudioConverter_SDL.Init(): boolean;
begin
Result := true;
end;
function TAudioConverter_SDL.Finalize(): boolean;
begin
Result := true;
end;
function TAudioConverter_SDL.Open(SrcFormatInfo: TAudioFormatInfo;
DstFormatInfo: TAudioFormatInfo): TAudioConvertStream;
begin
Result := TAudioConvertStream_SDL.Open(SrcFormatInfo, DstFormatInfo);
end;
{ TAudioConvertStream_SDL }
constructor TAudioConvertStream_SDL.Create(SrcFormatInfo: TAudioFormatInfo;
DstFormatInfo: TAudioFormatInfo);
begin
inherited Create(SrcFormatInfo, DstFormatInfo);
end;
destructor TAudioConvertStream_SDL.Destroy();
begin
inherited;
end;
class function TAudioConvertStream_SDL.Open(SrcFormatInfo: TAudioFormatInfo;
DstFormatInfo: TAudioFormatInfo): TAudioConvertStream_SDL;
var
Stream: TAudioConvertStream_SDL;
begin
Result := nil;
Stream := TAudioConvertStream_SDL.Create(SrcFormatInfo, DstFormatInfo);
if (not Stream.Init()) then
begin
Stream.Free;
Exit;
end;
Result := Stream;
end;
function TAudioConvertStream_SDL.Init(): boolean;
var
srcFormat: UInt16;
dstFormat: UInt16;
begin
Result := false;
if (not ConvertAudioFormatToSDL(srcFormatInfo.Format, srcFormat) or
not ConvertAudioFormatToSDL(dstFormatInfo.Format, dstFormat)) then
begin
Log.LogError('Audio-format not supported by SDL', 'TSoftMixerPlaybackStream.InitFormatConversion');
Exit;
end;
if (SDL_BuildAudioCVT(@cvt,
srcFormat, srcFormatInfo.Channels, Round(srcFormatInfo.SampleRate),
dstFormat, dstFormatInfo.Channels, Round(dstFormatInfo.SampleRate)) = -1) then
begin
Log.LogError(SDL_GetError(), 'TSoftMixerPlaybackStream.InitFormatConversion');
Exit;
end;
Result := true;
end;
(*
* Returns the size of the output buffer. This might be bigger than the actual
* size of resampled audio data.
*)
function TAudioConvertStream_SDL.GetOutputBufferSize(InputSize: integer): integer;
begin
// Note: len_ratio must not be used here. Even if the len_ratio is 1.0, len_mult might be 2.
// Example: 44.1kHz/mono to 22.05kHz/stereo -> len_ratio=1, len_mult=2
Result := InputSize * cvt.len_mult;
end;
function TAudioConvertStream_SDL.GetRatio(): double;
begin
Result := cvt.len_ratio;
end;
function TAudioConvertStream_SDL.Convert(InputBuffer: PByteArray; OutputBuffer: PByteArray; var InputSize: integer): integer;
begin
Result := -1;
if (InputSize <= 0) then
begin
// avoid div-by-zero problems
if (InputSize = 0) then
Result := 0;
Exit;
end;
// OutputBuffer is always bigger than or equal to InputBuffer
Move(InputBuffer[0], OutputBuffer[0], InputSize);
cvt.buf := PUint8(OutputBuffer);
cvt.len := InputSize;
if (SDL_ConvertAudio(@cvt) = -1) then
Exit;
Result := cvt.len_cvt;
end;
initialization
MediaManager.add(TAudioConverter_SDL.Create);
end.
|