-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
π΅ Implement deserialization for ogg files #72
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
91d65d4
π΅ Implement deserialization for ogg files
tomrijnbeek b417ce6
π Add missing summaries
tomrijnbeek ae07523
π Address review comments
tomrijnbeek 4abb6ec
π Do some more refactoring
tomrijnbeek ac1eaa7
π₯ Remove unnecessary unsafe property
tomrijnbeek bc523a5
π
tomrijnbeek a0e507a
π Another round of review comments
tomrijnbeek 52c1494
Merge branch 'main' into read-ogg
tomrijnbeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
.../goldens/SoundBufferDataTests.VerifyOggDeserialization_filename=44100hz_mono.verified.txt
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using NVorbis; | ||
|
||
namespace Bearded.Audio; | ||
|
||
sealed class OggReader : IDisposable | ||
{ | ||
private readonly VorbisReader reader; | ||
private float[] readBuffer = Array.Empty<float>(); | ||
|
||
public int ChannelCount => reader.Channels; | ||
|
||
public int SampleRate => reader.SampleRate; | ||
|
||
public bool Ended => reader.IsEndOfStream; | ||
|
||
private OggReader(VorbisReader reader) | ||
{ | ||
this.reader = reader; | ||
} | ||
|
||
public ImmutableArray<short[]> ReadAllRemainingBuffers(int maxBufferSize) | ||
{ | ||
if (maxBufferSize <= 0) | ||
{ | ||
throw new ArgumentException("Max buffer size must be positive.", nameof(maxBufferSize)); | ||
} | ||
if (Ended) | ||
{ | ||
return ImmutableArray<short[]>.Empty; | ||
} | ||
|
||
var bufferSize = largestBufferSizeDivisibleByChannelCount(maxBufferSize); | ||
var totalSampleCountRemaining = reader.TotalSamples - reader.SamplePosition; | ||
var fullBuffersNeeded = totalSampleCountRemaining / bufferSize; | ||
var partialBuffersNeeded = totalSampleCountRemaining % bufferSize > 0 ? 1 : 0; | ||
var totalBuffersNeeded = fullBuffersNeeded + partialBuffersNeeded; | ||
|
||
if (totalBuffersNeeded > int.MaxValue) | ||
{ | ||
throw new InvalidOperationException( | ||
$"Cannot read a stream with more than {int.MaxValue} buffers remaining."); | ||
} | ||
|
||
var builder = ImmutableArray.CreateBuilder<short[]>((int) totalBuffersNeeded); | ||
for (var i = 0; i < builder.Capacity; i++) | ||
{ | ||
builder.Add(readSamples(bufferSize)); | ||
} | ||
|
||
return builder.MoveToImmutable(); | ||
} | ||
|
||
public bool TryReadSingleBuffer([NotNullWhen(true)] out short[]? buffer, int maxBufferSize) | ||
{ | ||
if (maxBufferSize <= 0) | ||
{ | ||
throw new ArgumentException("Max buffer size must be positive.", nameof(maxBufferSize)); | ||
} | ||
|
||
if (Ended) | ||
{ | ||
buffer = default; | ||
return false; | ||
} | ||
|
||
var bufferSize = largestBufferSizeDivisibleByChannelCount(maxBufferSize); | ||
buffer = readSamples(bufferSize); | ||
|
||
return true; | ||
} | ||
|
||
private short[] readSamples(int sampleCount) | ||
{ | ||
const short maxSafeValue = 32767; | ||
|
||
ensureReadBufferCapacity(sampleCount); | ||
var readSpan = new Span<float>(readBuffer, 0, sampleCount); | ||
|
||
var readSampleCount = reader.ReadSamples(readSpan); | ||
|
||
var buffer = new short[readSampleCount]; | ||
for (var i = 0; i < buffer.Length; i++) | ||
{ | ||
buffer[i] = (short) (maxSafeValue * readSpan[i]); | ||
} | ||
|
||
return buffer; | ||
} | ||
|
||
private void ensureReadBufferCapacity(int capacity) | ||
{ | ||
if (readBuffer.Length < capacity) | ||
{ | ||
readBuffer = new float[capacity]; | ||
} | ||
} | ||
|
||
private int largestBufferSizeDivisibleByChannelCount(int maxBufferSize) | ||
{ | ||
return maxBufferSize - (maxBufferSize % ChannelCount); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
reader.Dispose(); | ||
} | ||
|
||
public static OggReader FromStream(Stream file) | ||
{ | ||
return new OggReader(new VorbisReader(file) { ClipSamples = true }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.IO; | ||
using OpenTK.Audio.OpenAL; | ||
|
||
namespace Bearded.Audio; | ||
|
||
public sealed partial class SoundBufferData | ||
{ | ||
/// <summary> | ||
/// Extracts the buffer data from an ogg file. | ||
/// </summary> | ||
/// <param name="file">The file to load the data from.</param> | ||
/// <returns>A SoundBufferData object containing the data from the specified file.</returns> | ||
public static SoundBufferData FromOgg(string file) | ||
{ | ||
return FromOgg(File.OpenRead(file)); | ||
} | ||
|
||
/// <summary> | ||
/// Extracts the buffer data from an ogg file. | ||
/// </summary> | ||
/// <param name="file">The file to load the data from.</param> | ||
/// <returns>A SoundBufferData object containing the data from the specified file.</returns> | ||
public static SoundBufferData FromOgg(Stream file) | ||
{ | ||
using var reader = OggReader.FromStream(file); | ||
var buffers = reader.ReadAllRemainingBuffers(maxBufferSize); | ||
return new SoundBufferData(buffers, getSoundFormat(reader.ChannelCount), reader.SampleRate); | ||
} | ||
|
||
private static ALFormat getSoundFormat(int channels) => channels switch | ||
{ | ||
1 => ALFormat.Mono16, | ||
2 => ALFormat.Stereo16, | ||
_ => throw new NotSupportedException("The specified sound format is not supported.") | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need to treat a potentially partial last buffer differently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
readSamples
already handles the partial buffer correctly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see - I did not realise that Reader.ReadSamples did this automatically. I was looking at the array/spam allocation and couldn't find it there. It's fine then (and so was the name of the readSamples parameter) though it is perhaps slightly unfortunate that we may allocate a big float array even if we are only gonna use a part of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly that's the way it is, and we try to limit this problem by not making the buffer size too big. In practice - especially for ogg files - the files will hopefully be so long that most of the time you're filling at least a few buffers fully.