Using miniaudio with pl_mpeg #743
Replies: 1 comment
-
|
Ok, so this seems to work. Not sure it's the best way to do it. Any feedback appreciated. var
ringBuffer: TRingBuffer<Single>;
device: ma_device;
plm: Pplm_t;
dataReady: Boolean = False;
CurrentVolume: Single = 0.5;
procedure ma_data_callback(pDevice: Pma_device; pOutput: Pointer; pInput: Pointer; frameCount: ma_uint32); cdecl;
var
readPointer: PSingle;
framesNeeded: Integer;
i: Integer;
output: PSingle;
begin
framesNeeded := frameCount * 2;
if ringbuffer.AvailableBytes >= framesNeeded then
begin
readPointer := ringBuffer.DirectReadPointer(framesNeeded);
output := PSingle(pOutput);
for i := 0 to framesNeeded - 1 do
begin
output^ := readPointer^ * UnitToScalarValue(CurrentVolume, 1);
Inc(readPointer);
Inc(output);
end;
end
else
begin
FillChar(pOutput^, framesNeeded * SizeOf(Single), 0);
end;
end;
procedure audio_decode_callback(plm: Pplm_t; samples: Pplm_samples_t; user: Pointer); cdecl;
begin
ringBuffer.Write(samples^.interleaved, samples^.count*2);
end;
procedure Test05;
var
lastTime,targetTime,currentTime,elapsedTime,remainingTime,lastFPSTime, endtime: double;
frameCount: integer;
framerate: integer;
laudio: TAudio;
begin
glfwInit;
lastTime := glfwGetTime();
lastFPSTime := lasttime;
targetTime := 1.0 / 60;
frameCount := 0;
framerate :=0;
endtime := 0;
ringBuffer := TRingBuffer<Single>.Create(44100);
var deviceConfig: ma_device_config := ma_device_config_init(ma_device_type_playback);
deviceConfig.playback.format := ma_format_f32;
deviceConfig.playback.channels := 2;
deviceConfig.sampleRate := 44100;
deviceConfig.dataCallback := @ma_data_callback;
if ma_device_init(nil, @deviceConfig, @device) <> MA_SUCCESS then begin
ringbuffer.free;
exit;
end;
ma_device_start(@device);
plm := plm_create_with_filename('arc/videos/GameKit.mpg');
if plm = nil then begin
ma_device_uninit(@device);
ringbuffer.free;
exit;
end;
plm_set_audio_decode_callback(plm, @audio_decode_callback, nil);
while true do
begin
// start frame
currentTime := glfwGetTime();
elapsedTime := currentTime - lastTime;
if AnyKeyPressed then Break;
plm_decode(plm, targetTime);
if plm_has_ended(plm) = MA_TRUE then
break;
// end frame
Inc(frameCount);
if (currentTime - lastFPSTime >= 1.0) then
begin
framerate := framecount;
lastFPSTime := currentTime;
frameCount := 0;
end;
lastTime := currentTime;
remainingTime := targetTime - (currentTime - lastTime);
if (remainingTime > 0) then
begin
endTime := currentTime + remainingTime;
while glfwGetTime() < endTime do
begin
// Busy-wait for the remaining time
end;
end;
end;
plm_destroy(plm);
ma_device_uninit(@device);
laudio.Free;
ringbuffer.Free;
glfwTerminate;
end; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I'm trying to figure out the best way to use ma to playback the audio from pl_mpeg. I'm currently using the high level api and would like to be able to use that if possible. What would be the best way to do this? If there is an example somewhere, kindly point me and I will check it out. I've been searching but not found anything so far.
Beta Was this translation helpful? Give feedback.
All reactions