home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / ht12_m68k.lha / HivelyTracker / Replayer / src / play_hvl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-12-17  |  4.4 KB  |  190 lines

  1.  
  2. #include <stdio.h>
  3. #include <strings.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. #include <exec/types.h>
  8.  
  9. #ifndef _AMIGAOS3_
  10. #include <exec/exectags.h>
  11. #endif
  12.  
  13. #include <proto/exec.h>
  14. #include <proto/dos.h>
  15.  
  16. #include <proto/ahi.h>
  17.  
  18. #ifdef _AMIGAOS3_
  19. #include <devices/ahi.h>
  20. #include "os3_compat.h"
  21. #endif
  22.  
  23. #include "hvl_replay.h"
  24.  
  25. uint32          audiobuflen;
  26. int8           *audiobuffer[2]  = { NULL, NULL };
  27.  
  28. // Libraries and interfaces
  29. struct Library  *AHIBase = NULL;
  30. struct AHIIFace *IAHI = NULL;
  31.  
  32. // AHI stuff
  33. struct MsgPort    *ahi_mp;
  34. struct AHIRequest *ahi_io[2] = { NULL, NULL };
  35. int32              ahi_dev = -1;
  36. uint32             ahisig = 0;
  37. struct AHIRequest *prev_req = NULL;
  38.  
  39. BOOL   need_wait[2];
  40. uint32 nextbuf;
  41.  
  42. #define FREQ 48000
  43.  
  44. BOOL init( void )
  45. {
  46.   uint32 i;
  47.  
  48.   audiobuflen = FREQ * sizeof( uint16 ) * 2 / 50;
  49.   audiobuflen = audiobuflen * 4;
  50.   printf("%ld\n",audiobuflen);
  51.   audiobuffer[0] = AllocVec( audiobuflen * 2, MEMF_ANY );
  52.   if( audiobuffer[0] == NULL )
  53.   {
  54.     printf( "Out of memory!\n" );
  55.     return FALSE;
  56.   }
  57.  
  58.   audiobuffer[1] = &audiobuffer[0][audiobuflen];
  59.   
  60.   for( i=0; i<audiobuflen; i++ )
  61.     audiobuffer[0][i] = 0;  
  62.  
  63.   ahi_mp = CreateMsgPort();
  64.   if( !ahi_mp )
  65.   {
  66.     printf( "Unable to create message port!\n" );
  67.     return FALSE;
  68.   }
  69.  
  70.   ahi_io[0] = (struct AHIRequest *)CreateIORequest( ahi_mp, sizeof( struct AHIRequest ) );
  71.   if( ahi_io[0] == NULL ) return FALSE;
  72.  
  73.   ahi_io[0]->ahir_Version = 4;
  74.   
  75.   ahi_dev = OpenDevice( AHINAME, AHI_DEFAULT_UNIT, (struct IORequest *)ahi_io[0], 0 ); 
  76.   if( ahi_dev == -1 ) return FALSE;
  77.   
  78.   ahi_io[1] = AllocVec( sizeof( struct AHIRequest ), MEMF_ANY );
  79.   if( ahi_io[1] == NULL ) return FALSE;
  80.   
  81.   CopyMem( ahi_io[0], ahi_io[1], sizeof( struct AHIRequest ) );
  82.  
  83.   ahisig  = 1L<<ahi_mp->mp_SigBit;
  84.   
  85.   return TRUE;
  86. }
  87.  
  88. void shut( void )
  89. {
  90.   if( ahi_io[1] )     FreeVec( ahi_io[1] );
  91.   if( ahi_dev != -1 )
  92.   {
  93.     CloseDevice( (struct IORequest *)ahi_io[0] );
  94.     DeleteIORequest( (struct IORequest *)ahi_io[0] );
  95.   }
  96.   if( ahi_mp )        DeleteMsgPort( ahi_mp );
  97.   if( audiobuffer[0] ) FreeVec( audiobuffer[0] );
  98. }
  99.  
  100. void mix_and_play( struct hvl_tune *ht )
  101. {
  102.  
  103.   if( need_wait[nextbuf] )
  104.     WaitIO( (struct IORequest *)ahi_io[nextbuf] );
  105.  
  106.   hvl_DecodeFrame( ht, audiobuffer[nextbuf], audiobuffer[nextbuf]+sizeof( int16 ), 4 );
  107.  
  108.   ahi_io[nextbuf]->ahir_Std.io_Command = CMD_WRITE;
  109.   ahi_io[nextbuf]->ahir_Std.io_Data    = audiobuffer[nextbuf];
  110.   ahi_io[nextbuf]->ahir_Std.io_Length  = audiobuflen;
  111.   ahi_io[nextbuf]->ahir_Std.io_Offset  = 0;
  112.   ahi_io[nextbuf]->ahir_Type           = AHIST_S16S;
  113.   ahi_io[nextbuf]->ahir_Frequency      = FREQ;
  114.   ahi_io[nextbuf]->ahir_Volume         = 0x10000;
  115.   ahi_io[nextbuf]->ahir_Position       = 0x8000;
  116.   ahi_io[nextbuf]->ahir_Link           = prev_req;
  117.   SendIO( (struct IORequest *)ahi_io[nextbuf] );
  118.             
  119.   prev_req = ahi_io[nextbuf];
  120.   need_wait[nextbuf] = TRUE;
  121.  
  122.   nextbuf ^= 1;  
  123.   
  124.   if( ( need_wait[nextbuf] == TRUE ) &&
  125.       ( CheckIO( (struct IORequest *)ahi_io[nextbuf] ) != NULL ) )
  126.   {
  127.     WaitIO( (struct IORequest *)ahi_io[nextbuf] );
  128.     need_wait[nextbuf] = FALSE;
  129.   }
  130.             
  131.   if( need_wait[nextbuf] == FALSE )
  132.   {
  133.     hvl_DecodeFrame( ht, audiobuffer[nextbuf], audiobuffer[nextbuf]+sizeof( int16 ), 4 );
  134.     ahi_io[nextbuf]->ahir_Std.io_Command = CMD_WRITE;
  135.     ahi_io[nextbuf]->ahir_Std.io_Data    = audiobuffer[nextbuf];
  136.     ahi_io[nextbuf]->ahir_Std.io_Length  = audiobuflen;
  137.     ahi_io[nextbuf]->ahir_Std.io_Offset  = 0;
  138.     ahi_io[nextbuf]->ahir_Type           = AHIST_S16S;
  139.     ahi_io[nextbuf]->ahir_Frequency      = FREQ;
  140.     ahi_io[nextbuf]->ahir_Volume         = 0x10000;
  141.     ahi_io[nextbuf]->ahir_Position       = 0x8000;
  142.     ahi_io[nextbuf]->ahir_Link           = prev_req;
  143.     SendIO( (struct IORequest *)ahi_io[nextbuf] );
  144.  
  145.     prev_req = ahi_io[nextbuf];
  146.     need_wait[nextbuf] = TRUE;
  147.     nextbuf ^= 1;  
  148.   }
  149. }
  150.  
  151. int main( int argc, char *argv[] )
  152. {
  153.   struct hvl_tune *tune;
  154.  
  155.   if( argc < 2 )
  156.   {
  157.     printf( "Usage: play_hvl <tune>\n" );
  158.     return 0;
  159.   }
  160.  
  161.   if( init() )
  162.   {
  163.     hvl_InitReplayer();
  164.     tune = hvl_LoadTune( argv[1], FREQ, 4 );
  165.     if( tune )
  166.     {
  167.       BOOL done;
  168.       uint32 gotsigs;
  169.       
  170.       hvl_InitSubsong( tune, 0 );
  171.  
  172.       nextbuf = 0;
  173.       mix_and_play( tune );
  174.  
  175.       done = FALSE;
  176.  
  177.       while( !done )
  178.       {
  179.         gotsigs = Wait( ahisig | SIGBREAKF_CTRL_C );
  180.       
  181.         if( gotsigs & ahisig ) mix_and_play( tune );
  182.         if( gotsigs & SIGBREAKF_CTRL_C ) done = TRUE;
  183.       }
  184.       hvl_FreeTune( tune );
  185.     }
  186.   }
  187.   shut();
  188.   return 0;
  189. }
  190.