Merge pull request #803 from stefolof/master

First commit of BPM to ms converter Goodie
master
Rob Emery 2015-02-07 01:14:34 +00:00
commit de7a4b6b51
3 changed files with 143 additions and 0 deletions

60
lib/DDG/Goodie/BPMToMs.pm Normal file
View File

@ -0,0 +1,60 @@
package DDG::Goodie::BPMToMs;
# ABSTRACT: Displays common note values in milliseconds for a given tempo measured in quarter notes per minute.
use DDG::Goodie;
zci answer_type => "bpmto_ms";
zci is_cached => 1;
name "BPM (beats per minute) to ms (milliseconds) converter";
description "Takes a tempo as BPM (beats per minute), eg. 120, and returns the corresponding note values as milliseconds.";
primary_example_queries "120 bpm to ms";
category "conversions";
topics "music";
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/BPMToMs.pm";
attribution github => ["https://github.com/stefolof", "stefolof"],
twitter => "stefolof";
triggers end => "bpm to ms", "bpm to milliseconds", "bpm to note values", "bpm to note lengths", "bpm", "bpm timings", "beats per minute to milliseconds",
"beats per minute to ms", "beats per minute to note values", "beats per minute to note lengths", "beats per minute", "beats per minute timings";
handle remainder => sub {
my $bpm = shift;
return unless $_ =~ /^\d+$/i; # Only integer values accepted
my @note_names = ( "Whole Note", "Half Note", "Quarter Note", "1/8 Note", "1/16 Note", "1/32 Note" );
my $straight_whole_note = 240000;
my @divisors = map { 2 ** $_ } 0 .. 5; # Create a list of divisors to calculate the values of half notes, quarter notes etc.
my @straight_values = map { int( $straight_whole_note / ($bpm * $_) + 0.5) } @divisors;
my $plain_text_content = "$bpm bpm in milliseconds
Whole Note: " . $straight_values[0] . "
Half Note: " . $straight_values[1] . "
Quarter Note: " . $straight_values[2] . "
1/8 Note: " . $straight_values[3] . "
1/16 Note: " . $straight_values[4] . "
1/32 Note: " . $straight_values[5];
my $html_content = "<div class=\"bpmto_ms\">";
$html_content = $html_content . "<h3 class=\"zci__header\">$bpm bpm in milliseconds</h3>";
$html_content = $html_content . "<div class=\"zci__content\">";
$html_content = $html_content . "<div class=\"record\">";
$html_content = $html_content . "<table class=\"maintable\">";
for my $i (0 .. $#note_names) {
$html_content = $html_content . "<tr class=\"record\">";
$html_content = $html_content . "<td class=\"record__cell__key record_keyspacing\">$note_names[$i]</td>";
$html_content = $html_content . "<td class=\"record__cell__value numbers\">$straight_values[$i]</td>";
$html_content = $html_content . "</tr>";
}
$html_content = $html_content .
"</table></div></div></div>";
return html => $html_content,
answer => $plain_text_content;
};
1;

View File

@ -0,0 +1,45 @@
@media (max-width: 600px) {
.bpmto_ms table {
table-layout: fixed;
width: 100%;
}
.bpmto_ms table td {
word-wrap: break-word;
}
.bpmto_ms .record td.record__cell__key {
width: 100px;
border-top: solid 1px #DDD;
border-bottom: solid 1px #DDD;
}
}
.bpmto_ms .maintable td {
width:12.5%;
white-space: nowrap;
}
.zci--answer .bpmto_ms .zci__more-at {
padding-top: 0;
}
.bpmto_ms .record > td {
padding: 0.4em;
padding-left: 0;
border-top: solid 1px #DDD;
border-bottom: 0;
}
.zci.zci--answer {
padding-bottom: 0.5em;
}
.bpmto_ms .record .numbers {
text-align: right;
}
.bpmto_ms .record .record__cell__key {
text-transform: capitalize;
width: 150px;
}

38
t/BPMToMs.t Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => "bpmto_ms";
zci is_cached => 1;
ddg_goodie_test(
[qw( DDG::Goodie::BPMToMs)],
(map { ("$_" => test_zci(
'120 bpm in milliseconds
Whole Note: 2000
Half Note: 1000
Quarter Note: 500
1/8 Note: 250
1/16 Note: 125
1/32 Note: 63',
html => qr#<td class="record__cell__key record_keyspacing">Whole Note</td><td class="record__cell__value numbers">2000</td>#
)) } ( '120 bpm to ms', '120 beats per minute to note lengths', '120 bpm timings' )),
(map { ("$_" => test_zci(
'61 bpm in milliseconds
Whole Note: 3934
Half Note: 1967
Quarter Note: 984
1/8 Note: 492
1/16 Note: 246
1/32 Note: 123',
html => qr#<td class="record__cell__key record_keyspacing">Whole Note</td><td class="record__cell__value numbers">3934</td>#
)) } ( '61 beats per minute to ms', '61 bpm', '61 bpm to note values' )),
'-1 bpm to ms' => undef,
'some bpm to ms' => undef,
'bpm' => undef,
);
done_testing;