1 
2 
3     /*
4     ** HivelyTracker Windows Commandline Replayer
5     **
6     ** Just quickly bodged together as an example.
7     */
8 
9     #define WIN32_LEAN_AND_MEAN // for stripping windows.h include
10 
11     #include <stdio.h>
12     #include <stdlib.h>
13     #include <signal.h>
14     #include <stddef.h>
15 
16     #include <windows.h>  // for mixer stream
17     #include <mmsystem.h> // for mixer stream
18 
19     #include "hvl_replay.h"
20 
21     #define BUFFNUM 8
22 
23     HWAVEOUT     hWaveOut = INVALID_HANDLE_VALUE; /* Device handle */
24     WAVEFORMATEX wfx;
25     LPSTR        audblock;
26     char audiobuffer[BUFFNUM][((44100*2*2)/50)];
27 
28     struct hvl_tune *ht = NULL;
29 
30     HANDLE eventh;
31 
init(char * name)32     BOOL init( char *name )
33     {
34       //MMRESULT result;
35 
36       wfx.nSamplesPerSec  = 44100;
37       wfx.wBitsPerSample  = 16;
38       wfx.nChannels       = 2;
39 
40       wfx.cbSize          = 0;
41       wfx.wFormatTag      = WAVE_FORMAT_PCM;
42       wfx.nBlockAlign     = (wfx.wBitsPerSample >> 3) * wfx.nChannels;
43       wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
44 
45       hvl_InitReplayer();
46 
47       ht = hvl_LoadTune( name, 44100, 2 );
48       if( !ht ) return FALSE;
49 
50       eventh = CreateEvent(
51             NULL,               // default security attributes
52             TRUE,               // manual-reset event
53             FALSE,              // initial state is nonsignaled
54             TEXT("WriteEvent")  // object name
55             );
56 
57       if( waveOutOpen( &hWaveOut, WAVE_MAPPER, &wfx, (unsigned int)eventh, 0, CALLBACK_EVENT ) != MMSYSERR_NOERROR )
58       {
59         printf( "Unable to open waveout\n" );
60         return FALSE;
61       }
62 
63       return TRUE;
64     }
65 
shut(void)66     void shut( void )
67     {
68       if( ht ) hvl_FreeTune( ht );
69       if( hWaveOut != INVALID_HANDLE_VALUE ) waveOutClose( hWaveOut );
70     }
71 
main(int argc,char * argv[])72     int main(int argc, char *argv[])
73     {
74       WAVEHDR header[BUFFNUM];
75       int nextbuf = 0;
76       //sigset_t base_mask, waiting_mask;
77 
78       printf( "Hively Tracker command line player 1.8\n" );
79 
80       if( argc < 2 )
81       {
82         printf( "Usage: play_hvl <tune>\n" );
83         return 0;
84       }
85 
86       if( init( argv[1] ) )
87       {
88         int i;
89 
90         printf( "Tune: '%s'\n", ht->ht_Name );
91         printf( "Instruments:\n" );
92         for( i=1; i<=ht->ht_InstrumentNr; i++ )
93           printf( "%02i: %s\n", i, ht->ht_Instruments[i].ins_Name );
94 
95         for ( i=0; i<BUFFNUM; i++ ){
96             memset( &header[i], 0, sizeof( WAVEHDR ) );
97             header[i].dwBufferLength = ((44100*2*2)/50);
98             header[i].lpData         = (LPSTR)audiobuffer[i];
99         }
100 
101         for ( i=0; i<BUFFNUM-1; i++ ){
102             hvl_DecodeFrame( ht, audiobuffer[nextbuf], audiobuffer[nextbuf]+2, 4 );
103             waveOutPrepareHeader( hWaveOut, &header[nextbuf], sizeof( WAVEHDR ) );
104             waveOutWrite( hWaveOut, &header[nextbuf], sizeof( WAVEHDR ) );
105             nextbuf = (nextbuf+1)%BUFFNUM;
106         }
107 
108         for(;;)
109         {
110           hvl_DecodeFrame( ht, audiobuffer[nextbuf], audiobuffer[nextbuf]+2, 4 );
111           waveOutPrepareHeader( hWaveOut, &header[nextbuf], sizeof( WAVEHDR ) );
112           waveOutWrite( hWaveOut, &header[nextbuf], sizeof( WAVEHDR ) );
113           nextbuf = (nextbuf+1)%BUFFNUM;
114 
115           // Don't do this in your own player or plugin :-)
116           //while( waveOutUnprepareHeader( hWaveOut, &header[nextbuf], sizeof( WAVEHDR ) ) == WAVERR_STILLPLAYING ) ;
117 
118           while( waveOutUnprepareHeader( hWaveOut, &header[nextbuf], sizeof( WAVEHDR ) ) == WAVERR_STILLPLAYING ){
119             WaitForSingleObject(eventh, INFINITE);
120           }
121           ResetEvent(eventh);
122         }
123 
124       }
125       shut();
126 
127       return 0;
128     }
129 
130