Audio::Analyzer::ToneDetect - Detect freq of tones in an audio file or stream
use Audio::Analyzer::ToneDetect; my $tone_detect = Audio::Analyzer::ToneDetect->new( source => \*STDIN ); my $tone = $tone_detect->get_next_tone(); say "I heard $tone!";
Consider this alpha software. It is still under fairly active development and the interface may change in incompatible ways.
Audio::Analyzer::ToneDetect is a module for detecting single frequency tones in an audio stream or file. It supports mono PCM data and defaults to STDIN. For supporting other formats, eg MP3, you can pipe things through sox.
Takes the following named parameters:
The audio source. Only Mono PCM is supported. You can pass the path to a WAV file or a file handle for an open file or stream. Defaults to STDIN.
Source sample rate, results will be orders of magnitude off if set incorrectly. Defaults to 16000.
Number of samples to analyze at once. Corresponds to dft_size in Audio::Analyzer. Must be a power of 2. Defaults to 1024.
Maximum number of chunks to process before returning. Returns false if it reaches this number of chunks without detecting a tone. With default chunk_size and sample_rate, the default of 70 equates to about 4.5 seconds of audio.
Minimum durration of a tone, in seconds, before we consider it detected. Due to sample rate, chunk size, and integer math, with defaults this ends up being 0.448 seconds. The formula for actual seconds is int( min_length * sample_rate / chunk_size ) * chunk_size / sample_rate. Default to 0.5
A list of valid (expected) tones. If supplied, the closest expected tone for a given detected tone is returned. Call get_next_tone() in list context or supply the following call back if you want both values. A value of 'builtin' uses a builtin list of valid classic Motorola Minitor tones. Defaults to unset.
A callback that if provided and valid_tones is set will be called just before get_next_tone or find_closest_valid returns. Arguments are the closest valid tone, the actual detected tone, the diference between the two in Hertz.
Example:
valid_error_cb => sub { printf "VF %s DF %s EF %.2f\n", @_; return }
Return value is expected to be one of three possibilities.
Has no effect on program flow, if you don't want your call back changing stuff make sure you have an explicit 'return' as the last line.
A return value of 0 discards the tone and continues the get_next_tone loop.
Any other value replaces the valid detected tone with the return value from the call back.
If specified, a reference to an array of frequencies that will be ignored. e.g roger beeps, repeater beeps, etc. Note, if you use valid tone detection, then this is the raw detected tone, not the closest match. Defaults to empty list.
Returns the arraref of valid tones currently being used. Optionally takes a reference to an array of new tones to use that will be copied to replace the current valid list.
Returns the next detected tone in the stream. Will return false if we go through chunk_max without detecting a tone but the buffer will be preserved between calls if the a tone begins just before hitting chunk_max. If valid_tones was supplied, returns the result of passing the tone to find_closest_valid(), following it's list vs scalar semantics.
Calls get next tone twice. Will return false if either tone returns false.
In scalar context, returns the closest valid tone in valid_tones. In list context returns the closest valid tone and the delta from detected tone.
Mike Greb <michael@thegrebs.com>
Copyright 2013 - Mike Greb
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Audio::Analyzer
Math::FFT
1 POD Error
The following errors were encountered while parsing the POD:
Expected text after =item, not a number
To install Audio::Analyzer::ToneDetect, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Audio::Analyzer::ToneDetect
CPAN shell
perl -MCPAN -e shell install Audio::Analyzer::ToneDetect
For more information on module installation, please visit the detailed CPAN module installation guide.