8bit to 16bit PCM on audio.play string input.

This commit is contained in:
Alessandro Proto 2023-06-29 17:34:26 +02:00
parent 4880fea8e3
commit 55c4f70533

View file

@ -89,6 +89,14 @@ public class AudioLib : IComponent
return 1; return 1;
} }
// Convert 8bit PCM to 16bit PCM
private static void PCMTo16bit(Span<byte> buffer, int i, byte value)
{
var value16bit = value / sbyte.MaxValue * short.MaxValue;
buffer[2 * i - 2] = (byte)(value16bit & 0xff);
buffer[2 * i - 1] = (byte)(value16bit >> 8);
}
private static int L_Play(IntPtr state) private static int L_Play(IntPtr state)
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
@ -97,20 +105,24 @@ public class AudioLib : IComponent
if (L.IsString(1)) if (L.IsString(1))
{ {
buffer = L.CheckBuffer(1); var inputBuffer = L.CheckBuffer(1);
buffer = new byte[inputBuffer.Length * 2];
var span = new Span<byte>(buffer);
for (int i = 1; i < inputBuffer.Length; i++)
{
PCMTo16bit(span, i, inputBuffer[i]);
}
} }
else else
{ {
L.CheckType(1, LuaType.Table); L.CheckType(1, LuaType.Table);
var len = L.RawLen(1); var len = L.RawLen(1);
buffer = new byte[len * 2]; buffer = new byte[len * 2];
var span = new Span<byte>(buffer);
for (int i = 1; i <= len; i++) for (int i = 1; i <= len; i++)
{ {
L.GetInteger(1, i); L.GetInteger(1, i);
// Convert 8bit PCM to 16bit PCM PCMTo16bit(span, i, (byte)L.CheckNumber(-1));
var value = (short)(L.CheckNumber(-1) / sbyte.MaxValue * short.MaxValue);
buffer[2 * i - 2] = (byte)(value & 0xff);
buffer[2 * i - 1] = (byte)(value >> 8);
L.Pop(1); L.Pop(1);
} }
} }
@ -135,7 +147,12 @@ public class AudioLib : IComponent
L.Error(ex.Message); L.Error(ex.Message);
} }
return 0; var playTime = _game.Audio.HQChannel.GetSampleDuration(buffer.Length);
L.PushBoolean(true);
L.PushNumber(playTime.TotalSeconds);
return 2;
} }
private static int L_Beep(IntPtr state) private static int L_Beep(IntPtr state)