Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SoundBuffer.cs
浏览该文件的文档.
1using System.Runtime.InteropServices;
2using Engine.Media;
3#if BROWSER
4using BufferFormat = Engine.Browser.AL.BufferFormat;
5#else
6using Silk.NET.OpenAL;
7#endif
8
9namespace Engine.Audio {
10 public class SoundBuffer : IDisposable {
11 public int m_buffer;
12
13 public int ChannelsCount { get; set; }
14
15 public int SamplingFrequency { get; set; }
16
17 public int SamplesCount { get; set; }
18
19 public int UseCount { get; set; }
20
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);
24 }
25
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);
29 }
30
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);
34 }
35
36 public SoundBuffer() { }
37
39 if (m_buffer != 0) {
40 Mixer.AL.DeleteBuffer((uint)m_buffer);
42 m_buffer = 0;
43 }
44 }
45
46 unsafe void CreateBuffer<T>(T[] data, int startIndex, int itemsCount, int channelsCount, int samplingFrequency) where T : unmanaged {
47 uint buffer = Mixer.AL.GenBuffer();
48 m_buffer = (int)buffer;
50 GCHandle gCHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
51 try {
52 int num = Utilities.SizeOf<T>();
53 Mixer.AL.BufferData(
54 buffer,
55 channelsCount == 1 ? BufferFormat.Mono16 : BufferFormat.Stereo16,
56 (gCHandle.AddrOfPinnedObject() + startIndex * num).ToPointer(),
57 itemsCount * num,
58 samplingFrequency
59 );
61 }
62 finally {
63 gCHandle.Free();
64 }
65 }
66
67 public void Dispose() {
68 if (UseCount != 0) {
69 throw new InvalidOperationException("无法处置正在使用的SoundBuffer");
70 }
72 }
73
74 public static SoundBuffer Load(SoundData soundData) => new(
75 soundData.Data,
76 0,
77 soundData.Data.Length,
78 soundData.ChannelsCount,
79 soundData.SamplingFrequency
80 );
81
82 public static SoundBuffer Load(Stream stream, SoundFileFormat format) => Load(SoundData.Load(stream, format));
83
84 public static SoundBuffer Load(string fileName, SoundFileFormat format) => Load(SoundData.Load(fileName, format));
85
86 public static SoundBuffer Load(Stream stream) => Load(SoundData.Load(stream));
87
88 public static SoundBuffer Load(string fileName) => Load(SoundData.Load(fileName));
89
90 public void InitializeProperties(int samplesCount, int channelsCount, int samplingFrequency) {
91 if (samplesCount <= 0) {
92 throw new InvalidOperationException("Buffer cannot have zero samples.");
93 }
94 if (channelsCount < 1
95 || channelsCount > 2) {
96 throw new ArgumentOutOfRangeException(nameof(channelsCount));
97 }
98 if (samplingFrequency < 8000
99 || samplingFrequency > 192000) {
100 throw new ArgumentOutOfRangeException(nameof(samplingFrequency));
101 }
102 ChannelsCount = channelsCount;
103 SamplingFrequency = samplingFrequency;
104 SamplesCount = samplesCount;
105 }
106
107 void Initialize<T>(T[] data, int startIndex, int itemsCount, int channelsCount, int samplingFrequency) where T : unmanaged {
108 int num = Utilities.SizeOf<T>();
109 InitializeProperties(itemsCount * num / channelsCount / 2, channelsCount, samplingFrequency);
110 ArgumentNullException.ThrowIfNull(data);
111 if (startIndex + itemsCount > data.Length) {
112 throw new ArgumentOutOfRangeException(nameof(itemsCount));
113 }
114 }
115
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.");
120 }
121 Initialize(array, 0, bytesCount, channelsCount, samplingFrequency);
122 return array;
123 }
124 }
125}
unsafe
定义 Main.cs:15
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)
static SoundData Load(Stream stream, SoundFileFormat format)