/// <summary> /// Get array of touch tones for given string. /// </summary> /// <param name="keys">String of touch tone characters to convert to touch tones.</param> /// <param name="keyDuration">Duration of touch tone key press in seconds, typically fractional.</param> /// <param name="interKeyPause">Time to wait between key presses in seconds, typically fractional.</param> /// <returns>Array of touch tones for given string.</returns> /// <remarks>Non-touch tone characters are ignored. Commas are interpreted as a one second pause.</remarks> public static DTMF[] GetTouchTones(string keys, double keyDuration, double interKeyPause) { List <DTMF> touchTones = new List <DTMF>(); TouchTone touchTone; DTMF pause = new DTMF(0.0D, 0.0D, interKeyPause); DTMF longPause = new DTMF(0.0D, 0.0D, 1.0D); foreach (char key in keys) { if (key == ',') { // Interpret commas as long pauses touchTones.Add(longPause); } else if (TryParse(key, out touchTone)) { if (touchTones.Count > 0) { touchTones.Add(pause); } touchTone.Duration = keyDuration; touchTones.Add(touchTone); } } return(touchTones.ToArray()); }
/// <summary> /// Generates the specified dual-tone multi-frequencies <paramref name="repeatCount"/> times storing them in the specified <see cref="WaveFile"/>. /// </summary> /// <param name="destination"><see cref="WaveFile"/> used to store generated dual-tone multi-frequencies.</param> /// <param name="tones">Dual-tone multi-frequencies to generate.</param> /// <param name="volume">Volume of generated dual-tones as a percentage (0 to 1).</param> /// <param name="repeatCount">Number of times to repeat each tone.</param> /// <exception cref="ArgumentOutOfRangeException">Value must be expressed as a fractional percentage between zero and one.</exception> /// <exception cref="InvalidOperationException"><see cref="DTMF"/> only generated for <see cref="WaveFile"/> with a sample rate of 8, 16, 24, 32 or 64 bits per sample.</exception> public static void Generate(WaveFile destination, DTMF[] tones, double volume, int repeatCount) { if (volume < 0.0D || volume > 1.0D) { throw new ArgumentOutOfRangeException("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(DTMF.ComputeFrequencies(tone, y, destination.SampleRate) * amplitude); } } } }
/// <summary> /// Generates the specified dual-tone multi-frequencies <paramref name="repeatCount"/> times storing them in the specified <see cref="WaveFile"/>. /// </summary> /// <param name="destination"><see cref="WaveFile"/> used to store generated dual-tone multi-frequencies.</param> /// <param name="tones">Dual-tone multi-frequencies to generate.</param> /// <param name="volume">Volume of generated dual-tones as a percentage (0 to 1).</param> /// <param name="repeatCount">Number of times to repeat each tone.</param> /// <exception cref="ArgumentOutOfRangeException">Value must be expressed as a fractional percentage between zero and one.</exception> /// <exception cref="InvalidOperationException"><see cref="DTMF"/> only generated for <see cref="WaveFile"/> with a sample rate of 8, 16, 24, 32 or 64 bits per sample.</exception> public static void Generate(WaveFile destination, DTMF[] tones, double volume, int repeatCount) { if (volume < 0.0D || volume > 1.0D) throw new ArgumentOutOfRangeException("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(DTMF.ComputeFrequencies(tone, y, destination.SampleRate) * amplitude); } } } }
/// <summary> /// Generates a single instance of each of the specified dual-tone multi-frequencies storing them in the specified <see cref="WaveFile"/>. /// </summary> /// <param name="destination"><see cref="WaveFile"/> used to store generated dual-tone multi-frequencies.</param> /// <param name="tones">Dual-tone multi-frequencies to generate.</param> /// <param name="volume">Volume of generated dual-tones as a percentage (0 to 1).</param> public static void Generate(WaveFile destination, DTMF[] tones, double volume) { Generate(destination, tones, volume, 1); }
/// <summary> /// Generates the specified dual-tone multi-frequency <paramref name="repeatCount"/> times storing it in the specified <see cref="WaveFile"/>. /// </summary> /// <param name="destination"><see cref="WaveFile"/> used to store generated dual-tone multi-frequencies.</param> /// <param name="tone">Dual-tone multi-frequency to generate.</param> /// <param name="volume">Volume of generated dual-tones as a percentage (0 to 1).</param> /// <param name="repeatCount">Number of times to repeat the tone.</param> public static void Generate(WaveFile destination, DTMF tone, double volume, int repeatCount) { Generate(destination, new DTMF[] { tone }, volume, repeatCount); }
/// <summary> /// Generates the specified dual-tone multi-frequency storing it in the specified <see cref="WaveFile"/>. /// </summary> /// <param name="destination"><see cref="WaveFile"/> used to store generated dual-tone multi-frequencies.</param> /// <param name="tone">Dual-tone multi-frequency to generate.</param> /// <param name="volume">Volume of generated dual-tones as a percentage (0 to 1).</param> public static void Generate(WaveFile destination, DTMF tone, double volume) { Generate(destination, new DTMF[] { tone }, volume, 1); }
// Static Methods /// <summary> /// Computes a dual-tone multi-frequency sound for the given <see cref="DTMF"/> information and time. /// </summary> /// <param name="tone">Instance of the <see cref="DTMF"/> specifying the duration as well as the low and high frequencies of the dual-tone.</param> /// <param name="sampleIndex">Sample index (represents time anywhere from zero to full length of tone).</param> /// <param name="sampleRate">Number of samples per second.</param> /// <returns>The amplitude for the dual-tone at the given time.</returns> /// <remarks> /// This method computes an amplitude representing the acoustic pressure of a /// <see cref="DTMF"/> of the given frequency for the given time. /// </remarks> 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; }
/// <summary> /// Generates the specified dual-tone multi-frequency <paramref name="repeatCount"/> times storing it in the specified <see cref="WaveFile"/>. /// </summary> /// <param name="destination"><see cref="WaveFile"/> used to store generated dual-tone multi-frequencies.</param> /// <param name="tone">Dual-tone multi-frequency to generate.</param> /// <param name="volume">Volume of generated dual-tones as a percentage (0 to 1).</param> /// <param name="repeatCount">Number of times to repeat the tone.</param> public static void Generate(WaveFile destination, DTMF tone, double volume, int repeatCount) { Generate(destination, new[] { tone }, volume, repeatCount); }
/// <summary> /// Generates the specified dual-tone multi-frequency storing it in the specified <see cref="WaveFile"/>. /// </summary> /// <param name="destination"><see cref="WaveFile"/> used to store generated dual-tone multi-frequencies.</param> /// <param name="tone">Dual-tone multi-frequency to generate.</param> /// <param name="volume">Volume of generated dual-tones as a percentage (0 to 1).</param> public static void Generate(WaveFile destination, DTMF tone, double volume) { Generate(destination, new[] { tone }, volume, 1); }
// Static Methods /// <summary> /// Computes a dual-tone multi-frequency sound for the given <see cref="DTMF"/> information and time. /// </summary> /// <param name="tone">Instance of the <see cref="DTMF"/> specifying the duration as well as the low and high frequencies of the dual-tone.</param> /// <param name="sampleIndex">Sample index (represents time anywhere from zero to full length of tone).</param> /// <param name="sampleRate">Number of samples per second.</param> /// <returns>The amplitude for the dual-tone at the given time.</returns> /// <remarks> /// This method computes an amplitude representing the acoustic pressure of a /// <see cref="DTMF"/> of the given frequency for the given time. /// </remarks> 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); }
/// <summary> /// Get array of touch tones for given string. /// </summary> /// <param name="keys">String of touch tone characters to convert to touch tones.</param> /// <param name="keyDuration">Duration of touch tone key press in seconds, typically fractional.</param> /// <param name="interKeyPause">Time to wait between key presses in seconds, typically fractional.</param> /// <returns>Array of touch tones for given string.</returns> /// <remarks>Non-touch tone characters are ignored. Commas are interpreted as a one second pause.</remarks> public static DTMF[] GetTouchTones(string keys, double keyDuration, double interKeyPause) { List<DTMF> touchTones = new List<DTMF>(); TouchTone touchTone; DTMF pause = new DTMF(0.0D, 0.0D, interKeyPause); DTMF longPause = new DTMF(0.0D, 0.0D, 1.0D); foreach (char key in keys) { if (key == ',') { // Interpret commas as long pauses touchTones.Add(longPause); } else if (TouchTone.TryParse(key, out touchTone)) { if (touchTones.Count > 0) touchTones.Add(pause); touchTone.Duration = keyDuration; touchTones.Add(touchTone); } } return touchTones.ToArray(); }