Skip to content

Commit 226bcab

Browse files
authored
Update PlayWav method to default stream to true
1 parent 1ec0729 commit 226bcab

File tree

1 file changed

+2
-56
lines changed

1 file changed

+2
-56
lines changed

EXILED/Exiled.API/Features/Toys/Speaker.cs

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,13 @@ public static void Play(AudioMessage message, IEnumerable<Player> targets = null
254254
/// <param name="stream">Whether to stream the audio or preload it.</param>
255255
/// <param name="destroyAfter">Whether to destroy the speaker after playback.</param>
256256
/// <param name="loop">Whether to loop the audio.</param>
257-
public void PlayWav(string path, bool stream = false, bool destroyAfter = false, bool loop = false)
257+
public void PlayWav(string path, bool stream = true, bool destroyAfter = false, bool loop = false)
258258
{
259259
Stop();
260260

261261
Loop = loop;
262262
DestroyAfter = destroyAfter;
263-
source = stream ? new WavStreamSource(path) : new PreloadedPcmSource(WavToPcm(path));
263+
source = stream ? new WavStreamSource(path) : new PreloadedPcmSource(path);
264264
playBackRoutine = Timing.RunCoroutine(PlayBackCoroutine().CancelWith(GameObject));
265265
}
266266

@@ -276,44 +276,6 @@ public void Stop()
276276
source = null;
277277
}
278278

279-
/// <summary>
280-
/// Skips the WAV header.
281-
/// </summary>
282-
/// <param name="br">The binary reader.</param>
283-
internal static void SkipWavHeader(BinaryReader br)
284-
{
285-
br.ReadBytes(12);
286-
287-
while (true)
288-
{
289-
string chunk = new(br.ReadChars(4));
290-
int size = br.ReadInt32();
291-
292-
if (chunk == "fmt ")
293-
{
294-
short format = br.ReadInt16();
295-
short channels = br.ReadInt16();
296-
int rate = br.ReadInt32();
297-
br.ReadInt32();
298-
br.ReadInt16();
299-
short bits = br.ReadInt16();
300-
301-
if (format != 1 || channels != 1 || rate != SampleRate || bits != 16)
302-
Log.Error("WAV must be PCM16 mono 48kHz");
303-
304-
br.BaseStream.Position += size - 16;
305-
}
306-
else if (chunk == "data")
307-
{
308-
return;
309-
}
310-
else
311-
{
312-
br.BaseStream.Position += size;
313-
}
314-
}
315-
}
316-
317279
private IEnumerator<float> PlayBackCoroutine()
318280
{
319281
timeAccumulator = 0f;
@@ -399,22 +361,6 @@ private void SendPacket(int len)
399361
}
400362
}
401363

402-
private float[] WavToPcm(string path)
403-
{
404-
using FileStream fs = File.OpenRead(path);
405-
using BinaryReader br = new(fs);
406-
407-
SkipWavHeader(br);
408-
409-
int samples = (int)((fs.Length - fs.Position) / 2);
410-
float[] pcm = new float[samples];
411-
412-
for (int i = 0; i < samples; i++)
413-
pcm[i] = br.ReadInt16() / 32768f;
414-
415-
return pcm;
416-
}
417-
418364
private void OnToyRemoved(AdminToyBase toy)
419365
{
420366
if (toy != Base)

0 commit comments

Comments
 (0)