//******************************************************************************************************
// WaveFormatExtensible.cs - Gbtc
//
// Copyright © 2010, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/eclipse-1.0.php
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 07/29/2009 - J. Ritchie Carroll
// Generated original version of source code.
// 08/06/2009 - Josh L. Patterson
// Edited Comments.
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 10/8/2012 - Danyelle Gilliam
// Modified Header
//
//******************************************************************************************************
#region [ Contributor License Agreements ]
/**************************************************************************\
Copyright © 2009 - J. Ritchie Carroll
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/
#endregion
using GSF.Parsing;
using System;
namespace GSF.Media
{
#region [ Enumerations ]
/// Spatial positioning flags for property.
[Flags]
public enum Speakers
{
/// Speaker positions undefined.
Undefined = 0x0,
/// Front left speaker position.
FrontLeft = 0x1,
/// Front right speaker position.
FrontRight = 0x2,
/// Front center speaker position.
FronCenter = 0x4,
/// Low frequency speaker.
LowFrequency = 0x8,
/// Back left speaker position.
BackLeft = 0x10,
/// back right speaker position.
BackRight = 0x20,
/// Front left of center speaker position.
FronLeftOfCenter = 0x40,
/// Front right of center speaker position.
FronRightOfCenter = 0x80,
/// Back center speaker position.
BackCenter = 0x100,
/// Side left speaker position.
SideLeft = 0x200,
/// Side right speaker position.
SideRight = 0x400,
/// Top center speaker position.
TopCenter = 0x800,
/// Top front left speaker position.
TopFrontLeft = 0x1000,
/// Top front center speaker position.
TopFrontCenter = 0x2000,
/// Top front right speaker position.
TopFrontRight = 0x4000,
/// Top back left speaker position.
TopBackLeft = 0x8000,
/// Top back center speaker position.
TopBackCenter = 0x10000,
/// Top back right speaker position.
TopBackRight = 0x20000,
/// Reserved flags for enumeration.
Reserved = 0x7FFC0000,
/// All Speaker positions defined.
All = -2147483648 // 0x80000000
}
/// Common sub-type GUID's for property.
public static class DataFormatSubType
{
/// Standard pulse-code modulation audio format
///
/// PCM (Pulse Code Modulation) is a common method of storing and transmitting uncompressed digital audio.
/// Since it is a generic format, it can be read by most audio applications—similar to the way a plain text
/// file can be read by any word-processing program. PCM is used by Audio CDs and digital audio tapes (DATs).
/// PCM is also a very common format for AIFF and WAV files.
///
public static Guid PCM = new Guid("00000001-0000-0010-8000-00aa00389b71");
/// Adpative differential pulse-code modulation encoding algorithm
public static Guid ADPCM = new Guid("00000002-0000-0010-8000-00aa00389b71");
/// Floating point PCM encoding algorithm
public static Guid IeeeFloat = new Guid("00000003-0000-0010-8000-00aa00389b71");
/// Digital Rights Management encoded format (for digital-audio content protected by Microsoft DRM).
public static Guid DRM = new Guid("00000009-0000-0010-8000-00aa00389b71");
/// A-law encoding algorithm (used in Europe and the rest of the world)
public static Guid ALaw = new Guid("00000006-0000-0010-8000-00aa00389b71");
/// μ-law encoding algorithm (used in North America and Japan)
public static Guid MuLaw = new Guid("00000007-0000-0010-8000-00aa00389b71");
/// MPEG Audio is a family of open standards for compressed audio that includes MP2, MP3 and AAC.
public static Guid Mpeg = new Guid("00000050-0000-0010-8000-00aa00389b71");
/// Analog sub-format.
public static Guid Analog = new Guid("6dba3190-67bd-11cf-a0f7-0020afd156e4");
}
#endregion
///
/// Represents the "extensible" format structure for a WAVE media format file.
///
///
/// For some special bit-encodings you may need to use the "WaveFormatExtensible" audio format,
/// here is an example of how to use that format:
///
/// using System;
/// using GSF.Media;
/// using GSF.Media.Sound;
///
/// static class Program
/// {
/// static void Main()
/// {
/// // Generate an 8000 Hz, 32 bits per sample, mono channeled WAVE file in "Extensible" format
/// WaveFile waveFile = new WaveFile(8000, 32, 1, (ushort)WaveFormat.WaveFormatExtensible);
///
/// // Apply the "WaveFormatExtensible" extra parameters
/// WaveFormatExtensible extensible = new WaveFormatExtensible(waveFile.FormatChunk);
/// waveFile.ExtraParameters = extensible.BinaryImage;
///
/// // Generate the EBS Alert noise
/// DTMF.Generate(waveFile, DTMF.EmergencyBroadcastSystemAlert, 0.25D);
///
/// // Save the generated tone
/// waveFile.Save("ExtensibleTest.wav");
///
/// Console.Write("File available to be played from Windows Media Player...");
/// Console.ReadKey();
/// }
/// }
///
///
public class WaveFormatExtensible : ISupportBinaryImage
{
#region [ Members ]
// Constants
///
/// The fixed byte length of a instance.
///
public const int FixedLength = 22;
// Fields
private ushort m_sampleValue;
private Speakers m_channelMask;
private Guid m_subFormat;
#endregion
#region [ Constructors ]
///
/// Creates a new .
///
public WaveFormatExtensible()
{
}
///
/// Creates a new object based on the settings.
///
/// A format.
public WaveFormatExtensible(WaveFormatChunk waveFormat)
{
m_sampleValue = (ushort)waveFormat.BitsPerSample;
m_channelMask = Speakers.All;
m_subFormat = DataFormatSubType.PCM;
}
///
/// Creates a new object based on the given settings.
///
/// An value representing the sample value.
/// A object.
/// A value.
[CLSCompliant(false)]
public WaveFormatExtensible(ushort sampleValue, Speakers channelMask, Guid subFormat)
{
m_sampleValue = sampleValue;
m_channelMask = channelMask;
m_subFormat = subFormat;
}
#endregion
#region [ Properties ]
///
/// Gets or sets sample value.
///
[CLSCompliant(false)]
public ushort SampleValue
{
get
{
return m_sampleValue;
}
set
{
m_sampleValue = value;
}
}
///
/// Gets or sets flags representing spatial locations of data channels (i.e., speaker locations).
///
public Speakers ChannelMask
{
get
{
return m_channelMask;
}
set
{
m_channelMask = value;
}
}
///
/// Gets or sets for sub-format type of extensible WaveFile.
///
public Guid SubFormat
{
get
{
return m_subFormat;
}
set
{
m_subFormat = value;
}
}
///
/// Gets the length of the binary representation of this instance.
///
public int BinaryLength
{
get
{
return FixedLength;
}
}
#endregion
#region [ Methods ]
///
/// Parses object by parsing the specified containing a binary image.
///
/// Buffer containing binary image to parse.
/// 0-based starting index in the to start parsing.
/// Valid number of bytes within from .
/// The number of bytes used for initialization in the (i.e., the number of bytes parsed).
/// is null.
///
/// or is less than 0 -or-
/// and will exceed length.
///
public int ParseBinaryImage(byte[] buffer, int startIndex, int length)
{
buffer.ValidateParameters(startIndex, length);
m_sampleValue = EndianOrder.LittleEndian.ToUInt16(buffer, startIndex);
m_channelMask = (Speakers)EndianOrder.LittleEndian.ToInt32(buffer, startIndex + 2);
m_subFormat = EndianOrder.LittleEndian.ToGuid(buffer, startIndex + 6);
return BinaryLength;
}
///
/// Generates a binary representation of this and copies it into the given buffer.
///
/// Buffer used to hold generated binary image of the source object.
/// 0-based starting index in the to start writing.
/// The number of bytes written to the .
/// is null.
///
/// or is less than 0 -or-
/// and will exceed length.
///
public int GenerateBinaryImage(byte[] buffer, int startIndex)
{
buffer.ValidateParameters(startIndex, FixedLength);
EndianOrder.LittleEndian.CopyBytes(m_sampleValue, buffer, 0);
EndianOrder.LittleEndian.CopyBytes((int)m_channelMask, buffer, 2);
EndianOrder.LittleEndian.CopyBytes(m_subFormat, buffer, 6);
return FixedLength;
}
#endregion
}
}