File: main.c

package info (click to toggle)
hivelytracker 0%2Bgit20180223-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 3,088 kB
  • sloc: ansic: 19,601; objc: 434; cpp: 175; makefile: 88
file content (129 lines) | stat: -rw-r--r-- 3,772 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129


    /*
    ** HivelyTracker Windows Commandline Replayer
    **
    ** Just quickly bodged together as an example.
    */

    #define WIN32_LEAN_AND_MEAN // for stripping windows.h include

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <stddef.h>

    #include <windows.h>  // for mixer stream
    #include <mmsystem.h> // for mixer stream

    #include "hvl_replay.h"

    #define BUFFNUM 8

    HWAVEOUT     hWaveOut = INVALID_HANDLE_VALUE; /* Device handle */
    WAVEFORMATEX wfx;
    LPSTR        audblock;
    char audiobuffer[BUFFNUM][((44100*2*2)/50)];

    struct hvl_tune *ht = NULL;

    HANDLE eventh;

    BOOL init( char *name )
    {
      //MMRESULT result;

      wfx.nSamplesPerSec  = 44100;
      wfx.wBitsPerSample  = 16;
      wfx.nChannels       = 2;

      wfx.cbSize          = 0;
      wfx.wFormatTag      = WAVE_FORMAT_PCM;
      wfx.nBlockAlign     = (wfx.wBitsPerSample >> 3) * wfx.nChannels;
      wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;

      hvl_InitReplayer();

      ht = hvl_LoadTune( name, 44100, 2 );
      if( !ht ) return FALSE;

      eventh = CreateEvent(
            NULL,               // default security attributes
            TRUE,               // manual-reset event
            FALSE,              // initial state is nonsignaled
            TEXT("WriteEvent")  // object name
            );

      if( waveOutOpen( &hWaveOut, WAVE_MAPPER, &wfx, (unsigned int)eventh, 0, CALLBACK_EVENT ) != MMSYSERR_NOERROR )
      {
        printf( "Unable to open waveout\n" );
        return FALSE;
      }

      return TRUE;
    }

    void shut( void )
    {
      if( ht ) hvl_FreeTune( ht );
      if( hWaveOut != INVALID_HANDLE_VALUE ) waveOutClose( hWaveOut );
    }

    int main(int argc, char *argv[])
    {
      WAVEHDR header[BUFFNUM];
      int nextbuf = 0;
      //sigset_t base_mask, waiting_mask;

      printf( "Hively Tracker command line player 1.8\n" );

      if( argc < 2 )
      {
        printf( "Usage: play_hvl <tune>\n" );
        return 0;
      }

      if( init( argv[1] ) )
      {
        int i;

        printf( "Tune: '%s'\n", ht->ht_Name );
        printf( "Instruments:\n" );
        for( i=1; i<=ht->ht_InstrumentNr; i++ )
          printf( "%02i: %s\n", i, ht->ht_Instruments[i].ins_Name );

        for ( i=0; i<BUFFNUM; i++ ){
            memset( &header[i], 0, sizeof( WAVEHDR ) );
            header[i].dwBufferLength = ((44100*2*2)/50);
            header[i].lpData         = (LPSTR)audiobuffer[i];
        }

        for ( i=0; i<BUFFNUM-1; i++ ){
            hvl_DecodeFrame( ht, audiobuffer[nextbuf], audiobuffer[nextbuf]+2, 4 );
            waveOutPrepareHeader( hWaveOut, &header[nextbuf], sizeof( WAVEHDR ) );
            waveOutWrite( hWaveOut, &header[nextbuf], sizeof( WAVEHDR ) );
            nextbuf = (nextbuf+1)%BUFFNUM;
        }

        for(;;)
        {
          hvl_DecodeFrame( ht, audiobuffer[nextbuf], audiobuffer[nextbuf]+2, 4 );
          waveOutPrepareHeader( hWaveOut, &header[nextbuf], sizeof( WAVEHDR ) );
          waveOutWrite( hWaveOut, &header[nextbuf], sizeof( WAVEHDR ) );
          nextbuf = (nextbuf+1)%BUFFNUM;

          // Don't do this in your own player or plugin :-)
          //while( waveOutUnprepareHeader( hWaveOut, &header[nextbuf], sizeof( WAVEHDR ) ) == WAVERR_STILLPLAYING ) ;
          
          while( waveOutUnprepareHeader( hWaveOut, &header[nextbuf], sizeof( WAVEHDR ) ) == WAVERR_STILLPLAYING ){
            WaitForSingleObject(eventh, INFINITE);
          }
          ResetEvent(eventh);
        }

      }
      shut();

      return 0;
    }