mirror of
https://github.com/Ale32bit/Capy64.git
synced 2025-01-18 18:46:43 +00:00
Added HQ audio channel.
Lowered sample rate to 16kHz to normal channels. Arbitrary audio can only be played on the HQ channel
This commit is contained in:
parent
8703733b91
commit
9cc8377503
2 changed files with 35 additions and 19 deletions
|
@ -29,12 +29,15 @@ public class Audio : IDisposable
|
||||||
Noise
|
Noise
|
||||||
}
|
}
|
||||||
|
|
||||||
public const int SampleRate = 48000;
|
public const int SampleRate = 16000;
|
||||||
|
public const int HQSampleRate = 48000;
|
||||||
public const AudioChannels AudioChannel = AudioChannels.Mono;
|
public const AudioChannels AudioChannel = AudioChannels.Mono;
|
||||||
public const int ChannelsCount = 5;
|
public const int ChannelsCount = 5;
|
||||||
public readonly DynamicSoundEffectInstance[] Channels = new DynamicSoundEffectInstance[ChannelsCount];
|
public readonly DynamicSoundEffectInstance[] Channels = new DynamicSoundEffectInstance[ChannelsCount];
|
||||||
private bool[] freeChannels = new bool[ChannelsCount];
|
private bool[] freeChannels = new bool[ChannelsCount];
|
||||||
|
|
||||||
|
public readonly DynamicSoundEffectInstance HQChannel = new(HQSampleRate, AudioChannel);
|
||||||
|
|
||||||
private static readonly Random rng = new();
|
private static readonly Random rng = new();
|
||||||
public Audio()
|
public Audio()
|
||||||
{
|
{
|
||||||
|
@ -44,6 +47,19 @@ public class Audio : IDisposable
|
||||||
freeChannels[i] = true;
|
freeChannels[i] = true;
|
||||||
Channels[i].BufferNeeded += Audio_BufferNeeded;
|
Channels[i].BufferNeeded += Audio_BufferNeeded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HQChannel.BufferNeeded += HQChannel_BufferNeeded;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HQChannel_BufferNeeded(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
var pending = HQChannel.PendingBufferCount;
|
||||||
|
Capy64.Instance.LuaRuntime.QueueEvent("audio_need", LK =>
|
||||||
|
{
|
||||||
|
LK.PushInteger(-1);
|
||||||
|
LK.PushInteger(pending);
|
||||||
|
return 2;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Audio_BufferNeeded(object sender, EventArgs e)
|
private void Audio_BufferNeeded(object sender, EventArgs e)
|
||||||
|
@ -71,6 +87,9 @@ public class Audio : IDisposable
|
||||||
return inp;
|
return inp;
|
||||||
|
|
||||||
if (inp == -1)
|
if (inp == -1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (inp == -2)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < ChannelsCount; i++)
|
for (int i = 0; i < ChannelsCount; i++)
|
||||||
{
|
{
|
||||||
|
@ -79,8 +98,6 @@ public class Audio : IDisposable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -2 is all?
|
|
||||||
|
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,6 +107,8 @@ public class Audio : IDisposable
|
||||||
|
|
||||||
if (resolvedId >= 0)
|
if (resolvedId >= 0)
|
||||||
channel = Channels[resolvedId];
|
channel = Channels[resolvedId];
|
||||||
|
else if (resolvedId == -1)
|
||||||
|
channel = HQChannel;
|
||||||
else
|
else
|
||||||
channel = null;
|
channel = null;
|
||||||
|
|
||||||
|
@ -106,7 +125,13 @@ public class Audio : IDisposable
|
||||||
return channel.GetSampleDuration(buffer.Length);
|
return channel.GetSampleDuration(buffer.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] GenerateWave(DynamicSoundEffectInstance channel, Waveform form, double frequency, TimeSpan time, float volume = 1f)
|
public TimeSpan SubmitHQ(byte[] buffer)
|
||||||
|
{
|
||||||
|
HQChannel.SubmitBuffer(buffer);
|
||||||
|
return HQChannel.GetSampleDuration(buffer.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] GenerateWave(DynamicSoundEffectInstance channel, Waveform form, double frequency, TimeSpan time, float volume = 1f)
|
||||||
{
|
{
|
||||||
var size = channel.GetSampleSizeInBytes(time);
|
var size = channel.GetSampleSizeInBytes(time);
|
||||||
var buffer = new byte[size];
|
var buffer = new byte[size];
|
||||||
|
|
|
@ -114,16 +114,7 @@ public class Audio : IComponent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var id = (int)L.OptInteger(2, -1);
|
if (_game.Audio.HQChannel.PendingBufferCount > queueLimit)
|
||||||
|
|
||||||
if (!_game.Audio.TryGetChannel(id, out var channel, out var rid))
|
|
||||||
{
|
|
||||||
L.PushBoolean(false);
|
|
||||||
L.PushString("Channel not available");
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (channel.PendingBufferCount > queueLimit)
|
|
||||||
{
|
{
|
||||||
L.PushBoolean(false);
|
L.PushBoolean(false);
|
||||||
L.PushString("queue is full");
|
L.PushString("queue is full");
|
||||||
|
@ -133,10 +124,10 @@ public class Audio : IComponent
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var ts = _game.Audio.Submit(rid, buffer);
|
var ts = _game.Audio.SubmitHQ(buffer);
|
||||||
|
|
||||||
if (channel.State != SoundState.Playing)
|
if (_game.Audio.HQChannel.State != SoundState.Playing)
|
||||||
channel.Play();
|
_game.Audio.HQChannel.Play();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -167,7 +158,7 @@ public class Audio : IComponent
|
||||||
null,
|
null,
|
||||||
});
|
});
|
||||||
|
|
||||||
var id = (int)L.OptInteger(5, -1);
|
var id = (int)L.OptInteger(5, -2);
|
||||||
|
|
||||||
if (!_game.Audio.TryGetChannel(id, out var channel, out var rid))
|
if (!_game.Audio.TryGetChannel(id, out var channel, out var rid))
|
||||||
{
|
{
|
||||||
|
@ -175,7 +166,7 @@ public class Audio : IComponent
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
var buffer = _game.Audio.GenerateWave(channel, (Waveform)form, freq, timespan, volume);
|
var buffer = GenerateWave(channel, (Waveform)form, freq, timespan, volume);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue