aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/Classes/UAudioPlayback_SDL.pas
blob: deef91e891ec52b9436e90a4fbf5509ef521ca5f (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
unit UAudioPlayback_SDL;

interface

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

{$I switches.inc}


uses
  Classes,
  SysUtils,
  UMusic;

implementation

uses
  sdl,
  UAudioPlayback_SoftMixer,
  ULog,
  UIni,
  UMain;

type
  TAudioPlayback_SDL = class(TAudioPlayback_SoftMixer)
    private
      Latency: double;
      function EnumDevices(): boolean;
    protected
      function InitializeAudioPlaybackEngine(): boolean; override;
      function StartAudioPlaybackEngine(): boolean;      override;
      procedure StopAudioPlaybackEngine();               override;
      function FinalizeAudioPlaybackEngine(): boolean;   override;
      function GetLatency(): double;                     override;
    public
      function GetName: String;                          override;
      procedure MixBuffers(dst, src: PChar; size: Cardinal; volume: Single); override;
  end;

  
{ TAudioPlayback_SDL }

procedure SDLAudioCallback(userdata: Pointer; stream: PChar; len: integer); cdecl;
var
  Engine: TAudioPlayback_SDL;
begin
  Engine := TAudioPlayback_SDL(userdata);
  Engine.AudioCallback(stream, len);
end;

function TAudioPlayback_SDL.GetName: String;
begin
  Result := 'SDL_Playback';
end;

function TAudioPlayback_SDL.EnumDevices(): boolean;
begin
  // Note: SDL does not provide Device-Selection capabilities (will be introduced in 1.3)
  ClearOutputDeviceList();
  SetLength(OutputDeviceList, 1);
  OutputDeviceList[0] := TAudioOutputDevice.Create();
  OutputDeviceList[0].Name := '[SDL Default-Device]';
  Result := true;
end;

function TAudioPlayback_SDL.InitializeAudioPlaybackEngine(): boolean;
var
  DesiredAudioSpec, ObtainedAudioSpec: TSDL_AudioSpec;
  SampleBufferSize: integer;
begin
  Result := false;

  EnumDevices();

  if (SDL_InitSubSystem(SDL_INIT_AUDIO) = -1) then
  begin
    Log.LogError('SDL_InitSubSystem failed!', 'TAudioPlayback_SDL.InitializeAudioPlaybackEngine');
    Exit;
  end;

  SampleBufferSize := IAudioOutputBufferSizeVals[Ini.AudioOutputBufferSizeIndex];
  if (SampleBufferSize <= 0) then
  begin
    // Automatic setting default
    // FIXME: too much glitches with 1024 samples
    SampleBufferSize := 2048; //1024;
  end;

  FillChar(DesiredAudioSpec, SizeOf(DesiredAudioSpec), 0);
  with DesiredAudioSpec do
  begin
    freq := 44100;
    format := AUDIO_S16SYS;
    channels := 2;
    samples := SampleBufferSize;
    callback := @SDLAudioCallback;
    userdata := Self;
  end;

  // Note: always use the "obtained" parameter, otherwise SDL might try to convert
  // the samples itself if the desired format is not available. This might lead
  // to problems if for example ALSA does not support 44100Hz and proposes 48000Hz.
  // Without the obtained parameter, SDL would try to convert 44.1kHz to 48kHz with
  // its crappy (non working) converter resulting in a wrong (too high) pitch.
  if(SDL_OpenAudio(@DesiredAudioSpec, @ObtainedAudioSpec) = -1) then
  begin
    Log.LogStatus('SDL_OpenAudio: ' + SDL_GetError(), 'TAudioPlayback_SDL.InitializeAudioPlaybackEngine');
    Exit;
  end;

  FormatInfo := TAudioFormatInfo.Create(
    ObtainedAudioSpec.channels,
    ObtainedAudioSpec.freq,
    asfS16
  );

  // Note: SDL does not provide info of the internal buffer state.
  // So we use the average buffer-size.
  Latency := (ObtainedAudioSpec.samples/2) / FormatInfo.SampleRate;

  Log.LogStatus('Opened audio device', 'TAudioPlayback_SDL.InitializeAudioPlaybackEngine');

  Result := true;
end;

function TAudioPlayback_SDL.StartAudioPlaybackEngine(): boolean;
begin
  SDL_PauseAudio(0);
  Result := true;
end;

procedure TAudioPlayback_SDL.StopAudioPlaybackEngine();
begin
  SDL_PauseAudio(1);
end;

function TAudioPlayback_SDL.FinalizeAudioPlaybackEngine(): boolean;
begin
  SDL_CloseAudio();
  SDL_QuitSubSystem(SDL_INIT_AUDIO);
  Result := true;
end;

function TAudioPlayback_SDL.GetLatency(): double;
begin
  Result := Latency;
end;

procedure TAudioPlayback_SDL.MixBuffers(dst, src: PChar; size: Cardinal; volume: Single);
begin
  SDL_MixAudio(PUInt8(dst), PUInt8(src), size, Round(volume * SDL_MIX_MAXVOLUME));
end;


initialization
  MediaManager.add(TAudioPlayback_SDL.Create);

end.