home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 1 / Mecomp-CD.iso / amiga / player / ahi / developer / examples / doublebuffer / doublebuffer.c next >
Encoding:
C/C++ Source or Header  |  1996-05-11  |  6.9 KB  |  225 lines

  1. /* Simple sample player for AHI using the low-level API
  2.    with double buffered AHIST_DYNAMICSAMPLE sounds.
  3.  
  4.    Usage: DoubleBuffer < [raw sample file]
  5.  
  6.    the file must be in mono 16 bit signed big endian format sampled
  7.    in 17640 Hz.
  8.  
  9.    This software is Public Domain. */
  10.  
  11. #include <devices/ahi.h>
  12. #include <exec/exec.h>
  13. #include <proto/ahi.h>
  14. #include <proto/dos.h>
  15. #include <proto/exec.h>
  16.  
  17. #define EQ ==
  18. #define MINBUFFLEN 10000
  19.  
  20. struct Library    *AHIBase;
  21. struct MsgPort    *AHImp=NULL;
  22. struct AHIRequest *AHIio=NULL;
  23. BYTE               AHIDevice=-1;
  24.  
  25. BYTE signal=-1;
  26.  
  27. struct AHIAudioModeRequester *req=NULL;
  28. struct AHIAudioCtrl *actrl=NULL;
  29.  
  30. BOOL DBflag=FALSE;  // double buffer flag
  31. ULONG BufferLen=NULL;
  32.  
  33. struct AHISampleInfo Sample0 =
  34. {
  35.   AHIST_M16S,
  36.   NULL,
  37.   NULL,
  38. };
  39.  
  40. struct AHISampleInfo Sample1 =
  41. {
  42.   AHIST_M16S,
  43.   NULL,
  44.   NULL,
  45. };
  46.  
  47. __asm __saveds ULONG SoundFunc(register __a0 struct Hook *hook,
  48.     register __a2 struct AHIAudioCtrl *actrl,
  49.     register __a1 struct AHISoundMessage *smsg)
  50. {
  51.   if(DBflag = !DBflag) // Flip and test
  52.     AHI_SetSound(0,1,0,0,actrl,NULL);
  53.   else
  54.     AHI_SetSound(0,0,0,0,actrl,NULL);
  55.   Signal(actrl->ahiac_UserData,(1L<<signal));
  56.   return NULL;
  57. }
  58.  
  59. struct Hook SoundHook =
  60. {
  61.   0,0,
  62.   SoundFunc,
  63.   NULL,
  64.   NULL,
  65. };
  66.  
  67.  
  68. int main()
  69. {
  70.   ULONG mixfreq,playsamples,readsamples,signals,i;
  71.  
  72.   if(AHImp=CreateMsgPort())
  73.   {
  74.     if(AHIio=(struct AHIRequest *)CreateIORequest(AHImp,sizeof(struct AHIRequest)))
  75.     {
  76.       AHIio->ahir_Version=1;  // Open at least version 1 of 'ahi.device'.
  77.       if(!(AHIDevice=OpenDevice(AHINAME,AHI_NO_UNIT,(struct IORequest *)AHIio,NULL)))
  78.       {
  79.         AHIBase=(struct Library *)AHIio->ahir_Std.io_Device;
  80.  
  81.         if(req=AHI_AllocAudioRequest(
  82.             AHIR_PubScreenName,"",
  83.             AHIR_TitleText,"Select a mode and rate",
  84.             AHIR_InitialMixFreq,17640,
  85.             AHIR_DoMixFreq,TRUE,
  86.             TAG_DONE))
  87.         {
  88.           if(AHI_AudioRequest(req,TAG_DONE))
  89.           {
  90.             if(actrl=AHI_AllocAudio(
  91.                 AHIA_AudioID,req->ahiam_AudioID,
  92.                 AHIA_MixFreq,req->ahiam_MixFreq,
  93.                 AHIA_Channels,1,
  94.                 AHIA_Sounds,2,
  95.                 AHIA_SoundFunc,&SoundHook,
  96.                 AHIA_UserData,FindTask(NULL),
  97.                 TAG_DONE))
  98.             {
  99.               AHI_GetAudioAttrs(AHI_INVALID_ID,actrl,
  100.                   AHIDB_MaxPlaySamples,&playsamples,
  101.                   TAG_DONE);
  102.               AHI_ControlAudio(actrl,
  103.                   AHIC_MixFreq_Query,&mixfreq,
  104.                   TAG_DONE);
  105.               BufferLen=playsamples*mixfreq/17640;
  106.               if (BufferLen<MINBUFFLEN)
  107.                 BufferLen=MINBUFFLEN;
  108.  
  109.               Sample0.ahisi_Length=BufferLen;
  110.               Sample1.ahisi_Length=BufferLen;
  111.               Sample0.ahisi_Address=AllocVec(BufferLen*2,MEMF_PUBLIC|MEMF_CLEAR);
  112.               Sample1.ahisi_Address=AllocVec(BufferLen*2,MEMF_PUBLIC|MEMF_CLEAR);
  113.  
  114.               if(Sample0.ahisi_Address && Sample1.ahisi_Address)
  115.               {
  116.                 if((!(AHI_LoadSound(0,AHIST_DYNAMICSAMPLE,&Sample0,actrl))) &&
  117.                    (!(AHI_LoadSound(1,AHIST_DYNAMICSAMPLE,&Sample1,actrl))))
  118.                 {
  119.                   if((signal=AllocSignal(-1)) != -1)
  120.                   {
  121.                     if(!(AHI_ControlAudio(actrl,
  122.                         AHIC_Play,TRUE,
  123.                         TAG_DONE)))
  124.                     {
  125. // Everything is set up now. Let's load the buffers and start rockin'...
  126.                       Read(Input(),Sample0.ahisi_Address,BufferLen*2);
  127.                       AHI_SetFreq(0,17640,actrl,AHISF_IMM);
  128.                       AHI_SetVol(0,0x10000L,0x8000L,actrl,AHISF_IMM);
  129.                       AHI_SetSound(0,0,0,0,actrl,AHISF_IMM);
  130.                       for(;;)
  131.                       {
  132.                         signals=Wait((1L<<signal) | SIGBREAKF_CTRL_C);
  133.                         if(signals & SIGBREAKF_CTRL_C)
  134.                           break;
  135.  
  136.                         if(DBflag)
  137.                         {
  138.                           readsamples=Read(Input(),Sample1.ahisi_Address,BufferLen*2)/2;
  139.                           if(readsamples<BufferLen)
  140.                           {
  141.                             // Clear rest of buffer
  142.                             for(i=readsamples;i<BufferLen;i++)
  143.                               ((WORD *)Sample1.ahisi_Address)[i]=0;
  144.                             Wait(1L<<signal);
  145.                             // Clear other buffer
  146.                             for(i=0;i<BufferLen;i++)
  147.                               ((WORD *)Sample0.ahisi_Address)[i]=0;
  148.                             break;
  149.                           }
  150.                         }
  151.                         else
  152.                         {
  153.                           readsamples=Read(Input(),Sample0.ahisi_Address,BufferLen*2)/2;
  154.                           if(readsamples<BufferLen)
  155.                           {
  156.                             // Clear rest of buffer
  157.                             for(i=readsamples;i<BufferLen;i++)
  158.                               ((WORD *)Sample0.ahisi_Address)[i]=0;
  159.                             Wait(1L<<signal);
  160.                             // Clear other buffer
  161.                             for(i=0;i<BufferLen;i++)
  162.                               ((WORD *)Sample1.ahisi_Address)[i]=0;
  163.                             break;
  164.                           }
  165.                         }
  166.                         
  167.  
  168.                       }
  169.                       if(signals & SIGBREAKF_CTRL_C)
  170.                         Printf("***Break\n");
  171.                       else
  172.                         Wait(1L<<signal);   // Wait for half-loaded buffer to finish.
  173.  
  174.                       AHI_ControlAudio(actrl,
  175.                           AHIC_Play,FALSE,
  176.                           TAG_DONE);
  177.                     }
  178.                     FreeSignal(signal);
  179.                     signal=-1;
  180.                   }
  181.                   
  182.                 }
  183.                 else
  184.                   Printf("Cannot intialize sample buffers\n");
  185.               }
  186.               else
  187.                 Printf("Out of memory.\n");
  188.  
  189.               FreeVec(Sample0.ahisi_Address);
  190.               FreeVec(Sample1.ahisi_Address);
  191.               Sample0.ahisi_Address=NULL;
  192.               Sample1.ahisi_Address=NULL;
  193.               AHI_FreeAudio(actrl);
  194.               actrl=NULL;
  195.             }
  196.             else
  197.               Printf("Unable to allocate sound hardware\n");
  198.           }
  199.           else
  200.           {
  201.             if(IoErr() EQ NULL)
  202.               Printf("Aborted.\n");
  203.             else if(IoErr() EQ ERROR_NO_MORE_ENTRIES)
  204.               Printf("No available audio modes.\n");
  205.             else if(IoErr() EQ ERROR_NO_FREE_STORE)
  206.               Printf("Out of memory.\n");
  207.           }
  208.           
  209.           AHI_FreeAudioRequest(req);
  210.           req=NULL;
  211.         }
  212.         else
  213.           Printf("Out of memory.\n");
  214.  
  215.         CloseDevice((struct IORequest *)AHIio);
  216.         AHIDevice=-1; // Good habit, IMHO.
  217.       }
  218.       DeleteIORequest((struct IORequest *)AHIio);
  219.       AHIio=NULL;
  220.     }
  221.     DeleteMsgPort(AHImp);
  222.     AHImp=NULL;
  223.   }
  224. }
  225.