home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09964.iso / sound / mpw32-5s.zip / HEADER.CPP < prev    next >
C/C++ Source or Header  |  1996-07-23  |  10KB  |  360 lines

  1. /*
  2.  *  @(#) header.cc 1.8, last edit: 6/15/94 16:51:44
  3.  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
  4.  *  @(#) Berlin University of Technology
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /*
  22.  *  Changes from version 1.1 to 1.2:
  23.  *    - iostreams manipulator calls like "cerr << setw (2) << ..." replaced by
  24.  *      "cerr.width (2); ..." due to problems with older GNU C++ releases.
  25.  *    - syncword recognition slightly changed
  26.  */
  27. #define STRICT
  28. #define WIN32_LEAN_AND_MEAN
  29. #define NOMCX
  30. #define NOIME
  31. #define NOGDI
  32. #define NOUSER
  33. #define NOSOUND
  34. #define NOCOMM
  35. #define NODRIVERS
  36. #define OEMRESOURCE
  37. #define NONLS
  38. #define NOSERVICE
  39. #define NOKANJI
  40. #define NOMINMAX
  41. #define NOLOGERROR
  42. #define NOPROFILER
  43. #define NOMEMMGR
  44. #define NOLFILEIO
  45. #define NOOPENFILE
  46. #define NORESOURCE
  47. #define NOATOM
  48. #define NOLANGUAGE
  49. #define NOLSTRING
  50. #define NODBCS
  51. #define NOKEYBOARDINFO
  52. #define NOGDICAPMASKS
  53. #define NOCOLOR
  54. #define NOGDIOBJ
  55. #define NODRAWTEXT
  56. #define NOTEXTMETRIC
  57. #define NOSCALABLEFONT
  58. #define NOBITMAP
  59. #define NORASTEROPS
  60. #define NOMETAFILE
  61. #define NOSYSMETRICS
  62. #define NOSYSTEMPARAMSINFO
  63. #define NOMSG
  64. #define NOWINSTYLES
  65. #define NOWINOFFSETS
  66. #define NOSHOWWINDOW
  67. #define NODEFERWINDOWPOS
  68. #define NOVIRTUALKEYCODES
  69. #define NOKEYSTATES
  70. #define NOWH
  71. #define NOMENUS
  72. #define NOSCROLL
  73. #define NOCLIPBOARD
  74. #define NOICONS
  75. #define NOMB
  76. #define NOSYSCOMMANDS
  77. #define NOMDI
  78. #define NOCTLMGR
  79. #define NOWINMESSAGES
  80. #define NOHELP
  81. #define _WINUSER_
  82. #define __oleidl_h__
  83. #define _OLE2_H_
  84.  
  85. #include <windows.h>
  86.  
  87. #include "header.h"
  88.  
  89. const uint32 Header::frequencies[3] = { 44100, 48000, 32000 };
  90.  
  91. BOOL Header::read_header (Ibitstream *stream, Crc16 **crcp)
  92. {
  93.   uint32 headerstring;
  94.  
  95.   if (!stream->get_header (&headerstring))
  96.      return(FALSE);
  97.  
  98. /*  if ((headerstring & 0xFFF80000) != 0xFFF80000)
  99.   {
  100.      cerr << "invalid syncword 0x";
  101.      cerr.width (8);
  102.      cerr.fill ('0');
  103.      cerr << hex << headerstring
  104.      << " found at fileoffset " << dec
  105.      << lseek (stream->filedescriptor (), 0, SEEK_CUR) - 4 << '\n';
  106.      return False;
  107.   } */
  108.  
  109. /*  if ((h_layer = (headerstring >> 17) & 3) == 0)
  110.   {
  111.      cerr << "unknown layer identifier found!\n";
  112.      exit (1);
  113.   }
  114.   h_layer = 4 - h_layer;        // now 1 means Layer I and 3 means Layer III   */
  115.  
  116.   h_layer = 4 - (headerstring >> 17) & 3;
  117.  
  118. /*  if (h_layer == 3)
  119.   {
  120.      cerr << "Sorry, Layer III not implemented!\n";
  121.      exit (1);
  122.   } */
  123.  
  124.   h_protection_bit = (headerstring >> 16) & 1;
  125.   h_bitrate_index  = (headerstring >> 12) & 0xF;
  126.  
  127. /*  if ((h_bitrate_index = (headerstring >> 12) & 0xF) == 15)
  128.   {
  129.      cerr << "unknown bitrate index found!\n";
  130.      exit (1);
  131.   }           */
  132. /*  if (!h_bitrate_index)
  133.   {
  134.      cerr << "free format not yet implemented!\n";
  135.      exit (1);
  136.   } */
  137.  
  138.   h_sample_frequency = (e_sample_frequency) (headerstring >> 10) & 3;
  139.  
  140. /*  if ((h_sample_frequency = (e_sample_frequency)((headerstring >> 10) & 3)) == 3)
  141.   {
  142.      cerr << "unknown sample frequency!\n";
  143.      exit (1);
  144.   }          */
  145.  
  146.  
  147.  
  148.   h_padding_bit = (headerstring >> 9) & 1;
  149.   h_mode = (e_mode)((headerstring >> 6) & 3);
  150.  
  151. /*  if (h_layer == 2)
  152.      // testing validity of mode and bitrate:
  153.      if ((((h_bitrate_index >= 1 && h_bitrate_index <= 3) || h_bitrate_index == 5) &&
  154.      h_mode != single_channel) ||
  155.     (h_bitrate_index >= 11 && h_mode == single_channel))
  156.      {
  157.         cerr << "illegal combination of mode and bitrate in a layer II stream:\n"
  158.             "  mode: " << mode_string ()
  159.         << "\n  bitrate: " << bitrate_string () << '\n';
  160.         exit (1);
  161.      } */
  162.  
  163.  
  164.   h_mode_extension = (headerstring >> 4) & 3;
  165.  
  166.   if (h_mode == joint_stereo)
  167.      h_intensity_stereo_bound = (h_mode_extension << 2) + 4;
  168.   else
  169.      h_intensity_stereo_bound = 0;        // should never be used
  170.  
  171.   h_copyright = (BOOL) ((headerstring >> 3) & 1);
  172.   h_original = (BOOL) ((headerstring >> 2) & 1);
  173.  
  174.   // calculate number of subbands:
  175.   if (h_layer == 1)
  176.      h_number_of_subbands = 32;
  177.   else
  178.   {
  179.      uint32 channel_bitrate = h_bitrate_index;
  180.  
  181.      // calculate bitrate per channel:
  182.      if (h_mode != single_channel)
  183.         if (channel_bitrate == 4)
  184.     channel_bitrate = 1;
  185.         else
  186.     channel_bitrate -= 4;
  187.  
  188.      if (channel_bitrate == 1 || channel_bitrate == 2)
  189.         if (h_sample_frequency == thirtytwo)
  190.     h_number_of_subbands = 12;
  191.         else
  192.     h_number_of_subbands = 8;
  193.      else
  194.         if (h_sample_frequency == fourtyeight || (channel_bitrate >= 3 && channel_bitrate <= 5))
  195.     h_number_of_subbands = 27;
  196.         else
  197.     h_number_of_subbands = 30;
  198.   }
  199.  
  200.   if (h_intensity_stereo_bound > h_number_of_subbands)
  201.      h_intensity_stereo_bound = h_number_of_subbands;
  202.  
  203.   // read framedata:
  204.   if (!stream->read_frame (calculate_framesize()))
  205.      return FALSE;
  206.  
  207.   if (!h_protection_bit)
  208.   {
  209.      // frame contains a crc checksum
  210.      checksum = (uint16)stream->get_bits (16);
  211.      if (!crc)
  212.         crc = new Crc16;
  213.      crc->add_bits (headerstring, 16);
  214.      *crcp = crc;
  215.   }
  216.   else
  217.      *crcp = (Crc16 *)0;
  218.  
  219.   if (h_sample_frequency == fourtyfour_point_one) {
  220.  
  221.      if (!stream->offset)
  222.         stream->offset = new int [max_number_of_frames(stream)];
  223.  
  224.      if (stream->current_frame > 0) {
  225.             if (stream->current_frame == stream->last_frame_number)
  226.               stream->offset[stream->current_frame] =
  227.                     stream->offset[stream->current_frame - 1] + h_padding_bit;
  228.         } else {
  229.           stream->offset[0] = h_padding_bit;
  230.         }
  231.   }
  232.  
  233.   return(TRUE);
  234. }
  235.  
  236.  
  237. uint32 Header::calculate_framesize ()
  238. /* calculates framesize in bytes excluding header size */
  239. {
  240.   static const int32 bitrates[3][15] = {
  241.     {0 /*free format*/, 32000, 64000, 96000, 128000, 160000, 192000,
  242.      224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000},
  243.     {0 /*free format*/, 32000, 48000, 56000, 64000, 80000, 96000,
  244.      112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000},
  245.     {0 /*free format*/, 32000, 40000, 48000, 56000, 64000, 80000,
  246.      96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000}
  247.   };
  248.  
  249.   static const samplefrequencies[3] = { 44100, 48000, 32000 };
  250.   uint32 framesize;
  251.  
  252.   if (h_layer == 1)
  253.   {
  254.      framesize = (12 * bitrates[0][h_bitrate_index]) / samplefrequencies[h_sample_frequency];
  255.      if (h_sample_frequency == fourtyfour_point_one && h_padding_bit)
  256.         ++framesize;
  257.      framesize <<= 2;        // one slot is 4 bytes long
  258.   }
  259.   else
  260.   {
  261.      framesize = (144 * bitrates[h_layer - 1][h_bitrate_index]) / samplefrequencies[h_sample_frequency];
  262.      if (h_padding_bit) framesize++;
  263.  
  264.      // Layer III slots
  265.      if (h_layer == 3)
  266.          nSlots = framesize - ((h_mode == single_channel) ? 17 : 32) // side info size
  267.                                   -  (h_protection_bit ? 0 : 2)                // CRC size
  268.                                   - 4;                                              // header size
  269.   }
  270.  
  271.   return(framesize - 4);        // subtract header size
  272. }
  273.  
  274. const char *Header::layer_string (void)
  275. {
  276.   switch (h_layer)
  277.   {
  278.      case 1:
  279.         return "I                                      ";
  280.      case 2:
  281.         return "II                                     ";
  282.      case 3:
  283.         return "III                                    ";
  284.   }
  285.   return NULL;            // dummy
  286. }
  287.  
  288.  
  289. const char *Header::bitrate_string (void)
  290. {
  291.  
  292.   static const char *bitrate_str[3][16] = {
  293.     {"free format", "32 kbit/s", "64 kbit/s", "96 kbit/s", "128 kbit/s", "160 kbit/s",
  294.      "192 kbit/s", "224 kbit/s", "256 kbit/s", "288 kbit/s", "320 kbit/s", "352 kbit/s",
  295.      "384 kbit/s", "416 kbit/s", "448 kbit/s", "forbidden"},
  296.     {"free format", "32 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s",
  297.      "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s", "192 kbit/s", "224 kbit/s",
  298.      "256 kbit/s", "320 kbit/s", "384 kbit/s", "forbidden"},
  299.     {"free format", "32 kbit/s", "40 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s",
  300.      "80 kbit/s" , "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s",
  301.      "192 kbit/s", "224 kbit/s", "256 kbit/s", "320 kbit/s", "forbidden"}
  302.   };
  303.  
  304.   return( bitrate_str[h_layer - 1][h_bitrate_index] );
  305. }
  306.  
  307. const char *Header::sample_frequency_string (void)
  308. {
  309.   switch (h_sample_frequency)
  310.   {
  311.      case thirtytwo:
  312.         return("32 kHz        ");
  313.      case fourtyfour_point_one:
  314.         return("44.1 kHz     ");
  315.      case fourtyeight:
  316.         return("48 kHz        ");
  317.   }
  318.   return(NULL);            // dummy
  319. }
  320.  
  321. const char *Header::mode_string (void)
  322. {
  323.   switch (h_mode)
  324.   {
  325.      case stereo:
  326.         return("Stereo                             ");
  327.      case joint_stereo:
  328.         return("Joint stereo                      ");
  329.      case dual_channel:
  330.         return("Dual channel                    ");
  331.      case single_channel:
  332.         return("Single channel                ");
  333.   }
  334.   return(NULL);            // dummy
  335. }
  336.  
  337. // Stream searching routines
  338. uint32 Header::max_number_of_frames (Ibitstream *stream)
  339. // Returns the maximum number of frames in the stream
  340. {
  341.       return ( stream->file_size() /
  342.                   (calculate_framesize() + 4 - h_padding_bit) );
  343. }
  344.  
  345. uint32 Header::min_number_of_frames (Ibitstream *stream)
  346. // Returns the minimum number of frames in the stream
  347. {
  348.       return ( stream->file_size() /
  349.                   (calculate_framesize() + 5 - h_padding_bit) );
  350. }
  351.  
  352. real ms_per_frame_array[3][3] = {{8.707483,  8.0, 12.0},
  353.                                             {26.12245, 24.0, 36.0},
  354.                                             {26.12245, 24.0, 36.0}};
  355. real Header::ms_per_frame(void)
  356. {
  357.     return (ms_per_frame_array[h_layer][h_sample_frequency]);
  358. }
  359.  
  360.