home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga Shareware Floppies / ma58.dms / ma58.adf / superplay-lib_DEV / Programmers / Example_Tools / SampleListPlay / SLP.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-27  |  10.7 KB  |  361 lines

  1. /* ======================================================================== */
  2. /* = Programmname    : SampleListDemo V2.1                      = */
  3. /* =                                      = */
  4. /* ======================================================================== */
  5. /* = Author/Copyright : (c) 1994 by Andreas Ralph Kleinert.               = */
  6. /* =               Freeware. All rights reserved.              = */
  7. /* =                                      = */
  8. /* =                   Use it as an example for programming               = */
  9. /* =                   superplay.library !                                = */
  10. /* =                                      = */
  11. /* ======================================================================== */
  12. /* = Function          : SPObject operations :                             = */
  13. /* =                Test SampleList functions.                        = */
  14. /* ======================================================================== */
  15. /* = Last Update      : 18.7.1994                      = */
  16. /* =                                      = */
  17. /* ======================================================================== */
  18. /* = Remarks          : Needs "asl.library" V37+                          = */
  19. /* =                and "superplay.library" V2+.                      = */
  20. /* =                                      = */
  21. /* ======================================================================== */
  22. /* = Compiler          : SAS/C V6.51                       = */
  23. /* =                (smakefile)                                       = */
  24. /* ======================================================================== */
  25.  
  26. #include <intuition/intuitionbase.h>
  27.  
  28. #include <exec/devices.h>
  29. #include <devices/audio.h>
  30.  
  31. #include <superplay/superplay.h>
  32. #include <spobjects/spobjects.h>
  33.  
  34. #include <libraries/asl.h>
  35. #include <workbench/startup.h>
  36. #include <exec/interrupts.h>
  37.  
  38. #include <proto/exec.h>
  39. #include <proto/dos.h>
  40. #include <proto/asl.h>
  41. #include <proto/superplay.h>
  42.  
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46.  
  47.  
  48.    /* Help- and Info- Texts */
  49.  
  50. char entry1_text  [] = "\2331;32;40mSampleListDemo V2.1 \2330;32;40m\2333;32;40m(FREEWARE)\2330;32;40m\n(c) 1994 by Andreas Ralph Kleinert.\nAndreas R. Kleinert, Grube Hohe Grethe 23, D-57074 Siegen, Germany.\n";
  51. char entry2_text  [] = "Plays Sound Samples and Modules via superplay.library.\n";
  52. char entry3_text  [] = "USAGE : \2330;33;40mSampleListDemo\2330;31;40m [? | -STOP | -REMOVE | <Sample/ModuleFileName>]\n";
  53.  
  54.  
  55. char ver_text [] = "\0$VER: SampleListDemo V2.1 (18.7.94)";
  56.  
  57.  
  58. /* *************************************************** */
  59. /* *                             * */
  60. /* * Error-Messages for Leave() and KF_Message()     * */
  61. /* *                             * */
  62. /* *************************************************** */
  63.  
  64. char asllib_text     [] = "You need \42asl.library\42 V37+ !";
  65. char splib_text     [] = "You need \42superplay.library\42 V2+ !";
  66.  
  67.  
  68. /* *************************************************** */
  69. /* *                             * */
  70. /* * MACROs for Version-Tests                 * */
  71. /* *                             * */
  72. /* *************************************************** */
  73.  
  74. #define LibVer(x) ( ((struct Library *) x)->lib_Version )
  75. #define OS_VER      LibVer(SysBase)
  76.  
  77.  
  78. /* *************************************************** */
  79. /* *                             * */
  80. /* * Function Declarations                 * */
  81. /* *                             * */
  82. /* *************************************************** */
  83.  
  84. void __regargs SP_Examine(char *filename);
  85. UBYTE * __regargs SPLI_PlaySample(UBYTE *smp_buffer, ULONG smp_length, ULONG smp_freq, ULONG smp_volume);
  86.  
  87. void __regargs Leave(char *endtext, long code);
  88.  
  89.  
  90.    /* Functions from module "SampleListDemo_Subs.o" : */
  91.  
  92. extern void __stdargs SP_Printf(char *formatstring, ...);
  93.  
  94.  
  95. /* *************************************************** */
  96. /* *                             * */
  97. /* * Additional Base Declarations             * */
  98. /* *                             * */
  99. /* *************************************************** */
  100.  
  101. extern struct ExecBase *SysBase;
  102.  
  103. struct SuperPlayBase *SuperPlayBase = N;
  104. struct Library       *AslBase       = N;
  105.  
  106.  
  107. /* *************************************************** */
  108. /* *                             * */
  109. /* * MAIN                         * */
  110. /* *                             * */
  111. /* *************************************************** */
  112.  
  113. void main(long argc, char **argv)
  114. {
  115.  if(argc==0) exit(0);
  116.  
  117.  if( argc > 2 || (argv[1][0] =='?') )
  118.   {
  119.    SP_Printf("%s%s%s", entry1_text, entry2_text, entry3_text);
  120.  
  121.    Leave(N, 0);
  122.   }
  123.  
  124.  AslBase = (struct Library *) OpenLibrary("asl.library", 37);
  125.  if(!AslBase) Leave(asllib_text, 102);
  126.  
  127.  SuperPlayBase = (struct SuperPlayBase *) OpenLibrary("superplay.library", 2);
  128.  if(!SuperPlayBase) Leave(splib_text, 102);
  129.  
  130.  
  131.  /* Play */
  132.  
  133.  if(argc < 2)
  134.   {
  135.    struct FileRequester     *request;
  136.    struct TagItem __aligned  tags[4];
  137.    char                      namebuffer [256];
  138.  
  139.    namebuffer[0] = (char) 0;
  140.  
  141.    tags[0].ti_Tag  = (Tag)   ASL_Hail;
  142.    tags[0].ti_Data = (ULONG) "Select Sample/Module to play :";
  143.  
  144.    tags[1].ti_Tag  = (Tag)   ASL_OKText;
  145.    tags[1].ti_Data = (ULONG) " Play ";
  146.  
  147.    tags[2].ti_Tag  = (Tag)   ASL_CancelText;
  148.    tags[2].ti_Data = (ULONG) " Quit ";
  149.  
  150.    tags[3].ti_Tag  = (Tag)   TAG_DONE;
  151.    tags[3].ti_Data = (ULONG) N;
  152.  
  153.    request = AllocAslRequest(ASL_FileRequest, N);
  154.    if(request)
  155.     {
  156.      if(AslRequest(request, &tags[0]))
  157.       {
  158.        strcpy(namebuffer, request->rf_Dir);
  159.        if(    (namebuffer[strlen(namebuffer)-1] !=      ':')
  160.            && (namebuffer[strlen(namebuffer)-1] !=      '/')
  161.            && (namebuffer[0]                    != (char) 0) ) strcat(namebuffer, "/");
  162.  
  163.        strcat(namebuffer, request->rf_File);
  164.  
  165.        if(namebuffer[0] != (char) 0) SP_Examine(namebuffer);
  166.       }
  167.  
  168.      FreeAslRequest(request);
  169.     }
  170.   }else SP_Examine(argv[1]);
  171.  
  172.  Leave(N, 0);
  173. }
  174.  
  175.  
  176. /* *************************************************** */
  177. /* *                             * */
  178. /* * LEAVE : Global Exit Function Replacement         * */
  179. /* *                             * */
  180. /* *************************************************** */
  181.  
  182. void __regargs Leave(char *endtext, long code)
  183. {
  184.  if(SuperPlayBase) CloseLibrary((APTR) SuperPlayBase);
  185.  if(AslBase)       CloseLibrary((APTR) AslBase);
  186.  
  187.  if(endtext)       SP_Printf("%s\n", endtext);
  188.  
  189.  exit(code);
  190. }
  191.  
  192. /* *************************************************** */
  193. /* *                             * */
  194. /* * Play-Function                     * */
  195. /* *                             * */
  196. /* *************************************************** */
  197.  
  198. void __regargs SP_Examine(char *filename)
  199. {
  200.  APTR handle;
  201.  ULONG retval = SPERR_NO_ERROR;
  202.  struct SPO_SampleList  *SampleList;
  203.  struct SPO_SampleEntry *entry;
  204.  
  205.  handle = SPL_AllocHandle(N);
  206.  if(handle)
  207.   {
  208.                retval = SPL_InitHandleAsDOS( handle, N);
  209.    if(!retval) retval = SPL_ReadPlayData(    handle, filename);
  210.    if(!retval) retval = SPL_GetSampleList(   handle, &SampleList);
  211.    if(!retval)
  212.     {
  213.      if(SampleList)
  214.       {
  215.        SP_Printf("\n This File contains %ld Entrie(s)\n", SampleList->sl_NumEntries);
  216.  
  217.        for(entry=(APTR) SampleList->sl_EntryList.lh_Head;(entry)&&(entry!=(APTR) &(SampleList->sl_EntryList.lh_Tail));)
  218.         {
  219.          SP_Printf("\n Entry-Version : %ld", entry->se_Version);
  220.  
  221.          switch(entry->se_Type)
  222.           {
  223.            case SE_TYPE_SAMPLE :
  224.             {
  225.              SP_Printf("\n Entry-Type    : SAMPLE"
  226.                        "\n SampleBuffer  : 0x%lx"
  227.                        "\n SampleSize    : %ld Bytes"
  228.                        "\n SampleBits    : %ld"
  229.                        "\n SamplesPerSec : %ld"
  230.                        "\n Volume        : %ld"
  231.                       , entry->se_SampleBuffer
  232.                       , entry->se_SampleSize
  233.                       , entry->se_SampleBits
  234.                       , entry->se_SamplesPerSec
  235.                       , entry->se_Volume
  236.                       );
  237.  
  238.              if(entry->se_SampleBits == 8)
  239.               {
  240.                UBYTE *retstr;
  241.  
  242.                SP_Printf("\n\nPlaying Sample ...");
  243.  
  244.                retstr = SPLI_PlaySample(entry->se_SampleBuffer, entry->se_SampleSize, entry->se_SamplesPerSec, entry->se_Volume);
  245.  
  246.                if(!retstr) SP_Printf(" Ready\n");
  247.                 else       SP_Printf("\n%s\n", retstr);
  248.               }
  249.  
  250.              break;
  251.             }
  252.            default :
  253.             {
  254.              SP_Printf("\n Entry-Type    : <Unknown> -> skipped");
  255.  
  256.              break;
  257.             }
  258.           }
  259.  
  260.          SP_Printf("\n ----------------------------------");
  261.  
  262.          entry = (struct SPO_SampleEntry *) entry->se_Node.ln_Succ;
  263.         }
  264.  
  265.        SP_Printf("\n");
  266.  
  267.       }else retval = SPERR_ILLEGAL_ACCESS;
  268.     }
  269.   
  270.    SPL_FreeHandle(handle);
  271.  
  272.   }else retval = SPERR_NO_HANDLE;
  273.  
  274.  if(retval) Leave(SPL_GetErrorString(retval), 0);
  275. }
  276.  
  277. UBYTE * __regargs SPLI_PlaySample(UBYTE *smp_buffer, ULONG smp_length, ULONG smp_freq, ULONG smp_volume)
  278. {
  279.  ULONG opres = N, sound_len, sound_play, sound_long, sound_rest;
  280.  UBYTE channel, *sound_buf;
  281.  struct IOAudio *audioreq  = N;
  282.  struct MsgPort *audioport = N;
  283.  APTR msg;
  284.  UBYTE *retstr = N;
  285.  
  286.  audioport = (struct MsgPort  *) CreatePort("8SVX.spobject", N);
  287.  if(audioport)
  288.   {
  289.    audioreq  = (struct IOAudio  *) CreateExtIO(audioport, sizeof(struct IOAudio));
  290.    if(audioreq)
  291.     {
  292.      channel = (UBYTE) 15;
  293.  
  294.      audioreq->ioa_Request.io_Message.mn_Node.ln_Pri = 10;
  295.      audioreq->ioa_Data                              = (APTR) &channel;
  296.      audioreq->ioa_Length                            = 1;
  297.      audioreq->ioa_AllocKey                          = 0;
  298.  
  299.      opres = OpenDevice("audio.device", 0, (struct IORequest *) audioreq, N);
  300.      if(!opres)
  301.       {
  302.        sound_play = TRUE;
  303.        sound_buf  = smp_buffer;
  304.        sound_rest = smp_length;
  305.  
  306.        while(sound_play)
  307.         {
  308.          if(sound_rest <= 0xFFFF)
  309.           {
  310.            sound_len  = sound_rest;
  311.            sound_long = FALSE;
  312.            sound_play = FALSE;
  313.           }else
  314.           {
  315.            sound_len  = 0xFFFF;
  316.            sound_long = TRUE;
  317.  
  318.            sound_rest -= sound_len;
  319.           }
  320.  
  321.          audioreq->ioa_Request.io_Command = CMD_WRITE;
  322.          audioreq->ioa_Request.io_Flags   = ADIOF_PERVOL;
  323.          audioreq->ioa_Data               = sound_buf;
  324.          audioreq->ioa_Cycles             = 1;
  325.          audioreq->ioa_Length             = sound_len;
  326.          audioreq->ioa_Period             = 3579546 / smp_freq;
  327.          audioreq->ioa_Volume             = smp_volume;
  328.  
  329.          BeginIO((struct IORequest *) audioreq);
  330.          WaitIO((struct IORequest *) audioreq);
  331.  
  332.          sound_buf += sound_len;
  333.         }
  334.  
  335.        audioreq->ioa_Request.io_Command = ADCMD_FINISH;
  336.        audioreq->ioa_Request.io_Flags   = 0;
  337.        audioreq->ioa_Data               = 0;
  338.        audioreq->ioa_Cycles             = 0;
  339.        audioreq->ioa_Length             = 0;
  340.        audioreq->ioa_Period             = 0;
  341.        audioreq->ioa_Volume             = 0;
  342.  
  343.        DoIO((struct IORequest *) audioreq);
  344.  
  345.        CloseDevice((struct IORequest *) audioreq);
  346.  
  347.       }else retstr = "Can't open \42audio.device\42 !";
  348.  
  349.      DeleteExtIO((struct IORequest *) audioreq);
  350.  
  351.     }else retstr = "Not enough memory !";
  352.  
  353.    while( msg = (APTR)GetMsg(audioport) ) ReplyMsg((APTR)msg);
  354.  
  355.    DeletePort(audioport);
  356.  
  357.   }else retstr = "Not enough memory !";
  358.  
  359.  return(retstr);
  360. }
  361.