Fixed audio format selection for OpenBSD (thanks Peter!)
authorSam Lantinga <slouken@libsdl.org>
Tue, 31 Jul 2001 05:39:36 +0000
changeset 1315d08b230932e
parent 130 14af14ff7c19
child 132 2604a7be65af
Fixed audio format selection for OpenBSD (thanks Peter!)
src/audio/openbsd/SDL_openbsdaudio.c
     1.1 --- a/src/audio/openbsd/SDL_openbsdaudio.c	Tue Jul 31 05:36:10 2001 +0000
     1.2 +++ b/src/audio/openbsd/SDL_openbsdaudio.c	Tue Jul 31 05:39:36 2001 +0000
     1.3 @@ -312,8 +312,7 @@
     1.4  OBSD_OpenAudio(_THIS, SDL_AudioSpec *spec)
     1.5  {
     1.6      char audiodev[64];
     1.7 -    Uint16 setenc;
     1.8 -    audio_encoding_t enc;
     1.9 +    Uint16 format;
    1.10      audio_info_t info;
    1.11  
    1.12      AUDIO_INITINFO(&info);
    1.13 @@ -340,66 +339,58 @@
    1.14      }
    1.15      
    1.16      mixbuf = NULL;
    1.17 -    setenc = 0;
    1.18 -
    1.19 -    for(enc.index = 0; (ioctl(audio_fd, AUDIO_GETENC, &enc)>=0)
    1.20 -	&& (enc.encoding != setenc); enc.index++)
    1.21 +    AUDIO_INITINFO(&info);
    1.22 +    for (format = SDL_FirstAudioFormat(spec->format); 
    1.23 +    	format; format = SDL_NextAudioFormat())
    1.24      {
    1.25 -	switch(spec->format)
    1.26 -	{
    1.27 -	    case AUDIO_U8:	/* 8-bit unsigned linear */
    1.28 -		setenc = AUDIO_ENCODING_PCM8;
    1.29 -		break;
    1.30 -	    case AUDIO_S8:	/* 8-bit signed linear */
    1.31 -		setenc = AUDIO_ENCODING_SLINEAR;
    1.32 -		break;
    1.33 -	    case AUDIO_U16LSB:  /* 16-bit unsigned linear, LSB */
    1.34 -		setenc = AUDIO_ENCODING_ULINEAR_LE;
    1.35 -		break;
    1.36 -	    case AUDIO_U16MSB:  /* 16-bit unsigned linear, MSB */
    1.37 -		setenc = AUDIO_ENCODING_ULINEAR_BE;
    1.38 -		break;
    1.39 -	    case AUDIO_S16LSB:  /* 16-bit signed linear, LSB */
    1.40 -		setenc = AUDIO_ENCODING_SLINEAR_LE;
    1.41 -		break;
    1.42 -	    case AUDIO_S16MSB:  /* 16-bit signed linear, MSB */
    1.43 -		setenc = AUDIO_ENCODING_SLINEAR_BE;
    1.44 -		break;
    1.45 +	switch(format) {
    1.46 +	case AUDIO_U8:
    1.47 +	    info.play.encoding = AUDIO_ENCODING_ULINEAR;
    1.48 +	    info.play.precision = 8;
    1.49 +	    break;
    1.50 +	case AUDIO_S8:
    1.51 +	    info.play.encoding = AUDIO_ENCODING_SLINEAR;
    1.52 +	    info.play.precision = 8;
    1.53 +	    break;
    1.54 +	case AUDIO_S16LSB:
    1.55 +	    info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
    1.56 +	    info.play.precision = 16;
    1.57 +	    break;
    1.58 +	case AUDIO_S16MSB:
    1.59 +	    info.play.encoding = AUDIO_ENCODING_SLINEAR_BE;
    1.60 +	    info.play.precision = 16;
    1.61 +	    break;
    1.62 +	case AUDIO_U16LSB:
    1.63 +	    info.play.encoding = AUDIO_ENCODING_ULINEAR_LE;
    1.64 +	    info.play.precision = 16;
    1.65 +	    break;
    1.66 +	case AUDIO_U16MSB:
    1.67 +	    info.play.encoding = AUDIO_ENCODING_ULINEAR_BE;
    1.68 +	    info.play.precision = 16;
    1.69 +	    break;
    1.70 +	default:
    1.71 +	    continue;
    1.72  	}
    1.73 -#ifdef DEBUG_AUDIO
    1.74 -	fprintf(stderr,"encoding #%i: \"%s\" %i-bit (0x%x) flags=%i...\n",
    1.75 -	    enc.index, enc.name, enc.precision, enc.encoding, enc.flags);
    1.76 -#endif
    1.77 +	if (ioctl(audio_fd, AUDIO_SETINFO, &info) == 0)
    1.78 +	    break;
    1.79      }
    1.80  
    1.81 -    if(!setenc) {
    1.82 +    if(!format) {
    1.83  	SDL_SetError("No supported encoding for 0x%x", spec->format);
    1.84  	return(-1);
    1.85      }
    1.86  
    1.87 -    /* Set audio encoding */
    1.88 -    info.play.encoding = enc.encoding;
    1.89 -    info.play.precision = enc.precision;
    1.90 -    if((ioctl(audio_fd, AUDIO_SETINFO, &info) < 0)) {
    1.91 -	SDL_SetError("Couldn't set encoding to 0x%x %i-bit",
    1.92 -	    enc.encoding, enc.precision);
    1.93 -	return(-1);
    1.94 -    }
    1.95 +    spec->format = format;
    1.96  
    1.97 -    /* Set audio channels */
    1.98 +    AUDIO_INITINFO(&info);
    1.99      info.play.channels = spec->channels;
   1.100 -    if(ioctl(audio_fd, AUDIO_SETINFO, &info) < 0) {
   1.101 -	info.play.channels = (spec->channels > 1);
   1.102 -	ioctl(audio_fd, AUDIO_SETINFO, &info);
   1.103 -    }
   1.104 -
   1.105 -    /* Set the sample rate */
   1.106 +    if (ioctl(audio_fd, AUDIO_SETINFO, &info) == -1)
   1.107 +    	spec->channels = 1;
   1.108 +    AUDIO_INITINFO(&info);
   1.109      info.play.sample_rate = spec->freq;
   1.110 -    if(ioctl(audio_fd, AUDIO_SETINFO, &info) < 0) {
   1.111 -	SDL_SetError("Couldn't set sample rate to %i Hz", spec->freq);
   1.112 -	return(-1);
   1.113 -    }
   1.114 -
   1.115 +    (void)ioctl(audio_fd, AUDIO_SETINFO, &info);
   1.116 +    (void)ioctl(audio_fd, AUDIO_GETINFO, &info);
   1.117 +    spec->freq  = info.play.sample_rate;
   1.118      /* Allocate mixing buffer */
   1.119      mixlen = spec->size;
   1.120      mixbuf = (Uint8*)SDL_AllocAudioMem(mixlen);