Skip to content

Commit

Permalink
Code quality improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Dec 13, 2023
1 parent cd0a336 commit e4fb549
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Cavern.Format/Output/AudioWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ public void WriteOffset(float[][] samples, int period) {
float[] empty = new float[samples[0].Length];
float[][] holder = new float[samples.Length][];
WriteHeader();
for (int curPeriod = 0; curPeriod < period; ++curPeriod) {
for (int channel = 0; channel < holder.Length; ++channel) {
for (int curPeriod = 0; curPeriod < period; curPeriod++) {
for (int channel = 0; channel < holder.Length; channel++) {
holder[channel] = channel % period == curPeriod ? samples[channel] : empty;
}
WriteBlock(holder, 0, holder[0].Length);
Expand All @@ -181,7 +181,7 @@ public void WriteOffset(float[][] samples, int period) {
/// <param name="channelCount">Output channel count</param>
public void WriteForEachChannel(float[] samples, int channelCount) {
float[][] holder = new float[channelCount][];
for (int channel = 0; channel < channelCount; ++channel) {
for (int channel = 0; channel < channelCount; channel++) {
holder[channel] = samples;
}
WriteOffset(holder);
Expand Down
24 changes: 18 additions & 6 deletions Cavern.Format/Output/LimitlessAudioFormatWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;

using Cavern.Format.Common;
using Cavern.Format.Consts;
Expand Down Expand Up @@ -94,9 +95,20 @@ public static void Write(string path, float[][] data, int sampleRate, BitDepth b
/// <param name="sampleRate">Output sample rate</param>
/// <param name="bits">Output bit depth</param>
/// <param name="channels">Output channel information</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteOffset(string path, float[][] data, int sampleRate, BitDepth bits, Channel[] channels) =>
WriteOffset(path, data, sampleRate, bits, channels, channels.Length);

/// <summary>
/// Export an audio file to be played back channel after channel, but some channels play simultaneously.
/// </summary>
/// <param name="path">Output file name</param>
/// <param name="data">Samples to write in the file</param>
/// <param name="sampleRate">Output sample rate</param>
/// <param name="bits">Output bit depth</param>
/// <param name="channels">Output channel information</param>
/// <param name="period">Channels separated by this many channels are played simultaneously</param>
public static void WriteOffset(string path, float[][] data, int sampleRate, BitDepth bits, Channel[] channels,
int period = -1) {
public static void WriteOffset(string path, float[][] data, int sampleRate, BitDepth bits, Channel[] channels, int period) {
LimitlessAudioFormatWriter writer = new LimitlessAudioFormatWriter(path, data[0].LongLength, sampleRate, bits, channels);
writer.WriteOffset(data, period);
}
Expand All @@ -123,12 +135,12 @@ internal void WriteHeader(bool objectMode, int objectCount) {
writer.WriteAny(qualityByte);
writer.WriteAny(objectMode ? (byte)1 : (byte)0); // Mode
writer.WriteAny(ChannelCount); // Channel/object count
for (int channel = 0; channel < objectCount; ++channel) { // Audible channel/object info
for (int channel = 0; channel < objectCount; channel++) { // Audible channel/object info
writer.WriteAny(channels[channel].X); // Rotation on vertical axis
writer.WriteAny(channels[channel].Y); // Rotation on horizontal axis
writer.WriteAny(channels[channel].LFE ? (byte)1 : (byte)0); // Low frequency
}
for (int channel = objectCount; channel < ChannelCount; ++channel) { // Positional track markers
for (int channel = objectCount; channel < ChannelCount; channel++) { // Positional track markers
writer.WriteAny(float.NaN); // Marker
writer.WriteAny(0); // Reserved
writer.WriteAny((byte)0); // Reserved
Expand Down Expand Up @@ -185,15 +197,15 @@ public override void WriteBlock(float[][] samples, long from, long to) {
/// <param name="until">Samples to dump from the <see cref="cache"/></param>
void DumpBlock(long until) {
bool[] toWrite = new bool[ChannelCount];
for (int channel = 0; channel < ChannelCount; ++channel) {
for (int channel = 0; channel < ChannelCount; channel++) {
for (int sample = channel; !toWrite[channel] && sample < cache.Length; sample += ChannelCount) {
if (cache[sample] != 0) {
toWrite[channel] = true;
}
}
}
byte[] layoutBytes = new byte[(ChannelCount & 7) == 0 ? ChannelCount >> 3 : ((ChannelCount >> 3) + 1)];
for (int channel = 0; channel < ChannelCount; ++channel) {
for (int channel = 0; channel < ChannelCount; channel++) {
if (toWrite[channel]) {
layoutBytes[channel >> 3] += (byte)(1 << (channel & 7));
}
Expand Down
33 changes: 27 additions & 6 deletions Cavern.Format/Output/RIFFWaveWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;

using Cavern.Channels;
using Cavern.Format.Common;
Expand Down Expand Up @@ -116,8 +117,19 @@ public static void Write(string path, float[][] data, int sampleRate, BitDepth b
/// <param name="data">Samples to write in the file</param>
/// <param name="sampleRate">Output sample rate</param>
/// <param name="bits">Output bit depth</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteOffset(string path, float[][] data, int sampleRate, BitDepth bits) =>
WriteOffset(path, data, sampleRate, bits, data.Length);

/// <summary>
/// Export an audio file to be played back channel after channel, but some channels play simultaneously.
/// </summary>
/// <param name="path">Output file name</param>
/// <param name="data">Samples to write in the file</param>
/// <param name="sampleRate">Output sample rate</param>
/// <param name="bits">Output bit depth</param>
/// <param name="period">Channels separated by this many channels are played simultaneously</param>
public static void WriteOffset(string path, float[][] data, int sampleRate, BitDepth bits, int period = -1) {
public static void WriteOffset(string path, float[][] data, int sampleRate, BitDepth bits, int period) {
RIFFWaveWriter writer = new RIFFWaveWriter(path, data.Length, data[0].LongLength, sampleRate, bits);
writer.WriteOffset(data, period);
}
Expand Down Expand Up @@ -267,23 +279,23 @@ public override void WriteBlock(float[][] samples, long from, long to) {
switch (Bits) {
case BitDepth.Int8:
while (from < to) {
for (int channel = 0; channel < samples.Length; ++channel) {
for (int channel = 0; channel < samples.Length; channel++) {
writer.WriteAny((sbyte)(samples[channel][from] * sbyte.MaxValue));
}
++from;
}
break;
case BitDepth.Int16:
while (from < to) {
for (int channel = 0; channel < samples.Length; ++channel) {
for (int channel = 0; channel < samples.Length; channel++) {
writer.WriteAny((short)(samples[channel][from] * short.MaxValue));
}
++from;
}
break;
case BitDepth.Int24:
while (from < to) {
for (int channel = 0; channel < samples.Length; ++channel) {
for (int channel = 0; channel < samples.Length; channel++) {
int src = (int)(samples[channel][from] * BitConversions.int24Max);
writer.WriteAny((short)src);
writer.WriteAny((byte)(src >> 16));
Expand All @@ -293,7 +305,7 @@ public override void WriteBlock(float[][] samples, long from, long to) {
break;
case BitDepth.Float32:
while (from < to) {
for (int channel = 0; channel < samples.Length; ++channel) {
for (int channel = 0; channel < samples.Length; channel++) {
writer.WriteAny(samples[channel][from]);
}
++from;
Expand All @@ -302,6 +314,15 @@ public override void WriteBlock(float[][] samples, long from, long to) {
}
}

/// <summary>
/// Append an extra chunk to the file, without considering its alignment to even bytes.
/// </summary>
/// <param name="id">4 byte identifier of the chunk</param>
/// <param name="data">Raw data of the chunk</param>
/// <remarks>The <paramref name="id"/> has a different byte order in the file to memory,
/// refer to <see cref="RIFFWave"/> for samples.</remarks>
public void WriteChunk(int id, byte[] data) => WriteChunk(id, data, false);

/// <summary>
/// Append an extra chunk to the file.
/// </summary>
Expand All @@ -310,7 +331,7 @@ public override void WriteBlock(float[][] samples, long from, long to) {
/// <param name="dwordPadded">Some RIFF readers only work if all chunks start at an even byte, this is for their support</param>
/// <remarks>The <paramref name="id"/> has a different byte order in the file to memory,
/// refer to <see cref="RIFFWave"/> for samples.</remarks>
public void WriteChunk(int id, byte[] data, bool dwordPadded = false) {
public void WriteChunk(int id, byte[] data, bool dwordPadded) {
writer.WriteAny(id);
if (data.LongLength > uint.MaxValue) {
largeChunkSizes ??= new List<Tuple<int, long>>();
Expand Down

0 comments on commit e4fb549

Please sign in to comment.