AIFF.pm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #------------------------------------------------------------------------------
  2. # File: AIFF.pm
  3. #
  4. # Description: Read AIFF meta information
  5. #
  6. # Revisions: 01/06/2006 - P. Harvey Created
  7. # 09/22/2008 - PH Added DjVu support
  8. #
  9. # References: 1) http://developer.apple.com/documentation/QuickTime/INMAC/SOUND/imsoundmgr.30.htm#pgfId=3190
  10. # 2) http://astronomy.swin.edu.au/~pbourke/dataformats/aiff/
  11. # 3) http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
  12. #------------------------------------------------------------------------------
  13. package Image::ExifTool::AIFF;
  14. use strict;
  15. use vars qw($VERSION);
  16. use Image::ExifTool qw(:DataAccess :Utils);
  17. use Image::ExifTool::ID3;
  18. $VERSION = '1.07';
  19. # information for time/date-based tags (time zero is Jan 1, 1904)
  20. my %timeInfo = (
  21. Groups => { 2 => 'Time' },
  22. ValueConv => 'ConvertUnixTime($val - ((66 * 365 + 17) * 24 * 3600))',
  23. PrintConv => '$self->ConvertDateTime($val)',
  24. );
  25. # AIFF info
  26. %Image::ExifTool::AIFF::Main = (
  27. GROUPS => { 2 => 'Audio' },
  28. NOTES => q{
  29. Tags extracted from Audio Interchange File Format (AIFF) files. See
  30. L<http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/AIFF/AIFF.html> for
  31. the AIFF specification.
  32. },
  33. # FORM => 'Format',
  34. FVER => {
  35. Name => 'FormatVersion',
  36. SubDirectory => { TagTable => 'Image::ExifTool::AIFF::FormatVers' },
  37. },
  38. COMM => {
  39. Name => 'Common',
  40. SubDirectory => { TagTable => 'Image::ExifTool::AIFF::Common' },
  41. },
  42. COMT => {
  43. Name => 'Comment',
  44. SubDirectory => { TagTable => 'Image::ExifTool::AIFF::Comment' },
  45. },
  46. NAME => {
  47. Name => 'Name',
  48. ValueConv => '$self->Decode($val, "MacRoman")',
  49. },
  50. AUTH => {
  51. Name => 'Author',
  52. Groups => { 2 => 'Author' },
  53. ValueConv => '$self->Decode($val, "MacRoman")',
  54. },
  55. '(c) ' => {
  56. Name => 'Copyright',
  57. Groups => { 2 => 'Author' },
  58. ValueConv => '$self->Decode($val, "MacRoman")',
  59. },
  60. ANNO => {
  61. Name => 'Annotation',
  62. ValueConv => '$self->Decode($val, "MacRoman")',
  63. },
  64. 'ID3 ' => {
  65. Name => 'ID3',
  66. SubDirectory => {
  67. TagTable => 'Image::ExifTool::ID3::Main',
  68. ProcessProc => \&Image::ExifTool::ID3::ProcessID3,
  69. },
  70. },
  71. # SSND => 'SoundData',
  72. # MARK => 'Marker',
  73. # INST => 'Instrument',
  74. # MIDI => 'MidiData',
  75. # AESD => 'AudioRecording',
  76. # APPL => 'ApplicationSpecific',
  77. );
  78. %Image::ExifTool::AIFF::Common = (
  79. PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData,
  80. GROUPS => { 2 => 'Audio' },
  81. FORMAT => 'int16u',
  82. 0 => 'NumChannels',
  83. 1 => { Name => 'NumSampleFrames', Format => 'int32u' },
  84. 3 => 'SampleSize',
  85. 4 => { Name => 'SampleRate', Format => 'extended' }, #3
  86. 9 => {
  87. Name => 'CompressionType',
  88. Format => 'string[4]',
  89. PrintConv => {
  90. NONE => 'None',
  91. ACE2 => 'ACE 2-to-1',
  92. ACE8 => 'ACE 8-to-3',
  93. MAC3 => 'MAC 3-to-1',
  94. MAC6 => 'MAC 6-to-1',
  95. sowt => 'Little-endian, no compression',
  96. },
  97. },
  98. 11 => { #PH
  99. Name => 'CompressorName',
  100. Format => 'pstring',
  101. ValueConv => '$self->Decode($val, "MacRoman")',
  102. },
  103. );
  104. %Image::ExifTool::AIFF::FormatVers = (
  105. PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData,
  106. FORMAT => 'int32u',
  107. 0 => { Name => 'FormatVersionTime', %timeInfo },
  108. );
  109. %Image::ExifTool::AIFF::Comment = (
  110. PROCESS_PROC => \&Image::ExifTool::AIFF::ProcessComment,
  111. GROUPS => { 2 => 'Audio' },
  112. 0 => { Name => 'CommentTime', %timeInfo },
  113. 1 => 'MarkerID',
  114. 2 => {
  115. Name => 'Comment',
  116. ValueConv => '$self->Decode($val, "MacRoman")',
  117. },
  118. );
  119. %Image::ExifTool::AIFF::Composite = (
  120. Duration => {
  121. Require => {
  122. 0 => 'AIFF:SampleRate',
  123. 1 => 'AIFF:NumSampleFrames',
  124. },
  125. RawConv => '($val[0] and $val[1]) ? $val[1] / $val[0] : undef',
  126. PrintConv => 'ConvertDuration($val)',
  127. },
  128. );
  129. # add our composite tags
  130. Image::ExifTool::AddCompositeTags('Image::ExifTool::AIFF');
  131. #------------------------------------------------------------------------------
  132. # Process AIFF Comment chunk
  133. # Inputs: 0) ExifTool object reference, 1) DirInfo reference, 2) tag table ref
  134. # Returns: 1 on success
  135. sub ProcessComment($$$)
  136. {
  137. my ($et, $dirInfo, $tagTablePtr) = @_;
  138. my $dataPt = $$dirInfo{DataPt};
  139. my $dirLen = $$dirInfo{DirLen};
  140. my $verbose = $et->Options('Verbose');
  141. return 0 unless $dirLen > 2;
  142. my $numComments = unpack('n',$$dataPt);
  143. my $pos = 2;
  144. my $i;
  145. $verbose and $et->VerboseDir('Comment', $numComments);
  146. for ($i=0; $i<$numComments; ++$i) {
  147. last if $pos + 8 > $dirLen;
  148. my ($time, $markerID, $size) = unpack("x${pos}Nnn", $$dataPt);
  149. $et->HandleTag($tagTablePtr, 0, $time);
  150. $et->HandleTag($tagTablePtr, 1, $markerID) if $markerID;
  151. $pos += 8;
  152. last if $pos + $size > $dirLen;
  153. my $val = substr($$dataPt, $pos, $size);
  154. $et->HandleTag($tagTablePtr, 2, $val);
  155. ++$size if $size & 0x01; # account for padding byte if necessary
  156. $pos += $size;
  157. }
  158. }
  159. #------------------------------------------------------------------------------
  160. # Extract information from a AIFF file
  161. # Inputs: 0) ExifTool object reference, 1) DirInfo reference
  162. # Returns: 1 on success, 0 if this wasn't a valid AIFF file
  163. sub ProcessAIFF($$)
  164. {
  165. my ($et, $dirInfo) = @_;
  166. my $raf = $$dirInfo{RAF};
  167. my ($buff, $err, $tagTablePtr, $page, $type);
  168. # verify this is a valid AIFF file
  169. return 0 unless $raf->Read($buff, 12) == 12;
  170. my $fast3 = $$et{OPTIONS}{FastScan} && $$et{OPTIONS}{FastScan} == 3;
  171. my $pos = 12;
  172. # check for DjVu image
  173. if ($buff =~ /^AT&TFORM/) {
  174. # http://www.djvu.org/
  175. # http://djvu.sourceforge.net/specs/djvu3changes.txt
  176. my $buf2;
  177. return 0 unless $raf->Read($buf2, 4) == 4 and $buf2 =~ /^(DJVU|DJVM)/;
  178. $pos += 4;
  179. $buff = substr($buff, 4) . $buf2;
  180. $et->SetFileType('DJVU');
  181. return 1 if $fast3;
  182. $tagTablePtr = GetTagTable('Image::ExifTool::DjVu::Main');
  183. # modifiy FileType to indicate a multi-page document
  184. $$et{VALUE}{FileType} .= " (multi-page)" if $buf2 eq 'DJVM';
  185. $type = 'DjVu';
  186. } else {
  187. return 0 unless $buff =~ /^FORM....(AIF(F|C))/s;
  188. $et->SetFileType($1);
  189. return 1 if $fast3;
  190. $tagTablePtr = GetTagTable('Image::ExifTool::AIFF::Main');
  191. $type = 'AIFF';
  192. }
  193. SetByteOrder('MM');
  194. my $verbose = $et->Options('Verbose');
  195. #
  196. # Read through the IFF chunks
  197. #
  198. for (;;) {
  199. $raf->Read($buff, 8) == 8 or last;
  200. $pos += 8;
  201. my ($tag, $len) = unpack('a4N', $buff);
  202. my $tagInfo = $et->GetTagInfo($tagTablePtr, $tag);
  203. $et->VPrint(0, "AIFF '$tag' chunk ($len bytes of data):\n");
  204. # AIFF chunks are padded to an even number of bytes
  205. my $len2 = $len + ($len & 0x01);
  206. if ($tagInfo) {
  207. if ($$tagInfo{TypeOnly}) {
  208. $len = $len2 = 4;
  209. $page = ($page || 0) + 1;
  210. $et->VPrint(0, $$et{INDENT} . "Page $page:\n");
  211. }
  212. $raf->Read($buff, $len2) >= $len or $err=1, last;
  213. unless ($$tagInfo{SubDirectory} or $$tagInfo{Binary}) {
  214. $buff =~ s/\0+$//; # remove trailing nulls
  215. }
  216. $et->HandleTag($tagTablePtr, $tag, $buff,
  217. DataPt => \$buff,
  218. DataPos => $pos,
  219. Start => 0,
  220. Size => $len,
  221. );
  222. } elsif ($verbose > 2 and $len2 < 1024000) {
  223. $raf->Read($buff, $len2) == $len2 or $err = 1, last;
  224. HexDump(\$buff, undef, MaxLen => 512);
  225. } else {
  226. $raf->Seek($len2, 1) or $err=1, last;
  227. }
  228. $pos += $len2;
  229. }
  230. $err and $et->Warn("Error reading $type file (corrupted?)");
  231. return 1;
  232. }
  233. 1; # end
  234. __END__
  235. =head1 NAME
  236. Image::ExifTool::AIFF - Read AIFF meta information
  237. =head1 SYNOPSIS
  238. This module is used by Image::ExifTool
  239. =head1 DESCRIPTION
  240. This module contains routines required by Image::ExifTool to extract
  241. information from AIFF (Audio Interchange File Format) audio files.
  242. =head1 AUTHOR
  243. Copyright 2003-2016, Phil Harvey (phil at owl.phy.queensu.ca)
  244. This library is free software; you can redistribute it and/or modify it
  245. under the same terms as Perl itself.
  246. =head1 REFERENCES
  247. =over 4
  248. =item L<http://developer.apple.com/documentation/QuickTime/INMAC/SOUND/imsoundmgr.30.htm#pgfId=3190>
  249. =item L<http://astronomy.swin.edu.au/~pbourke/dataformats/aiff/>
  250. =item L<http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/>
  251. =back
  252. =head1 SEE ALSO
  253. L<Image::ExifTool::TagNames/AIFF Tags>,
  254. L<Image::ExifTool(3pm)|Image::ExifTool>
  255. =cut