summaryrefslogtreecommitdiff
path: root/datasets/dataset.pl
blob: 3b02199fc7129a8c2a514c37a159038b9cfe0ca1 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/perl

use strict;
use Getopt::Std;

our $opt_c;
our $opt_j;
getopts('c'); # normalize the file
getopts('j'); # jitter the pitch of each subsample by N rather than repitching big chunks
print "<<<< DATASET >>>>\n";

my $fmt = <<FMT;
Input File     : '01_snapping_clean.wav'
Channels       : 1
Sample Rate    : 44100
Precision      : 16-bit
Duration       : 00:03:09.35 = 8350438 samples = 14201.4 CDDA sectors
File Size      : 16.7M
Bit Rate       : 706k
Sample Encoding: 16-bit Signed Integer PCM
FMT

sub process($) {
  my $filename = shift or die "Usage: $0 [...filenames]\n";
  my ($name, $ext) = split(/\./, $filename, 2);
  if ($ext !~ /(wav|mp3|aiff?|flac)/) {
    print "not a valid file extension: $ext\n";
    return;
  }

  if ($ext eq 'mp3') {
    system('ffmpeg', '-i', $filename, $name . '.wav');
    $filename = $name . '.wav';
  }
  my $soxi = `soxi $filename`;
  my @lines = split("\n", $soxi);

  print $soxi;

  my $seconds;
  for my $line (@lines) {
    if ($line =~ /Duration       : (\d\d):(\d\d):(\d\d)\./) {
      my $h = $1;
      my $m = $2;
      my $s = $3;
      $seconds = (((($h * 60) + $m) * 60) + $s) + 0;
    }
  }


  print "Seconds: $seconds\n";

  my $a_tmp = "a_" . $filename;
  my $b_tmp = "b_" . $filename;
  if (!$opt_c) {
    print "Normalizing...";
    system("sox", "-v", 0.945, $filename, $a_tmp);
  } else {
    $a_tmp = $filename;
  }

  # jitter mide:  make copies of the original file and join together,
  #   split into chunks, and then pitchshift the chunks.
  if ($opt_j) {
    my $copies = 4080 / $seconds;
    print "Copies to make: $copies\n";
    print "\n";
    for (my $i = 0; $i < $copies; $i++) {
     system('cp', $a_tmp, sprintf("tmp_%d.wav", $i));
    }
    system 'sox', glob('tmp_*'), $b_tmp;
    system 'rm', glob 'tmp_*';
    system './split44k.sh', $b_tmp, 8, $name;
    opendir my $dh, $name or die "cant opendir $name: $!";
    while (readdir $dh) {
      next if /^\./;
      my $fn = $_;
      my $n = rand( 2 * $opt_j ) - $opt_j;
      system 'sox', $name . '/' . $fn, $name . '/z_' . $fn, 'pitch', $n;
      system 'rm', $name . '/' . $fn;
    }
    closedir $dh;
  }

  # standard mode: make a bunch of copies and then join them together,
  #   then subdivide into 8s chunks
  else {
    my $scale = sprintf("%.09f", 5e-7 * $seconds);
    print "Scale factor: $scale\n";
    print "\n";
    system("./spread.sh", $a_tmp, $b_tmp, 0.999, $scale, 1.001);
    system("./split44k.sh", $b_tmp, 8, $name);
  }
  if (!$opt_c) {
    system("/bin/rm", $a_tmp);
  }
  system("/bin/rm", $b_tmp);
}

foreach my $file (@ARGV) {
  print $file . "\n";
  if ( -e $file ) {
    process($file);
  }
}