User:Kyz/Sandbox

Before:

sub get_inst_fasttracker {
  my $path = shift;
  print "$path\n";
  open(MOD, $path) || die "Unable to open $path\n";
  read(MOD, my $mark, 17);
  if ( lc($mark) eq "extended module: " ) {

    seek(MOD, 70, 0);
    # get number of patterns
    read(MOD, my $numpatt, 2);
    $numpatt = unpack("v",$numpatt);
    # and number of instruments
    read(MOD, my $numinst, 2);
    $numinst = unpack("v",$numinst);

    # get header size
    seek(MOD, 60, 0);
    read(MOD, my $headsize, 4);
    $headsize = unpack("V",$headsize);

    my $pattstart = $headsize + 60;

    # seek to pattern start
    seek(MOD, $pattstart, 0);
    for (my $i=0;$i<$numpatt;$i++) {
      read(MOD, my $pattheadsize, 4);
      seek(MOD, 3, 1);
      read(MOD, my $pattdatasize, 2);
      seek(MOD, (unpack("V",$pattheadsize) - 9) + unpack("v",$pattdatasize), 1);
    }

    my @inst;
    my @samp;
    #print "Instruments: $numinst\n";
    # seek to pattern start
    for (my $i=0;$i<$numinst;$i++) {
      read(MOD, my $instheadsize, 4) || next;
      $instheadsize = unpack("V",$instheadsize);
      read(MOD, my $instname, 22) || next;
      seek(MOD, 1, 1) || next;
      read(MOD, my $numsamples, 2) || next;
      $numsamples = unpack("v",$numsamples);
      seek(MOD, $instheadsize - 29, 1);
      my $samples = '';
      if ( $numsamples != 0 ) {
        my $samplesize_all = 0;
        for (my $j=0;$j<$numsamples;$j++) {
          read(MOD, my $samplesize, 4) || next;
          $samplesize = unpack('V',$samplesize);
          seek(MOD, 14, 1);
          read(MOD, my $samplename, 22) || next;
          $samplename =~ s/[\x00-\x1f]//g;
          $samplename =~ s/ +$//;
          push @samp, "$i\t$samplename\t$samplesize";
          $samplesize_all += $samplesize;
        }
        seek(MOD, $samplesize_all, 1);
        $samples = "\t".join(", ",@samp);
      }
      $instname =~ s/[\x00-\x1f]//g;
      push @inst, "$i\t$instname\t";
    }
    push @inst, '', @samp;
    close(MOD);
    return join("\n",@inst);
  } else {
    close(MOD);
    return undef;
  }
}

After:

sub read_xm_file {
     die "can't read $_[0]: $!" unless open(my $fh, '<', $_[0]);
     my $result = _parse_xm($fh);
     close $fh;
     return $result;
}

sub _parse_xm {
     my $fh = shift;
     my $buf;

     # read the module header
     return undef unless read $fh, $buf, 80;
     my ($id, $mod_name, $id2, $tracker_name, $version, $revision,
	$mod_header_size, $song_length, $restart_pos, $num_channels,
	$num_patterns, $num_instruments, $flags, $tempo, $bpm)
         = unpack 'a17 A20 C A20 C C V v v v v v v v v', $buf;

     # check module is valid
     return undef unless
        lc($id) eq 'extended module: ' &&
	$id2 == 26 &&
	$mod_header_size > 20 &&
        $num_instruments <= 128;

     # skip past pattern data
     return undef unless seek $fh, (60 + $mod_header_size), 0;
     for (1 .. $num_patterns) {
	return undef unless read $fh, $buf, 9;
	my ($header_size, $packing_type, $num_rows, $packed_size)
	    = unpack 'V C v v', $buf;
	return undef unless $header_size >= 9;
	return undef unless seek $fh, ($header_size - 9 + $packed_size), 1;
     }

     # read instrument definitions
     my @instruments;
     for (1 .. $num_instruments) {
	return undef unless read $fh, $buf, 29;
	my ($header_size, $name, $type, $num_samples)
	    = unpack 'V A22 C v', $buf;

	return undef unless $header_size >= 29;
	return undef unless seek $fh, $header_size - 29, 1;

	my @samples;
	my $all_samples_size = 0;
	for (1 .. $num_samples) {
	    return undef unless read $fh, $buf, 40;
	    my ($size, $loop_start, $loop_length, $volume, $finetune,
		$type, $panning, $relative_note, $reserved, $sample_name)
	    = unpack 'V V V C c C C c C A22', $buf;

	    push @samples, [$sample_name, $size];
	    $all_samples_size += $size;
	}
        seek $fh, $all_samples_size, 1;
	push @instruments, [$name, @samples];
     }

     return [$mod_name, @instruments];
}

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.