src/audio/nto/SDL_nto_audio.c
author Sam Lantinga
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 252 e8157fcb3114
permissions -rw-r--r--
Initial revision
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@0
    20
    slouken@devolution.com
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
#include <sched.h>
slouken@0
    37
#include <sys/asoundlib.h>
slouken@0
    38
slouken@0
    39
#include "SDL_audio.h"
slouken@0
    40
#include "SDL_error.h"
slouken@0
    41
#include "SDL_audiomem.h"
slouken@0
    42
#include "SDL_audio_c.h"
slouken@0
    43
#include "SDL_timer.h"
slouken@0
    44
#include "SDL_nto_audio.h"
slouken@0
    45
slouken@0
    46
/* The tag name used by NTO audio */
slouken@0
    47
#define DRIVER_NAME         "nto"
slouken@0
    48
slouken@0
    49
/* default card and device numbers as listed in dev/snd */
slouken@0
    50
static int card_no = 0;
slouken@0
    51
static int device_no = 0;
slouken@0
    52
slouken@0
    53
/* default channel communication parameters */
slouken@0
    54
#define DEFAULT_CPARAMS_RATE 22050
slouken@0
    55
#define DEFAULT_CPARAMS_VOICES 1
slouken@0
    56
#define DEFAULT_CPARAMS_FRAG_SIZE 4096  //was 512
slouken@0
    57
#define DEFAULT_CPARAMS_FRAGS_MIN 1
slouken@0
    58
#define DEFAULT_CPARAMS_FRAGS_MAX -1
slouken@0
    59
slouken@0
    60
/* Open the audio device for playback, and don't block if busy */
slouken@0
    61
#define OPEN_FLAGS	SND_PCM_OPEN_PLAYBACK
slouken@0
    62
slouken@0
    63
/* Audio driver functions */
slouken@0
    64
static int NTO_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
    65
static void NTO_WaitAudio(_THIS);
slouken@0
    66
static void NTO_PlayAudio(_THIS);
slouken@0
    67
static Uint8 *NTO_GetAudioBuf(_THIS);
slouken@0
    68
static void NTO_CloseAudio(_THIS);
slouken@0
    69
slouken@0
    70
static snd_pcm_channel_status_t cstatus;
slouken@0
    71
static	snd_pcm_channel_params_t cparams;
slouken@0
    72
static	snd_pcm_channel_setup_t  csetup;
slouken@0
    73
slouken@0
    74
/* PCM transfer channel parameters initialize function */
slouken@0
    75
static void init_pcm_cparams(snd_pcm_channel_params_t* cparams)
slouken@0
    76
{
slouken@0
    77
	memset(cparams,0,sizeof(snd_pcm_channel_params_t));
slouken@0
    78
slouken@0
    79
	cparams->channel = SND_PCM_CHANNEL_PLAYBACK;
slouken@0
    80
	cparams->mode = SND_PCM_MODE_BLOCK;
slouken@0
    81
	cparams->start_mode = SND_PCM_START_DATA; //_FULL
slouken@0
    82
	cparams->stop_mode  = SND_PCM_STOP_STOP;
slouken@0
    83
	cparams->format.format = SND_PCM_SFMT_S16_LE;
slouken@0
    84
	cparams->format.interleave = 1;
slouken@0
    85
	cparams->format.rate = DEFAULT_CPARAMS_RATE;
slouken@0
    86
	cparams->format.voices = DEFAULT_CPARAMS_VOICES;
slouken@0
    87
	cparams->buf.block.frag_size = DEFAULT_CPARAMS_FRAG_SIZE;
slouken@0
    88
	cparams->buf.block.frags_min = DEFAULT_CPARAMS_FRAGS_MIN;
slouken@0
    89
	cparams->buf.block.frags_max = DEFAULT_CPARAMS_FRAGS_MAX;
slouken@0
    90
}
slouken@0
    91
slouken@0
    92
/* Audio driver bootstrap functions */
slouken@0
    93
slouken@0
    94
static int Audio_Available(void)
slouken@0
    95
/*
slouken@0
    96
	See if we can open a nonblocking channel.
slouken@0
    97
	Return value '1' means we can.
slouken@0
    98
	Return value '0' means we cannot.
slouken@0
    99
*/
slouken@0
   100
{
slouken@0
   101
	int available;
slouken@0
   102
	int rval;
slouken@0
   103
	snd_pcm_t *handle;
slouken@0
   104
slouken@0
   105
	available = 0;
slouken@0
   106
	handle = NULL;
slouken@0
   107
	
slouken@0
   108
	//JB modified to take advantage of software mixer
slouken@0
   109
	rval = snd_pcm_open_preferred(&handle, &card_no, &device_no, OPEN_FLAGS);
slouken@0
   110
slouken@0
   111
	if (rval >= 0)
slouken@0
   112
	{
slouken@0
   113
			available = 1;
slouken@0
   114
slouken@0
   115
        if ((rval = snd_pcm_close(handle)) < 0)
slouken@0
   116
        {
slouken@0
   117
            SDL_SetError("snd_pcm_close failed: %s\n",snd_strerror(rval));
slouken@0
   118
			available = 0;
slouken@0
   119
        }
slouken@0
   120
	}
slouken@0
   121
	else
slouken@0
   122
	{
slouken@0
   123
	
slouken@0
   124
       SDL_SetError("snd_pcm_open failed: %s\n", snd_strerror(rval));
slouken@0
   125
	}
slouken@0
   126
	
slouken@0
   127
#ifdef DEBUG_AUDIO
slouken@0
   128
	fprintf(stderr,"AudioAvailable rtns %d\n", available);
slouken@0
   129
#endif
slouken@0
   130
	return(available);
slouken@0
   131
}
slouken@0
   132
slouken@0
   133
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
   134
{
slouken@0
   135
#ifdef DEBUG_AUDIO
slouken@0
   136
	fprintf(stderr,"Audio_DeleteDevice\n");
slouken@0
   137
#endif
slouken@0
   138
slouken@0
   139
	
slouken@0
   140
	free(device->hidden);
slouken@0
   141
	free(device);
slouken@0
   142
}
slouken@0
   143
slouken@0
   144
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
   145
{
slouken@0
   146
	SDL_AudioDevice *this;
slouken@0
   147
#ifdef DEBUG_AUDIO
slouken@0
   148
	fprintf(stderr,"Audio_CreateDevice\n");
slouken@0
   149
#endif
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 = NTO_OpenAudio;
slouken@0
   169
	this->WaitAudio = NTO_WaitAudio;
slouken@0
   170
	this->PlayAudio = NTO_PlayAudio;
slouken@0
   171
	this->GetAudioBuf = NTO_GetAudioBuf;
slouken@0
   172
	this->CloseAudio = NTO_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
/* Don't change the name from "ALSA_bootstrap" - that's how it's called */
slouken@0
   180
AudioBootStrap ALSA_bootstrap = {
slouken@0
   181
	DRIVER_NAME, "Neutrino PCM audio",
slouken@0
   182
	Audio_Available, Audio_CreateDevice
slouken@0
   183
};
slouken@0
   184
slouken@0
   185
/* This function waits until it is possible to write a full sound buffer */
slouken@0
   186
static void NTO_WaitAudio(_THIS)
slouken@0
   187
{
slouken@0
   188
	int rval;
slouken@0
   189
	int totalbytes,roomavail;
slouken@0
   190
	/*we consider a full sound buffer to be of size pcm_len bytes */
slouken@0
   191
	
slouken@0
   192
#ifdef DEBUG_AUDIO
slouken@0
   193
	fprintf(stderr,"NTO_WaitAudio\n");
slouken@0
   194
#endif
slouken@0
   195
slouken@0
   196
	while(1)
slouken@0
   197
	{
slouken@0
   198
	memset(&cstatus, 0, sizeof(cstatus));
slouken@0
   199
	 if( (rval = snd_pcm_plugin_status(audio_handle, &cstatus)) < 0 )
slouken@0
   200
    {
slouken@0
   201
		 SDL_SetError("snd_pcm_plugin_status failed: %s\n", snd_strerror(rval));
slouken@0
   202
   	    return;
slouken@0
   203
	}	
slouken@0
   204
	
slouken@0
   205
	totalbytes = csetup.buf.block.frag_size *csetup.buf.block.frags;
slouken@0
   206
	roomavail = totalbytes - cstatus.count;
slouken@0
   207
slouken@0
   208
#ifdef DEBUG_AUDIO
slouken@0
   209
	fprintf(stderr,"NTO_WaitAudio roomavail %d pcm_len %d\n",roomavail,pcm_len);
slouken@0
   210
#endif
slouken@0
   211
	
slouken@0
   212
	if ((roomavail >= pcm_len) || (roomavail < 0))
slouken@0
   213
		return;
slouken@0
   214
		
slouken@0
   215
		SDL_Delay(10);	
slouken@0
   216
	}	
slouken@0
   217
      
slouken@0
   218
}
slouken@0
   219
slouken@0
   220
slouken@0
   221
slouken@0
   222
static void NTO_PlayAudio(_THIS)
slouken@0
   223
{
slouken@0
   224
    int written, rval;
slouken@0
   225
    int towrite;
slouken@0
   226
slouken@0
   227
#ifdef DEBUG_AUDIO
slouken@0
   228
		fprintf(stderr, "NTO_PlayAudio\n");
slouken@0
   229
#endif
slouken@0
   230
slouken@0
   231
	if( !this->enabled)
slouken@0
   232
	   return;
slouken@0
   233
slouken@0
   234
	towrite = pcm_len;
slouken@0
   235
	
slouken@0
   236
slouken@0
   237
    /* Write the audio data, checking for EAGAIN (buffer full) and underrun */
slouken@0
   238
    do {
slouken@0
   239
		written = snd_pcm_plugin_write(audio_handle, pcm_buf, towrite);
slouken@0
   240
#ifdef DEBUG_AUDIO
slouken@0
   241
		fprintf(stderr, "NTO_PlayAudio: written = %d towrite = %d\n",written,towrite);
slouken@0
   242
#endif
slouken@0
   243
		if (written != towrite)
slouken@0
   244
		{
slouken@0
   245
	        if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
slouken@0
   246
			{
slouken@0
   247
            	SDL_Delay(1);   /* Let a little CPU time go by and try to write again */
slouken@0
   248
#ifdef DEBUG_AUDIO
slouken@0
   249
				fprintf(stderr, "errno == EAGAIN written %d\n", written);
slouken@0
   250
				towrite -= written; //we wrote some data
slouken@0
   251
#endif
slouken@0
   252
				continue;
slouken@0
   253
        	}		
slouken@0
   254
			else if((errno == EINVAL) || (errno == EIO))
slouken@0
   255
			{
slouken@0
   256
#ifdef DEBUG_AUDIO
slouken@0
   257
					if(errno == EIO)
slouken@0
   258
						fprintf(stderr,"snd_pcm_plugin_write failed EIO: %s\n", snd_strerror(written));	
slouken@0
   259
					if(errno == EINVAL)
slouken@0
   260
						fprintf(stderr,"snd_pcm_plugin_write failed EINVAL: %s\n", snd_strerror(written));	
slouken@0
   261
								
slouken@0
   262
#endif			
slouken@0
   263
			
slouken@0
   264
				  memset(&cstatus, 0, sizeof(cstatus));			
slouken@0
   265
		         if( (rval = snd_pcm_plugin_status(audio_handle, &cstatus)) < 0 )
slouken@0
   266
        		 {
slouken@0
   267
#ifdef DEBUG_AUDIO
slouken@0
   268
					fprintf(stderr, "snd_pcm_plugin_status failed %s\n",snd_strerror(rval));
slouken@0
   269
#endif
slouken@0
   270
		            SDL_SetError("snd_pcm_plugin_status failed: %s\n", snd_strerror(rval));
slouken@0
   271
        		   return;
slouken@0
   272
		        }	
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
#ifdef DEBUG_AUDIO
slouken@0
   283
						fprintf(stderr, "NTO_PlayAudio: prepare failed %s\n",snd_strerror(rval));
slouken@0
   284
#endif
slouken@0
   285
						SDL_SetError("snd_pcm_plugin_prepare failed: %s\n",snd_strerror(rval) );
slouken@0
   286
						return;
slouken@0
   287
					}
slouken@0
   288
					
slouken@0
   289
				}		        		
slouken@0
   290
 				continue;
slouken@0
   291
			}
slouken@0
   292
			else
slouken@0
   293
			{
slouken@0
   294
#ifdef DEBUG_AUDIO
slouken@0
   295
						fprintf(stderr, "NTO_PlayAudio: snd_pcm_plugin_write failed unknown errno %d %s\n",errno, snd_strerror(rval));
slouken@0
   296
#endif
slouken@0
   297
				return;
slouken@0
   298
			
slouken@0
   299
			}
slouken@0
   300
			
slouken@0
   301
		}
slouken@0
   302
		else
slouken@0
   303
		{
slouken@0
   304
			towrite -= written; //we wrote all remaining data
slouken@0
   305
		}
slouken@0
   306
    } while ( (towrite > 0)  && (this->enabled) );
slouken@0
   307
slouken@0
   308
    /* If we couldn't write, assume fatal error for now */
slouken@0
   309
    if ( towrite != 0 ) {
slouken@0
   310
        this->enabled = 0;
slouken@0
   311
    }
slouken@0
   312
	return;
slouken@0
   313
}
slouken@0
   314
slouken@0
   315
static Uint8 *NTO_GetAudioBuf(_THIS)
slouken@0
   316
{
slouken@0
   317
 #ifdef DEBUG_AUDIO
slouken@0
   318
		fprintf(stderr, "NTO_GetAudioBuf: pcm_buf %X\n",(Uint8 *)pcm_buf);
slouken@0
   319
#endif
slouken@0
   320
	return(pcm_buf);
slouken@0
   321
}
slouken@0
   322
slouken@0
   323
static void NTO_CloseAudio(_THIS)
slouken@0
   324
{
slouken@0
   325
	int rval;
slouken@0
   326
	
slouken@0
   327
#ifdef DEBUG_AUDIO
slouken@0
   328
		fprintf(stderr, "NTO_CloseAudio\n");
slouken@0
   329
#endif
slouken@0
   330
slouken@0
   331
	 this->enabled = 0;
slouken@0
   332
slouken@0
   333
	if ( audio_handle != NULL ) {
slouken@0
   334
		if ((rval = snd_pcm_plugin_flush(audio_handle,SND_PCM_CHANNEL_PLAYBACK)) < 0)
slouken@0
   335
		{
slouken@0
   336
        	SDL_SetError("snd_pcm_plugin_flush failed: %s\n",snd_strerror(rval));
slouken@0
   337
			return;
slouken@0
   338
		}
slouken@0
   339
		if ((rval = snd_pcm_close(audio_handle)) < 0)
slouken@0
   340
		{
slouken@0
   341
			SDL_SetError("snd_pcm_close failed: %s\n",snd_strerror(rval));
slouken@0
   342
			return;
slouken@0
   343
		}
slouken@0
   344
		audio_handle = NULL;
slouken@0
   345
	}
slouken@0
   346
}
slouken@0
   347
slouken@0
   348
static int NTO_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   349
{
slouken@0
   350
	int rval;
slouken@0
   351
	int format;
slouken@0
   352
	Uint16 test_format;
slouken@0
   353
	int twidth;
slouken@0
   354
	int found;
slouken@0
   355
slouken@0
   356
#ifdef DEBUG_AUDIO
slouken@0
   357
		fprintf(stderr, "NTO_OpenAudio\n");
slouken@0
   358
#endif
slouken@0
   359
	
slouken@0
   360
	audio_handle = NULL;
slouken@0
   361
	 this->enabled = 0;
slouken@0
   362
slouken@0
   363
	if ( pcm_buf != NULL ) {
slouken@0
   364
		free((Uint8 *)pcm_buf); 
slouken@0
   365
		pcm_buf = NULL;
slouken@0
   366
	}
slouken@0
   367
	 
slouken@0
   368
	/* initialize channel transfer parameters to default */
slouken@0
   369
	init_pcm_cparams(&cparams);
slouken@0
   370
slouken@0
   371
	/* Open the audio device */
slouken@0
   372
	
slouken@0
   373
	rval = snd_pcm_open_preferred(&audio_handle, &card_no, &device_no, OPEN_FLAGS);
slouken@0
   374
	if ( rval < 0 ) {
slouken@0
   375
		SDL_SetError("snd_pcm_open failed: %s\n", snd_strerror(rval));
slouken@0
   376
		return(-1);
slouken@0
   377
	}
slouken@0
   378
slouken@0
   379
    /* set to nonblocking mode */
slouken@0
   380
    if ((rval = snd_pcm_nonblock_mode(audio_handle, 1))<0) //I assume 1 means on
slouken@0
   381
    {
slouken@0
   382
        SDL_SetError("snd_pcm_nonblock_mode failed: %s\n", snd_strerror(rval));
slouken@0
   383
        return(-1);
slouken@0
   384
    }
slouken@0
   385
slouken@0
   386
    /* enable count status parameter */
slouken@0
   387
    if ((rval = snd_plugin_set_disable(audio_handle, PLUGIN_DISABLE_MMAP))<0)
slouken@0
   388
    {
slouken@0
   389
        SDL_SetError("snd_plugin_set_disable failed: %s\n", snd_strerror(rval));
slouken@0
   390
        return(-1);
slouken@0
   391
    }
slouken@0
   392
slouken@0
   393
slouken@0
   394
	/* Try for a closest match on audio format */
slouken@0
   395
	format = 0;
slouken@0
   396
 	found = 0; // can't use format as SND_PCM_SFMT_U8 = 0 in nto
slouken@0
   397
	for ( test_format = SDL_FirstAudioFormat(spec->format);	!found ; ) 
slouken@0
   398
slouken@0
   399
	{
slouken@0
   400
#ifdef DEBUG_AUDIO
slouken@0
   401
		fprintf(stderr, "Trying format 0x%4.4x spec->samples %d\n", test_format,spec->samples);
slouken@0
   402
#endif
slouken@0
   403
			/* if match found set format to equivalent ALSA format */
slouken@0
   404
        switch ( test_format ) {
slouken@0
   405
			case AUDIO_U8:
slouken@0
   406
				format = SND_PCM_SFMT_U8;
slouken@0
   407
				cparams.buf.block.frag_size = spec->samples * spec->channels;
slouken@0
   408
				found = 1;
slouken@0
   409
				break;
slouken@0
   410
			case AUDIO_S8:
slouken@0
   411
				format = SND_PCM_SFMT_S8;
slouken@0
   412
				cparams.buf.block.frag_size = spec->samples * spec->channels;
slouken@0
   413
				found = 1;
slouken@0
   414
				break;
slouken@0
   415
			case AUDIO_S16LSB:
slouken@0
   416
				format = SND_PCM_SFMT_S16_LE;
slouken@0
   417
				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
slouken@0
   418
				found = 1;
slouken@0
   419
				break;
slouken@0
   420
			case AUDIO_S16MSB:
slouken@0
   421
				format = SND_PCM_SFMT_S16_BE;
slouken@0
   422
				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
slouken@0
   423
				found = 1;
slouken@0
   424
				break;
slouken@0
   425
			case AUDIO_U16LSB:
slouken@0
   426
				format = SND_PCM_SFMT_U16_LE;
slouken@0
   427
				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
slouken@0
   428
				found = 1;
slouken@0
   429
				break;
slouken@0
   430
			case AUDIO_U16MSB:
slouken@0
   431
				format = SND_PCM_SFMT_U16_BE;
slouken@0
   432
				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
slouken@0
   433
				found = 1;
slouken@0
   434
				break;
slouken@0
   435
			default:
slouken@0
   436
				break;
slouken@0
   437
		}
slouken@0
   438
		if ( ! found ) {
slouken@0
   439
			test_format = SDL_NextAudioFormat();
slouken@0
   440
		}
slouken@0
   441
	}
slouken@0
   442
	
slouken@0
   443
	/* assumes test_format not 0 on success */
slouken@0
   444
	if ( test_format == 0 ) {
slouken@0
   445
		SDL_SetError("Couldn't find any hardware audio formats");
slouken@0
   446
		return(-1);
slouken@0
   447
	}
slouken@0
   448
	
slouken@0
   449
	spec->format = test_format;
slouken@0
   450
slouken@0
   451
	/* Set the audio format */
slouken@0
   452
	cparams.format.format = format;
slouken@0
   453
slouken@0
   454
	/* Set mono or stereo audio (currently only two channels supported) */
slouken@0
   455
	cparams.format.voices = spec->channels;
slouken@0
   456
	
slouken@0
   457
#ifdef DEBUG_AUDIO
slouken@0
   458
	fprintf(stderr,"intializing channels %d\n", cparams.format.voices);
slouken@0
   459
#endif
slouken@0
   460
slouken@0
   461
	
slouken@0
   462
	/* Set rate */
slouken@0
   463
	cparams.format.rate = spec->freq ;
slouken@0
   464
slouken@0
   465
	/* Setup the transfer parameters according to cparams */
slouken@0
   466
	rval = snd_pcm_plugin_params(audio_handle, &cparams);
slouken@0
   467
	if (rval < 0) {
slouken@0
   468
		SDL_SetError("snd_pcm_channel_params failed: %s\n", snd_strerror (rval));
slouken@0
   469
		return(-1);
slouken@0
   470
	}
slouken@0
   471
slouken@0
   472
    /*  Make sure channel is setup right one last time */
slouken@0
   473
    memset( &csetup, 0, sizeof( csetup ) );
slouken@0
   474
    csetup.channel = SND_PCM_CHANNEL_PLAYBACK;
slouken@0
   475
    if ( snd_pcm_plugin_setup( audio_handle, &csetup ) < 0 )
slouken@0
   476
    {
slouken@0
   477
        SDL_SetError("Unable to setup playback channel\n" );
slouken@0
   478
        return(-1);
slouken@0
   479
    }
slouken@0
   480
    else
slouken@0
   481
    {
slouken@0
   482
#ifdef DEBUG_AUDIO
slouken@0
   483
        fprintf(stderr,"requested format: %d\n",cparams.format.format);
slouken@0
   484
        fprintf(stderr,"requested frag size: %d\n",cparams.buf.block.frag_size);
slouken@0
   485
        fprintf(stderr,"requested max frags: %d\n\n",cparams.buf.block.frags_max);
slouken@0
   486
slouken@0
   487
        fprintf(stderr,"real format: %d\n", csetup.format.format );
slouken@0
   488
        fprintf(stderr,"real frag size : %d\n", csetup.buf.block.frag_size );
slouken@0
   489
		fprintf(stderr,"real max frags : %d\n", csetup.buf.block.frags_max );
slouken@0
   490
#endif // DEBUG_AUDIO
slouken@0
   491
    }
slouken@0
   492
slouken@0
   493
slouken@0
   494
    /*  Allocate memory to the audio buffer and initialize with silence
slouken@0
   495
        (Note that buffer size must be a multiple of fragment size, so find closest multiple)
slouken@0
   496
    */
slouken@0
   497
    
slouken@0
   498
    twidth = snd_pcm_format_width(format);
slouken@0
   499
    if (twidth < 0) {
slouken@0
   500
        printf("snd_pcm_format_width failed\n");
slouken@0
   501
        twidth = 0;
slouken@0
   502
    }
slouken@0
   503
    
slouken@0
   504
#ifdef DEBUG_AUDIO
slouken@0
   505
    fprintf(stderr,"format is %d bits wide\n",twidth);
slouken@0
   506
#endif      
slouken@0
   507
    
slouken@0
   508
    pcm_len = spec->size ;
slouken@0
   509
    
slouken@0
   510
  
slouken@0
   511
#ifdef DEBUG_AUDIO    
slouken@0
   512
    fprintf(stderr,"pcm_len set to %d\n", pcm_len);
slouken@0
   513
#endif
slouken@0
   514
    
slouken@0
   515
    if (pcm_len == 0)
slouken@0
   516
    {
slouken@0
   517
        pcm_len = csetup.buf.block.frag_size;
slouken@0
   518
    }
slouken@0
   519
    
slouken@0
   520
    pcm_buf = (Uint8*)malloc(pcm_len);
slouken@0
   521
    if (pcm_buf == NULL) {
slouken@0
   522
        SDL_SetError("pcm_buf malloc failed\n");
slouken@0
   523
        return(-1);
slouken@0
   524
    }
slouken@0
   525
    memset(pcm_buf,spec->silence,pcm_len);
slouken@0
   526
slouken@0
   527
#ifdef DEBUG_AUDIO
slouken@0
   528
	fprintf(stderr,"pcm_buf malloced and silenced.\n");
slouken@0
   529
#endif
slouken@0
   530
slouken@0
   531
    /* get the file descriptor */
slouken@0
   532
    if( (audio_fd = snd_pcm_file_descriptor(audio_handle, SND_PCM_CHANNEL_PLAYBACK)) < 0)
slouken@0
   533
    {
slouken@0
   534
       fprintf(stderr, "snd_pcm_file_descriptor failed with error code: %d\n", audio_fd);
slouken@0
   535
    }
slouken@0
   536
slouken@0
   537
	/* Trigger audio playback */
slouken@0
   538
	rval = snd_pcm_plugin_prepare( audio_handle, SND_PCM_CHANNEL_PLAYBACK);
slouken@0
   539
	if (rval < 0) {
slouken@0
   540
       SDL_SetError("snd_pcm_plugin_prepare failed: %s\n", snd_strerror (rval));
slouken@0
   541
       return(-1);
slouken@0
   542
	}
slouken@0
   543
	
slouken@0
   544
	 this->enabled = 1;
slouken@0
   545
	 
slouken@0
   546
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   547
	parent = getpid();
slouken@0
   548
slouken@0
   549
	/* We're ready to rock and roll. :-) */
slouken@0
   550
	return(0);
slouken@0
   551
}
slouken@0
   552
slouken@0
   553
slouken@0
   554