2f107db...5202af9

This commit is contained in:
Joonas Rikkonen
2019-03-18 21:42:26 +02:00
parent 58c92888b7
commit 044fd3344b
395 changed files with 27417 additions and 19754 deletions
@@ -1,6 +1,7 @@
using System;
using OpenTK.Audio.OpenAL;
using NVorbis;
using System.Collections.Generic;
namespace Barotrauma.Sounds
{
@@ -8,7 +9,10 @@ namespace Barotrauma.Sounds
{
private VorbisReader reader;
public OggSound(SoundManager owner,string filename,bool stream) : base(owner,filename,stream)
//key = sample rate, value = filter
private static Dictionary<int, BiQuad> muffleFilters = new Dictionary<int, BiQuad>();
public OggSound(SoundManager owner, string filename, bool stream) : base(owner, filename, stream, true)
{
if (!ToolBox.IsProperFilenameCase(filename))
{
@@ -40,7 +44,7 @@ namespace Barotrauma.Sounds
throw new Exception("Failed to set buffer data for non-streamed audio! "+AL.GetErrorString(alError));
}
MuffleBuffer(floatBuffer, reader.Channels);
MuffleBuffer(floatBuffer, SampleRate, reader.Channels);
CastBuffer(floatBuffer, shortBuffer, readSamples);
@@ -60,53 +64,28 @@ namespace Barotrauma.Sounds
public override int FillStreamBuffer(int samplePos, short[] buffer)
{
if (!Stream) throw new Exception("Called FillStreamBuffer on a non-streamed sound!");
if (samplePos >= reader.TotalSamples * reader.Channels * 2) return 0;
samplePos /= reader.Channels*2;
samplePos /= reader.Channels * 2;
reader.DecodedPosition = samplePos;
float[] floatBuffer = new float[buffer.Length];
int readSamples = reader.ReadSamples(floatBuffer, 0, buffer.Length/2);
int readSamples = reader.ReadSamples(floatBuffer, 0, buffer.Length / 2);
//MuffleBuffer(floatBuffer, reader.Channels);
CastBuffer(floatBuffer, buffer, readSamples);
return readSamples*2;
return readSamples * 2;
}
static void MuffleBuffer(float[] buffer,int channelCount)
static void MuffleBuffer(float[] buffer, int sampleRate, int channelCount)
{
//this function will probably have to replace EFX on OSX
float[] avgvals = new float[channelCount];
for (int j = 0; j < channelCount; j++)
if (!muffleFilters.TryGetValue(sampleRate, out BiQuad filter))
{
avgvals[j] = buffer[j];
}
for (int i = 0; i < buffer.Length; i+=channelCount)
{
for (int j = 0; j < channelCount; j++)
{
float fval = buffer[i + j];
float weight = 0.7f;
weight = 1.0f - weight;
weight *= weight * weight;
avgvals[j] = (avgvals[j] * (1.0f - weight) + fval * weight);
fval = avgvals[j]*1.7f;
buffer[i + j] = fval;
}
}
}
static void CastBuffer(float[] inBuffer, short[] outBuffer, int length)
{
for (int i = 0; i < length; i++)
{
float fval = Math.Max(Math.Min(inBuffer[i], 1.0f), -1.0f);
int temp = (int)(32767f * fval);
if (temp > short.MaxValue) temp = short.MaxValue;
else if (temp < short.MinValue) temp = short.MinValue;
outBuffer[i] = (short)temp;
filter = new LowpassFilter(sampleRate, 400);
muffleFilters.Add(sampleRate, filter);
}
filter.Process(buffer);
}
public override void Dispose()