src/audio/alsa/SDL_alsa_audio.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 12:38:15 +0000
changeset 252 e8157fcb3114
parent 0 74212992fb08
child 297 f6ffac90895c
permissions -rw-r--r--
Updated the source with the correct e-mail address
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
slouken@0
    24
slouken@0
    25
/* Allow access to a raw mixing buffer */
slouken@0
    26
slouken@0
    27
#include <stdlib.h>
slouken@0
    28
#include <stdio.h>
slouken@0
    29
#include <string.h>
slouken@0
    30
#include <errno.h>
slouken@0
    31
#include <unistd.h>
slouken@0
    32
#include <fcntl.h>
slouken@0
    33
#include <signal.h>
slouken@0
    34
#include <sys/types.h>
slouken@0
    35
#include <sys/time.h>
slouken@0
    36
slouken@0
    37
#include "SDL_audio.h"
slouken@0
    38
#include "SDL_error.h"
slouken@0
    39
#include "SDL_audiomem.h"
slouken@0
    40
#include "SDL_audio_c.h"
slouken@0
    41
#include "SDL_timer.h"
slouken@0
    42
#include "SDL_alsa_audio.h"
slouken@0
    43
slouken@0
    44
/* The tag name used by ALSA audio */
slouken@0
    45
#define DRIVER_NAME         "alsa"
slouken@0
    46
slouken@0
    47
/* default card and device numbers as listed in dev/snd */
slouken@0
    48
static int card_no = 0;
slouken@0
    49
static int device_no = 0;
slouken@0
    50
slouken@0
    51
/* default channel communication parameters */
slouken@0
    52
#define DEFAULT_CPARAMS_RATE 22050
slouken@0
    53
#define DEFAULT_CPARAMS_VOICES 1
slouken@0
    54
#define DEFAULT_CPARAMS_FRAG_SIZE 512
slouken@0
    55
#define DEFAULT_CPARAMS_FRAGS_MIN 1
slouken@0
    56
#define DEFAULT_CPARAMS_FRAGS_MAX -1
slouken@0
    57
slouken@0
    58
/* Open the audio device for playback, and don't block if busy */
slouken@0
    59
#define OPEN_FLAGS	(SND_PCM_OPEN_PLAYBACK|SND_PCM_OPEN_NONBLOCK)
slouken@0
    60
slouken@0
    61
/* Audio driver functions */
slouken@0
    62
static int PCM_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
    63
static void PCM_WaitAudio(_THIS);
slouken@0
    64
static void PCM_PlayAudio(_THIS);
slouken@0
    65
static Uint8 *PCM_GetAudioBuf(_THIS);
slouken@0
    66
static void PCM_CloseAudio(_THIS);
slouken@0
    67
slouken@0
    68
/* PCM transfer channel parameters initialize function */
slouken@0
    69
static void init_pcm_cparams(snd_pcm_channel_params_t* cparams)
slouken@0
    70
{
slouken@0
    71
	memset(cparams,0,sizeof(snd_pcm_channel_params_t));
slouken@0
    72
slouken@0
    73
	cparams->channel = SND_PCM_CHANNEL_PLAYBACK;
slouken@0
    74
	cparams->mode = SND_PCM_MODE_BLOCK;
slouken@0
    75
	cparams->start_mode = SND_PCM_START_DATA; //_FULL
slouken@0
    76
	cparams->stop_mode  = SND_PCM_STOP_STOP;
slouken@0
    77
	cparams->format.format = SND_PCM_SFMT_S16_LE;
slouken@0
    78
	cparams->format.interleave = 1;
slouken@0
    79
	cparams->format.rate = DEFAULT_CPARAMS_RATE;
slouken@0
    80
	cparams->format.voices = DEFAULT_CPARAMS_VOICES;
slouken@0
    81
	cparams->buf.block.frag_size = DEFAULT_CPARAMS_FRAG_SIZE;
slouken@0
    82
	cparams->buf.block.frags_min = DEFAULT_CPARAMS_FRAGS_MIN;
slouken@0
    83
	cparams->buf.block.frags_max = DEFAULT_CPARAMS_FRAGS_MAX;
slouken@0
    84
}
slouken@0
    85
slouken@0
    86
/* Audio driver bootstrap functions */
slouken@0
    87
slouken@0
    88
static int Audio_Available(void)
slouken@0
    89
/*
slouken@0
    90
	See if we can open a nonblocking channel.
slouken@0
    91
	Return value '1' means we can.
slouken@0
    92
	Return value '0' means we cannot.
slouken@0
    93
*/
slouken@0
    94
{
slouken@0
    95
	int available;
slouken@0
    96
	int rval;
slouken@0
    97
	snd_pcm_t *handle;
slouken@0
    98
	snd_pcm_channel_params_t cparams;
slouken@0
    99
#ifdef DEBUG_AUDIO
slouken@0
   100
	snd_pcm_channel_status_t cstatus;
slouken@0
   101
#endif
slouken@0
   102
slouken@0
   103
	available = 0;
slouken@0
   104
	handle = NULL;
slouken@0
   105
slouken@0
   106
	init_pcm_cparams(&cparams);
slouken@0
   107
	
slouken@0
   108
	rval = snd_pcm_open(&handle, card_no, device_no, OPEN_FLAGS);
slouken@0
   109
	if (rval >= 0)
slouken@0
   110
	{
slouken@0
   111
		rval = snd_pcm_plugin_params(handle, &cparams);
slouken@0
   112
slouken@0
   113
#ifdef DEBUG_AUDIO
slouken@0
   114
		snd_pcm_plugin_status(handle, &cstatus);
slouken@0
   115
		printf("status after snd_pcm_plugin_params call = %d\n",cstatus.status);
slouken@0
   116
#endif
slouken@0
   117
		if (rval >= 0)
slouken@0
   118
		{
slouken@0
   119
			available = 1;
slouken@0
   120
		}
slouken@0
   121
		else
slouken@0
   122
		{
slouken@0
   123
	        	SDL_SetError("snd_pcm_channel_params failed: %s\n", snd_strerror (rval));
slouken@0
   124
		}
slouken@0
   125
slouken@0
   126
        if ((rval = snd_pcm_close(handle)) < 0)
slouken@0
   127
        {
slouken@0
   128
            SDL_SetError("snd_pcm_close failed: %s\n",snd_strerror(rval));
slouken@0
   129
			available = 0;
slouken@0
   130
        }
slouken@0
   131
	}
slouken@0
   132
	else
slouken@0
   133
	{
slouken@0
   134
       SDL_SetError("snd_pcm_open failed: %s\n", snd_strerror(rval));
slouken@0
   135
	}
slouken@0
   136
slouken@0
   137
	return(available);
slouken@0
   138
}
slouken@0
   139
slouken@0
   140
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
   141
{
slouken@0
   142
	free(device->hidden);
slouken@0
   143
	free(device);
slouken@0
   144
}
slouken@0
   145
slouken@0
   146
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
   147
{
slouken@0
   148
	SDL_AudioDevice *this;
slouken@0
   149
slouken@0
   150
	/* Initialize all variables that we clean on shutdown */
slouken@0
   151
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
   152
	if ( this ) {
slouken@0
   153
		memset(this, 0, (sizeof *this));
slouken@0
   154
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
   155
				malloc((sizeof *this->hidden));
slouken@0
   156
	}
slouken@0
   157
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
   158
		SDL_OutOfMemory();
slouken@0
   159
		if ( this ) {
slouken@0
   160
			free(this);
slouken@0
   161
		}
slouken@0
   162
		return(0);
slouken@0
   163
	}
slouken@0
   164
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   165
	audio_handle = NULL;
slouken@0
   166
slouken@0
   167
	/* Set the function pointers */
slouken@0
   168
	this->OpenAudio = PCM_OpenAudio;
slouken@0
   169
	this->WaitAudio = PCM_WaitAudio;
slouken@0
   170
	this->PlayAudio = PCM_PlayAudio;
slouken@0
   171
	this->GetAudioBuf = PCM_GetAudioBuf;
slouken@0
   172
	this->CloseAudio = PCM_CloseAudio;
slouken@0
   173
slouken@0
   174
	this->free = Audio_DeleteDevice;
slouken@0
   175
slouken@0
   176
	return this;
slouken@0
   177
}
slouken@0
   178
slouken@0
   179
AudioBootStrap ALSA_bootstrap = {
slouken@0
   180
	DRIVER_NAME, "ALSA PCM audio",
slouken@0
   181
	Audio_Available, Audio_CreateDevice
slouken@0
   182
};
slouken@0
   183
slouken@0
   184
/* This function waits until it is possible to write a full sound buffer */
slouken@0
   185
static void PCM_WaitAudio(_THIS)
slouken@0
   186
{
slouken@0
   187
slouken@0
   188
	/* Check to see if the thread-parent process is still alive */
slouken@0
   189
	{ static int cnt = 0;
slouken@0
   190
		/* Note that this only works with thread implementations 
slouken@0
   191
		   that use a different process id for each thread.
slouken@0
   192
		*/
slouken@0
   193
		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
slouken@0
   194
			if ( kill(parent, 0) < 0 ) {
slouken@0
   195
				this->enabled = 0;
slouken@0
   196
			}
slouken@0
   197
		}
slouken@0
   198
	}
slouken@0
   199
slouken@0
   200
	/* See if we need to use timed audio synchronization */
slouken@0
   201
	if ( frame_ticks ) 
slouken@0
   202
	{
slouken@0
   203
		/* Use timer for general audio synchronization */
slouken@0
   204
		Sint32 ticks;
slouken@0
   205
slouken@0
   206
		ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
slouken@0
   207
		if ( ticks > 0 ) 
slouken@0
   208
		{
slouken@0
   209
			SDL_Delay(ticks);
slouken@0
   210
		}
slouken@0
   211
	}
slouken@0
   212
    else 
slouken@0
   213
	{
slouken@0
   214
    	/* Use select() for audio synchronization */
slouken@0
   215
		fd_set fdset;
slouken@0
   216
	    struct timeval timeout;
slouken@0
   217
    	FD_ZERO(&fdset);
slouken@0
   218
	    FD_SET(audio_fd, &fdset);
slouken@0
   219
    	timeout.tv_sec = 10;
slouken@0
   220
	    timeout.tv_usec = 0;
slouken@0
   221
#ifdef DEBUG_AUDIO
slouken@0
   222
    	fprintf(stderr, "Waiting for audio to get ready\n");
slouken@0
   223
#endif
slouken@0
   224
	    if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) 
slouken@0
   225
		{
slouken@0
   226
            const char *message =
slouken@0
   227
            "Audio timeout - buggy audio driver? (disabled)";
slouken@0
   228
	        /* In general we should never print to the screen,
slouken@0
   229
       	       but in this case we have no other way of letting
slouken@0
   230
               the user know what happened.
slouken@0
   231
            */
slouken@0
   232
       	    fprintf(stderr, "SDL: %s\n", message);
slouken@0
   233
   	        this->enabled = 0;
slouken@0
   234
            /* Don't try to close - may hang */
slouken@0
   235
            audio_fd = -1;
slouken@0
   236
#ifdef DEBUG_AUDIO
slouken@0
   237
       	    fprintf(stderr, "Done disabling audio\n");
slouken@0
   238
#endif
slouken@0
   239
    	    }
slouken@0
   240
#ifdef DEBUG_AUDIO
slouken@0
   241
        fprintf(stderr, "Ready!\n");
slouken@0
   242
#endif
slouken@0
   243
    }
slouken@0
   244
}
slouken@0
   245
slouken@0
   246
static snd_pcm_channel_status_t cstatus;
slouken@0
   247
slouken@0
   248
static void PCM_PlayAudio(_THIS)
slouken@0
   249
{
slouken@0
   250
    int written, rval;
slouken@0
   251
slouken@0
   252
    /* Write the audio data, checking for EAGAIN (buffer full) and underrun */
slouken@0
   253
    do {
slouken@0
   254
		written = snd_pcm_plugin_write(audio_handle, pcm_buf, pcm_len);
slouken@0
   255
#ifdef DEBUG_AUDIO
slouken@0
   256
		fprintf(stderr, "written = %d pcm_len = %d\n",written,pcm_len);
slouken@0
   257
#endif
slouken@0
   258
		if (written != pcm_len)
slouken@0
   259
		{
slouken@0
   260
	        if (errno == EAGAIN) 
slouken@0
   261
			{
slouken@0
   262
            	SDL_Delay(1);   /* Let a little CPU time go by and try to write again */
slouken@0
   263
#ifdef DEBUG_AUDIO
slouken@0
   264
				fprintf(stderr, "errno == EAGAIN\n");
slouken@0
   265
#endif
slouken@0
   266
        	}
slouken@0
   267
			else
slouken@0
   268
			{
slouken@0
   269
		        if( (rval = snd_pcm_plugin_status(audio_handle, &cstatus)) < 0 )
slouken@0
   270
        		{
slouken@0
   271
		            SDL_SetError("snd_pcm_plugin_status failed: %s\n", snd_strerror(rval));
slouken@0
   272
        		    return;
slouken@0
   273
		        }
slouken@0
   274
				if ( (cstatus.status == SND_PCM_STATUS_UNDERRUN)
slouken@0
   275
					||(cstatus.status == SND_PCM_STATUS_READY) )
slouken@0
   276
				{
slouken@0
   277
#ifdef DEBUG_AUDIO
slouken@0
   278
					fprintf(stderr, "buffer underrun\n");
slouken@0
   279
#endif
slouken@0
   280
					if ( (rval = snd_pcm_plugin_prepare (audio_handle,SND_PCM_CHANNEL_PLAYBACK)) < 0 )
slouken@0
   281
					{
slouken@0
   282
						SDL_SetError("snd_pcm_plugin_prepare failed: %s\n",snd_strerror(rval) );
slouken@0
   283
						return;
slouken@0
   284
					}
slouken@0
   285
					/* if we reach here, try to write again */
slouken@0
   286
				}
slouken@0
   287
			}
slouken@0
   288
		}
slouken@0
   289
    } while ( (written < 0) && ((errno == 0) || (errno == EAGAIN)) );
slouken@0
   290
slouken@0
   291
    /* Set the next write frame */
slouken@0
   292
   if ( frame_ticks ) {
slouken@0
   293
	    next_frame += frame_ticks;
slouken@0
   294
	}
slouken@0
   295
slouken@0
   296
    /* If we couldn't write, assume fatal error for now */
slouken@0
   297
    if ( written < 0 ) {
slouken@0
   298
        this->enabled = 0;
slouken@0
   299
    }
slouken@0
   300
	return;
slouken@0
   301
}
slouken@0
   302
slouken@0
   303
static Uint8 *PCM_GetAudioBuf(_THIS)
slouken@0
   304
{
slouken@0
   305
	return(pcm_buf);
slouken@0
   306
}
slouken@0
   307
slouken@0
   308
static void PCM_CloseAudio(_THIS)
slouken@0
   309
{
slouken@0
   310
	int rval;
slouken@0
   311
slouken@0
   312
	if ( pcm_buf != NULL ) {
slouken@0
   313
		free(pcm_buf);
slouken@0
   314
		pcm_buf = NULL;
slouken@0
   315
	}
slouken@0
   316
	if ( audio_handle != NULL ) {
slouken@0
   317
		if ((rval = snd_pcm_plugin_flush(audio_handle,SND_PCM_CHANNEL_PLAYBACK)) < 0)
slouken@0
   318
		{
slouken@0
   319
        	SDL_SetError("snd_pcm_plugin_flush failed: %s\n",snd_strerror(rval));
slouken@0
   320
			return;
slouken@0
   321
		}
slouken@0
   322
		if ((rval = snd_pcm_close(audio_handle)) < 0)
slouken@0
   323
		{
slouken@0
   324
			SDL_SetError("snd_pcm_close failed: %s\n",snd_strerror(rval));
slouken@0
   325
			return;
slouken@0
   326
		}
slouken@0
   327
		audio_handle = NULL;
slouken@0
   328
	}
slouken@0
   329
}
slouken@0
   330
slouken@0
   331
static int PCM_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   332
{
slouken@0
   333
	int rval;
slouken@0
   334
	snd_pcm_channel_params_t cparams;
slouken@0
   335
	snd_pcm_channel_setup_t  csetup;
slouken@0
   336
	int format;
slouken@0
   337
	Uint16 test_format;
slouken@0
   338
	int twidth;
slouken@0
   339
slouken@0
   340
	/* initialize channel transfer parameters to default */
slouken@0
   341
	init_pcm_cparams(&cparams);
slouken@0
   342
slouken@0
   343
	/* Reset the timer synchronization flag */
slouken@0
   344
	frame_ticks = 0.0;
slouken@0
   345
slouken@0
   346
	/* Open the audio device */
slouken@0
   347
	
slouken@0
   348
	rval = snd_pcm_open(&audio_handle, card_no, device_no, OPEN_FLAGS);
slouken@0
   349
	if ( rval < 0 ) {
slouken@0
   350
		SDL_SetError("snd_pcm_open failed: %s\n", snd_strerror(rval));
slouken@0
   351
		return(-1);
slouken@0
   352
	}
slouken@0
   353
slouken@0
   354
#ifdef PLUGIN_DISABLE_MMAP /* This is gone in newer versions of ALSA? */
slouken@0
   355
    /* disable count status parameter */
slouken@0
   356
    if ((rval = snd_plugin_set_disable(audio_handle, PLUGIN_DISABLE_MMAP))<0)
slouken@0
   357
    {
slouken@0
   358
        SDL_SetError("snd_plugin_set_disable failed: %s\n", snd_strerror(rval));
slouken@0
   359
        return(-1);
slouken@0
   360
    }
slouken@0
   361
#endif
slouken@0
   362
slouken@0
   363
	pcm_buf = NULL;
slouken@0
   364
slouken@0
   365
	/* Try for a closest match on audio format */
slouken@0
   366
	format = 0;
slouken@0
   367
	for ( test_format = SDL_FirstAudioFormat(spec->format);
slouken@0
   368
						! format && test_format; ) 
slouken@0
   369
	{
slouken@0
   370
#ifdef DEBUG_AUDIO
slouken@0
   371
		fprintf(stderr, "Trying format 0x%4.4x spec->samples %d\n", test_format,spec->samples);
slouken@0
   372
#endif
slouken@0
   373
			/* if match found set format to equivalent ALSA format */
slouken@0
   374
        switch ( test_format ) {
slouken@0
   375
			case AUDIO_U8:
slouken@0
   376
				format = SND_PCM_SFMT_U8;
slouken@0
   377
				cparams.buf.block.frag_size = spec->samples * spec->channels;
slouken@0
   378
				break;
slouken@0
   379
			case AUDIO_S8:
slouken@0
   380
				format = SND_PCM_SFMT_S8;
slouken@0
   381
				cparams.buf.block.frag_size = spec->samples * spec->channels;
slouken@0
   382
				break;
slouken@0
   383
			case AUDIO_S16LSB:
slouken@0
   384
				format = SND_PCM_SFMT_S16_LE;
slouken@0
   385
				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
slouken@0
   386
				break;
slouken@0
   387
			case AUDIO_S16MSB:
slouken@0
   388
				format = SND_PCM_SFMT_S16_BE;
slouken@0
   389
				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
slouken@0
   390
				break;
slouken@0
   391
			case AUDIO_U16LSB:
slouken@0
   392
				format = SND_PCM_SFMT_U16_LE;
slouken@0
   393
				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
slouken@0
   394
				break;
slouken@0
   395
			case AUDIO_U16MSB:
slouken@0
   396
				format = SND_PCM_SFMT_U16_BE;
slouken@0
   397
				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
slouken@0
   398
				break;
slouken@0
   399
			default:
slouken@0
   400
				break;
slouken@0
   401
		}
slouken@0
   402
		if ( ! format ) {
slouken@0
   403
			test_format = SDL_NextAudioFormat();
slouken@0
   404
		}
slouken@0
   405
	}
slouken@0
   406
	if ( format == 0 ) {
slouken@0
   407
		SDL_SetError("Couldn't find any hardware audio formats");
slouken@0
   408
		return(-1);
slouken@0
   409
	}
slouken@0
   410
	spec->format = test_format;
slouken@0
   411
slouken@0
   412
	/* Set the audio format */
slouken@0
   413
	cparams.format.format = format;
slouken@0
   414
slouken@0
   415
	/* Set mono or stereo audio (currently only two channels supported) */
slouken@0
   416
	cparams.format.voices = spec->channels;
slouken@0
   417
	
slouken@0
   418
	#ifdef DEBUG_AUDIO
slouken@0
   419
	printf("intializing channels %d\n", cparams.format.voices);
slouken@0
   420
	#endif
slouken@0
   421
	
slouken@0
   422
	/* Set rate */
slouken@0
   423
	cparams.format.rate = spec->freq ;
slouken@0
   424
slouken@0
   425
	/* Setup the transfer parameters according to cparams */
slouken@0
   426
	rval = snd_pcm_plugin_params(audio_handle, &cparams);
slouken@0
   427
	if (rval < 0) {
slouken@0
   428
		SDL_SetError("snd_pcm_channel_params failed: %s\n", snd_strerror (rval));
slouken@0
   429
		return(-1);
slouken@0
   430
	}
slouken@0
   431
slouken@0
   432
    /*  Make sure channel is setup right one last time */
slouken@0
   433
    memset( &csetup, 0, sizeof( csetup ) );
slouken@0
   434
    csetup.channel = SND_PCM_CHANNEL_PLAYBACK;
slouken@0
   435
    if ( snd_pcm_plugin_setup( audio_handle, &csetup ) < 0 )
slouken@0
   436
    {
slouken@0
   437
        SDL_SetError("Unable to setup playback channel\n" );
slouken@0
   438
        return(-1);
slouken@0
   439
    }
slouken@0
   440
slouken@0
   441
#ifdef DEBUG_AUDIO
slouken@0
   442
    else
slouken@0
   443
    {
slouken@0
   444
        fprintf(stderr,"requested format: %d\n",cparams.format.format);
slouken@0
   445
        fprintf(stderr,"requested frag size: %d\n",cparams.buf.block.frag_size);
slouken@0
   446
        fprintf(stderr,"requested max frags: %d\n\n",cparams.buf.block.frags_max);
slouken@0
   447
slouken@0
   448
        fprintf(stderr,"real format: %d\n", csetup.format.format );
slouken@0
   449
        fprintf(stderr,"real frag size : %d\n", csetup.buf.block.frag_size );
slouken@0
   450
		fprintf(stderr,"real max frags : %d\n", csetup.buf.block.frags_max );
slouken@0
   451
    }
slouken@0
   452
#endif // DEBUG_AUDIO
slouken@0
   453
slouken@0
   454
    /*  Allocate memory to the audio buffer and initialize with silence
slouken@0
   455
        (Note that buffer size must be a multiple of fragment size, so find closest multiple)
slouken@0
   456
    */
slouken@0
   457
    
slouken@0
   458
    twidth = snd_pcm_format_width(format);
slouken@0
   459
    if (twidth < 0) {
slouken@0
   460
        printf("snd_pcm_format_width failed\n");
slouken@0
   461
        twidth = 0;
slouken@0
   462
    }
slouken@0
   463
#ifdef DEBUG_AUDIO
slouken@0
   464
    printf("format is %d bits wide\n",twidth);
slouken@0
   465
#endif      
slouken@0
   466
    
slouken@0
   467
    pcm_len = csetup.buf.block.frag_size * (twidth/8) * csetup.format.voices ;
slouken@0
   468
    
slouken@0
   469
#ifdef DEBUG_AUDIO    
slouken@0
   470
    printf("pcm_len set to %d\n", pcm_len);
slouken@0
   471
#endif
slouken@0
   472
    
slouken@0
   473
    if (pcm_len == 0)
slouken@0
   474
    {
slouken@0
   475
        pcm_len = csetup.buf.block.frag_size;
slouken@0
   476
    }
slouken@0
   477
    
slouken@0
   478
    pcm_buf = (Uint8*)malloc(pcm_len);
slouken@0
   479
    if (pcm_buf == NULL) {
slouken@0
   480
        SDL_SetError("pcm_buf malloc failed\n");
slouken@0
   481
        return(-1);
slouken@0
   482
    }
slouken@0
   483
    memset(pcm_buf,spec->silence,pcm_len);
slouken@0
   484
slouken@0
   485
#ifdef DEBUG_AUDIO
slouken@0
   486
	fprintf(stderr,"pcm_buf malloced and silenced.\n");
slouken@0
   487
#endif
slouken@0
   488
slouken@0
   489
    /* get the file descriptor */
slouken@0
   490
    if( (audio_fd = snd_pcm_file_descriptor(audio_handle, device_no)) < 0)
slouken@0
   491
    {
slouken@0
   492
       fprintf(stderr, "snd_pcm_file_descriptor failed with error code: %d\n", audio_fd);
slouken@0
   493
    }
slouken@0
   494
slouken@0
   495
	/* Trigger audio playback */
slouken@0
   496
	rval = snd_pcm_plugin_prepare( audio_handle, SND_PCM_CHANNEL_PLAYBACK);
slouken@0
   497
	if (rval < 0) {
slouken@0
   498
       SDL_SetError("snd_pcm_plugin_prepare failed: %s\n", snd_strerror (rval));
slouken@0
   499
       return(-1);
slouken@0
   500
	}
slouken@0
   501
	rval =  snd_pcm_playback_go(audio_handle);
slouken@0
   502
    if (rval < 0) {
slouken@0
   503
       SDL_SetError("snd_pcm_playback_go failed: %s\n", snd_strerror (rval));
slouken@0
   504
       return(-1);
slouken@0
   505
    }
slouken@0
   506
slouken@0
   507
    /* Check to see if we need to use select() workaround */
slouken@0
   508
    { char *workaround;
slouken@0
   509
        workaround = getenv("SDL_DSP_NOSELECT");
slouken@0
   510
        if ( workaround ) {
slouken@0
   511
            frame_ticks = (float)(spec->samples*1000)/spec->freq;
slouken@0
   512
            next_frame = SDL_GetTicks()+frame_ticks;
slouken@0
   513
        }
slouken@0
   514
    }
slouken@0
   515
slouken@0
   516
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   517
	parent = getpid();
slouken@0
   518
slouken@0
   519
	/* We're ready to rock and roll. :-) */
slouken@0
   520
	return(0);
slouken@0
   521
}