Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Mp3.cs
浏览该文件的文档.
1using NAudio.Wave;
2using NLayer.NAudioSupport;
3
4namespace Engine.Media {
5 public static class Mp3 {
7 public Mp3FileReaderBase m_reader;
8
10
12
13 public override int ChannelsCount => m_reader.WaveFormat.Channels;
14
15 public override int SamplingFrequency => m_reader.WaveFormat.SampleRate;
16
17 public override long Position {
18 get => m_position;
19 set {
20 if (m_reader.CanSeek) {
21 long num = value * ChannelsCount * 4; //NLayer获取到的是32位float拆分成的8位byte数组
22 if (num < 0
23 || num > BytesCount) {
24 throw new ArgumentOutOfRangeException();
25 }
26 m_reader.Position = num;
27 m_position = value;
28 return;
29 }
30 throw new NotSupportedException("Underlying stream cannot be seeked.");
31 }
32 }
33
34 public override long BytesCount => m_reader.Length / 2;
35
36 public Mp3StreamingSource(Stream stream) {
37 m_stream = new MemoryStream();
38 stream.Position = 0L;
39 stream.CopyTo(m_stream);
40 m_stream.Position = 0L;
41 m_reader = new Mp3FileReaderBase(m_stream, wf => new Mp3FrameDecompressor(wf));
42 }
43
44 public override void Dispose() {
45 m_reader.Dispose();
46 m_stream.Dispose();
47 }
48
49 public override int Read(byte[] buffer, int offset, int count) {
50 ArgumentNullException.ThrowIfNull(buffer);
51 if (offset < 0
52 || count < 0
53 || offset + count > buffer.Length) {
54 throw new InvalidOperationException("Invalid range.");
55 }
56 count = (int)Math.Min(count, BytesCount - Position);
57 byte[] sample = new byte[count * 2];
58 int num = m_reader.Read(sample, 0, count * 2);
59 for (int i = 0; i < num; i += 4) {
60 float sample32Bit = BitConverter.ToSingle(sample, i);
61 short sample16Bit = (short)(sample32Bit * short.MaxValue);
62 byte[] sampleBytes = BitConverter.GetBytes(sample16Bit);
63 buffer[offset++] = sampleBytes[0];
64 buffer[offset++] = sampleBytes[1];
65 }
66 m_position += num / 2 / ChannelsCount;
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 Mp3StreamingSource(memoryStream);
80 }
81 }
82
83 public static bool IsFlacStream(Stream stream) {
84 ArgumentNullException.ThrowIfNull(stream);
85 long position = stream.Position;
86 stream.Position = 0;
87 bool result = Id3v2Tag.ReadTag(stream) != null;
88 stream.Position = position;
89 return result;
90 }
91
92 public static StreamingSource Stream(Stream stream) {
93 ArgumentNullException.ThrowIfNull(stream);
94 return new Mp3StreamingSource(stream);
95 }
96
97 public static SoundData Load(Stream stream) {
98 using (StreamingSource streamingSource = Stream(stream)) {
99 if (streamingSource.BytesCount > int.MaxValue) {
100 throw new InvalidOperationException("Sound data too long.");
101 }
102 byte[] array = new byte[(int)streamingSource.BytesCount];
103 streamingSource.Read(array, 0, array.Length);
104 SoundData soundData = new(streamingSource.ChannelsCount, streamingSource.SamplingFrequency, array.Length);
105 Buffer.BlockCopy(array, 0, soundData.Data, 0, array.Length);
106 return soundData;
107 }
108 }
109 }
110}
override StreamingSource Duplicate()
复制出一个新的流
定义 Mp3.cs:74
override int Read(byte[] buffer, int offset, int count)
定义 Mp3.cs:49
static StreamingSource Stream(Stream stream)
定义 Mp3.cs:92
static SoundData Load(Stream stream)
定义 Mp3.cs:97
static bool IsFlacStream(Stream stream)
定义 Mp3.cs:83
int Read(byte[] buffer, int offset, int count)