-
Notifications
You must be signed in to change notification settings - Fork 43
/
wvheader.pm
64 lines (57 loc) · 1.73 KB
/
wvheader.pm
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
# Copyright (C) 2007 Quentin Sculo <squentin@free.fr>
#
# This file is part of Gmusicbrowser.
# Gmusicbrowser is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation
package Tag::WVfile;
use strict;
use warnings;
our @ISA=('Tag::MP3');
my @sample_rates;
INIT
{ @sample_rates = (6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000, 192000);
}
sub new
{ my ($class,$file,$findlength)=@_;
my $self=bless {}, $class;
local $_;
# check that the file exists
unless (-e $file)
{ warn "File '$file' does not exist.\n";
return undef;
}
$self->{filename} = $file;
$self->_open or return undef;
$self->_FindTags;
$self->_ReadHeader;
return undef unless $self->{info};
$self->_close;
return $self;
}
sub _ReadHeader
{ my $self=$_[0];
my %info;
my $fh=$self->{fileHandle};
my $offset=$self->{startaudio};
seek $fh,$offset,0;
my $buf;
return unless read($fh,$buf,32)==32;
return unless $buf=~m/^wvpk/;
my ($block_size,$ver,$total_samples,$block_index,$block_samples,$flags)=unpack 'x4Vvx2VVVV',$buf;
$total_samples=0 if $total_samples==0xffff; #unknown length
$ver=sprintf '4.%x',$ver; $ver=~s/(\d)$/.$1/;
$info{version}=$ver;
$info{channels}= ($flags>>2) & 1 ? 1 : 2;
$info{frames}=$total_samples;
$info{rate}= $sample_rates[ ($flags>>23) & 0b1111 ];
#my $bytes_per_sample= ($flags & 0b11)+1;
if ($total_samples==0) {$info{seconds}=$info{bitrate}=0}
else
{ $info{seconds}=( $total_samples/$info{rate} );
$info{bitrate}=( $self->{endaudio}-$self->{startaudio} )*8/$info{seconds};
}
#warn "$_=$info{$_}\n" for keys %info;
$self->{info}=\%info;
}
1;