@techno_neko
組み込み系
Hokkaido.pm
簡単な紹介
こんな感じ
ちゃんとやります!
Perl Data Language
行列の計算とか簡単にできる!
$ cpanm PDL
use v5.10; use PDL; my $a = pdl( 1, 2, 3 ); say ref $a; # PDL say $a; # [1 2 3] # 上と同じ my $b = pdl [ 1, 2, 3 ]; say $b; # [1 2 3]
use v5.10; use PDL; my $a = pdl( 2, 4 ); say '$a => ', $a; # $a => [2 4] say '$a + 2 => ', $a + 2; # $a + 2 => [4 6] say '$a - 2 => ', $a - 2; # $a - 2 => [0 2] say '$a * 2 => ', $a * 2; # $a * 2 => [4 8] say '$a / 2 => ', $a / 2; # $a / 2 => [1 2]
use v5.10; use PDL; use PDL::Matrix; my $a = mpdl( [0, 1, 2], [2, 1, 0], [0, 0, 2] ); say ref $a; # PDL::Matrix say $a; #[ # [0 1 2] # [2 1 0] # [0 0 2] #]
use v5.10; use PDL; use PDL::Matrix; my $a = mpdl( [0, 1, 2], [2, 1, 0], [0, 0, 2] ); my $b = inv $a; say $b; #[ # [-0.5 1 0] # [ 0.5 0 0] # [ 0.5 -1 0.5] #]
use v5.10; use PDL; use PDL::Matrix; my $a = pdl( [0, 1, 2], [2, 1, 0], [0, 0, 2] ); my $b = inv $a; my $c = $a x $b; # 'x'を使ってる点に注意 say $c; #[ # [1 0 0] # [0 1 0] # [0 0 1] #]
リズムマシンのような音を出す方法について
WEB+DB PRESS Vol.69のサポートページから!
是非、お手元に!
cpanmのハマりどころを9ページに渡って網羅!
すべての要素に対する掛け算(音量の適用)
配列同士の足し算
配列内の-1.0未満の要素を-1.0に、
+1.0を超える要素を+1.0に書き換える
my @samples = (); foreach my $ch_info ( @beats ) { my $ch = create_channel( $bpm, $ch_info ); my $vol = $ch_info->{vol}; for (my $i=0; $i<scalar(@{$ch}); $i++) { $samples[$i] += ( $ch->[$i] * $vol ); } }
my $samples = zeros( 0 ); foreach my $ch_info ( @beats ) { my $ch = pdl( create_channel($bpm, $ch_info) ) * $ch_info->{vol}; # ミックスするデータの長さを揃える ... $samples += $ch; }
my $samples = zeros( 0 ); foreach my $ch_info ( @beats ) { my $ch = pdl( create_channel($bpm, $ch_info) ) * $ch_info->{vol}; # ミックスするデータの長さを揃える if ( $samples->nelem < $ch->nelem ) { $samples = $samples->append( zeros($ch->nelem - $samples->nelem) ); } elsif ( $ch->nelem < $samples->nelem ) { $ch = $ch->append( zeros($samples->nelem - $ch->nelem) ); } $samples += $ch; }
@samples = map { ( 1.0 < $_ ) ? 1.0 : ( ($_ < -1.0) ? -1.0 : $_ ); } @samples;
$samples = $samples->clip( -1, 1 );
use v5.10; use PDL; my $a = pdl( 1, 2, 3 ); say $a; # [1 2 3] say join( ',', list($a) ); # 1,2,3 my $ary_ref = unpdl( $a ); say join( ',', @{$ary_ref} ); # 1,2,3
/
#