Audio-Analyzer-ToneDetect

 view release on metacpan or  search on metacpan

lib/Audio/Analyzer/ToneDetect.pm  view on Meta::CPAN

        $lower = $possibility if $possibility <= $freq;
        $upper = $possibility if $possibility > $freq;
    }
    $upper ||= $lower;
    my $valid_tone
        = ( $freq - $lower ) < ( $upper - $freq )
        ? $lower
        : $upper;

    if ( $self->{valid_error_cb} ) {
        my $cb_result = $self->{valid_error_cb}
            ->( $valid_tone, $freq, $freq - $valid_tone );
        if ( defined $cb_result ) {
            return if $cb_result == 0;
            $valid_tone = $cb_result;
        }
    }

    return ( $valid_tone, $freq - $valid_tone );
}

sub _all_match { my $l = shift; $_ == $l->[0] || return 0 for @$l; return 1 }

sub _is_pow_of_two {

    # if pow of 2 exactly 1 bit is set all others unset and n - 1 will have
    # that bit unset and all lower bits set thus binary AND of n & n -1 will
    # result in 0
    return $_[0] != 0 && ( $_[0] & ( $_[0] - 1 ) ) == 0;
}

sub _get_builtin_tones {

    # via http://sourceforge.net/projects/tonedetect/ not sure complete/accurate
    # want better list

    return [ ( qw (
                282.2 288.5 294.7 296.5 304.7 307.8 313.0 321.4 321.7
                330.5 335.6 339.6 346.7 349.0 350.5 358.6 358.9 366.0
                368.5 371.5 378.6 382.3 384.6 389.0 398.1 399.2 399.8
                410.8 412.1 416.9 422.1 426.6 433.7 435.3 441.6 445.7
                454.6 457.1 457.9 470.5 473.2 474.8 483.5 489.8 495.8
                496.8 507.0 510.5 517.5 517.8 524.6 524.8 532.5 539.0
                540.7 543.3 547.5 553.9 562.3 562.5 564.7 569.1 577.5
                582.1 584.8 589.7 592.5 600.9 602.6 604.2 607.5 615.8
                617.4 622.5 623.7 631.5 634.5 637.5 640.6 643.0 645.7
                651.9 652.6 662.3 667.5 668.3 669.9 672.0 682.5 688.3
                691.8 693.0 697.5 701.0 707.3 712.5 716.7 726.8 727.1
                727.5 732.0 741.3 746.8 757.5 761.3 765.0 767.4 767.4
                772.5 787.5 788.5 794.3 795.4 799.0 802.5 810.2 817.5
                822.2 832.5 832.5 832.9 834.0 847.5 851.1 855.5 862.5
                870.5 871.0 877.5 879.0 881.0 892.5 903.2 907.9 910.0
                911.5 912.0 922.5 928.1 937.5 944.1 950.0 952.4 952.5
                953.7 967.5 977.2 979.9 984.4 992.0 996.8 1006.9 1011.6
                1034.7 1036.0 1041.2 1047.1 1063.2 1082.0 1084.0 1089.0 1092.4
                1122.1 1122.5 1130.0 1140.2 1153.4 1161.4 1180.0 1185.2 1191.4
                1217.8 1232.0 1246.0 1251.4 1285.8 1287.0 1304.0 1321.2 1344.0
                1357.6 1362.1 1395.0 1403.0 1423.5 1433.4 1465.0 1488.4 1530.0
                1556.7 1598.0 1628.3 1642.0 1669.0 1717.1 1743.0 1795.6 1820.0
                1877.5 1901.0 1985.0 2051.6 2073.0 2143.8 2164.0 2260.0 2341.8
                2361.0 2447.6 2465.0 2556.9 2575.0 2672.9 2688.0 2792.4 2807.0
                2932.0 3062.0 3197.0 3339.0 3487.0 )
        ) ];
}

1;
__END__

=encoding utf-8

=head1 NAME

Audio::Analyzer::ToneDetect - Detect freq of tones in an audio file or stream

=begin HTML

<a href="https://travis-ci.org/mikegrb/Audio-Analyzer-ToneDetect"><img src="https://api.travis-ci.org/mikegrb/Audio-Analyzer-ToneDetect.png" width="77" height="19" alt="Build Status" /></a>

=end HTML

=head1 SYNOPSIS

  use Audio::Analyzer::ToneDetect;
  my $tone_detect = Audio::Analyzer::ToneDetect->new( source => \*STDIN );
  my $tone = $tone_detect->get_next_tone();
  say "I heard $tone!";

=head1 DESCRIPTION

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.

=head1 USAGE

=head2 new (%opts)

Takes the following named parameters:

=over 4

=item source \*FH or $path

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.


=item sample_rate 16000

Source sample rate, results will be orders of magnitude off if set incorrectly.
Defaults to 16000.

=item chunk_size 1024

Number of samples to analyze at once.  Corresponds to dft_size in L<Audio::Analyzer>.
Must be a power of 2.  Defaults to 1024.

=item chunk_max 70



( run in 1.563 second using v1.01-cache-2.11-cpan-39bf76dae61 )