Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #41 from GothicKit/bugfix/include-samplerate-and-c…
Browse files Browse the repository at this point in the history
…hannel-number

Add sampleRate and number of channels to PxSoundData
  • Loading branch information
JucanAndreiDaniel authored Nov 14, 2023
2 parents de7367d + 0e82563 commit 247ef7d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
8 changes: 8 additions & 0 deletions PxCs.Tests/PxSoundTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ public void Test_Load_SFX()
Assert.IsType<byte[]>(soundByteArray.sound);

Assert.True(soundByteArray.sound.Length >= 1024, "Sound needs to have more than 1kB at least");
Assert.True(soundByteArray.channels == 1, "Wrong number of channels");
Assert.True(soundByteArray.sampleRate == 22050, "Sample rate is wrong");

var soundFloatArray = PxSound.GetSoundArrayFromVfs<float>(vfsPtr, "levelup.wav");
Assert.True(soundFloatArray!.sound.Length >= 1024, "Sound needs to have more than 1kB at least");
Assert.True(soundFloatArray.channels == 1, "Wrong number of channels");
Assert.True(soundFloatArray.sampleRate == 22050, "Sample rate is wrong");

DestroyVfs(vfsPtr);

Expand All @@ -33,11 +37,15 @@ public void Test_Load_Dialogue()

Assert.IsType<float[]>(soundFloatArray!.sound);
Assert.True(soundFloatArray.sound.Length >= 1024, "Sound needs to have more than 1kB at least");
Assert.True(soundFloatArray.channels == 1, "Wrong number of channels");
Assert.True(soundFloatArray.sampleRate == 44100, "Sample rate is wrong");

var soundByteArray = PxSound.GetSoundArrayFromVfs<byte>(vfsPtr, "svm_1_diemonster.wav");

Assert.NotNull(soundByteArray!.sound);
Assert.True(soundByteArray.sound.Length >= 1024, "Sound needs to have more than 1kB at least");
Assert.True(soundByteArray.channels == 1, "Wrong number of channels");
Assert.True(soundByteArray.sampleRate == 44100, "Sample rate is wrong");

DestroyVfs(vfsPtr);
}
Expand Down
2 changes: 2 additions & 0 deletions PxCs/Data/Sound/PxSoundData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class PxSoundData<T>
{
public Type arrayType;
public T[] sound;
public ushort channels;
public int sampleRate;

public PxSoundData()
{
Expand Down
18 changes: 11 additions & 7 deletions PxCs/Interface/PxSound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,30 @@ public enum BitDepth
{
var size = PxBuffer.pxBufferSize(wavSound);
var arrayPtr = PxBuffer.pxBufferArray(wavSound);
var wavFile = arrayPtr.MarshalAsArray<byte>((uint)size);

UInt16 channels = BitConverter.ToUInt16(wavFile, 22);
int sampleRate = BitConverter.ToInt32(wavFile, 24);

if (typeof(T) == typeof(byte))
{
var wavFile = arrayPtr.MarshalAsArray<T>((uint)size);
return new PxSoundData<T>
{
sound = wavFile
sound = wavFile as T[],
channels = channels,
sampleRate = sampleRate
};
}
else if (typeof(T) == typeof(float))
{
var wavFile = arrayPtr.MarshalAsArray<byte>((uint)size);
var floatArray = ConvertWAVByteArrayToFloatArray(wavFile);
T[] soundArray = new T[floatArray.Length];
Array.Copy(floatArray, soundArray, floatArray.Length);
return new PxSoundData<T>
{
sound = soundArray
sound = soundArray,
channels = channels,
sampleRate = sampleRate
};
}
else
Expand Down Expand Up @@ -112,7 +118,6 @@ private static float[] ConvertByteArrayToFloatArray(byte[] source, int headerOff
data[i] = (float)source[i] / maxValue;

return data;

}
else if (bit == BitDepth.bit_16)
{
Expand Down Expand Up @@ -160,5 +165,4 @@ private static string FormatCode(UInt16 code)
}
}
}
}

}

0 comments on commit 247ef7d

Please sign in to comment.