-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathSoundBufferRecorder.cs
85 lines (78 loc) · 3.16 KB
/
SoundBufferRecorder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
namespace SFML.Audio
{
////////////////////////////////////////////////////////////
/// <summary>
/// Specialized SoundRecorder which saves the captured
/// audio data into a sound buffer
/// </summary>
////////////////////////////////////////////////////////////
public class SoundBufferRecorder : SoundRecorder
{
////////////////////////////////////////////////////////////
/// <summary>
/// Sound buffer containing the captured audio data
///
/// The sound buffer is valid only after the capture has ended.
/// This function provides a reference to the internal
/// sound buffer, but you should make a copy of it if you want
/// to make any modifications to it.
/// </summary>
////////////////////////////////////////////////////////////
public SoundBuffer SoundBuffer { get; private set; }
////////////////////////////////////////////////////////////
/// <summary>
/// Provide a string describing the object
/// </summary>
/// <returns>String description of the object</returns>
////////////////////////////////////////////////////////////
public override string ToString()
{
if (IsInvalid)
{
return MakeDisposedObjectString();
}
return "[SoundBufferRecorder]" +
" SampleRate(" + SampleRate + ")" +
" SoundBuffer(" + SoundBuffer + ")";
}
////////////////////////////////////////////////////////////
/// <summary>
/// Called when a new capture starts
/// </summary>
/// <returns>False to abort recording audio data, true to continue</returns>
////////////////////////////////////////////////////////////
protected override bool OnStart()
{
_samplesArray.Clear();
return true;
}
////////////////////////////////////////////////////////////
/// <summary>
/// Process a new chunk of recorded samples
/// </summary>
/// <param name="samples">Array of samples to process</param>
/// <returns>False to stop recording audio data, true to continue</returns>
////////////////////////////////////////////////////////////
protected override bool OnProcessSamples(ReadOnlySpan<short> samples)
{
for (var i = 0; i < samples.Length; i++)
{
_samplesArray.Add(samples[i]);
}
return true;
}
////////////////////////////////////////////////////////////
/// <summary>
/// Called when the current capture stops
/// </summary>
////////////////////////////////////////////////////////////
protected override void OnStop() => SoundBuffer = new SoundBuffer(_samplesArray.ToArray(), 1, SampleRate, _channels);
private readonly List<short> _samplesArray = new List<short>();
private static readonly SoundChannel[] _channels = new SoundChannel[]
{
SoundChannel.Mono
};
}
}