1using System.Runtime.InteropServices;
21 public SoundBuffer(
byte[] data,
int startIndex,
int itemsCount,
int channelsCount,
int samplingFrequency) {
22 Initialize(data, startIndex, itemsCount, channelsCount, samplingFrequency);
23 CreateBuffer(data, startIndex, itemsCount, channelsCount, samplingFrequency);
26 public SoundBuffer(
short[] data,
int startIndex,
int itemsCount,
int channelsCount,
int samplingFrequency) {
27 Initialize(data, startIndex, itemsCount, channelsCount, samplingFrequency);
28 CreateBuffer(data, startIndex, itemsCount, channelsCount, samplingFrequency);
31 public SoundBuffer(Stream stream,
int bytesCount,
int channelsCount,
int samplingFrequency) {
32 byte[] array =
Initialize(stream, bytesCount, channelsCount, samplingFrequency);
33 CreateBuffer(array, 0, array.Length, channelsCount, samplingFrequency);
46 unsafe void CreateBuffer<T>(T[] data,
int startIndex,
int itemsCount,
int channelsCount,
int samplingFrequency) where T : unmanaged {
47 uint buffer =
Mixer.
AL.GenBuffer();
50 GCHandle gCHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
55 channelsCount == 1 ? BufferFormat.Mono16 : BufferFormat.Stereo16,
56 (gCHandle.AddrOfPinnedObject() + startIndex * num).ToPointer(),
69 throw new InvalidOperationException(
"无法处置正在使用的SoundBuffer");
77 soundData.
Data.Length,
79 soundData.SamplingFrequency
91 if (samplesCount <= 0) {
92 throw new InvalidOperationException(
"Buffer cannot have zero samples.");
95 || channelsCount > 2) {
96 throw new ArgumentOutOfRangeException(nameof(channelsCount));
98 if (samplingFrequency < 8000
99 || samplingFrequency > 192000) {
100 throw new ArgumentOutOfRangeException(nameof(samplingFrequency));
107 void Initialize<T>(T[] data,
int startIndex,
int itemsCount,
int channelsCount,
int samplingFrequency) where T : unmanaged {
110 ArgumentNullException.ThrowIfNull(data);
111 if (startIndex + itemsCount > data.Length) {
112 throw new ArgumentOutOfRangeException(nameof(itemsCount));
116 byte[]
Initialize(Stream stream,
int bytesCount,
int channelsCount,
int samplingFrequency) {
117 byte[] array =
new byte[bytesCount];
118 if (stream.Read(array, 0, bytesCount) != bytesCount) {
119 throw new InvalidOperationException(
"Not enough data in stream.");
121 Initialize(array, 0, bytesCount, channelsCount, samplingFrequency);
static AudioError CheckALError()
void Initialize< T >(T[] data, int startIndex, int itemsCount, int channelsCount, int samplingFrequency)
void InitializeProperties(int samplesCount, int channelsCount, int samplingFrequency)
SoundBuffer(byte[] data, int startIndex, int itemsCount, int channelsCount, int samplingFrequency)
static SoundBuffer Load(SoundData soundData)
static SoundBuffer Load(string fileName)
unsafe void CreateBuffer< T >(T[] data, int startIndex, int itemsCount, int channelsCount, int samplingFrequency)
SoundBuffer(Stream stream, int bytesCount, int channelsCount, int samplingFrequency)
static SoundBuffer Load(string fileName, SoundFileFormat format)
static SoundBuffer Load(Stream stream, SoundFileFormat format)
static SoundBuffer Load(Stream stream)
SoundBuffer(short[] data, int startIndex, int itemsCount, int channelsCount, int samplingFrequency)
byte[] Initialize(Stream stream, int bytesCount, int channelsCount, int samplingFrequency)