File: StreamHeader.cs

package info (click to toggle)
taglib-sharp 2.1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,880 kB
  • sloc: cs: 29,524; sh: 594; makefile: 229
file content (373 lines) | stat: -rw-r--r-- 9,950 bytes parent folder | download | duplicates (6)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//
// StreamHeader.cs: Provides support for reading Monkey's Audio APE stream
// properties.
//
// Author:
//   Helmut Wahrmann
//
// Copyright (C) 2007 Helmut Wahrmann
// Copyright (C) 2007 Brian Nickel
//
// This library is free software; you can redistribute it and/or modify
// it  under the terms of the GNU Lesser General Public License version
// 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//

using System;
using System.Globalization;

namespace TagLib.Ape {
	/// <summary>
	///    Indicates the compression level used when encoding a Monkey's
	///    Audio APE file.
	/// </summary>
	public enum CompressionLevel {
		/// <summary>
		///    The audio is not compressed.
		/// </summary>
		None = 0,
		
		/// <summary>
		///    The audio is mildly compressed.
		/// </summary>
		Fast = 1000,
		
		/// <summary>
		///    The audio is compressed at a normal level.
		/// </summary>
		Normal = 2000,
		
		/// <summary>
		///    The audio is highly compressed.
		/// </summary>
		High = 3000,
		
		/// <summary>
		///    The audio is extremely highly compressed.
		/// </summary>
		ExtraHigh = 4000,
		
		/// <summary>
		///    The audio is compressed to an insane level.
		/// </summary>
		Insane
	}
	
	/// <summary>
	///    This struct implements <see cref="IAudioCodec" /> to provide
	///    support for reading Monkey's Audio APE stream properties.
	/// </summary>
	public struct StreamHeader : IAudioCodec, ILosslessAudioCodec
	{
		#region Private Fields
		
		/// <summary>
		///    Contains the APE version.
		/// </summary>
		/// <remarks>
		///    This value is stored in bytes (4,5) of the file and is
		///    1000 times the actual version number, so 3810 indicates
		///    version 3.81.
		/// </remarks>
		private ushort version;
		
		// Ape Header (24 bytes) starting at Offest 52 into the file
		/// <summary>
		///    Contains the compression level.
		/// </summary>
		/// <remarks>
		///    This value is stored in bytes (51,52).
		/// </remarks>
		private CompressionLevel compression_level;
		
		/*
		/// <summary>
		///    Contains the format flags.
		/// </summary>
		/// <remarks>
		///    This value is stored in bytes (53,54).
		/// </remarks>
		private ushort format_flags;
		*/
		
		/// <summary>
		///    Contains the number of audio blocks in one frame.
		/// </summary>
		/// <remarks>
		///    This value is stored in bytes (55-58).
		/// </remarks>
		private uint blocks_per_frame;
		
		/// <summary>
		///    Contains the number of audio blocks in the final frame.
		/// </summary>
		/// <remarks>
		///    This value is stored in bytes (59-62).
		/// </remarks>
		private uint final_frame_blocks;
		
		/// <summary>
		///    Contains the total number of frames.
		/// </summary>
		/// <remarks>
		///    This value is stored in bytes (63-66).
		/// </remarks>
		private uint total_frames;
		
		/// <summary>
		///    Contains the number of bits per sample.
		/// </summary>
		/// <remarks>
		///    This value is stored in bytes (67,68) and is typically
		///    16.
		/// </remarks>
		private ushort bits_per_sample;
		
		/// <summary>
		///    Contains the number of channels.
		/// </summary>
		/// <remarks>
		///    This value is stored in bytes (69,70) and is typically
		///    1 or 2.
		/// </remarks>
		private ushort channels;
		
		/// <summary>
		///    Contains the sample rate.
		/// </summary>
		/// <remarks>
		///    This value is stored in bytes (71-74) and is typically
		///    44100.
		/// </remarks>
		private uint sample_rate;
		
		/// <summary>
		///    Contains the length of the audio stream.
		/// </summary>
		/// <remarks>
		///    This value is provided by the constructor.
		/// </remarks>
		private long stream_length;
		
		#endregion
		
		
		
		#region Public Static Fields
		
		/// <summary>
		///    The size of a Monkey Audio header.
		/// </summary>
		public const uint Size = 76;
		
		/// <summary>
		///    The identifier used to recognize a WavPack file.
		/// </summary>
		/// <value>
		///    "MAC "
		/// </value>
		public static readonly ReadOnlyByteVector FileIdentifier =
			"MAC ";
		
		#endregion
		
		
		
		#region Constructors
		
		/// <summary>
		///    Constructs and initializes a new instance of <see
		///    cref="StreamHeader" /> for a specified header block and
		///    stream length.
		/// </summary>
		/// <param name="data">
		///    A <see cref="ByteVector" /> object containing the stream
		///    header data.
		/// </param>
		/// <param name="streamLength">
		///    A <see cref="long" /> value containing the length of the
		///    Monkey Audio stream in bytes.
		/// </param>
		/// <exception cref="ArgumentNullException">
		///    <paramref name="data" /> is <see langword="null" />.
		/// </exception>
		/// <exception cref="CorruptFileException">
		///    <paramref name="data" /> does not begin with <see
		///    cref="FileIdentifier" /> or is less than <see cref="Size"
		///    /> bytes long.
		/// </exception>
		public StreamHeader(ByteVector data, long streamLength)
		{
			if (data == null)
				throw new ArgumentNullException("data");
			
			if (!data.StartsWith(FileIdentifier))
				throw new CorruptFileException(
					"Data does not begin with identifier.");
			
			if (data.Count < Size)
				throw new CorruptFileException(
					"Insufficient data in stream header");
			
			stream_length = streamLength;
			version = data.Mid (4, 2).ToUShort(false);
			compression_level = (CompressionLevel) data.Mid(52, 2)
				.ToUShort(false);
			// format_flags = data.Mid(54, 2).ToUShort(false);
			blocks_per_frame = data.Mid(56, 4).ToUInt(false);
			final_frame_blocks = data.Mid(60, 4).ToUInt(false);
			total_frames = data.Mid(64, 4).ToUInt(false);
			bits_per_sample = data.Mid(68, 2).ToUShort(false);
			channels = data.Mid(70, 2).ToUShort(false);
			sample_rate = data.Mid(72, 4).ToUInt(false);
		}
		
		#endregion
		
		
		
		#region Public Properties
		
		/// <summary>
		///    Gets the duration of the media represented by the current
		///    instance.
		/// </summary>
		/// <value>
		///    A <see cref="TimeSpan" /> containing the duration of the
		///    media represented by the current instance.
		/// </value>
		public TimeSpan Duration {
			get {
				if (sample_rate <= 0 || total_frames <= 0)
					return TimeSpan.Zero;
				
				return TimeSpan.FromSeconds (
					(double) ((total_frames - 1) *
					blocks_per_frame + final_frame_blocks) /
					(double) sample_rate);
			}
		}
		
		/// <summary>
		///    Gets the types of media represented by the current
		///    instance.
		/// </summary>
		/// <value>
		///    Always <see cref="MediaTypes.Audio" />.
		/// </value>
		public MediaTypes MediaTypes {
			get {return MediaTypes.Audio;}
		}
		
		/// <summary>
		///    Gets a text description of the media represented by the
		///    current instance.
		/// </summary>
		/// <value>
		///    A <see cref="string" /> object containing a description
		///    of the media represented by the current instance.
		/// </value>
		public string Description {
			get {
				return string.Format(
					CultureInfo.InvariantCulture,
					"Monkey's Audio APE Version {0:0.000}",
					Version);
			}
		}
		
		/// <summary>
		///    Gets the bitrate of the audio represented by the current
		///    instance.
		/// </summary>
		/// <value>
		///    A <see cref="int" /> value containing a bitrate of the
		///    audio represented by the current instance.
		/// </value>
		public int AudioBitrate {
			get {
				TimeSpan d = Duration;
				if (d <= TimeSpan.Zero)
					return 0;
				
				return (int) ((stream_length * 8L) /
					d.TotalSeconds) / 1000;
			}
		}
		
		/// <summary>
		///    Gets the sample rate of the audio represented by the
		///    current instance.
		/// </summary>
		/// <value>
		///    A <see cref="int" /> value containing the sample rate of
		///    the audio represented by the current instance.
		/// </value>
		public int AudioSampleRate {
			get {return (int)sample_rate;}
		}
		
		/// <summary>
		///    Gets the number of channels in the audio represented by
		///    the current instance.
		/// </summary>
		/// <value>
		///    A <see cref="int" /> value containing the number of
		///    channels in the audio represented by the current
		///    instance.
		/// </value>
		public int AudioChannels {
			get {return channels;}
		}
		
		/// <summary>
		///    Gets the APE version of the audio represented by the
		///    current instance.
		/// </summary>
		/// <value>
		///    A <see cref="double" /> value containing the APE version
		///    of the audio represented by the current instance.
		/// </value>
		public double Version {
			get {return (double) version / (double) 1000;}
		}
		
		/// <summary>
		///    Gets the number of bits per sample in the audio
		///    represented by the current instance.
		/// </summary>
		/// <value>
		///    A <see cref="int" /> value containing the number of bits
		///    per sample in the audio represented by the current
		///    instance.
		/// </value>
		public int BitsPerSample {
			get {return bits_per_sample;}
		}
		
		/// <summary>
		///    Gets the level of compression used when encoding the
		///    audio represented by the current instance.
		/// </summary>
		/// <value>
		///    A <see cref="CompressionLevel" /> value indicating the
		///    level of compression used when encoding the audio
		///    represented by the current instance.
		/// </value>
		public CompressionLevel Compression {
			get {return compression_level;}
		}
		
		#endregion
	}
}