//******************************************************************************************************
// DTMF.cs - Gbtc
//
// Copyright © 2012, 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 MIT License (MIT), 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/MIT
//
// 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.
// 12/14/2012 - Starlynn 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 System;
using GSF.Media.Music;
namespace GSF.Media.Sound
{
/// Dual Tone Multi-Frequency Class.
///
/// This example generates some familiar dual tone multi-frequency sounds
/// and plays them over the computer's speakers:
///
/// using System;
/// using GSF.Media;
/// using GSF.Media.Sound;
///
/// static class Program
/// {
/// static void Main()
/// {
/// WaveFile waveFile = new WaveFile(SampleRate.Hz8000, BitsPerSample.Bits16, DataChannels.Mono);
/// double volume = 0.25D; // Set volume of tones to 25% of maximum
/// DTMF tone;
///
/// // Get the dial tone dual-frequencies
/// tone = DTMF.DialTone;
///
/// // Change the duration of the dial-tone to 3 seconds
/// tone.Duration = 3.0D;
///
/// // Generate a dial tone
/// DTMF.Generate(waveFile, tone, volume);
///
/// // Generate a busy-signal tone, repeat four times
/// DTMF.Generate(waveFile, DTMF.BusySignal, volume, 4);
///
/// // Generate an off-the-hook tone, repeat eight times
/// DTMF.Generate(waveFile, DTMF.OffHook, volume, 8);
///
/// // Get the EBS Alert dual-frequencies
/// tone = DTMF.EmergencyBroadcastSystemAlert;
///
/// // The official duration of an EBS Alert is 22.5 seconds, but
/// // the noise is rather annoying - so we set it to 4 seconds
/// tone.Duration = 4.0D;
///
/// // Generate the EBS Alert noise
/// DTMF.Generate(waveFile, tone, volume);
///
/// // Play all the generated tones
/// waveFile.Play();
///
/// Console.ReadKey();
/// }
/// }
///
///
public class DTMF
{
#region [ Members ]
// Fields
/// Low frequency of .
public double LowFrequency;
/// High frequency of .
public double HighFrequency;
/// Frequency duration, in seconds, of .
public double Duration;
#endregion
#region [ Constructors ]
///
/// Constructs a new .
///
public DTMF()
{
}
///
/// Constructs a new using specified parameters.
///
/// A low frequency value.
/// A high frequency value.
/// A duration value.
public DTMF(double lowFrequency, double highFrequency, double duration)
{
LowFrequency = lowFrequency;
HighFrequency = highFrequency;
Duration = duration;
}
#endregion
#region [ Static ]
// Static Properties
/// Gets the instance representing a telephone dial tone.
public static DTMF DialTone
{
get
{
return new DTMF(350.0D, 440.0D, 1.0D);
}
}
/// Gets the instances representing a telephone off-the-hook signal.
public static DTMF[] OffHook
{
get
{
return new[] { new DTMF(1400.0D, 2450.0D, 0.1D), new DTMF(2060.0D, 2600.0D, 0.1D) };
}
}
/// Gets the instances representing a telephone busy signal.
public static DTMF[] BusySignal
{
get
{
return new[] { new DTMF(480.0D, 620.0D, 0.5D), new DTMF(0.0D, 0.0D, 0.5D) };
}
}
/// Gets the instance representing the Emergency Broadcast System alert tone.
/// The official duration of an EBS Alert is 22.5 seconds.
public static DTMF EmergencyBroadcastSystemAlert
{
get
{
return new DTMF(853.0D, 960.0D, 22.5D);
}
}
// Static Methods
///
/// Computes a dual-tone multi-frequency sound for the given information and time.
///
/// Instance of the specifying the duration as well as the low and high frequencies of the dual-tone.
/// Sample index (represents time anywhere from zero to full length of tone).
/// Number of samples per second.
/// The amplitude for the dual-tone at the given time.
///
/// This method computes an amplitude representing the acoustic pressure of a
/// of the given frequency for the given time.
///
public static double ComputeFrequencies(DTMF tone, long sampleIndex, int sampleRate)
{
return (Timbre.PureTone(tone.LowFrequency, sampleIndex, 0, sampleRate) + Timbre.PureTone(tone.HighFrequency, sampleIndex, 0, sampleRate)) / 2;
}
///
/// Generates the specified dual-tone multi-frequency storing it in the specified .
///
/// used to store generated dual-tone multi-frequencies.
/// Dual-tone multi-frequency to generate.
/// Volume of generated dual-tones as a percentage (0 to 1).
public static void Generate(WaveFile destination, DTMF tone, double volume)
{
Generate(destination, new[] { tone }, volume, 1);
}
///
/// Generates the specified dual-tone multi-frequency times storing it in the specified .
///
/// used to store generated dual-tone multi-frequencies.
/// Dual-tone multi-frequency to generate.
/// Volume of generated dual-tones as a percentage (0 to 1).
/// Number of times to repeat the tone.
public static void Generate(WaveFile destination, DTMF tone, double volume, int repeatCount)
{
Generate(destination, new[] { tone }, volume, repeatCount);
}
///
/// Generates a single instance of each of the specified dual-tone multi-frequencies storing them in the specified .
///
/// used to store generated dual-tone multi-frequencies.
/// Dual-tone multi-frequencies to generate.
/// Volume of generated dual-tones as a percentage (0 to 1).
public static void Generate(WaveFile destination, DTMF[] tones, double volume)
{
Generate(destination, tones, volume, 1);
}
///
/// Generates the specified dual-tone multi-frequencies times storing them in the specified .
///
/// used to store generated dual-tone multi-frequencies.
/// Dual-tone multi-frequencies to generate.
/// Volume of generated dual-tones as a percentage (0 to 1).
/// Number of times to repeat each tone.
/// Value must be expressed as a fractional percentage between zero and one.
/// only generated for with a sample rate of 8, 16, 24, 32 or 64 bits per sample.
public static void Generate(WaveFile destination, DTMF[] tones, double volume, int repeatCount)
{
if (volume < 0.0D || volume > 1.0D)
throw new ArgumentOutOfRangeException(nameof(volume), "Value must be expressed as a fractional percentage between zero and one");
double amplitude = destination.AmplitudeScalar * volume;
// Iterate through each repeat count
for (int x = 0; x < repeatCount; x++)
{
// Interate through each tone
foreach (DTMF tone in tones)
{
// Iterate through each sample for total DTMF duration
for (long y = 0; y < tone.Duration * destination.SampleRate; y++)
{
// Compute frequencies of DTMF at given time and add to wave file
destination.AddSample(ComputeFrequencies(tone, y, destination.SampleRate) * amplitude);
}
}
}
}
#endregion
}
}