home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 2 / MECOMP-CD-II.iso / amiga / datatypes / mpegaudio_datatype / classbase.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-01  |  11.6 KB  |  402 lines

  1.  
  2. /*
  3. **
  4. **  $VER: classbase.c 2.2 (1.6.98)
  5. **  mpegaudio.datatype 2.2
  6. **
  7. **  Library routines for a DataTypes class
  8. **
  9. **  Written 1996-1998 by Roland 'Gizzy' Mainz
  10. **  Original example source from David N. Junod
  11. **
  12. */
  13.  
  14.  
  15. /* main includes */
  16. #include "classbase.h"
  17.  
  18.  
  19. /****** mpegaudio.datatype/MAIN **********************************************
  20. *
  21. *    INTRODUCTION
  22. *        Datatypes class for MPEG audio streams.
  23. *
  24. *    REQUIREMENTS
  25. *        - You need at least Kick/WB 3.0.
  26. *
  27. *        - mc68020 (required by the "mpega" decoder).
  28. *          This datatype checks the CPU installed and rejects any attempt to
  29. *          open it if the CPU is not available.
  30. *
  31. *        - "datatypes/sound.datatype", >= V39.
  32. *
  33. *    USAGE
  34. *        If the datatypes descriptor file was activated, any attempt to load
  35. *        a MPEG audio stream using GMultiView, MultiView, AmigaGuide or
  36. *        SwitchWindow will load and play the sample.
  37. *
  38. *        If you want to save the current sample in mpegaudio.datatype's local
  39. *        format, use MultiView's "Project/Save As..." menu (or GMultiView's
  40. *        "Project/Save As Raw...").
  41. *        (NOT IMPLEMENTED YET)
  42. *
  43. *    INSTALLATION
  44. *        After unpacking this archive:
  45. *        Because this version does not include an Installer script, you have
  46. *        to do the installation manually through the shell:
  47. *
  48. *          - Unpack this archive and copy the "mpegaudio.datatype" to
  49. *            SYS:Classes/DataTypes:
  50. *
  51. *     Copy CLONE FROM "mpegaudio.datatype" TO
  52. *       "SYS:Classes/DataTypes/mpegaudio.datatype"
  53. *
  54. *          - Then copy the datatypes descriptor into the DEVS:DataTypes
  55. *            directory.
  56. *            If the descriptor already exists, you should not replace it,
  57. *            otherwise you may loose "toolnodes" and other settings stored in
  58. *            the existing descriptor.
  59. *
  60. *     Copy CLONE FROM "MPEG Audio(%|.info)" TO DEVS:Datatypes/
  61. *
  62. *    NOTE
  63. *        The datatype "hooks" on an external mpeg audio decoder like "mpega".
  64. *
  65. *    SOURCE
  66. *        Source is fully included here.
  67. *
  68. *    AUTHOR
  69. *        If you want to blame me, report any bugs, or wants a new version
  70. *        send your letter to:
  71. *                        Roland Mainz
  72. *                        Hohenstaufenstraße 8
  73. *                        52388 Nörvenich
  74. *                        GERMANY
  75. *
  76. *        Phone: (+49)(0)2426/901568
  77. *        Fax:   (+49)(0)2426/901569
  78. *
  79. *        EMAIL is also available (if you want to send me attachments
  80. *        larger than 1MB (up to 5MB, more with my permission):
  81. *
  82. *        GISBURN@w-specht.rhein-ruhr.de
  83. *
  84. *        Up to August 1998 I'm reachable using this email address, too:
  85. *        Reinhold.A.Mainz@KBV.DE
  86. *
  87. *        | Please put your name and address in your mails !
  88. *        | German mailers should add their phone numbers.
  89. *        | See BUGS section above when submitting bug reports.
  90. *
  91. *        Sorry, but I can only look once a week for mails.
  92. *        If you don't hear something from me within three weeks, please
  93. *        send your mail again (but watch about new releases) (problems with
  94. *        this email port are caused by reconfigurations, hackers, network
  95. *        problems etc.).
  96. *
  97. *        The  entire  "mpegaudio.datatype"  package  may  be  noncommercially
  98. *        redistributed, provided  that  the package  is always  distributed
  99. *        in it's complete  form (including it's documentation). A small
  100. *        copy fee  for media costs is okay but any kind of commercial
  101. *        distribution is strictly forbidden without my permission !
  102. *        Comments and suggestions how to improve this program are
  103. *        generally appreciated!
  104. *
  105. *        Thanks to David Junod, who wrote the datatypes environment and lots
  106. *        of the datatypes example code, Matt Dillon for his DICE, Olaf 
  107. *        'Olsen' Barthel for his help, ideas and some text clips from his 
  108. *        documentations.
  109. *
  110. ******************************************************************************
  111. *
  112. */
  113.  
  114.  
  115. /****** mpegaudio.datatype/--datasheed-- *************************************
  116. *
  117. *   NAME
  118. *       mpegaudio.datatype -- data type for MPEG audio streams
  119. *
  120. *   SUPERCLASS
  121. *       sound.datatype
  122. *
  123. *   DESCRIPTION
  124. *       The mpegaudio.datatype, a sub-class of the sound.datatype, is
  125. *       used to load and save MPEG audio streams using "maplay" / "mpega"
  126. *       and the 8SVX/AIFF datatype.
  127. *
  128. *   METHODS
  129. *       OM_NEW -- Create a new sound object from a description file. The
  130. *           source may only be a file. An empty object (DTST_RAM) can
  131. *           also be created (unless the project was compiled with
  132. *           the NO_ENCODER flag).
  133. *
  134. *       OM_UPDATE -- Perform an ICM_CHECKLOOP check, and if succesfull, the
  135. *           method will be executed like OM_SET downstairs.
  136. *
  137. *       OM_SET -- Pass msg to superclass and call GM_RENDER if retval from
  138. *           superclass was != 0UL.
  139. *
  140. *       DTM_WRITE -- Save data in local (MPEG audio) or superclass (IFF 8SVX)
  141. *           format.
  142. *           Local format has not been implemented yet.
  143. *
  144. *       All other methods are passed unchanged to superclass.
  145. *
  146. *   ATTRIBUTES
  147. *       Following attributes are set by the object and are READ-ONLY for
  148. *       applications (if the datatype is used for saving (e.g. the
  149. *       application creates an empty mpegaudio.datatype object and fills
  150. *       in some required atrributes as shown in the "DTConvert" example),
  151. *       these attributes are used by the encoder to create a MPEG audio
  152. *       sound file):
  153. *
  154. *       DTA_ObjName         -- Set by DTA_Name's value.
  155. *       SDTA_VoiceHeader    -- Copy from the embedded 8svx/aiff.datatype 
  156. *                              object
  157. *       SDTA_Sample         -- 8 bit sample data
  158. *       SDTA_SampleLength   -- length of 8 bit sample data
  159. *       SDTA_Volume         -- volume (got from the voiceheader)
  160. *       SDTA_Period         -- created from (VoiceHeader -> vh_SamplesPerSec)
  161. *                              and system's clock
  162. *
  163. *   BUGS
  164. *       The project has been not very well tested yet. Therefore...
  165. *       ... no known bugs.
  166. *
  167. *   TODO
  168. *       - Fixing the bugs above
  169. *
  170. *       - Support for SDTM_LOADFRAME / SDTM_UNLOADFRAME (implies also support
  171. *         for 16-bit audio á la RTA /  AHI).
  172. *
  173. *       - Encoder support (e.g. DTM_WRITE).
  174. *
  175. *   HISTORY
  176. *       V1.1
  177. *         First public release.
  178. *
  179. *       V1.2
  180. *         - Swichted to Stephane Tavenard's "mpega".
  181. *           (Have gave me the tip how to get in sync with the video (using
  182. *           the options -b 1 -t 0).
  183. *           Thank you very much Stephane !
  184. *
  185. *         - Small code cleanup (the class dispatcher is now a static
  186. *           function etc.)
  187. *
  188. *       V2.1
  189. *         - Swichted to Stephane Tavenard's "mpega.library".
  190. *           Thank you very much Stephane !
  191. *
  192. *       V2.2
  193. *         - Now stero files are decoded with both left+right channel instead
  194. *           the left channel only.
  195. *
  196. *         - Reduced the decoder's buffer size from 2048 down to 1024 bytes
  197. *           to get a much more accurate lip sync if used within
  198. *           mpegsystem.datatype (for tests only...).
  199. *
  200. *   SEE ALSO
  201. *       sound.datatype
  202. *
  203. *******************************************************************************
  204. *
  205. */
  206.  
  207.  
  208. /****** mpegaudio.datatype/--input_format-- **********************************
  209. *
  210. *    NAME
  211. *        MPEG audio --
  212. *
  213. *    DESCRIPTION
  214. *        <not written yet>
  215. *
  216. *    SEE ALSO
  217. *
  218. *******************************************************************************
  219. *
  220. */
  221.  
  222.  
  223.  
  224. /*****************************************************************************/
  225.  
  226. DISPATCHERFLAGS
  227. struct IClass *ObtainMPEGAudioEngine( REGA6 struct ClassBase *cb )
  228. {
  229.     return( (cb -> cb_Lib . cl_Class) );
  230. }
  231.  
  232. /*****************************************************************************/
  233.  
  234. DISPATCHERFLAGS
  235. struct Library *LibInit( REGD0 struct ClassBase *cb, REGA0 BPTR seglist, REGA6 struct ExecBase *sysbase )
  236. {
  237.     cb -> cb_SegList = seglist;
  238.     cb -> cb_SysBase = sysbase;
  239.  
  240.     InitSemaphore( (&(cb -> cb_Lock)) );
  241.  
  242. #ifdef REQUIREDAFF
  243.     /* Check if the requested cpu/fpu is available */
  244.     if( ((sysbase -> AttnFlags) | REQUIREDAFF) != (sysbase -> AttnFlags) )
  245.     {
  246.       return( NULL );
  247.     }
  248. #endif /* REQUIREDAFF */
  249.  
  250.     if( (cb -> cb_SysBase -> LibNode . lib_Version) >= 39UL )
  251.     {
  252.       /* Obtain ROM libs */
  253.       if( cb -> cb_UtilityBase = OpenLibrary( "utility.library", 39UL ) )
  254.       {
  255.         if( cb -> cb_DOSBase = OpenLibrary( "dos.library", 39UL ) )
  256.         {
  257.           if( cb -> cb_IntuitionBase = OpenLibrary( "intuition.library", 39UL ) )
  258.           {
  259.             return( (&(cb -> cb_Lib . cl_Lib)) );
  260.  
  261. #ifdef COMMENTED_OUT
  262.               CloseLibrary( (cb -> cb_IntuitionBase) );
  263. #endif /* COMMENTED_OUT */
  264.           }
  265.  
  266.           CloseLibrary( (cb -> cb_DOSBase) );
  267.         }
  268.  
  269.         CloseLibrary( (cb -> cb_UtilityBase) );
  270.       }
  271.     }
  272.  
  273.     return( NULL );
  274. }
  275.  
  276. /*****************************************************************************/
  277.  
  278. DISPATCHERFLAGS
  279. LONG LibOpen( REGA6 struct ClassBase *cb )
  280. {
  281.     LONG retval = (LONG)cb;
  282.     BOOL success = TRUE;
  283.  
  284.     ObtainSemaphore( (&(cb -> cb_Lock)) );
  285.  
  286.     /* Use an internal use counter */
  287.     cb -> cb_Lib . cl_Lib . lib_OpenCnt++;
  288.     cb -> cb_Lib . cl_Lib . lib_Flags &= ~LIBF_DELEXP;
  289.  
  290.     if( (cb -> cb_Lib . cl_Lib . lib_OpenCnt) == 1U )
  291.     {
  292.       if( (cb -> cb_Lib . cl_Class) == NULL )
  293.       {
  294.         success = FALSE;
  295.  
  296.         if( cb -> cb_MPEGABase = OpenLibrary( "mpega.library", 0UL ) )
  297.         {
  298.           if( cb -> cb_DataTypesBase = OpenLibrary( "datatypes.library", 39UL ) )
  299.           {
  300.             if( cb -> cb_SuperClassBase = OpenLibrary( "datatypes/sound.datatype", 39UL ) )
  301.             {
  302.               if( cb -> cb_Lib . cl_Class = initClass( cb ) )
  303.               {
  304.                 success = TRUE;
  305.               }
  306.             }
  307.           }
  308.         }
  309.       }
  310.     }
  311.  
  312.     if( !success )
  313.     {
  314.       CloseLibrary( (cb -> cb_SuperClassBase) );
  315.       CloseLibrary( (cb -> cb_DataTypesBase) );
  316.       CloseLibrary( (cb -> cb_MPEGABase) );
  317.  
  318.       cb -> cb_DataTypesBase = cb -> cb_SuperClassBase = NULL;
  319.  
  320.       (cb -> cb_Lib . cl_Lib . lib_OpenCnt)--;
  321.  
  322.       retval = 0L;
  323.     }
  324.  
  325.     ReleaseSemaphore( (&(cb -> cb_Lock)) );
  326.  
  327.     return( retval );
  328. }
  329.  
  330. /*****************************************************************************/
  331.  
  332. DISPATCHERFLAGS
  333. LONG LibClose( REGA6 struct ClassBase *cb )
  334. {
  335.     LONG retval = 0L;
  336.  
  337.     ObtainSemaphore( (&(cb -> cb_Lock)) );
  338.  
  339.     if( cb -> cb_Lib . cl_Lib . lib_OpenCnt )
  340.     {
  341.       (cb -> cb_Lib . cl_Lib . lib_OpenCnt)--;
  342.     }
  343.  
  344.     if( ((cb -> cb_Lib . cl_Lib . lib_OpenCnt) == 0U) && (cb -> cb_Lib . cl_Class) )
  345.     {
  346.       if( FreeClass( (cb -> cb_Lib . cl_Class) ) )
  347.       {
  348.         cb -> cb_Lib . cl_Class = NULL;
  349.  
  350.         CloseLibrary( (cb -> cb_SuperClassBase) );
  351.         CloseLibrary( (cb -> cb_DataTypesBase) );
  352.         CloseLibrary( (cb -> cb_MPEGABase) );
  353.       }
  354.       else
  355.       {
  356.         cb -> cb_Lib . cl_Lib . lib_Flags |= LIBF_DELEXP;
  357.       }
  358.     }
  359.  
  360.     ReleaseSemaphore( (&(cb -> cb_Lock)) );
  361.  
  362.     if( cb -> cb_Lib . cl_Lib . lib_Flags & LIBF_DELEXP )
  363.     {
  364.       retval = LibExpunge( cb );
  365.     }
  366.  
  367.     return( retval );
  368. }
  369.  
  370. /*****************************************************************************/
  371.  
  372. DISPATCHERFLAGS
  373. LONG LibExpunge( REGA6 struct ClassBase *cb )
  374. {
  375.     BPTR seg;
  376.  
  377.     if( cb -> cb_Lib . cl_Lib . lib_OpenCnt )
  378.     {
  379.       cb -> cb_Lib . cl_Lib . lib_Flags |= LIBF_DELEXP;
  380.  
  381.       seg = NULL;
  382.     }
  383.     else
  384.     {
  385.       Remove( (&(cb -> cb_Lib . cl_Lib . lib_Node)) );
  386.  
  387.       seg = cb -> cb_SegList;
  388.  
  389.       CloseLibrary( (cb -> cb_IntuitionBase) );
  390.       CloseLibrary( (cb -> cb_DOSBase) );
  391.       CloseLibrary( (cb -> cb_UtilityBase) );
  392.  
  393.       FreeMem( (APTR)((ULONG)(cb) - (ULONG)(cb -> cb_Lib . cl_Lib . lib_NegSize)), (ULONG)((cb -> cb_Lib . cl_Lib . lib_NegSize) + (cb -> cb_Lib . cl_Lib . lib_PosSize)) );
  394.     }
  395.  
  396.     return( (LONG)seg );
  397. }
  398.  
  399.  
  400.  
  401.  
  402.