-
Notifications
You must be signed in to change notification settings - Fork 0
/
dec_a.cpp
306 lines (232 loc) · 7.59 KB
/
dec_a.cpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <[email protected]>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
dec_audio.c
Functions to play sound on DEC OSF/1 with MME support
Ported by Chi Ming HUNG <[email protected]>
Note: This was tested on a DEC alpha 250 4/266. At the default sampling
rate of 32kHz an occasional break in playing could be detected under
CPU load of around 3.0 Try using lower sampling rate or using some of
the hacks in config.h if you have a slow and/or busy machine.
Oh timidity works great as a MIDI-"viewer" for WWW browsers. Just add
audio/midi; timidity1 %s >/dev/null 2>&1
to your .mailcap file and you can click and play MIDI files on the net!
Try e.g. the Classical MIDI Archive:
http://www.prs.net/midi.html
Thanks Tuukka for a great program!!
*/
#ifdef AU_DEC
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <string.h>
#include <mme/mme_api.h>
#include "config.h"
#include "output.h"
#include "controls.h"
static int open_output(void); /* 0=success, 1=warning, -1=fatal error */
static void close_output(void);
static void output_data(int32 *buf, uint32 count);
static int driver_output_data(unsigned char *buf, uint32 count);
static void flush_output(void);
static void purge_output(void);
static int output_count(uint32 ct);
/* export the playback mode */
#define dpm dec_play_mode
PlayMode dpm = {
DEFAULT_RATE, PE_16BIT|PE_SIGNED,
-1,
{0,0,0,0,0}, /* no extra parameters so far */
"DEC audio device", 'd',
"",
open_output,
close_output,
output_data,
driver_output_data,
flush_output,
purge_output,
output_count
};
/* Global variables */
static HWAVEOUT mms_device_handle = 0;
static UINT mms_device_id = 0;
static LPWAVEHDR mms_lpWaveHeader;
/********** MMS_Audio_Init **********************
This just does some generic initialization. The actual audio
output device open is done in the Audio_On routine so that we
can get a device which matches the requested sample rate & format
*****/
void MMS_Audio_Init()
{
MMRESULT status;
LPWAVEOUTCAPS lpCaps;
if( waveOutGetNumDevs() < 1 )
{
fprintf(stderr,"Audio disabled - No Multimedia Services compatible audio devices available\n");
return;
}
/* Figure out device capabilities - Just use device 0 for now */
if((lpCaps = (LPWAVEOUTCAPS)mmeAllocMem(sizeof(WAVEOUTCAPS))) == NULL ) {
fprintf(stderr,"Failed to allocate WAVEOUTCAPS struct\n");
return;
}
status = waveOutGetDevCaps( 0, lpCaps, sizeof(WAVEOUTCAPS));
if( status != MMSYSERR_NOERROR ) {
fprintf(stderr,"waveOutGetDevCaps failed - status = %d\n", status);
}
mmeFreeMem(lpCaps);
}
/********* MMS_wave_callback ****************
* This is only used so that we know the audio device has finished
* playing one block of data and is ready to accept the next
**********/
static void MMS_wave_callback(HANDLE hWaveOut, UINT wMsg, DWORD dwInstance,
LPARAM lParam1, LPARAM lParam2)
{
switch(wMsg)
{
case WOM_OPEN:
case WOM_CLOSE: {
break;
}
case WOM_DONE: {
break;
}
default: {
fprintf(stderr,
"Unknown MMS waveOut callback messages receieved.\n");
break;
}
}
}
/********** MMS_Audio_On **********************
* Turn On Audio Stream.
*
*****/
void MMS_Audio_On()
{
MMRESULT status;
LPPCMWAVEFORMAT lpWaveFormat;
/* Setting up the parameters for audio device */
if((lpWaveFormat = (LPPCMWAVEFORMAT)
mmeAllocMem(sizeof(PCMWAVEFORMAT))) == NULL )
{
fprintf(stderr,"Failed to allocate PCMWAVEFORMAT struct\n");
return;
}
lpWaveFormat->wf.nSamplesPerSec = dpm.rate;
lpWaveFormat->wf.nChannels = 2;
lpWaveFormat->wBitsPerSample = 16;
lpWaveFormat->wf.wFormatTag = WAVE_FORMAT_PCM;
/* lpWaveFormat->wf.nBlockAlign = lpWaveFormat->wf.nChannels *
* ((lpWaveFormat->wBitsPerSample+7)/8) ;
*lpWaveFormat->wf.nAvgBytesPerSec = lpWaveFormat->wf.nBlockAlign *
* lpWaveFormat->wf.nSamplesPerSec ; */
/* Open the audio device in the appropriate rate/format */
mms_device_handle = 0;
status = waveOutOpen( &mms_device_handle,
WAVE_MAPPER,
(LPWAVEFORMAT)lpWaveFormat,
(void (*)()) MMS_wave_callback,
NULL,
WAVE_ALLOWSYNC | CALLBACK_FUNCTION );
if( status != MMSYSERR_NOERROR ) {
fprintf(stderr,"waveOutOpen failed - status = %d\n", status);
}
fprintf(stderr,"Opened waveOut device #%d \n",
mms_device_id);
fprintf(stderr,
"Format=%d, Rate=%d, channels=%d, bps=%d \n",
lpWaveFormat->wf.wFormatTag,
lpWaveFormat->wf.nSamplesPerSec,
lpWaveFormat->wf.nChannels,
lpWaveFormat->wBitsPerSample );
mmeFreeMem(lpWaveFormat);
/* Allocate wave header for use in write */
if((mms_lpWaveHeader = (LPWAVEHDR)
mmeAllocMem(sizeof(WAVEHDR))) == NULL )
{
fprintf(stderr,"Failed to allocate WAVEHDR struct\n");
return;
}
/* Allocate shared audio buffer for communicating with audio device */
if ( (mms_lpWaveHeader->lpData = (LPSTR)
mmeAllocMem(1024*8 )) == NULL)
{
fprintf(stderr,"Failed to allocate shared audio buffer\n");
return;
}
}
/* Open the audio device */
static int open_output(void)
{
int warnings=0;
MMS_Audio_Init();
MMS_Audio_On();
return warnings;
}
static int driver_output_data(unsigned char *buf, uint32 count)
{
return count;
}
/* Output of audio data from timidity */
static void output_data(int32 *buf, uint32 count)
{
MMRESULT status;
if (!(dpm.encoding & PE_MONO)) count*=2; /* Stereo samples */
/* Convert data to signed 16-bit PCM */
s32tos16l(buf, count);
/* write output data to audio device */
mms_lpWaveHeader->dwBufferLength = 2*count;
memcpy( mms_lpWaveHeader->lpData, buf, 2*count);
status = waveOutWrite(mms_device_handle, mms_lpWaveHeader,
sizeof(WAVEHDR));
if( status != MMSYSERR_NOERROR )
{
fprintf(stderr,"waveOutWrite failed - status = %d\n",status);
}
/* Wait for callback from audio device before continuing */
mmeWaitForCallbacks();
mmeProcessCallbacks();
}
/* close output device */
static void close_output(void)
{
MMRESULT status;
status = waveOutReset(mms_device_handle);
if( status != MMSYSERR_NOERROR ) {
fprintf(stderr,"waveOutReset failed - status = %d\n", status);
}
status = waveOutClose(mms_device_handle);
if( status != MMSYSERR_NOERROR ) {
fprintf(stderr,"waveOutClose failed - status = %d\n", status);
}
mmeFreeMem(mms_lpWaveHeader);
}
/* not sure what to do here */
static void flush_output(void) {}
static void purge_output(void)
{
MMRESULT status;
status = waveOutReset(mms_device_handle);
if( status != MMSYSERR_NOERROR ) {
fprintf(stderr,"waveOutReset failed - status = %d\n", status);
}
}
static int output_count(uint32 ct)
{
return (int)ct;
}
#endif /* __osf__ */