unit raudio;
{$mode ObjFPC}{$H+}
interface
uses
DynLibs, SysUtils, CTypes;
const
{$IFDEF windows}
RAUDIO_LIB_NAME = 'raylib.dll';
{$ENDIF}
{$IFDEF unix}
{$IFDEF darwin}
RAUDIO_LIB_NAME = 'libraylib.dylib';
{$ELSE}
RAUDIO_LIB_NAME = 'libraylib.so';
{$ENDIF}
{$ENDIF}
type
(* Wave, audio wave data *)
PWave = ^TWave;
TWave = record
frameCount : LongWord; // Total number of frames (considering channels)
sampleRate : LongWord; // Frequency (samples per second)
sampleSize : LongWord; // Bit depth (bits per sample): 8, 16, 32 (24 not supported)
channels : LongWord; // Number of channels (1-mono, 2-stereo, ...)
data : Pointer; // Buffer data pointer
end;
PrAudioBuffer = ^TrAudioBuffer;
TrAudioBuffer = record end;
PrAudioProcessor = ^TrAudioProcessor;
TrAudioProcessor = record end;
(* AudioStream, custom audio stream *)
PAudioStream = ^TAudioStream;
TAudioStream = record
buffer : PrAudioBuffer; // Pointer to internal data used by the audio system
processor : PrAudioProcessor; // Pointer to internal data processor, useful for audio effects
sampleRate : LongWord; // Frequency (samples per second)
sampleSize : LongWord; // Bit depth (bits per sample): 8, 16, 32 (24 not supported)
channels : LongWord; // Number of channels (1-mono, 2-stereo, ...)
end;
(* Sound *)
PSound = ^TSound;
TSound = record
stream : TAudioStream; // Audio stream
frameCount : LongWord; // Total number of frames (considering channels)
end;
(*Music, audio stream, anything longer than ~10 seconds should be streamed *)
PMusic = ^TMusic;
TMusic = record
stream : TAudioStream; // Audio stream
frameCount : LongWord; // Total number of frames (considering channels)
looping : Boolean; // Music looping enable
ctxType : Integer; // Type of music context (audio filetype)
ctxData : Pointer; // Audio context data, depends on type
end;
type
PAudioCallback = ^TAudioCallback;
TAudioCallback = procedure (bufferData: Pointer; frames: LongWord); cdecl;
var
(* Audio device management functions *)
InitAudioDevice: procedure(); cdecl; // Initialize audio device and context
CloseAudioDevice: procedure(); cdecl; // Close the audio device and context
IsAudioDeviceReady: function(): boolean; cdecl; // Check if audio device has been initialized successfully
SetMasterVolume: procedure(volume: single); cdecl; // Set master volume (listener)
GetMasterVolume: function(): single; cdecl; // Get master volume (listener)
(* Wave/Sound loading/unloading functions *)
LoadWave: function(const fileName: PChar): TWave; cdecl;
LoadWaveFromMemory: function (const fileType: PChar; const fileData: PByte; dataSize: Integer): TWave; cdecl;
IsWaveValid: function(wave: TWave): Boolean; cdecl;
LoadSound: function(const fileName: PChar): TSound; cdecl;
LoadSoundFromWave: function(wave: TWave): TSound; cdecl;
LoadSoundAlias: function(source: TSound): TSound; cdecl;
IsSoundValid: function(sound: TSound): Boolean; cdecl;
UpdateSound: procedure(sound: TSound; const data: Pointer; sampleCount: Integer); cdecl;
UnloadWave: procedure(wave: TWave); cdecl;
UnloadSound: procedure(sound: TSound); cdecl;
UnloadSoundAlias: procedure(alias: TSound); cdecl;
ExportWave: function(wave: TWave; const fileName: PChar): Boolean; cdecl;
ExportWaveAsCode: function(wave: TWave; const fileName: PChar): Boolean; cdecl;
(* Wave/Sound management functions *)
PlaySound: procedure(sound: TSound); cdecl;
StopSound: procedure(sound: TSound); cdecl;
PauseSound: procedure(sound: TSound); cdecl;
ResumeSound: procedure(sound: TSound); cdecl;
IsSoundPlaying: function(sound: TSound): Boolean; cdecl;
SetSoundVolume: procedure(sound: TSound; volume: Single); cdecl;
SetSoundPitch: procedure(sound: TSound; pitch: Single); cdecl;
SetSoundPan: procedure(sound: TSound; pan: Single); cdecl;
WaveCopy: function(wave: TWave): TWave; cdecl;
WaveCrop: procedure(wave: PWave; initFrame, finalFrame: Integer); cdecl;
WaveFormat: procedure(wave: PWave; sampleRate, sampleSize, channels: Integer); cdecl;
LoadWaveSamples: function(wave: TWave): PSingle; cdecl;
UnloadWaveSamples: procedure(samples: PSingle); cdecl;
(* Music management functions *)
LoadMusicStream: function(const fileName: PChar): TMusic; cdecl;
LoadMusicStreamFromMemory: function(const fileType: PChar; const data: PByte; dataSize: Integer): TMusic; cdecl;
IsMusicValid: function(music: TMusic): Boolean; cdecl;
UnloadMusicStream: procedure(music: TMusic); cdecl;
PlayMusicStream: procedure(music: TMusic); cdecl;
IsMusicStreamPlaying: function(music: TMusic): Boolean; cdecl;
Upda]"]>BlockedsicStream: procedure(music: TMusic); cdecl;
StopMusicStream: procedure(music: TMusic); cdecl;
PauseMusicStream: procedure(music: TMusic); cdecl;
ResumeMusicStream: procedure(music: TMusic); cdecl;
SeekMusicStream: procedure(music: TMusic; position: Single); cdecl;
SetMusicVolume: procedure(music: TMusic; volume: Single); cdecl;
SetMusicPitch: procedure(music: TMusic; pitch: Single); cdecl;
SetMusicPan: procedure(music: TMusic; pan: Single); cdecl;
GetMusicTimeLength: function(music: TMusic): Single; cdecl;
GetMusicTimePlayed: function(music: TMusic): Single; cdecl;
(* AudioStream management functions *)
LoadAudioStream: function(sampleRate, sampleSize, channels: LongWord): TAudioStream; cdecl;
IsAudioStreamValid: function(stream: TAudioStream): boolean; cdecl;
UnloadAudioStream: procedure(stream: TAudioStream); cdecl;
UpdateAudioStream: procedure(stream: TAudioStream; const data: Pointer; frameCount: Integer); cdecl;
IsAudioStreamProcessed: function(stream: TAudioStream): Boolean; cdecl;
PlayAudioStream: procedure(stream: TAudioStream); cdecl;
PauseAudioStream: procedure(stream: TAudioStream); cdecl;
ResumeAudioStream: procedure(stream: TAudioStream); cdecl;
IsAudioStreamPlaying: function(stream: TAudioStream): Boolean; cdecl;
StopAudioStream: procedure(stream: TAudioStream); cdecl;
SetAudioStreamVolume: procedure(stream: TAudioStream; volume: Single); cdecl;
SetAudioStreamPitch: procedure(stream: TAudioStream; pitch: Single); cdecl;
SetAudioStreamPan: procedure(stream: TAudioStream; pan: Single); cdecl;
SetAudioStreamBufferSizeDefault: procedure(size: Integer); cdecl;
SetAudioStreamCallback: procedure(stream: TAudioStream; callback: TAudioCallback); cdecl;
AttachAudioStreamProcessor: procedure(stream: TAudioStream; processor: TAudioCallback); cdecl;
DetachAudioStreamProcessor: procedure(stream: TAudioStream; processor: TAudioCallback); cdecl;
AttachAudioMixedProcessor: procedure(processor: TAudioCallback); cdecl;
DetachAudioMixedProcessor: procedure(processor: TAudioCallback); cdecl;
{Special function for dynamic loading of lib ...}
var
rAudio_Handle: TLibHandle = dynlibs.NilHandle;
ReferenceCounter: cardinal = 0; // Reference counter
function rAudio_IsLoaded: Boolean; inline;
function rAudio_Load(const libfilename: string): Boolean; // load the lib
procedure rAudio_Unload(); // unload and frees the lib from memory : do not forget to call it before close application.
implementation
function rAudio_IsLoaded: Boolean;
begin
Result := (rAudio_Handle <> dynlibs.NilHandle);
end;
function rAudio_Load(const libfilename: string): Boolean;
var
thelib: string;
begin
Result := False;
if rAudio_Handle<>0 then
begin
Inc(ReferenceCounter);
result:=true {is it already there ?}
end else
begin
{go & load the library}
if Length(libfilename) = 0 then thelib := RAUDIO_LIB_NAME else
thelib := libfilename;
rAudio_Handle:=DynLibs.LoadLibrary(thelib); // obtain the handle we want
if rAudio_Handle <> DynLibs.NilHandle then
begin {now we tie the functions to the VARs from above}
(* Audio device management functions *)
Pointer(InitAudioDevice):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('InitAudioDevice'));
Pointer(CloseAudioDevice):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('CloseAudioDevice'));
Pointer(IsAudioDeviceReady):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('IsAudioDeviceReady'));
Pointer(SetMasterVolume):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SetMasterVolume'));
Pointer(GetMasterVolume):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('GetMasterVolume'));
(* Wave/Sound loading/unloading functions *)
Pointer(LoadWave):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('LoadWave'));
Pointer(LoadWaveFromMemory):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('LoadWaveFromMemory'));
Pointer(IsWaveValid):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('IsWaveValid'));
Pointer(LoadSound):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('LoadSound'));
Pointer(LoadSoundFromWave):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('LoadSoundFromWave'));
Pointer(LoadSoundAlias):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('LoadSoundAlias'));
Pointer(IsSoundValid):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('IsSoundValid'));
Pointer(UpdateSound):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('UpdateSound'));
Pointer(UnloadWave):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('UnloadWave'));
Pointer(UnloadSound):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('UnloadSound'));
Pointer(UnloadSoundAlias):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('UnloadSoundAlias'));
Pointer(ExportWave):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('UnloadSoundAlias'));
Pointer(ExportWaveAsCode):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('UnloadSoundAlias'));
(* Wave/Sound management functions *)
Pointer(PlaySound):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('PlaySound'));
Pointer(StopSound):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('StopSound'));
Pointer(PauseSound):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('PauseSound'));
Pointer(ResumeSound):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('ResumeSound'));
Pointer(IsSoundPlaying):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('IsSoundPlaying'));
Pointer(SetSoundVolume):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SetSoundVolume'));
Pointer(SetSoundPitch):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SetSoundPitch'));
Pointer(SetSoundPan):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SetSoundPan'));
Pointer(WaveCopy):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('WaveCopy'));
Pointer(WaveCrop):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('WaveCrop'));
Pointer(WaveFormat):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('WaveFormat'));
Pointer(LoadWaveSamples):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('LoadWaveSamples'));
Pointer(UnloadWaveSamples):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('UnloadWaveSamples'));
(* Music management functions *)
Pointer(LoadMusicStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('LoadMusicStream'));
Pointer(LoadMusicStreamFromMemory):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('LoadMusicStreamFromMemory'));
Pointer(IsMusicValid):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('IsMusicValid'));
Pointer(UnloadMusicStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('UnloadMusicStream'));
Pointer(PlayMusicStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('PlayMusicStream'));
Pointer(IsMusicStreamPlaying):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('IsMusicStreamPlaying'));
Pointer(Upda]"]>BlockedsicStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('Upda]"]>BlockedsicStream'));
Pointer(StopMusicStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('StopMusicStream'));
Pointer(PauseMusicStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('PauseMusicStream'));
Pointer(ResumeMusicStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('ResumeMusicStream'));
Pointer(SeekMusicStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SeekMusicStream'));
Pointer(SetMusicVolume):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SetMusicVolume'));
Pointer(SetMusicPitch):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SetMusicPitch'));
Pointer(SetMusicPan):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SetMusicPan'));
Pointer(GetMusicTimeLength):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('GetMusicTimeLength'));
Pointer(GetMusicTimePlayed):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('GetMusicTimePlayed'));
(* AudioStream management functions *)
Pointer(LoadAudioStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('LoadAudioStream'));
Pointer(IsAudioStreamValid):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('IsAudioStreamValid'));
Pointer(UnloadAudioStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('UnloadAudioStream'));
Pointer(UpdateAudioStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('UpdateAudioStream'));
Pointer(IsAudioStreamProcessed):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('IsAudioStreamProcessed'));
Pointer(PlayAudioStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('PlayAudioStream'));
Pointer(PauseAudioStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('PauseAudioStream'));
Pointer(ResumeAudioStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('ResumeAudioStream'));
Pointer(IsAudioStreamPlaying):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('IsAudioStreamPlaying'));
Pointer(StopAudioStream):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('StopAudioStream'));
Pointer(SetAudioStreamVolume):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SetAudioStreamVolume'));
Pointer(SetAudioStreamPitch):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SetAudioStreamPitch'));
Pointer(SetAudioStreamPan):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SetAudioStreamPan'));
Pointer(SetAudioStreamBufferSizeDefault):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SetAudioStreamBufferSizeDefault'));
Pointer(SetAudioStreamCallback):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('SetAudioStreamCallback'));
Pointer(AttachAudioStreamProcessor):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('AttachAudioStreamProcessor'));
Pointer(DetachAudioStreamProcessor):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('DetachAudioStreamProcessor'));
Pointer(AttachAudioMixedProcessor):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('AttachAudioMixedProcessor'));
Pointer(DetachAudioMixedProcessor):=DynLibs.GetProcedureAddress(rAudio_Handle,PChar('DetachAudioMixedProcessor'));
end;
Result := rAudio_IsLoaded;
ReferenceCounter:=1;
end;
end;
procedure rAudio_Unload();
begin
if ReferenceCounter > 0 then dec(ReferenceCounter);
if ReferenceCounter > 0 then exit;
if rAudio_IsLoaded then
begin
DynLibs.UnloadLibrary(rAudio_Handle);
rAudio_Handle:=DynLibs.NilHandle;
end;
end;
end.