Skip to content

Commit

Permalink
Additional AudioSynthesis culture fixes
Browse files Browse the repository at this point in the history
Reproduced issue of MIDI synth not playing on system with Turkish language.
Required additional fixes to string culture in comparisons.
Successfully tested build in Turkish language playing default MIDI songs and using a custom SoundFont.
  • Loading branch information
Interkarma committed Apr 23, 2023
1 parent a003609 commit 27f75e0
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 31 deletions.
20 changes: 10 additions & 10 deletions Assets/Scripts/AudioSynthesis/Bank/PatchBank.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Collections.Generic;
using DaggerfallWorkshop.AudioSynthesis.Bank.Patches;
Expand Down Expand Up @@ -192,51 +192,51 @@ private void LoadMyBank(Stream stream)
//read riff chunk
string id = IOHelper.Read8BitString(reader, 4).ToLower();
int size = reader.ReadInt32();
if(!id.Equals("riff"))
if(!id.Equals("riff", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid bank file. The riff header is missing.");
if(!new RiffTypeChunk(id, size, reader).TypeId.ToLower().Equals("bank"))
if(!new RiffTypeChunk(id, size, reader).TypeId.Equals("bank", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid bank file. The riff type is incorrect.");
//read info chunk
id = IOHelper.Read8BitString(reader, 4).ToLower();
size = reader.ReadInt32();
if (!id.Equals("info"))
if (!id.Equals("info", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid bank file. The INFO chunk is missing.");
if (reader.ReadSingle() != BankVersion)
throw new Exception(string.Format("Invalid bank file. The bank version is incorrect, the correct version is {0:0.000}.", BankVersion));
this.comment = IOHelper.Read8BitString(reader, size - 4);
//read asset list chunk
id = IOHelper.Read8BitString(reader, 4).ToLower();
size = reader.ReadInt32();
if (!id.Equals("list"))
if (!id.Equals("list", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid bank file. The ASET LIST chunk is missing.");
long readTo = reader.BaseStream.Position + size;
id = IOHelper.Read8BitString(reader, 4).ToLower();
if (!id.Equals("aset"))
if (!id.Equals("aset", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid bank file. The LIST chunk is not of type ASET.");
//--read assets
while(reader.BaseStream.Position < readTo)
{
id = IOHelper.Read8BitString(reader, 4).ToLower();
size = reader.ReadInt32();
if (!id.Equals("smpl"))
if (!id.Equals("smpl", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid bank file. Only SMPL chunks are allowed in the asset list chunk.");
assets.SampleAssetList.Add(new SampleDataAsset(size, reader));
}
//read instrument list chunk
id = IOHelper.Read8BitString(reader, 4).ToLower();
size = reader.ReadInt32();
if (!id.Equals("list"))
if (!id.Equals("list", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid bank file. The INST LIST chunk is missing.");
readTo = reader.BaseStream.Position + size;
id = IOHelper.Read8BitString(reader, 4).ToLower();
if (!id.Equals("inst"))
if (!id.Equals("inst", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid bank file. The LIST chunk is not of type INST.");
//--read instruments
while (reader.BaseStream.Position < readTo)
{
id = IOHelper.Read8BitString(reader, 4).ToLower();
size = reader.ReadInt32();
if (!id.Equals("ptch"))
if (!id.Equals("ptch", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid bank file. Only PTCH chunks are allowed in the instrument list chunk.");
string patchName = IOHelper.Read8BitString(reader, 20);
string patchType = IOHelper.Read8BitString(reader, 4);
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/AudioSynthesis/Bank/Patches/MultiPatch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using DaggerfallWorkshop.AudioSynthesis.Synthesis;
using DaggerfallWorkshop.AudioSynthesis.Sf2;
Expand Down Expand Up @@ -123,7 +123,7 @@ public override void Load(DescriptorList description, AssetManager assets)
intervalList = new PatchInterval[description.CustomDescriptions.Length];
for (int x = 0; x < intervalList.Length; x++)
{
if (!description.CustomDescriptions[x].ID.ToLower().Equals("mpat"))
if (!description.CustomDescriptions[x].ID.Equals("mpat", StringComparison.InvariantCultureIgnoreCase))
throw new Exception(string.Format("The patch: {0} has an invalid descriptor with id {1}", this.patchName, description.CustomDescriptions[x].ID));
string patchName = (string)description.CustomDescriptions[x].Objects[0];
PatchAsset pAsset = assets.FindPatch(patchName);
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/AudioSynthesis/Midi/MidiFile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DaggerfallWorkshop.AudioSynthesis.Midi
namespace DaggerfallWorkshop.AudioSynthesis.Midi
{
using System;
using System.IO;
Expand Down Expand Up @@ -164,7 +164,7 @@ private static MidiTrack ReadTrack(BinaryReader reader)
int channelList = 0;
int noteOnCount = 0;
int totalTime = 0;
while (!new string(IOHelper.Read8BitChars(reader, 4)).Equals("MTrk"))
while (!new string(IOHelper.Read8BitChars(reader, 4)).Equals("MTrk", StringComparison.InvariantCultureIgnoreCase))
{
int length = BigEndianHelper.ReadInt32(reader);
while (length > 0)
Expand Down
9 changes: 5 additions & 4 deletions Assets/Scripts/AudioSynthesis/Sf2/SoundFontInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace DaggerfallWorkshop.AudioSynthesis.Sf2
namespace DaggerfallWorkshop.AudioSynthesis.Sf2
{
using System;
using System.Globalization;
using System.IO;
using DaggerfallWorkshop.AudioSynthesis.Util;

Expand Down Expand Up @@ -41,17 +42,17 @@ public SoundFontInfo(BinaryReader reader)
{
string id = new string(IOHelper.Read8BitChars(reader, 4));
int size = reader.ReadInt32();
if(!id.ToLower().Equals("list"))
if(!id.Equals("list", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid soundfont. Could not find INFO LIST chunk.");
long readTo = reader.BaseStream.Position + size;
id = new string(IOHelper.Read8BitChars(reader, 4));
if (!id.ToLower().Equals("info"))
if (!id.Equals("info", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid soundfont. The LIST chunk is not of type INFO.");
while (reader.BaseStream.Position < readTo)
{
id = new string(IOHelper.Read8BitChars(reader, 4));
size = reader.ReadInt32();
switch (id.ToLower())
switch (id.ToLower(CultureInfo.InvariantCulture))
{
case "ifil":
verMajorSF = reader.ReadInt16();
Expand Down
9 changes: 5 additions & 4 deletions Assets/Scripts/AudioSynthesis/Sf2/SoundFontPresets.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System;
using System.IO;
using DaggerfallWorkshop.AudioSynthesis.Util;
using DaggerfallWorkshop.AudioSynthesis.Sf2.Chunks;
using System.Globalization;

namespace DaggerfallWorkshop.AudioSynthesis.Sf2
{
Expand All @@ -28,11 +29,11 @@ public SoundFontPresets(BinaryReader reader)
{
string id = new string(IOHelper.Read8BitChars(reader, 4));
int size = reader.ReadInt32();
if(!id.ToLower().Equals("list"))
if(!id.Equals("list", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid soundfont. Could not find pdta LIST chunk.");
long readTo = reader.BaseStream.Position + size;
id = new string(IOHelper.Read8BitChars(reader, 4));
if (!id.ToLower().Equals("pdta"))
if (!id.Equals("pdta", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Invalid soundfont. The LIST chunk is not of type pdta.");

Modulator[] presetModulators = null;
Expand All @@ -50,7 +51,7 @@ public SoundFontPresets(BinaryReader reader)
id = new string(IOHelper.Read8BitChars(reader, 4));
size = reader.ReadInt32();

switch (id.ToLower())
switch (id.ToLower(CultureInfo.InvariantCulture))
{
case "phdr":
phdr = new PresetHeaderChunk(id, size, reader);
Expand Down
9 changes: 5 additions & 4 deletions Assets/Scripts/AudioSynthesis/Sf2/SoundFontSampleData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace DaggerfallWorkshop.AudioSynthesis.Sf2
namespace DaggerfallWorkshop.AudioSynthesis.Sf2
{
using System;
using System.Globalization;
using System.IO;
using DaggerfallWorkshop.AudioSynthesis.Util;

Expand All @@ -21,19 +22,19 @@ public byte[] SampleData
//--Methods
public SoundFontSampleData(BinaryReader reader)
{
if (new string(IOHelper.Read8BitChars(reader, 4)).ToLower().Equals("list") == false)
if (new string(IOHelper.Read8BitChars(reader, 4)).Equals("list", StringComparison.InvariantCultureIgnoreCase) == false)
throw new Exception("Invalid soundfont. Could not find SDTA LIST chunk.");
long readTo = reader.ReadInt32();
readTo += reader.BaseStream.Position;
if (new string(IOHelper.Read8BitChars(reader, 4)).Equals("sdta") == false)
if (new string(IOHelper.Read8BitChars(reader, 4)).Equals("sdta", StringComparison.InvariantCultureIgnoreCase) == false)
throw new Exception("Invalid soundfont. List is not of type sdta.");
bitsPerSample = 0;
byte[] rawSampleData = null;
while (reader.BaseStream.Position < readTo)
{
string subID = new string(IOHelper.Read8BitChars(reader, 4));
int size = reader.ReadInt32();
switch (subID.ToLower())
switch (subID.ToLower(CultureInfo.InvariantCulture))
{
case "smpl":
bitsPerSample = 16;
Expand Down
5 changes: 3 additions & 2 deletions Assets/Scripts/AudioSynthesis/Sfz/SfzReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System;
using System.Text;
using System.IO;
using DaggerfallWorkshop.AudioSynthesis.Bank.Components;
Expand Down Expand Up @@ -162,7 +163,7 @@ private void ToRegion(string regionText, SfzRegion region)
region.offBy = int.Parse(parameter);
break;
case "off_mode":
region.offMode = parameter.Equals("fast") ? SfzRegion.OffModeEnum.Fast : SfzRegion.OffModeEnum.Normal;
region.offMode = parameter.Equals("fast", StringComparison.InvariantCultureIgnoreCase) ? SfzRegion.OffModeEnum.Fast : SfzRegion.OffModeEnum.Normal;
break;
case "delay":
region.delay = float.Parse(parameter);
Expand Down
7 changes: 4 additions & 3 deletions Assets/Scripts/AudioSynthesis/Wave/WaveFileReader.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
namespace DaggerfallWorkshop.AudioSynthesis.Wave
namespace DaggerfallWorkshop.AudioSynthesis.Wave
{
using System;
using System.IO;
using System.Collections.Generic;
using DaggerfallWorkshop.AudioSynthesis.Util;
using DaggerfallWorkshop.AudioSynthesis.Util.Riff;
using System.Globalization;

public sealed class WaveFileReader : IDisposable
{
Expand Down Expand Up @@ -53,7 +54,7 @@ internal static Chunk[] ReadAllChunks(BinaryReader reader)
long offset = reader.BaseStream.Position + 8;
List<Chunk> chunks = new List<Chunk>();
RiffTypeChunk head = new RiffTypeChunk(new string(IOHelper.Read8BitChars(reader, 4)), reader.ReadInt32(), reader);
if (!head.ChunkId.ToLower().Equals("riff") || !head.TypeId.ToLower().Equals("wave"))
if (!head.ChunkId.Equals("riff", StringComparison.InvariantCultureIgnoreCase) || !head.TypeId.Equals("wave", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("The asset could not be loaded because the RIFF chunk was missing or was not of type WAVE.");
while (reader.BaseStream.Position - offset < head.ChunkSize)
{
Expand All @@ -67,7 +68,7 @@ internal static Chunk ReadNextChunk(BinaryReader reader)
{
string id = new string(IOHelper.Read8BitChars(reader, 4));
int size = reader.ReadInt32();
switch (id.ToLower())
switch (id.ToLower(CultureInfo.InvariantCulture))
{
case "riff":
return new RiffTypeChunk(id, size, reader);
Expand Down

0 comments on commit 27f75e0

Please sign in to comment.