-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanalyze-low-level
82 lines (67 loc) · 2.35 KB
/
analyze-low-level
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env perl
use strict;
use warnings;
use GD::Chart::Radial;
use Math::Utils qw(uniform_01scaling);
use Mojo::File;
use Statistics::Basic qw(mean);
use WebService::AcousticBrainz;
use WebService::MusicBrainz;
use constant PATH => $ENV{HOME} . '/tmp/radial/';
my $artist_id = shift || 'b1ac6222-a637-49cd-96df-d5a0ffa7ae14';
my $mb = WebService::MusicBrainz->new;
my $result = $mb->search(artist => { mbid => $artist_id, inc => ['recordings'] });
my @mbids = map { $_->{id} } @{ $result->{recordings} };
my $ab = WebService::AcousticBrainz->new;
my %stats;
my $i = 0;
for my $mbid (@mbids) {
$i++;
warn "$i. Fetching: $mbid\n";
my $r = $ab->fetch(mbid => $mbid, endpoint => 'low-level');
my $title = $r->{metadata}{tags}{title}[0];
$stats{$title} = {
dissonance => uniform_01scaling([0,0.5], $r->{lowlevel}{dissonance}{mean}),
dynamic_complexity => uniform_01scaling([0,8], $r->{lowlevel}{dynamic_complexity}),
spectral_complexity => uniform_01scaling([0,15], $r->{lowlevel}{spectral_complexity}{mean}),
# spectral_energy => uniform_01scaling([0,0.5], $r->{lowlevel}{spectral_energy}{mean}),
spectral_entropy => uniform_01scaling([3,9.5], $r->{lowlevel}{spectral_entropy}{mean}),
danceability => uniform_01scaling([0,2], $r->{rhythm}{danceability}),
# chords_changes_rate => uniform_01scaling([0,1], $r->{tonal}{chords_changes_rate}),
};
sleep 4; # play nice
}
my @stats = qw(
dissonance
dynamic_complexity
spectral_complexity
spectral_entropy
danceability
);
my %avgs;
for my $title (keys %stats) {
for my $stat (@stats) {
push @{ $avgs{$stat} }, $stats{$title}->{$stat};
}
}
for my $stat (keys %avgs) {
$avgs{$stat} = mean($avgs{$stat});
}
for my $title (sort keys %stats) {
my $chart = GD::Chart::Radial->new(1000, 1000);
$chart->set(
title => $title,
y_max_value => 1,
y_tick_number => 0.1,
colours => [qw(white black grey red)],
);
my @data = (
[ sort keys %{ $stats{$title} } ],
[ map { $avgs{$_} } sort keys %{ $stats{$title} } ],
[ map { $stats{$title}->{$_} } sort keys %{ $stats{$title} } ],
);
$chart->plot(\@data);
$title =~ s/\W/_/g;
my $file = Mojo::File->new(PATH . $title . '.png');
$file->spurt($chart->png);
}