示例#1
0
        /// <summary>Creates a new instance of LiveAudioDtmfAnalyzer.</summary>
        /// <param name="waveIn">The audio data source.</param>
        /// <param name="forceMono">Indicates whether the audio data should be converted to mono
        /// first. Default is true.</param>
        public LiveAudioDtmfAnalyzer(IWaveIn waveIn, bool forceMono = true)
        {
            this.waveIn = waveIn;
            var config = new DetectorConfig();

            dtmfAudio = DtmfAudio.CreateFrom(new StreamingSampleSource(config, Buffer(waveIn), forceMono), config);
        }
        /// <summary>Reads a WaveStream and enumerates all present DTMF tones.</summary>
        /// <remarks>By default this method forces a mono conversion by averaging all audio channels first. Turn it off with the
        ///  forceMono flag in order to analyze each channel separately.</remarks>
        /// <param name="waveFile">The audio data to analyze.</param>
        /// <param name="forceMono">Indicates whether the audio data should be converted to mono first. Default is true.</param>
        /// <returns>All detected DTMF tones along with their positions (i.e. audio channel, start time, and duration).</returns>
        public static IEnumerable <DtmfOccurence> DtmfTones(this WaveStream waveFile, bool forceMono = true)
        {
            var config        = new DetectorConfig();
            var dtmfAudio     = DtmfAudio.CreateFrom(new StaticSampleSource(config, waveFile, forceMono), config);
            var detectedTones = new Queue <DtmfOccurence>();

            while (detectedTones.Any() || detectedTones.AddNextFrom(dtmfAudio, waveFile))
            {
                if (detectedTones.Any())
                {
                    yield return(detectedTones.Dequeue());
                }
            }

            // Yield any tones that might have been cut off by EOF.
            foreach (var tone in detectedTones)
            {
                yield return(tone);
            }
        }
 public static bool AddNextFrom(this Queue <DtmfOccurence> detectedTones, DtmfAudio dtmfAudio, WaveStream waveFile)
 {
     return(dtmfAudio.Forward(
                (channel, tone) => waveFile.CurrentTime,
                (channel, start, tone) => detectedTones.Enqueue(new DtmfOccurence(tone, channel, start, waveFile.CurrentTime - start))));
 }