forked from morkt/GARbro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudio.cs
204 lines (175 loc) · 6.23 KB
/
Audio.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! \file Audio.cs
//! \date Tue Nov 04 17:35:54 2014
//! \brief audio format class.
//
// Copyright (C) 2014 by morkt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Linq;
namespace GameRes
{
public struct WaveFormat
{
public ushort FormatTag;
public ushort Channels;
public uint SamplesPerSecond;
public uint AverageBytesPerSecond;
public ushort BlockAlign;
public ushort BitsPerSample;
public ushort ExtraSize;
public void SetBPS ()
{
AverageBytesPerSecond = (uint)(SamplesPerSecond * Channels * BitsPerSample / 8);
}
}
public abstract class SoundInput : Stream
{
public abstract int SourceBitrate { get; }
public abstract string SourceFormat { get; }
public WaveFormat Format { get; protected set; }
public Stream Source { get; protected set; }
public long PcmSize { get; protected set; }
protected SoundInput (Stream input)
{
Source = input;
}
public virtual void Reset ()
{
Position = 0;
}
#region System.IO.Stream methods
public override bool CanRead { get { return Source.CanRead; } }
public override bool CanWrite { get { return false; } }
public override long Length { get { return PcmSize; } }
public override void Flush()
{
Source.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
if (origin == SeekOrigin.Begin)
Position = offset;
else if (origin == SeekOrigin.Current)
Position += offset;
else
Position = Length + offset;
return Position;
}
public override void SetLength (long length)
{
throw new System.NotSupportedException ("SoundInput.SetLength method is not supported");
}
public override void Write (byte[] buffer, int offset, int count)
{
throw new System.NotSupportedException ("SoundInput.Write method is not supported");
}
public override void WriteByte (byte value)
{
throw new System.NotSupportedException ("SoundInput.WriteByte method is not supported");
}
#endregion
#region IDisposable Members
bool disposed = false;
protected override void Dispose (bool disposing)
{
if (!disposed)
{
if (disposing)
{
Source.Dispose();
}
disposed = true;
base.Dispose (disposing);
}
}
#endregion
}
/// <summary>
/// Class representing raw PCM sound input.
/// </summary>
public class RawPcmInput : SoundInput
{
public override string SourceFormat { get { return "raw"; } }
public override int SourceBitrate
{
get { return (int)Format.AverageBytesPerSecond * 8; }
}
public RawPcmInput (Stream file, WaveFormat format) : base (file)
{
this.Format = format;
this.PcmSize = file.Length;
}
#region IO.Stream methods
public override long Position
{
get { return Source.Position; }
set { Source.Position = value; }
}
public override bool CanSeek { get { return Source.CanSeek; } }
public override long Seek (long offset, SeekOrigin origin)
{
return Source.Seek (offset, origin);
}
public override int Read (byte[] buffer, int offset, int count)
{
return Source.Read (buffer, offset, count);
}
public override int ReadByte ()
{
return Source.ReadByte();
}
#endregion
}
public abstract class AudioFormat : IResource
{
public override string Type { get { return "audio"; } }
public abstract SoundInput TryOpen (IBinaryStream file);
public virtual void Write (SoundInput source, Stream output)
{
throw new System.NotImplementedException ("AudioFormat.Write not implemenented");
}
public static SoundInput Read (IBinaryStream file)
{
foreach (var impl in FormatCatalog.Instance.FindFormats<AudioFormat> (file.Name, file.Signature))
{
try
{
file.Position = 0;
SoundInput sound = impl.TryOpen (file);
if (null != sound)
return sound;
}
catch (OperationCanceledException)
{
throw;
}
catch (System.Exception X)
{
FormatCatalog.Instance.LastError = X;
}
}
return null;
}
public static AudioFormat Wav { get { return s_WavFormat.Value; } }
static readonly Lazy<AudioFormat> s_WavFormat = new Lazy<AudioFormat> (() => FormatCatalog.Instance.AudioFormats.FirstOrDefault (x => x.Tag == "WAV"));
}
}