Skip to content

Commit

Permalink
com.utilities.encoder.wav 2.0.0 (#22)
Browse files Browse the repository at this point in the history
- com.utilities.audio -> 2.0.1
- refactored StreamSaveToDiskAsync
- added StreamRecordingAsync
  • Loading branch information
StephenHodgson authored Nov 12, 2024
1 parent 4d87745 commit 40ce276
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 174 deletions.
12 changes: 3 additions & 9 deletions Runtime/AudioClipExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static byte[] EncodeToWav(this AudioClip audioClip, PCMFormatSize bitDept
throw new ArgumentNullException(nameof(audioClip));
}

// prep data
var bitsPerSample = 8 * (int)bitDepth;
var sampleRate = audioClip.frequency;
var channels = audioClip.channels;
Expand All @@ -60,19 +59,14 @@ public static async Task<byte[]> EncodeToWavAsync(this AudioClip audioClip, PCMF
throw new ArgumentNullException(nameof(audioClip));
}

await Awaiters.UnityMainThread;

// prep data
await Awaiters.UnityMainThread; // ensure we're on main thread so we can access unity apis
var bitsPerSample = 8 * (int)bitDepth;
var sampleRate = audioClip.frequency;
var channels = audioClip.channels;
var pcmData = audioClip.EncodeToPCM(bitDepth, trim);

// Switch to background thread
await Task.Delay(1, cancellationToken).ConfigureAwait(false);

await Awaiters.BackgroundThread; // switch to background thread to prevent blocking main thread
var encodedBytes = WavEncoder.EncodeWav(pcmData, channels, sampleRate, bitsPerSample);
await Awaiters.UnityMainThread;
await Awaiters.UnityMainThread; // return to main thread before returning result
return encodedBytes;
}
}
Expand Down
Loading

0 comments on commit 40ce276

Please sign in to comment.