Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Ogg.cs
浏览该文件的文档.
1using NVorbis;
2
3namespace Engine.Media {
4 public static class Ogg {
6 public VorbisReader m_reader;
7
9
10 public float[] m_samples = new float[1024];
11
12 public override int ChannelsCount => m_reader.Channels;
13
14 public override int SamplingFrequency => m_reader.SampleRate;
15
16 public override long Position {
17 get => m_reader.SamplePosition;
18 set => m_reader.SamplePosition = value;
19 }
20
21 public override long BytesCount => m_reader.TotalSamples * 2;
22
23 public OggStreamingSource(Stream stream, bool leaveOpen = false) {
24 MemoryStream memoryStream = new();
25 stream.Position = 0L;
26 stream.CopyTo(memoryStream);
27 memoryStream.Position = 0L;
28 m_stream = memoryStream;
29 m_reader = new VorbisReader(m_stream, leaveOpen);
30 }
31
32 public override void Dispose() {
33 m_reader.Dispose();
34 }
35
36 public override int Read(byte[] buffer, int offset, int count) {
37 ArgumentNullException.ThrowIfNull(buffer);
38 if (offset < 0
39 || count < 0
40 || offset + count > buffer.Length) {
41 throw new InvalidOperationException("Invalid range.");
42 }
43 int num = 0;
44 while (count >= 2) {
45 int count2 = Math.Min(count / 2, m_samples.Length);
46 int num2 = m_reader.ReadSamples(m_samples, 0, count2);
47 if (num2 == 0) {
48 break;
49 }
50 num += num2;
51 if (BitConverter.IsLittleEndian) {
52 for (int i = 0; i < num2; i++) {
53 short num3 = (short)(m_samples[i] * 32767f);
54 buffer[offset++] = (byte)num3;
55 buffer[offset++] = (byte)(num3 >> 8);
56 }
57 }
58 else {
59 for (int j = 0; j < num2; j++) {
60 short num4 = (short)(m_samples[j] * 32767f);
61 buffer[offset++] = (byte)(num4 >> 8);
62 buffer[offset++] = (byte)num4;
63 }
64 }
65 count -= num2 * 2;
66 }
67 return num * 2;
68 }
69
74 public override StreamingSource Duplicate() {
75 MemoryStream memoryStream = new();
76 m_stream.Position = 0L;
77 m_stream.CopyTo(memoryStream);
78 memoryStream.Position = 0L;
79 return new OggStreamingSource(memoryStream);
80 }
81 }
82
83 public static bool IsOggStream(Stream stream) {
84 long position = stream.Position;
85 int num = stream.ReadByte();
86 int num2 = stream.ReadByte();
87 int num3 = stream.ReadByte();
88 int num4 = stream.ReadByte();
89 stream.Position = position;
90 return num == 79 && num2 == 103 && num3 == 103 && num4 == 83;
91 }
92
93 public static StreamingSource Stream(Stream stream, bool leaveOpen = false) {
94 ArgumentNullException.ThrowIfNull(stream);
95 return new OggStreamingSource(stream, leaveOpen);
96 }
97
98 public static SoundData Load(Stream stream) {
99 using (StreamingSource streamingSource = Stream(stream, true)) {
100 if (streamingSource.BytesCount > int.MaxValue) {
101 throw new InvalidOperationException("Sound data too long.");
102 }
103 byte[] array = new byte[(int)streamingSource.BytesCount];
104 streamingSource.Read(array, 0, array.Length);
105 SoundData soundData = new(streamingSource.ChannelsCount, streamingSource.SamplingFrequency, array.Length);
106 Buffer.BlockCopy(array, 0, soundData.Data, 0, array.Length);
107 return soundData;
108 }
109 }
110 }
111}
override int Read(byte[] buffer, int offset, int count)
定义 Ogg.cs:36
override StreamingSource Duplicate()
复制出一个新的流
定义 Ogg.cs:74
OggStreamingSource(Stream stream, bool leaveOpen=false)
定义 Ogg.cs:23
static SoundData Load(Stream stream)
定义 Ogg.cs:98
static bool IsOggStream(Stream stream)
定义 Ogg.cs:83
static StreamingSource Stream(Stream stream, bool leaveOpen=false)
定义 Ogg.cs:93
int Read(byte[] buffer, int offset, int count)