summarylogtreecommitdiffstats
path: root/sidplay2-2.0.9-alsa.patch
blob: 11da9f8f37206266fc013effebd8919460579cb8 (plain)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
diff -Naur sidplay-2.0.9.ORIG/configure.ac sidplay-2.0.9/configure.ac
--- sidplay-2.0.9.ORIG/configure.ac	2004-05-06 06:13:58.000000000 +1000
+++ sidplay-2.0.9/configure.ac	2011-03-18 14:33:10.000000000 +1100
@@ -37,10 +37,17 @@
 #undef HAVE_WAV_ONLY
 ])
 
+AC_CHECK_HEADER(alsa/asoundlib.h, [OK_ASOUNDLIB_H=1])
+AC_CHECK_LIB(asound, main, [OK_LIBASOUND=1])
+
+# use ALSA is available
+audio="$host"
+test "$OK_ASOUNDLIB_H" -a "$OK_LIBASOUND" && audio="alsa"
+
 dnl Audio Subsystem Test
 AC_MSG_CHECKING([which audio subsystem is available])
 audiodrv_libadd=""
-case "$host" in
+case "$audio" in
     *cygwin*) AC_DEFINE(HAVE_MMSYSTEM)
         audiodrv_libadd="./audio/mmsystem/libmmsystem.a"
         AC_MSG_RESULT(mmsystem)
@@ -67,12 +74,12 @@
     *netbsd*) AC_DEFINE(HAVE_OSS)
         audiodrv_libadd="./audio/oss/liboss.a"
         AC_MSG_RESULT(oss)
-        AC_CHECK_LIB(ossaudio, main, [AUDIO_LDADD=-lossaudio AC_SUBST(AUDIO_LDFLAGS)]) 
+        AC_CHECK_LIB(ossaudio, main, [AUDIO_LDFLAGS=-lossaudio AC_SUBST(AUDIO_LDFLAGS)]) 
         ;;
-    *qnx*) AC_DEFINE(HAVE_ALSA)
+    *qnx*|alsa) AC_DEFINE(HAVE_ALSA)
         audiodrv_libadd="./audio/alsa/libalsa.a"
         AC_MSG_RESULT(alsa)
-        AC_CHECK_LIB(asound, main, [AUDIO_LDADD=-lasound AC_SUBST(AUDIO_LDFLAGS)])
+        AC_CHECK_LIB(asound, main, [AUDIO_LDFLAGS=-lasound AC_SUBST(AUDIO_LDFLAGS)])
         ;;
     *sgi*) AC_DEFINE(HAVE_SGI)
         AC_MSG_RESULT(sgi)
diff -Naur sidplay-2.0.9.ORIG/src/Makefile.am sidplay-2.0.9/src/Makefile.am
--- sidplay-2.0.9.ORIG/src/Makefile.am	2003-07-08 05:58:53.000000000 +1000
+++ sidplay-2.0.9/src/Makefile.am	2011-03-18 14:14:43.000000000 +1100
@@ -10,7 +10,7 @@
 player.cpp player.h
 
 sidplay2_LDADD = ./audio/null/libnull.a ./audio/wav/libwav.a \
-$(AUDIO_DRV) $(AUDIO_LDFLAGS) \
+$(AUDIO_DRV) $(AUDIO_LDFLAGS) $(AUDIO_LDADD) \
 $(LIBSIDPLAY2_LDFLAGS) $(LIBSIDUTILS_LDFLAGS) \
 $(BUILDERS_LDFLAGS)
 
diff -Naur sidplay-2.0.9.ORIG/src/audio/alsa/audiodrv.cpp sidplay-2.0.9/src/audio/alsa/audiodrv.cpp
--- sidplay-2.0.9.ORIG/src/audio/alsa/audiodrv.cpp	2002-03-05 06:07:48.000000000 +1100
+++ sidplay-2.0.9/src/audio/alsa/audiodrv.cpp	2011-03-18 14:14:43.000000000 +1100
@@ -3,6 +3,9 @@
 // --------------------------------------------------------------------------
 /***************************************************************************
  *  $Log: audiodrv.cpp,v $
+ *  Revision 1.8  2008/07/19 13:58:04  wnd
+ *  Almost complete rewrite to support new ALSA API.
+ *
  *  Revision 1.7  2002/03/04 19:07:48  s_a_white
  *  Fix C++ use of nothrow.
  *
@@ -29,171 +32,277 @@
  ***************************************************************************/
 
 #include "audiodrv.h"
-#ifdef   HAVE_ALSA
+#ifdef HAVE_ALSA
 
 #include <stdio.h>
 #ifdef HAVE_EXCEPTIONS
-#   include <new>
+#include <new>
 #endif
 
+#include <alsa/asoundlib.h>
+#include <string.h>
+
+#define ALSA_DEFAULT "default"
+
 Audio_ALSA::Audio_ALSA()
 {
-    // Reset everything.
-    outOfOrder();
+	// Reset everything.
+	outOfOrder();
+}
+
+Audio_ALSA::~Audio_ALSA()
+{
+	close ();
 }
 
-Audio_ALSA::~Audio_ALSA ()
+void
+Audio_ALSA::outOfOrder()
 {
-    close ();
+	// Reset everything.
+	_swapEndian = false;
+	_bufShift = 0;
+	_errorString = "None";
+	_audioHandle = NULL;
 }
 
-void Audio_ALSA::outOfOrder ()
-{
-    // Reset everything.
-    _errorString = "None";
-    _audioHandle = NULL;
-}
-
-void *Audio_ALSA::open (AudioConfig &cfg, const char *)
-{
-    AudioConfig tmpCfg;
-    int mask, wantedFormat, format;
-    int rtn;
-    int card = -1, dev = 0;
-   
-    if (_audioHandle != NULL)
-    {
-        _errorString = "ERROR: Device already in use";
-        return NULL;
-    }
-
-    if ((rtn = snd_pcm_open_preferred (&_audioHandle, &card, &dev, SND_PCM_OPEN_PLAYBACK)))
-    {
-        _errorString = "ERROR: Could not open audio device.";
-        goto open_error;
-    }
-    
-    // Transfer input parameters to this object.
-    // May later be replaced with driver defaults.
-    tmpCfg = cfg;
-
-    snd_pcm_channel_params_t pp;
-    snd_pcm_channel_setup_t setup;
- 
-    snd_pcm_channel_info_t pi;
-   
-    memset (&pi, 0, sizeof (pi));
-    pi.channel = SND_PCM_CHANNEL_PLAYBACK;
-    if ((rtn = snd_pcm_plugin_info (_audioHandle, &pi)))
-    {
-        _errorString = "ALSA: snd_pcm_plugin_info failed.";
-        goto open_error;
-    }
-			
-    memset(&pp, 0, sizeof (pp));
+bool
+Audio_ALSA::fmt(AudioConfig &cfg, snd_pcm_hw_params_t *hw)
+{
+	struct precinfo {
+		snd_pcm_format_t format;
+		int encoding;
+		int precision;
+		int swap;
+	};
+	struct precinfo info[] = {
+#if defined(WORDS_BIGENDIAN)
+		{ SND_PCM_FORMAT_S16_BE, AUDIO_SIGNED_PCM, 16, false },
+		{ SND_PCM_FORMAT_U16_BE, AUDIO_UNSIGNED_PCM, 16, false },
+		{ SND_PCM_FORMAT_S16_LE, AUDIO_SIGNED_PCM, 16, true },
+		{ SND_PCM_FORMAT_U16_LE, AUDIO_UNSIGNED_PCM, 16, true },
+#else
+		{ SND_PCM_FORMAT_S16_LE, AUDIO_SIGNED_PCM, 16, false },
+		{ SND_PCM_FORMAT_U16_LE, AUDIO_UNSIGNED_PCM, 16, false },
+		{ SND_PCM_FORMAT_S16_BE, AUDIO_SIGNED_PCM, 16, true },
+		{ SND_PCM_FORMAT_U16_BE, AUDIO_UNSIGNED_PCM, 16, true },
+#endif
+		{ SND_PCM_FORMAT_S8, AUDIO_SIGNED_PCM, 8, false },
+		{ SND_PCM_FORMAT_U8, AUDIO_UNSIGNED_PCM, 8, false }
+	};
+
+	switch (cfg.precision) {
+		case 8:
+			for (int i = 0; i < 2; i++) {
+				struct precinfo t;
+				memcpy(&t, &info[i], sizeof(t));
+				memcpy(&info[i + 4], &info[i], sizeof(t));
+				memcpy(&info[i], &t, sizeof(t));
+			}
+			break;
+		case 16:
+			break;
+		default:
+			fprintf(stderr, "Unsupported precision: %d bits\n",
+					cfg.precision);
+			return false;
+	}
+
+	int err;
+	for (int i = 0; i < 6; i++) {
+		err = snd_pcm_hw_params_set_format(_audioHandle, hw,
+				info[i].format);
+		if (err < 0) {
+			continue;
+		}
+
+		cfg.precision = info[i].precision;
+		cfg.encoding = info[i].encoding;
+		_swapEndian = info[i].swap;
+		_bufShift = info[i].precision == 8 ? 0 : 1;
+		return true;
+	}
 	
-    pp.mode = SND_PCM_MODE_BLOCK;
-    pp.channel = SND_PCM_CHANNEL_PLAYBACK;
-    pp.start_mode = SND_PCM_START_FULL;
-    pp.stop_mode = SND_PCM_STOP_STOP;
-				     
-    pp.buf.block.frag_size = pi.max_fragment_size;
-
-    pp.buf.block.frags_max = 1;
-    pp.buf.block.frags_min = 1;
-   
-    pp.format.interleave = 1;
-    pp.format.rate = tmpCfg.frequency;
-    pp.format.voices = tmpCfg.channels;
-   
-    // Set sample precision and type of encoding.
-    if ( tmpCfg.precision == 8 )
-    {
-        tmpCfg.encoding = AUDIO_UNSIGNED_PCM;
-        pp.format.format = SND_PCM_SFMT_U8;
-    }
-    if ( tmpCfg.precision == 16 )
-    {
-        tmpCfg.encoding = AUDIO_SIGNED_PCM;
-        pp.format.format = SND_PCM_SFMT_S16_LE;
-    }
-
-    if ((rtn = snd_pcm_plugin_params (_audioHandle, &pp)) < 0)
-    {
-        _errorString = "ALSA: snd_pcm_plugin_params failed.";
-        goto open_error;
-    }
-   
-    if ((rtn = snd_pcm_plugin_prepare (_audioHandle, SND_PCM_CHANNEL_PLAYBACK)) < 0)
-    {
-        _errorString = "ALSA: snd_pcm_plugin_prepare failed.";
-        goto open_error;
-    }
-   
-    memset (&setup, 0, sizeof (setup));
-    setup.channel = SND_PCM_CHANNEL_PLAYBACK;
-    if ((rtn = snd_pcm_plugin_setup (_audioHandle, &setup)) < 0)
-    {
-        _errorString = "ALSA: snd_pcm_plugin_setup failed.";
-        goto open_error;
-    }
+	fprintf(stderr, "Cannot set sample format: %s\n", snd_strerror(err));
+	return false;
+}
+
+
+
+bool
+Audio_ALSA::init(AudioConfig &cfg, snd_pcm_hw_params_t *hw)
+{
+	int err;
+
+	err = snd_pcm_hw_params_any(_audioHandle, hw);
+	if (err < 0) {
+		fprintf(stderr, "Cannot initialize hardware "
+				"parameter structure: %s\n",
+				snd_strerror(err));
+		return false;
+	}
+
+	err = snd_pcm_hw_params_set_access(_audioHandle, hw,
+			SND_PCM_ACCESS_RW_INTERLEAVED);
+	if (err < 0) {
+		fprintf(stderr, "Cannot set access type: %s\n",
+				snd_strerror(err));
+		return false;
+	}
+
+	/* try to find supported format/precision */
+	if (! fmt(cfg, hw)) {
+		return false;
+	}
+
+	/* use closest frequency */
+	unsigned int realFreq = (unsigned int) cfg.frequency;
+	err = snd_pcm_hw_params_set_rate_near(_audioHandle, hw, &realFreq, 0);
+	if (err < 0) {
+		fprintf(stderr, "Cannot set sample rate: %s\n",
+				snd_strerror(err));
+		return false;
+	}
+	cfg.frequency = realFreq;
+
+	switch (cfg.channels) {
+		case 2:
+			_bufShift++;
+			break;
+		case 1:
+			break;
+		default:
+			fprintf(stderr, "Unsupported channel count: %d\n",
+					cfg.channels);
+			return false;
+	}
+	err = snd_pcm_hw_params_set_channels(_audioHandle, hw,
+			cfg.channels);
+	if (err < 0) {
+		fprintf(stderr, "Cannot set channel count: %s\n",
+				snd_strerror(err));
+		return false;
+	}
+
+	err = snd_pcm_hw_params(_audioHandle, hw);
+	if (err < 0) {
+		fprintf(stdout, "Cannot set parameters: %s\n",
+				snd_strerror(err));
+		return false;
+	}
 
-    tmpCfg.bufSize = setup.buf.block.frag_size;
+	return true;
+}
+
+
+
+void *
+Audio_ALSA::open(AudioConfig &cfg, const char *name)
+{
+	AudioConfig tmpCfg = cfg;
+	snd_pcm_hw_params_t *hw;
+	int err;
+
+	if (_audioHandle != NULL) {
+		_errorString = "ERROR: Device already in use";
+		return NULL;
+	}
+
+	err = snd_pcm_open(&_audioHandle, ALSA_DEFAULT,
+			SND_PCM_STREAM_PLAYBACK, 0);
+	if (err < 0) {
+		_errorString = "ERROR: Could not open audio device.";
+		fprintf(stderr, "Cannot open audio device: %s\n",
+				snd_strerror(err));
+		return false;
+	}
+
+	err = snd_pcm_hw_params_malloc(&hw);
+	if (err < 0) {
+		_errorString = "ERROR: Cannot initialise ALSA";
+		fprintf(stderr, "Cannot allocate hardware "
+				"parameter structure: %s\n",
+				snd_strerror(err));
+		return false;
+	}
+
+	bool init_ok = init(tmpCfg, hw);
+	snd_pcm_hw_params_free(hw);
+	if (! init_ok) {
+		_errorString = "ERROR: Cannot initialise ALSA";
+		return false;
+	}
+
+	err = snd_pcm_prepare(_audioHandle);
+	if (err < 0) {
+		_errorString = "Cannot initialise ALSA";
+		fprintf(stderr, "cannot prepare audio interface for use: %s\n",
+				snd_strerror(err));
+		return false;
+	}
+
+	tmpCfg.bufSize = 4096;
 #ifdef HAVE_EXCEPTIONS
-    _sampleBuffer = new(std::nothrow) int_least8_t[tmpCfg.bufSize];
+	_sampleBuffer = new(std::nothrow) int_least8_t[tmpCfg.bufSize];
 #else
-    _sampleBuffer = new int_least8_t[tmpCfg.bufSize];
+	_sampleBuffer = new int_least8_t[tmpCfg.bufSize];
 #endif
 
-    if (!_sampleBuffer)
-    {
-        _errorString = "AUDIO: Unable to allocate memory for sample buffers.";
-        goto open_error;
-    }
-
-    // Setup internal Config
-    _settings = tmpCfg;
-    // Update the users settings
-    getConfig (cfg);
-    return _sampleBuffer;
-
-open_error:
-    if (_audioHandle != NULL)
-    {
-        close ();
-    }
-
-    perror ("ALSA");
-return NULL;
+	if (!_sampleBuffer) {
+		_errorString = "Unable to allocate memory for sample buffers";
+		return NULL;
+	}
+
+	// Setup internal Config
+	_settings = tmpCfg;
+	// Update the users settings
+	getConfig (cfg);
+	return _sampleBuffer;
 }
 
+
+
 // Close an opened audio device, free any allocated buffers and
 // reset any variables that reflect the current state.
-void Audio_ALSA::close ()
+void
+Audio_ALSA::close()
 {
-    if (_audioHandle != NULL )
-    {
-        snd_pcm_close(_audioHandle);
-        delete [] _sampleBuffer;
-        outOfOrder ();
-    }
+	if (_audioHandle != NULL) {
+		snd_pcm_close(_audioHandle);
+		delete [] _sampleBuffer;
+		outOfOrder ();
+	}
 }
 
-void *Audio_ALSA::reset ()
+
+
+void *
+Audio_ALSA::reset ()
 {
-    return (void *) _sampleBuffer;   
+	return (void *) _sampleBuffer;   
 }
 
-void *Audio_ALSA::write ()
-{
-    if (_audioHandle == NULL)
-    {
-        _errorString = "ERROR: Device not open.";
-        return NULL;
-    }
 
-    snd_pcm_plugin_write (_audioHandle, _sampleBuffer, _settings.bufSize);
-    return (void *) _sampleBuffer;
+
+void *
+Audio_ALSA::write()
+{
+	if (_audioHandle == NULL)
+	{
+		_errorString = "ERROR: Device not open.";
+		return NULL;
+	}
+
+	if (_swapEndian) {
+		for (int n = 0; n < _settings.bufSize; n += 2) {
+			char t = ((char *) _sampleBuffer)[n];
+			((char *) _sampleBuffer)[n] =
+				((char *) _sampleBuffer)[n + 1];
+			((char *) _sampleBuffer)[n + 1] = t;
+		}
+	}
+	snd_pcm_writei(_audioHandle, _sampleBuffer,
+			_settings.bufSize >> _bufShift);
+	return (void *) _sampleBuffer;
 }
 
 #endif // HAVE_ALSA
diff -Naur sidplay-2.0.9.ORIG/src/audio/alsa/audiodrv.h sidplay-2.0.9/src/audio/alsa/audiodrv.h
--- sidplay-2.0.9.ORIG/src/audio/alsa/audiodrv.h	2002-01-11 06:04:00.000000000 +1100
+++ sidplay-2.0.9/src/audio/alsa/audiodrv.h	2011-03-18 14:14:43.000000000 +1100
@@ -3,6 +3,9 @@
 // --------------------------------------------------------------------------
 /***************************************************************************
  *  $Log: audiodrv.h,v $
+ *  Revision 1.6  2008/07/19 13:58:04  wnd
+ *  Update to new ALSA API.
+ *
  *  Revision 1.5  2002/01/10 19:04:00  s_a_white
  *  Interface changes for audio drivers.
  *
@@ -36,7 +39,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
-#include <sys/asoundlib.h>
+#include <alsa/asoundlib.h>
 #include "../AudioBase.h"
 
 
@@ -44,6 +47,8 @@
 {	
 private:  // ------------------------------------------------------- private
     snd_pcm_t * _audioHandle;
+    bool _swapEndian;
+    int _bufShift;
 
     void outOfOrder ();
 
@@ -51,6 +56,8 @@
     Audio_ALSA();
     ~Audio_ALSA();
 
+    bool  fmt   (AudioConfig &cfg, snd_pcm_hw_params_t *hw);
+    bool  init  (AudioConfig &cfg, snd_pcm_hw_params_t *hw);
     void *open  (AudioConfig &cfg, const char *name);
     void  close ();
     // Rev 1.2 (saw) - Changed, see AudioBase.h