File: win32sound.c

package info (click to toggle)
fuse-emulator 1.0.0.1a%2Bdfsg1-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 9,568 kB
  • sloc: ansic: 67,895; sh: 10,265; perl: 3,386; makefile: 787; yacc: 227; lex: 139
file content (231 lines) | stat: -rw-r--r-- 7,050 bytes parent folder | download
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* win32sound.c: Win32 sound using MS Windows Mulitmedia API
   Copyright (c) 2007 Marek Januszewski

   $Id: win32sound.c 3754 2008-08-20 03:34:23Z specu $

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   E-mail: philip-fuse@shadowmagic.org.uk

*/

#include <config.h>

#include <windows.h>

#include "settings.h"
#include "sound.h"
#include "ui/ui.h"

static HWAVEOUT hwaveout;
static WAVEHDR wavehdr[2];
static HANDLE sem_sound_done;
static CRITICAL_SECTION sound_lock;

#define BUFFER_SIZE 4096

static void *buffers[2];
static int buffer_used[2];
static int current_buffer;
static char buffer1[ BUFFER_SIZE ];
static char buffer2[ BUFFER_SIZE ];

static int sixteenbit;

static void
sound_display_mmresult( char *func, MMRESULT result );

static void CALLBACK
sound_callback( HWAVEOUT hwo,UINT uMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2 );

int
sound_lowlevel_init( const char *device, int *freqptr, int *stereoptr )
{
  WAVEFORMATEX pcmwf; /* waveformat struct */
  MMRESULT result;

  /* create wave format description */
  memset( &pcmwf, 0, sizeof( WAVEFORMATEX ) );

  pcmwf.cbSize = 0;
  if( settings_current.sound_force_8bit ) {
    pcmwf.wBitsPerSample = 8;
    sixteenbit = 0;
  } else {
    pcmwf.wBitsPerSample = 16;
    sixteenbit = 1;
  }
  pcmwf.nChannels = *stereoptr ? 2 : 1;
  pcmwf.nBlockAlign = pcmwf.nChannels * ( sixteenbit ? 2 : 1 );
  pcmwf.nSamplesPerSec = *freqptr;
  pcmwf.nAvgBytesPerSec = pcmwf.nSamplesPerSec * pcmwf.nBlockAlign;
  pcmwf.wFormatTag = WAVE_FORMAT_PCM;

  /* Open sound device for output */
  result = waveOutOpen( &hwaveout, WAVE_MAPPER,
                        &pcmwf, ( DWORD_PTR ) sound_callback, 0,
                        CALLBACK_FUNCTION );
  if( result != MMSYSERR_NOERROR )
    sound_display_mmresult( "waveOutOpen", result );

  buffers[0] = buffer1;
  buffers[1] = buffer2;
  buffer_used[0] = 0;
  buffer_used[1] = 0;
  current_buffer = 0;
  memset( &wavehdr[0], 0, sizeof( WAVEHDR ) );
  memset( &wavehdr[1], 0, sizeof( WAVEHDR ) );

  sem_sound_done = CreateSemaphore( NULL, 0, 2, NULL );
  InitializeCriticalSection( &sound_lock );

  return 0;
}

void
sound_lowlevel_end( void )
{
  MMRESULT result;

  /* reset the sound */
  result = waveOutReset( hwaveout );
  if( result != MMSYSERR_NOERROR ) {
    settings_current.sound = 0;
    sound_display_mmresult( "Couldn't stop sound (waveOutReset)", result );
    return;
  }

  /* unprepare wave headers */
  if( wavehdr[ 0 ].dwFlags & WHDR_PREPARED ) {
    result = waveOutUnprepareHeader( hwaveout, &wavehdr[ 0 ], sizeof( WAVEHDR ) );
    if( result != MMSYSERR_NOERROR )
      sound_display_mmresult( "waveOutUnprepareHeader", result );
  }
  
  if( wavehdr[ 1 ].dwFlags & WHDR_PREPARED ) {
    result = waveOutUnprepareHeader( hwaveout, &wavehdr[ 1 ], sizeof( WAVEHDR ) );
    if( result != MMSYSERR_NOERROR )
      sound_display_mmresult( "waveOutUnprepareHeader", result );
  }

  /* close the device */
  result = waveOutClose( hwaveout );
  if( result != MMSYSERR_NOERROR ) {
    settings_current.sound = 0;
    sound_display_mmresult( "Couldn't stop sound (waveOutClose)", result );
  }

  CloseHandle( sem_sound_done );
  DeleteCriticalSection( &sound_lock );
  
  free( buffers[ 0 ] );
  free( buffers[ 1 ] );
}

void
sound_lowlevel_frame( libspectrum_signed_word *data, int len )
{
  static unsigned char buf8[4096];
  MMRESULT result;

  /* Convert to bytes */
  unsigned char *bytes = (unsigned char *) data;
  len <<= 1;
  if( !sixteenbit ) {
    libspectrum_signed_word *src;
    unsigned char *dst;
    int f;

    src = data; dst = buf8;
    len >>= 1;
    /* TODO: confirm byteorder on IA64 */
    for( f = 0; f < len; f++ )
      *dst++ = 128 + (int)( (*src++) / 256 );

    bytes = buf8;
  }
  
  if( len > BUFFER_SIZE ) {
    ui_error( UI_ERROR_WARNING, "%s: requested wave size exceeds the buffer size", __func__ );
    return;
  }

  /* wait for the buffer to finish playing */
  if( buffer_used[ current_buffer ] > 0 )
    WaitForSingleObject( sem_sound_done, INFINITE );
  
  /* unprepare the header if it's prepared */
  if( wavehdr[ current_buffer ].dwFlags & WHDR_PREPARED ) {
    result = waveOutUnprepareHeader( hwaveout, &wavehdr[ current_buffer ], sizeof( WAVEHDR ) );
    if( result != MMSYSERR_NOERROR )
      sound_display_mmresult( "waveOutUnprepareHeader", result );
  }
  
  /* copy the new wave into the buffer */
  memcpy( buffers[ current_buffer ], bytes, len );
  buffer_used[ current_buffer ] = len;
  
  /* prepare the header */
  wavehdr[ current_buffer ].lpData = ( LPSTR ) bytes;
  wavehdr[ current_buffer ].dwBufferLength = len;
  wavehdr[ current_buffer ].dwLoops |= WHDR_BEGINLOOP | WHDR_ENDLOOP;

  result = waveOutPrepareHeader( hwaveout, &wavehdr[ current_buffer ], sizeof( WAVEHDR ) );
  if( result != MMSYSERR_NOERROR )
    sound_display_mmresult( "waveOutPrepareHeader", result );

  /* play */
  result = waveOutWrite( hwaveout, &wavehdr[ current_buffer ], sizeof( WAVEHDR ) );
  if( result != MMSYSERR_NOERROR )
    sound_display_mmresult( "waveOutWrite", result );
  
  /* FIXME this could be done way easier */
  current_buffer++;
  if( current_buffer == 2 )
    current_buffer = 0;
}

static void
sound_display_mmresult( char *func, MMRESULT result )
{
  char *mmresult;

  switch ( result ) {
    case WAVERR_BADFORMAT:     mmresult = "WAVERR_BADFORMAT"; break;
    case WAVERR_SYNC:          mmresult = "WAVERR_SYNC"; break;
    case WAVERR_STILLPLAYING:  mmresult = "WAVERR_STILLPLAYING"; break;
    case MMSYSERR_NOERROR:     mmresult = "MMSYSERR_NOERROR"; break;
    case MMSYSERR_BADDEVICEID: mmresult = "MMSYSERR_BADDEVICEID"; break;
    case MMSYSERR_NODRIVER:    mmresult = "MMSYSERR_NODRIVER"; break;
    case MMSYSERR_INVALHANDLE: mmresult = "MMSYSERR_INVALHANDLE"; break;
    case MMSYSERR_NOMEM:       mmresult = "MMSYSERR_NOMEM"; break;
    case MMSYSERR_ALLOCATED:   mmresult = "MMSYSERR_ALLOCATED"; break;
    default:                   mmresult = "UNKNOWN"; break;
  }

  ui_error( UI_ERROR_ERROR, "%s error: %s(error no: %d)", func, mmresult, result );
}

static void CALLBACK
sound_callback( HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2 )
{
  if( uMsg == WOM_DONE ) {
    EnterCriticalSection( &sound_lock );
    ReleaseSemaphore( sem_sound_done, 1, NULL );
    LeaveCriticalSection( &sound_lock );
  }
}