1
/***************************************************************************
2
copyright : (C) 2006 by Brian Nickel
3
email : brian.nickel@gmail.com
4
based on : audioproperties.cpp from TagLib
5
***************************************************************************/
7
/***************************************************************************
8
* This library is free software; you can redistribute it and/or modify *
9
* it under the terms of the GNU Lesser General Public License version *
10
* 2.1 as published by the Free Software Foundation. *
12
* This library is distributed in the hope that it will be useful, but *
13
* WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15
* Lesser General Public License for more details. *
17
* You should have received a copy of the GNU Lesser General Public *
18
* License along with this library; if not, write to the Free Software *
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
21
***************************************************************************/
23
using System.Collections;
26
namespace TagLib.Ogg.Vorbis
28
public class Properties : AudioProperties
30
//////////////////////////////////////////////////////////////////////////
32
//////////////////////////////////////////////////////////////////////////
33
private TimeSpan duration;
35
private int sample_rate;
37
private int vorbis_version;
38
private int bitrate_maximum;
39
private int bitrate_nominal;
40
private int bitrate_minimum;
42
private static byte [] vorbis_comment_header_id = {0x01, (byte)'v', (byte)'o', (byte)'r', (byte)'b', (byte)'i', (byte)'s'};
45
//////////////////////////////////////////////////////////////////////////
47
//////////////////////////////////////////////////////////////////////////
48
public Properties (File file, ReadStyle style) : base (style)
50
duration = TimeSpan.Zero;
62
public Properties (File file) : this (file, ReadStyle.Average)
67
//////////////////////////////////////////////////////////////////////////
69
//////////////////////////////////////////////////////////////////////////
70
public override TimeSpan Duration {get {return duration;}}
71
public override int Bitrate {get {return (int) ((float)bitrate / 1000f + 0.5);}}
72
public override int SampleRate {get {return sample_rate;}}
73
public override int Channels {get {return channels;}}
74
public int VorbisVersion {get {return vorbis_version;}}
75
public int BitrateMaximum {get {return bitrate_maximum;}}
76
public int BitrateNominal {get {return bitrate_nominal;}}
77
public int BitrateMinimum {get {return bitrate_minimum;}}
80
//////////////////////////////////////////////////////////////////////////
82
//////////////////////////////////////////////////////////////////////////
83
private void Read (File file, ReadStyle style)
85
// Get the identification header from the Ogg implementation.
87
ByteVector data = file.GetPacket (0);
91
if (data.Mid (pos, 7) != vorbis_comment_header_id)
93
Debugger.Debug ("Vorbis.Properties.Read() -- invalid Vorbis identification header");
99
vorbis_version = (int) data.Mid(pos, 4).ToUInt (false);
102
channels = data [pos];
105
sample_rate = (int) data.Mid(pos, 4).ToUInt (false);
108
bitrate_maximum = (int) data.Mid(pos, 4).ToUInt (false);
111
bitrate_nominal = (int) data.Mid(pos, 4).ToUInt (false);
114
bitrate_minimum = (int) data.Mid(pos, 4).ToUInt (false);
116
// TODO: Later this should be only the "fast" mode.
117
bitrate = bitrate_nominal;
119
// Find the length of the file. See http://wiki.xiph.org/VorbisStreamLength/
120
// for my notes on the topic.
122
Ogg.PageHeader first = file.FirstPageHeader;
123
Ogg.PageHeader last = file.LastPageHeader;
125
if (first != null && last != null)
127
long start = first.AbsoluteGranularPosition;
128
long end = last.AbsoluteGranularPosition;
130
if (start >= 0 && end >= 0 && sample_rate > 0)
131
duration = TimeSpan.FromSeconds (((double)(end - start) / (double) sample_rate));
133
Debugger.Debug ("Vorbis.Properties.Read() -- Either the PCM " +
134
"values for the start or end of this file was " +
135
"incorrect or the sample rate is zero.");
138
Debugger.Debug("Vorbis.Properties.Read() -- Could not find valid first and last Ogg pages.");