static int MPFAUD_OpenAudio(_THIS, SDL_AudioSpec *spec) { int format; float bytes_per_sec = 0.0f; /* Allocate mixing buffer */ this->hidden->mixlen = spec->size; this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); if ( this->hidden->mixbuf == NULL ) { return(-1); } SDL_memset(this->hidden->mixbuf, spec->silence, spec->size); bytes_per_sec = (float) (((spec->format & 0xFF) / 8) * spec->channels * spec->freq); switch (spec->format & 0xff) { case 8:format=8;break; case 16:format=16;break; default: SDL_SetError("Unsupported audio format"); return -1; } sys_sound_init(spec->freq,format,spec->channels); /* We're ready to rock and roll. :-) */ return(0); }
static int DCAUD_OpenAudio(_THIS, SDL_AudioSpec *spec) { switch(spec->format&0xff) { case 8: spec->format = AUDIO_S8; break; case 16: spec->format = AUDIO_S16LSB; break; default: SDL_SetError("Unsupported audio format"); return(-1); } /* Update the fragment size as size in bytes */ SDL_CalculateAudioSpec(spec); /* Allocate mixing buffer */ this->hidden->mixlen = spec->size; this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); if ( this->hidden->mixbuf == NULL ) { return(-1); } memset(this->hidden->mixbuf, spec->silence, spec->size); this->hidden->leftpos = 0x11000; this->hidden->rightpos = 0x11000+spec->size; this->hidden->playing = 0; this->hidden->nextbuf = 0; /* We're ready to rock and roll. :-) */ return(0); }
static int DISKAUD_OpenAudio(_THIS, SDL_AudioSpec *spec) { const char *fname = DISKAUD_GetOutputFilename(); /* Open the audio device */ this->hidden->audio_fd = open(fname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); if ( this->hidden->audio_fd < 0 ) { SDL_SetError("Couldn't open %s: %s", fname, strerror(errno)); return(-1); } fprintf(stderr, "WARNING: You are using the SDL disk writer" " audio driver!\n Writing to file [%s].\n", fname); /* Allocate mixing buffer */ this->hidden->mixlen = spec->size; this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); if ( this->hidden->mixbuf == NULL ) { return(-1); } memset(this->hidden->mixbuf, spec->silence, spec->size); /* We're ready to rock and roll. :-) */ return(0); }
static int DISKAUD_OpenAudio(_THIS, SDL_AudioSpec *spec) { const char *fname = DISKAUD_GetOutputFilename(); /* Open the audio device */ this->hidden->output = SDL_RWFromFile(fname, "wb"); if ( this->hidden->output == NULL ) { return(-1); } #if HAVE_STDIO_H fprintf(stderr, "WARNING: You are using the SDL disk writer" " audio driver!\n Writing to file [%s].\n", fname); #endif /* Allocate mixing buffer */ this->hidden->mixlen = spec->size; this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); if ( this->hidden->mixbuf == NULL ) { return(-1); } SDL_memset(this->hidden->mixbuf, spec->silence, spec->size); /* We're ready to rock and roll. :-) */ return(0); }
static int ANDROIDAUDIOTRACK_OpenAudio(_THIS, SDL_AudioSpec *spec) { initAndroidAudio(this, spec->format, spec->freq, spec->channels, spec->size); float bytes_per_sec = 0.0f; /* Allocate mixing buffer */ this->hidden->mixlen = spec->size; this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); if ( this->hidden->mixbuf == NULL ) { return(-1); } SDL_memset(this->hidden->mixbuf, spec->silence, spec->size); bytes_per_sec = (float) (((spec->format & 0xFF) / 8) * spec->channels * spec->freq); /* * We try to make this request more audio at the correct rate for * a given audio spec, so timing stays fairly faithful. * Also, we have it not block at all for the first two calls, so * it seems like we're filling two audio fragments right out of the * gate, like other SDL drivers tend to do. */ this->hidden->initial_calls = 2; this->hidden->write_delay = (Uint32) ((((float) spec->size) / bytes_per_sec) * 1000.0f); /* We're ready to rock and roll. :-) */ return(0); }
static int ESD_OpenAudio(_THIS, SDL_AudioSpec *spec) { esd_format_t format; /* Convert audio spec to the ESD audio format */ format = (ESD_STREAM | ESD_PLAY); switch ( spec->format & 0xFF ) { case 8: format |= ESD_BITS8; break; case 16: format |= ESD_BITS16; break; default: SDL_SetError("Unsupported ESD audio format"); return(-1); } if ( spec->channels == 1 ) { format |= ESD_MONO; } else { format |= ESD_STEREO; } #if 0 spec->samples = ESD_BUF_SIZE; /* Darn, no way to change this yet */ #endif /* Open a connection to the ESD audio server */ audio_fd = SDL_NAME(esd_play_stream)(format, spec->freq, NULL, get_progname()); if ( audio_fd < 0 ) { SDL_SetError("Couldn't open ESD connection"); return(-1); } /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(spec); frame_ticks = (float)(spec->samples*1000)/spec->freq; next_frame = SDL_GetTicks()+frame_ticks; /* Allocate mixing buffer */ mixlen = spec->size; mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); if ( mixbuf == NULL ) { return(-1); } memset(mixbuf, spec->silence, spec->size); /* Get the parent process id (we're the parent of the audio thread) */ parent = getpid(); /* We're ready to rock and roll. :-) */ return(0); }
static int DISKAUD_OpenDevice(_THIS, const char *devname, int iscapture) { const char *envr = SDL_getenv(DISKENVR_WRITEDELAY); const char *fname = DISKAUD_GetOutputFilename(devname); this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc(sizeof(*this->hidden)); if (this->hidden == NULL) { SDL_OutOfMemory(); return 0; } SDL_memset(this->hidden, 0, sizeof(*this->hidden)); /* Open the audio device */ this->hidden->output = SDL_RWFromFile(fname, "wb"); if (this->hidden->output == NULL) { DISKAUD_CloseDevice(this); return 0; } /* Allocate mixing buffer */ this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); if (this->hidden->mixbuf == NULL) { DISKAUD_CloseDevice(this); return 0; } SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); this->hidden->mixlen = this->spec.size; this->hidden->write_delay = (envr) ? SDL_atoi(envr) : DISKDEFAULT_WRITEDELAY; #if HAVE_STDIO_H fprintf(stderr, "WARNING: You are using the SDL disk writer audio driver!\n" " Writing to file [%s].\n", fname); #endif /* We're ready to rock and roll. :-) */ return 1; }
static int QSA_OpenDevice(_THIS, const char *devname, int iscapture) { int status = 0; int format = 0; SDL_AudioFormat test_format = 0; int found = 0; snd_pcm_channel_setup_t csetup; snd_pcm_channel_params_t cparams; /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, (sizeof (struct SDL_PrivateAudioData))); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_memset(this->hidden, 0, sizeof(struct SDL_PrivateAudioData)); /* Initialize channel transfer parameters to default */ QSA_InitAudioParams(&cparams); /* Initialize channel direction: capture or playback */ this->hidden->iscapture = iscapture; /* Find deviceid and cardid by device name for playback */ if ((!this->hidden->iscapture) && (devname != NULL)) { uint32_t device; int32_t status; /* Search in the playback devices */ device = 0; do { status = SDL_strcmp(qsa_playback_device[device].name, devname); if (status == 0) { /* Found requested device */ this->hidden->deviceno = qsa_playback_device[device].deviceno; this->hidden->cardno = qsa_playback_device[device].cardno; break; } device++; if (device >= qsa_playback_devices) { QSA_CloseDevice(this); return SDL_SetError("No such playback device"); } } while (1); } /* Find deviceid and cardid by device name for capture */ if ((this->hidden->iscapture) && (devname != NULL)) { /* Search in the capture devices */ uint32_t device; int32_t status; /* Searching in the playback devices */ device = 0; do { status = SDL_strcmp(qsa_capture_device[device].name, devname); if (status == 0) { /* Found requested device */ this->hidden->deviceno = qsa_capture_device[device].deviceno; this->hidden->cardno = qsa_capture_device[device].cardno; break; } device++; if (device >= qsa_capture_devices) { QSA_CloseDevice(this); return SDL_SetError("No such capture device"); } } while (1); } /* Check if SDL requested default audio device */ if (devname == NULL) { /* Open system default audio device */ if (!this->hidden->iscapture) { status = snd_pcm_open_preferred(&this->hidden->audio_handle, &this->hidden->cardno, &this->hidden->deviceno, SND_PCM_OPEN_PLAYBACK); } else { status = snd_pcm_open_preferred(&this->hidden->audio_handle, &this->hidden->cardno, &this->hidden->deviceno, SND_PCM_OPEN_CAPTURE); } } else { /* Open requested audio device */ if (!this->hidden->iscapture) { status = snd_pcm_open(&this->hidden->audio_handle, this->hidden->cardno, this->hidden->deviceno, SND_PCM_OPEN_PLAYBACK); } else { status = snd_pcm_open(&this->hidden->audio_handle, this->hidden->cardno, this->hidden->deviceno, SND_PCM_OPEN_CAPTURE); } } /* Check if requested device is opened */ if (status < 0) { this->hidden->audio_handle = NULL; QSA_CloseDevice(this); return QSA_SetError("snd_pcm_open", status); } if (!QSA_CheckBuggyCards(this, QSA_MMAP_WORKAROUND)) { /* Disable QSA MMAP plugin for buggy audio drivers */ status = snd_pcm_plugin_set_disable(this->hidden->audio_handle, PLUGIN_DISABLE_MMAP); if (status < 0) { QSA_CloseDevice(this); return QSA_SetError("snd_pcm_plugin_set_disable", status); } } /* Try for a closest match on audio format */ format = 0; /* can't use format as SND_PCM_SFMT_U8 = 0 in qsa */ found = 0; for (test_format = SDL_FirstAudioFormat(this->spec.format); !found;) { /* if match found set format to equivalent QSA format */ switch (test_format) { case AUDIO_U8: { format = SND_PCM_SFMT_U8; found = 1; } break; case AUDIO_S8: { format = SND_PCM_SFMT_S8; found = 1; } break; case AUDIO_S16LSB: { format = SND_PCM_SFMT_S16_LE; found = 1; } break; case AUDIO_S16MSB: { format = SND_PCM_SFMT_S16_BE; found = 1; } break; case AUDIO_U16LSB: { format = SND_PCM_SFMT_U16_LE; found = 1; } break; case AUDIO_U16MSB: { format = SND_PCM_SFMT_U16_BE; found = 1; } break; case AUDIO_S32LSB: { format = SND_PCM_SFMT_S32_LE; found = 1; } break; case AUDIO_S32MSB: { format = SND_PCM_SFMT_S32_BE; found = 1; } break; case AUDIO_F32LSB: { format = SND_PCM_SFMT_FLOAT_LE; found = 1; } break; case AUDIO_F32MSB: { format = SND_PCM_SFMT_FLOAT_BE; found = 1; } break; default: { break; } } if (!found) { test_format = SDL_NextAudioFormat(); } } /* assumes test_format not 0 on success */ if (test_format == 0) { QSA_CloseDevice(this); return SDL_SetError("QSA: Couldn't find any hardware audio formats"); } this->spec.format = test_format; /* Set the audio format */ cparams.format.format = format; /* Set mono/stereo/4ch/6ch/8ch audio */ cparams.format.voices = this->spec.channels; /* Set rate */ cparams.format.rate = this->spec.freq; /* Setup the transfer parameters according to cparams */ status = snd_pcm_plugin_params(this->hidden->audio_handle, &cparams); if (status < 0) { QSA_CloseDevice(this); return QSA_SetError("snd_pcm_channel_params", status); } /* Make sure channel is setup right one last time */ SDL_memset(&csetup, 0, sizeof(csetup)); if (!this->hidden->iscapture) { csetup.channel = SND_PCM_CHANNEL_PLAYBACK; } else { csetup.channel = SND_PCM_CHANNEL_CAPTURE; } /* Setup an audio channel */ if (snd_pcm_plugin_setup(this->hidden->audio_handle, &csetup) < 0) { QSA_CloseDevice(this); return SDL_SetError("QSA: Unable to setup channel"); } /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(&this->spec); this->hidden->pcm_len = this->spec.size; if (this->hidden->pcm_len == 0) { this->hidden->pcm_len = csetup.buf.block.frag_size * this->spec.channels * (snd_pcm_format_width(format) / 8); } /* * Allocate memory to the audio buffer and initialize with silence * (Note that buffer size must be a multiple of fragment size, so find * closest multiple) */ this->hidden->pcm_buf = (Uint8 *) SDL_AllocAudioMem(this->hidden->pcm_len); if (this->hidden->pcm_buf == NULL) { QSA_CloseDevice(this); return SDL_OutOfMemory(); } SDL_memset(this->hidden->pcm_buf, this->spec.silence, this->hidden->pcm_len); /* get the file descriptor */ if (!this->hidden->iscapture) { this->hidden->audio_fd = snd_pcm_file_descriptor(this->hidden->audio_handle, SND_PCM_CHANNEL_PLAYBACK); } else { this->hidden->audio_fd = snd_pcm_file_descriptor(this->hidden->audio_handle, SND_PCM_CHANNEL_CAPTURE); } if (this->hidden->audio_fd < 0) { QSA_CloseDevice(this); return QSA_SetError("snd_pcm_file_descriptor", status); } /* Prepare an audio channel */ if (!this->hidden->iscapture) { /* Prepare audio playback */ status = snd_pcm_plugin_prepare(this->hidden->audio_handle, SND_PCM_CHANNEL_PLAYBACK); } else { /* Prepare audio capture */ status = snd_pcm_plugin_prepare(this->hidden->audio_handle, SND_PCM_CHANNEL_CAPTURE); } if (status < 0) { QSA_CloseDevice(this); return QSA_SetError("snd_pcm_plugin_prepare", status); } /* We're really ready to rock and roll. :-) */ return 0; }
static int SUNAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) { const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT); SDL_AudioFormat format = 0; audio_info_t info; /* We don't care what the devname is...we'll try to open anything. */ /* ...but default to first name in the list... */ if (devname == NULL) { devname = SDL_GetAudioDeviceName(0, iscapture); if (devname == NULL) { return SDL_SetError("No such audio device"); } } /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_memset(this->hidden, 0, (sizeof *this->hidden)); /* Open the audio device */ this->hidden->audio_fd = open(devname, flags, 0); if (this->hidden->audio_fd < 0) { return SDL_SetError("Couldn't open %s: %s", devname, strerror(errno)); } #ifdef AUDIO_SETINFO int enc; #endif int desired_freq = this->spec.freq; /* Determine the audio parameters from the AudioSpec */ switch (SDL_AUDIO_BITSIZE(this->spec.format)) { case 8: { /* Unsigned 8 bit audio data */ this->spec.format = AUDIO_U8; #ifdef AUDIO_SETINFO enc = AUDIO_ENCODING_LINEAR8; #endif } break; case 16: { /* Signed 16 bit audio data */ this->spec.format = AUDIO_S16SYS; #ifdef AUDIO_SETINFO enc = AUDIO_ENCODING_LINEAR; #endif } break; default: { /* !!! FIXME: fallback to conversion on unsupported types! */ return SDL_SetError("Unsupported audio format"); } } this->hidden->audio_fmt = this->spec.format; this->hidden->ulaw_only = 0; /* modern Suns do support linear audio */ #ifdef AUDIO_SETINFO for (;;) { audio_info_t info; AUDIO_INITINFO(&info); /* init all fields to "no change" */ /* Try to set the requested settings */ info.play.sample_rate = this->spec.freq; info.play.channels = this->spec.channels; info.play.precision = (enc == AUDIO_ENCODING_ULAW) ? 8 : this->spec.format & 0xff; info.play.encoding = enc; if (ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info) == 0) { /* Check to be sure we got what we wanted */ if (ioctl(this->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) { return SDL_SetError("Error getting audio parameters: %s", strerror(errno)); } if (info.play.encoding == enc && info.play.precision == (this->spec.format & 0xff) && info.play.channels == this->spec.channels) { /* Yow! All seems to be well! */ this->spec.freq = info.play.sample_rate; break; } } switch (enc) { case AUDIO_ENCODING_LINEAR8: /* unsigned 8bit apparently not supported here */ enc = AUDIO_ENCODING_LINEAR; this->spec.format = AUDIO_S16SYS; break; /* try again */ case AUDIO_ENCODING_LINEAR: /* linear 16bit didn't work either, resort to µ-law */ enc = AUDIO_ENCODING_ULAW; this->spec.channels = 1; this->spec.freq = 8000; this->spec.format = AUDIO_U8; this->hidden->ulaw_only = 1; break; default: /* oh well... */ return SDL_SetError("Error setting audio parameters: %s", strerror(errno)); } } #endif /* AUDIO_SETINFO */ this->hidden->written = 0; /* We can actually convert on-the-fly to U-Law */ if (this->hidden->ulaw_only) { this->spec.freq = desired_freq; this->hidden->fragsize = (this->spec.samples * 1000) / (this->spec.freq / 8); this->hidden->frequency = 8; this->hidden->ulaw_buf = (Uint8 *) SDL_malloc(this->hidden->fragsize); if (this->hidden->ulaw_buf == NULL) { return SDL_OutOfMemory(); } this->spec.channels = 1; } else { this->hidden->fragsize = this->spec.samples; this->hidden->frequency = this->spec.freq / 1000; } #ifdef DEBUG_AUDIO fprintf(stderr, "Audio device %s U-Law only\n", this->hidden->ulaw_only ? "is" : "is not"); fprintf(stderr, "format=0x%x chan=%d freq=%d\n", this->spec.format, this->spec.channels, this->spec.freq); #endif /* Update the fragment size as size in bytes */ SDL_CalculateAudioSpec(&this->spec); /* Allocate mixing buffer */ this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->spec.size); if (this->hidden->mixbuf == NULL) { return SDL_OutOfMemory(); } SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); /* We're ready to rock and roll. :-) */ return 0; }
static int AL_OpenAudio(_THIS, SDL_AudioSpec * spec) { Uint16 test_format = SDL_FirstAudioFormat(spec->format); long width = 0; long fmt = 0; int valid = 0; #ifdef OLD_IRIX_AUDIO { long audio_param[2]; audio_param[0] = AL_OUTPUT_RATE; audio_param[1] = spec->freq; valid = (ALsetparams(AL_DEFAULT_DEVICE, audio_param, 2) < 0); } #else { ALpv audio_param; audio_param.param = AL_RATE; audio_param.value.i = spec->freq; valid = (alSetParams(AL_DEFAULT_OUTPUT, &audio_param, 1) < 0); } #endif while ((!valid) && (test_format)) { valid = 1; spec->format = test_format; switch (test_format) { case AUDIO_S8: width = AL_SAMPLE_8; fmt = AL_SAMPFMT_TWOSCOMP; break; case AUDIO_S16SYS: width = AL_SAMPLE_16; fmt = AL_SAMPFMT_TWOSCOMP; break; default: valid = 0; test_format = SDL_NextAudioFormat(); break; } if (valid) { ALconfig audio_config = alNewConfig(); valid = 0; if (audio_config) { if (alSetChannels(audio_config, spec->channels) < 0) { if (spec->channels > 2) { /* can't handle > stereo? */ spec->channels = 2; /* try again below. */ } } if ((alSetSampFmt(audio_config, fmt) >= 0) && ((!width) || (alSetWidth(audio_config, width) >= 0)) && (alSetQueueSize(audio_config, spec->samples * 2) >= 0) && (alSetChannels(audio_config, spec->channels) >= 0)) { audio_port = alOpenPort("SDL audio", "w", audio_config); if (audio_port == NULL) { /* docs say AL_BAD_CHANNELS happens here, too. */ int err = oserror(); if (err == AL_BAD_CHANNELS) { spec->channels = 2; alSetChannels(audio_config, spec->channels); audio_port = alOpenPort("SDL audio", "w", audio_config); } } if (audio_port != NULL) { valid = 1; } } alFreeConfig(audio_config); } } } if (!valid) { SDL_SetError("Unsupported audio format"); return (-1); } /* Update the fragment size as size in bytes */ SDL_CalculateAudioSpec(spec); /* Allocate mixing buffer */ mixbuf = (Uint8 *) SDL_AllocAudioMem(spec->size); if (mixbuf == NULL) { SDL_OutOfMemory(); return (-1); } SDL_memset(mixbuf, spec->silence, spec->size); /* We're ready to rock and roll. :-) */ return (0); }
static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec) { char audiodev[1024]; int format; int value; Uint16 test_format; /* Reset the timer synchronization flag */ frame_ticks = 0.0; /* Open the audio device */ audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0); if ( audio_fd < 0 ) { SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); return(-1); } mixbuf = NULL; #ifdef USE_BLOCKING_WRITES /* Make the file descriptor use blocking writes with fcntl() */ { long flags; flags = fcntl(audio_fd, F_GETFL); flags &= ~O_NONBLOCK; if ( fcntl(audio_fd, F_SETFL, flags) < 0 ) { SDL_SetError("Couldn't set audio blocking mode"); return(-1); } } #endif /* Get a list of supported hardware formats */ if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) { SDL_SetError("Couldn't get audio format list"); return(-1); } /* Try for a closest match on audio format */ format = 0; for ( test_format = SDL_FirstAudioFormat(spec->format); ! format && test_format; ) { #ifdef DEBUG_AUDIO fprintf(stderr, "Trying format 0x%4.4x\n", test_format); #endif switch ( test_format ) { case AUDIO_U8: if ( value & AFMT_U8 ) { format = AFMT_U8; } break; case AUDIO_S8: if ( value & AFMT_S8 ) { format = AFMT_S8; } break; case AUDIO_S16LSB: if ( value & AFMT_S16_LE ) { format = AFMT_S16_LE; } break; case AUDIO_S16MSB: if ( value & AFMT_S16_BE ) { format = AFMT_S16_BE; } break; case AUDIO_U16LSB: if ( value & AFMT_U16_LE ) { format = AFMT_U16_LE; } break; case AUDIO_U16MSB: if ( value & AFMT_U16_BE ) { format = AFMT_U16_BE; } break; default: format = 0; break; } if ( ! format ) { test_format = SDL_NextAudioFormat(); } } if ( format == 0 ) { SDL_SetError("Couldn't find any hardware audio formats"); return(-1); } spec->format = test_format; /* Set the audio format */ value = format; if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || (value != format) ) { SDL_SetError("Couldn't set audio format"); return(-1); } /* Set the number of channels of output */ value = spec->channels; #ifdef SNDCTL_DSP_CHANNELS if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0 ) { #endif value = (spec->channels > 1); ioctl(audio_fd, SNDCTL_DSP_STEREO, &value); value = (value ? 2 : 1); #ifdef SNDCTL_DSP_CHANNELS } #endif spec->channels = value; /* Because some drivers don't allow setting the buffer size after setting the format, we must re-open the audio device once we know what format and channels are supported */ if ( DSP_ReopenAudio(this, audiodev, format, spec) < 0 ) { /* Error is set by DSP_ReopenAudio() */ return(-1); } /* Allocate mixing buffer */ mixlen = spec->size; mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); if ( mixbuf == NULL ) { return(-1); } memset(mixbuf, spec->silence, spec->size); #ifndef USE_BLOCKING_WRITES /* Check to see if we need to use select() workaround */ { char *workaround; workaround = getenv("SDL_DSP_NOSELECT"); if ( workaround ) { frame_ticks = (float)(spec->samples*1000)/spec->freq; next_frame = SDL_GetTicks()+frame_ticks; } } #endif /* !USE_BLOCKING_WRITES */ /* Get the parent process id (we're the parent of the audio thread) */ parent = getpid(); /* We're ready to rock and roll. :-) */ return(0); }
static int ESD_OpenDevice(_THIS, const char *devname, int iscapture) { esd_format_t format = (ESD_STREAM | ESD_PLAY); SDL_AudioFormat test_format = 0; int found = 0; /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_memset(this->hidden, 0, (sizeof *this->hidden)); this->hidden->audio_fd = -1; /* Convert audio spec to the ESD audio format */ /* Try for a closest match on audio format */ for (test_format = SDL_FirstAudioFormat(this->spec.format); !found && test_format; test_format = SDL_NextAudioFormat()) { #ifdef DEBUG_AUDIO fprintf(stderr, "Trying format 0x%4.4x\n", test_format); #endif found = 1; switch (test_format) { case AUDIO_U8: format |= ESD_BITS8; break; case AUDIO_S16SYS: format |= ESD_BITS16; break; default: found = 0; break; } } if (!found) { ESD_CloseDevice(this); return SDL_SetError("Couldn't find any hardware audio formats"); } if (this->spec.channels == 1) { format |= ESD_MONO; } else { format |= ESD_STEREO; } #if 0 this->spec.samples = ESD_BUF_SIZE; /* Darn, no way to change this yet */ #endif /* Open a connection to the ESD audio server */ this->hidden->audio_fd = SDL_NAME(esd_play_stream) (format, this->spec.freq, NULL, get_progname()); if (this->hidden->audio_fd < 0) { ESD_CloseDevice(this); return SDL_SetError("Couldn't open ESD connection"); } /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(&this->spec); this->hidden->frame_ticks = (float) (this->spec.samples * 1000) / this->spec.freq; this->hidden->next_frame = SDL_GetTicks() + this->hidden->frame_ticks; /* Allocate mixing buffer */ this->hidden->mixlen = this->spec.size; this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); if (this->hidden->mixbuf == NULL) { ESD_CloseDevice(this); return SDL_OutOfMemory(); } SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); /* Get the parent process id (we're the parent of the audio thread) */ this->hidden->parent = getpid(); /* We're ready to rock and roll. :-) */ return 0; }
static int SDL_FS_OpenDevice(_THIS, const char *devname, int iscapture) { int bytes; SDL_AudioFormat test_format = 0, format = 0; FSSampleFormat fs_format; FSStreamDescription desc; DirectResult ret; /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_memset(this->hidden, 0, (sizeof *this->hidden)); /* Try for a closest match on audio format */ for (test_format = SDL_FirstAudioFormat(this->spec.format); !format && test_format;) { #ifdef DEBUG_AUDIO fprintf(stderr, "Trying format 0x%4.4x\n", test_format); #endif switch (test_format) { case AUDIO_U8: fs_format = FSSF_U8; bytes = 1; format = 1; break; case AUDIO_S16SYS: fs_format = FSSF_S16; bytes = 2; format = 1; break; case AUDIO_S32SYS: fs_format = FSSF_S32; bytes = 4; format = 1; break; case AUDIO_F32SYS: fs_format = FSSF_FLOAT; bytes = 4; format = 1; break; default: format = 0; break; } if (!format) { test_format = SDL_NextAudioFormat(); } } if (format == 0) { SDL_FS_CloseDevice(this); return SDL_SetError("Couldn't find any hardware audio formats"); } this->spec.format = test_format; /* Retrieve the main sound interface. */ ret = SDL_NAME(FusionSoundCreate) (&this->hidden->fs); if (ret) { SDL_FS_CloseDevice(this); return SDL_SetError("Unable to initialize FusionSound: %d", ret); } this->hidden->mixsamples = this->spec.size / bytes / this->spec.channels; /* Fill stream description. */ desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE | FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT | FSSDF_PREBUFFER; desc.samplerate = this->spec.freq; desc.buffersize = this->spec.size * FUSION_BUFFERS; desc.channels = this->spec.channels; desc.prebuffer = 10; desc.sampleformat = fs_format; ret = this->hidden->fs->CreateStream(this->hidden->fs, &desc, &this->hidden->stream); if (ret) { SDL_FS_CloseDevice(this); return SDL_SetError("Unable to create FusionSoundStream: %d", ret); } /* See what we got */ desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE | FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT; ret = this->hidden->stream->GetDescription(this->hidden->stream, &desc); this->spec.freq = desc.samplerate; this->spec.size = desc.buffersize / FUSION_BUFFERS * bytes * desc.channels; this->spec.channels = desc.channels; /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(&this->spec); /* Allocate mixing buffer */ this->hidden->mixlen = this->spec.size; this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); if (this->hidden->mixbuf == NULL) { SDL_FS_CloseDevice(this); return SDL_OutOfMemory(); } SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); /* We're ready to rock and roll. :-) */ return 0; }
int DSP_OpenAudio(_THIS, SDL_AudioSpec * spec) { char audiodev[1024]; #ifdef AUDIO_SETINFO int enc; #endif int desired_freq = spec->freq; /* Initialize our freeable variables, in case we fail */ audio_fd = -1; mixbuf = NULL; ulaw_buf = NULL; /* Determine the audio parameters from the AudioSpec */ switch (SDL_AUDIO_BITSIZE(spec->format)) { case 8: { /* Unsigned 8 bit audio data */ spec->format = AUDIO_U8; #ifdef AUDIO_SETINFO enc = AUDIO_ENCODING_LINEAR8; #endif } break; case 16: { /* Signed 16 bit audio data */ spec->format = AUDIO_S16SYS; #ifdef AUDIO_SETINFO enc = AUDIO_ENCODING_LINEAR; #endif } break; default: { /* !!! FIXME: fallback to conversion on unsupported types! */ SDL_SetError("Unsupported audio format"); return (-1); } } audio_fmt = spec->format; /* Open the audio device */ audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 1); if (audio_fd < 0) { SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); return (-1); } ulaw_only = 0; /* modern Suns do support linear audio */ #ifdef AUDIO_SETINFO for (;;) { audio_info_t info; AUDIO_INITINFO(&info); /* init all fields to "no change" */ /* Try to set the requested settings */ info.play.sample_rate = spec->freq; info.play.channels = spec->channels; info.play.precision = (enc == AUDIO_ENCODING_ULAW) ? 8 : spec->format & 0xff; info.play.encoding = enc; if (ioctl(audio_fd, AUDIO_SETINFO, &info) == 0) { /* Check to be sure we got what we wanted */ if (ioctl(audio_fd, AUDIO_GETINFO, &info) < 0) { SDL_SetError("Error getting audio parameters: %s", strerror(errno)); return -1; } if (info.play.encoding == enc && info.play.precision == (spec->format & 0xff) && info.play.channels == spec->channels) { /* Yow! All seems to be well! */ spec->freq = info.play.sample_rate; break; } } switch (enc) { case AUDIO_ENCODING_LINEAR8: /* unsigned 8bit apparently not supported here */ enc = AUDIO_ENCODING_LINEAR; spec->format = AUDIO_S16SYS; break; /* try again */ case AUDIO_ENCODING_LINEAR: /* linear 16bit didn't work either, resort to µ-law */ enc = AUDIO_ENCODING_ULAW; spec->channels = 1; spec->freq = 8000; spec->format = AUDIO_U8; ulaw_only = 1; break; default: /* oh well... */ SDL_SetError("Error setting audio parameters: %s", strerror(errno)); return -1; } } #endif /* AUDIO_SETINFO */ written = 0; /* We can actually convert on-the-fly to U-Law */ if (ulaw_only) { spec->freq = desired_freq; fragsize = (spec->samples * 1000) / (spec->freq / 8); frequency = 8; ulaw_buf = (Uint8 *) SDL_malloc(fragsize); if (ulaw_buf == NULL) { SDL_OutOfMemory(); return (-1); } spec->channels = 1; } else { fragsize = spec->samples; frequency = spec->freq / 1000; } #ifdef DEBUG_AUDIO fprintf(stderr, "Audio device %s U-Law only\n", ulaw_only ? "is" : "is not"); fprintf(stderr, "format=0x%x chan=%d freq=%d\n", spec->format, spec->channels, spec->freq); #endif /* Update the fragment size as size in bytes */ SDL_CalculateAudioSpec(spec); /* Allocate mixing buffer */ mixbuf = (Uint8 *) SDL_AllocAudioMem(spec->size); if (mixbuf == NULL) { SDL_OutOfMemory(); return (-1); } SDL_memset(mixbuf, spec->silence, spec->size); /* We're ready to rock and roll. :-) */ return (0); }
static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec) { char audiodev[1024]; int format; int value; int frag_spec; Uint16 test_format; /* Open the audio device */ audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0); if ( audio_fd < 0 ) { SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); return(-1); } mixbuf = NULL; /* Make the file descriptor use blocking writes with fcntl() */ { long flags; flags = fcntl(audio_fd, F_GETFL); flags &= ~O_NONBLOCK; if ( fcntl(audio_fd, F_SETFL, flags) < 0 ) { SDL_SetError("Couldn't set audio blocking mode"); return(-1); } } /* Get a list of supported hardware formats */ if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) { perror("SNDCTL_DSP_GETFMTS"); SDL_SetError("Couldn't get audio format list"); return(-1); } /* Try for a closest match on audio format */ format = 0; for ( test_format = SDL_FirstAudioFormat(spec->format); ! format && test_format; ) { #ifdef DEBUG_AUDIO fprintf(stderr, "Trying format 0x%4.4x\n", test_format); #endif switch ( test_format ) { case AUDIO_U8: if ( value & AFMT_U8 ) { format = AFMT_U8; } break; case AUDIO_S16LSB: if ( value & AFMT_S16_LE ) { format = AFMT_S16_LE; } break; case AUDIO_S16MSB: if ( value & AFMT_S16_BE ) { format = AFMT_S16_BE; } break; #if 0 /* * These formats are not used by any real life systems so they are not * needed here. */ case AUDIO_S8: if ( value & AFMT_S8 ) { format = AFMT_S8; } break; case AUDIO_U16LSB: if ( value & AFMT_U16_LE ) { format = AFMT_U16_LE; } break; case AUDIO_U16MSB: if ( value & AFMT_U16_BE ) { format = AFMT_U16_BE; } break; #endif default: format = 0; break; } if ( ! format ) { test_format = SDL_NextAudioFormat(); } } if ( format == 0 ) { SDL_SetError("Couldn't find any hardware audio formats"); return(-1); } spec->format = test_format; /* Set the audio format */ value = format; if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || (value != format) ) { perror("SNDCTL_DSP_SETFMT"); SDL_SetError("Couldn't set audio format"); return(-1); } /* Set the number of channels of output */ value = spec->channels; if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0 ) { perror("SNDCTL_DSP_CHANNELS"); SDL_SetError("Cannot set the number of channels"); return(-1); } spec->channels = value; /* Set the DSP frequency */ value = spec->freq; if ( ioctl(audio_fd, SNDCTL_DSP_SPEED, &value) < 0 ) { perror("SNDCTL_DSP_SPEED"); SDL_SetError("Couldn't set audio frequency"); return(-1); } spec->freq = value; /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(spec); /* Determine the power of two of the fragment size */ for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec ); if ( (0x01<<frag_spec) != spec->size ) { SDL_SetError("Fragment size must be a power of two"); return(-1); } frag_spec |= 0x00020000; /* two fragments, for low latency */ /* Set the audio buffering parameters */ #ifdef DEBUG_AUDIO fprintf(stderr, "Requesting %d fragments of size %d\n", (frag_spec >> 16), 1<<(frag_spec&0xFFFF)); #endif if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) { perror("SNDCTL_DSP_SETFRAGMENT"); fprintf(stderr, "Warning: Couldn't set audio fragment size\n"); } #ifdef DEBUG_AUDIO { audio_buf_info info; ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info); fprintf(stderr, "fragments = %d\n", info.fragments); fprintf(stderr, "fragstotal = %d\n", info.fragstotal); fprintf(stderr, "fragsize = %d\n", info.fragsize); fprintf(stderr, "bytes = %d\n", info.bytes); } #endif /* Allocate mixing buffer */ mixlen = spec->size; mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); if ( mixbuf == NULL ) { return(-1); } memset(mixbuf, spec->silence, spec->size); /* Get the parent process id (we're the parent of the audio thread) */ parent = getpid(); /* We're ready to rock and roll. :-) */ return(0); }
static int IRIXAUDIO_OpenDevice(_THIS, const char *devname, int iscapture) { SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format); long width = 0; long fmt = 0; int valid = 0; /* !!! FIXME: Handle multiple devices and capture? */ /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { SDL_OutOfMemory(); return 0; } SDL_memset(this->hidden, 0, (sizeof *this->hidden)); #ifdef OLD_IRIX_AUDIO { long audio_param[2]; audio_param[0] = AL_OUTPUT_RATE; audio_param[1] = this->spec.freq; valid = (ALsetparams(AL_DEFAULT_DEVICE, audio_param, 2) < 0); } #else { ALpv audio_param; audio_param.param = AL_RATE; audio_param.value.i = this->spec.freq; valid = (alSetParams(AL_DEFAULT_OUTPUT, &audio_param, 1) < 0); } #endif while ((!valid) && (test_format)) { valid = 1; this->spec.format = test_format; switch (test_format) { case AUDIO_S8: width = AL_SAMPLE_8; fmt = AL_SAMPFMT_TWOSCOMP; break; case AUDIO_S16SYS: width = AL_SAMPLE_16; fmt = AL_SAMPFMT_TWOSCOMP; break; case AUDIO_F32SYS: width = 0; /* not used here... */ fmt = AL_SAMPFMT_FLOAT; break; /* Docs say there is int24, but not int32.... */ default: valid = 0; test_format = SDL_NextAudioFormat(); break; } if (valid) { ALconfig audio_config = alNewConfig(); valid = 0; if (audio_config) { if (alSetChannels(audio_config, this->spec.channels) < 0) { if (this->spec.channels > 2) { /* can't handle > stereo? */ this->spec.channels = 2; /* try again below. */ } } if ((alSetSampFmt(audio_config, fmt) >= 0) && ((!width) || (alSetWidth(audio_config, width) >= 0)) && (alSetQueueSize(audio_config, this->spec.samples * 2) >= 0) && (alSetChannels(audio_config, this->spec.channels) >= 0)) { this->hidden->audio_port = alOpenPort("SDL audio", "w", audio_config); if (this->hidden->audio_port == NULL) { /* docs say AL_BAD_CHANNELS happens here, too. */ int err = oserror(); if (err == AL_BAD_CHANNELS) { this->spec.channels = 2; alSetChannels(audio_config, this->spec.channels); this->hidden->audio_port = alOpenPort("SDL audio", "w", audio_config); } } if (this->hidden->audio_port != NULL) { valid = 1; } } alFreeConfig(audio_config); } } } if (!valid) { IRIXAUDIO_CloseDevice(this); SDL_SetError("Unsupported audio format"); return 0; } /* Update the fragment size as size in bytes */ SDL_CalculateAudioSpec(&this->spec); /* Allocate mixing buffer */ this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->spec.size); if (this->hidden->mixbuf == NULL) { IRIXAUDIO_CloseDevice(this); SDL_OutOfMemory(); return 0; } SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); /* We're ready to rock and roll. :-) */ return 1; }
static int PULSE_OpenAudio(_THIS, SDL_AudioSpec *spec) { int state; Uint16 test_format; pa_sample_spec paspec; pa_buffer_attr paattr; pa_channel_map pacmap; pa_stream_flags_t flags = 0; paspec.format = PA_SAMPLE_INVALID; for ( test_format = SDL_FirstAudioFormat(spec->format); test_format; ) { switch ( test_format ) { case AUDIO_U8: paspec.format = PA_SAMPLE_U8; break; case AUDIO_S16LSB: paspec.format = PA_SAMPLE_S16LE; break; case AUDIO_S16MSB: paspec.format = PA_SAMPLE_S16BE; break; } if ( paspec.format != PA_SAMPLE_INVALID ) break; } if (paspec.format == PA_SAMPLE_INVALID ) { SDL_SetError("Couldn't find any suitable audio formats"); return(-1); } spec->format = test_format; paspec.channels = spec->channels; paspec.rate = spec->freq; /* Calculate the final parameters for this audio specification */ #ifdef PA_STREAM_ADJUST_LATENCY spec->samples /= 2; /* Mix in smaller chunck to avoid underruns */ #endif SDL_CalculateAudioSpec(spec); /* Allocate mixing buffer */ mixlen = spec->size; mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); if ( mixbuf == NULL ) { return(-1); } SDL_memset(mixbuf, spec->silence, spec->size); /* Reduced prebuffering compared to the defaults. */ #ifdef PA_STREAM_ADJUST_LATENCY paattr.tlength = mixlen * 4; /* 2x original requested bufsize */ paattr.prebuf = -1; paattr.maxlength = -1; paattr.minreq = mixlen; /* -1 can lead to pa_stream_writable_size() >= mixlen never becoming true */ flags = PA_STREAM_ADJUST_LATENCY; #else paattr.tlength = mixlen*2; paattr.prebuf = mixlen*2; paattr.maxlength = mixlen*2; paattr.minreq = mixlen; #endif /* The SDL ALSA output hints us that we use Windows' channel mapping */ /* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */ SDL_NAME(pa_channel_map_init_auto)( &pacmap, spec->channels, PA_CHANNEL_MAP_WAVEEX); /* Set up a new main loop */ if (!(mainloop = SDL_NAME(pa_mainloop_new)())) { PULSE_CloseAudio(this); SDL_SetError("pa_mainloop_new() failed"); return(-1); } mainloop_api = SDL_NAME(pa_mainloop_get_api)(mainloop); if (!(context = SDL_NAME(pa_context_new)(mainloop_api, get_progname()))) { PULSE_CloseAudio(this); SDL_SetError("pa_context_new() failed"); return(-1); } /* Connect to the PulseAudio server */ if (SDL_NAME(pa_context_connect)(context, NULL, 0, NULL) < 0) { PULSE_CloseAudio(this); SDL_SetError("Could not setup connection to PulseAudio"); return(-1); } do { if (SDL_NAME(pa_mainloop_iterate)(mainloop, 1, NULL) < 0) { PULSE_CloseAudio(this); SDL_SetError("pa_mainloop_iterate() failed"); return(-1); } state = SDL_NAME(pa_context_get_state)(context); if (!PA_CONTEXT_IS_GOOD(state)) { PULSE_CloseAudio(this); SDL_SetError("Could not connect to PulseAudio"); return(-1); } } while (state != PA_CONTEXT_READY); stream = SDL_NAME(pa_stream_new)( context, "Simple DirectMedia Layer", /* stream description */ &paspec, /* sample format spec */ &pacmap /* channel map */ ); if ( stream == NULL ) { PULSE_CloseAudio(this); SDL_SetError("Could not setup PulseAudio stream"); return(-1); } if (SDL_NAME(pa_stream_connect_playback)(stream, NULL, &paattr, flags, NULL, NULL) < 0) { PULSE_CloseAudio(this); SDL_SetError("Could not connect PulseAudio stream"); return(-1); } do { if (SDL_NAME(pa_mainloop_iterate)(mainloop, 1, NULL) < 0) { PULSE_CloseAudio(this); SDL_SetError("pa_mainloop_iterate() failed"); return(-1); } state = SDL_NAME(pa_stream_get_state)(stream); if (!PA_STREAM_IS_GOOD(state)) { PULSE_CloseAudio(this); SDL_SetError("Could not create to PulseAudio stream"); return(-1); } } while (state != PA_STREAM_READY); return(0); }
static int PULSE_OpenAudio(_THIS, SDL_AudioSpec *spec) { int state; Uint16 test_format; pa_sample_spec paspec; pa_buffer_attr paattr; pa_channel_map pacmap; pa_stream_flags_t flags = 0; paspec.format = PA_SAMPLE_INVALID; for ( test_format = SDL_FirstAudioFormat(spec->format); test_format; ) { switch ( test_format ) { case AUDIO_U8: paspec.format = PA_SAMPLE_U8; break; case AUDIO_S16LSB: paspec.format = PA_SAMPLE_S16LE; break; case AUDIO_S16MSB: paspec.format = PA_SAMPLE_S16BE; break; } if ( paspec.format != PA_SAMPLE_INVALID ) break; test_format = SDL_NextAudioFormat(); } if (paspec.format == PA_SAMPLE_INVALID ) { SDL_SetError("Couldn't find any suitable audio formats"); return(-1); } spec->format = test_format; paspec.channels = spec->channels; paspec.rate = spec->freq; #ifdef PA_STREAM_ADJUST_LATENCY spec->samples /= 2; #endif SDL_CalculateAudioSpec(spec); mixlen = spec->size; mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); if ( mixbuf == NULL ) { return(-1); } SDL_memset(mixbuf, spec->silence, spec->size); #ifdef PA_STREAM_ADJUST_LATENCY paattr.tlength = mixlen * 4; paattr.prebuf = -1; paattr.maxlength = -1; paattr.minreq = mixlen; flags = PA_STREAM_ADJUST_LATENCY; #else paattr.tlength = mixlen*2; paattr.prebuf = mixlen*2; paattr.maxlength = mixlen*2; paattr.minreq = mixlen; #endif SDL_NAME(pa_channel_map_init_auto)( &pacmap, spec->channels, PA_CHANNEL_MAP_WAVEEX); if (!(mainloop = SDL_NAME(pa_mainloop_new)())) { PULSE_CloseAudio(this); SDL_SetError("pa_mainloop_new() failed"); return(-1); } if (this->hidden->caption == NULL) { char *title = NULL; SDL_WM_GetCaption(&title, NULL); PULSE_SetCaption(this, title); } mainloop_api = SDL_NAME(pa_mainloop_get_api)(mainloop); if (!(context = SDL_NAME(pa_context_new)(mainloop_api, this->hidden->caption))) { PULSE_CloseAudio(this); SDL_SetError("pa_context_new() failed"); return(-1); } if (SDL_NAME(pa_context_connect)(context, NULL, 0, NULL) < 0) { PULSE_CloseAudio(this); SDL_SetError("Could not setup connection to PulseAudio"); return(-1); } do { if (SDL_NAME(pa_mainloop_iterate)(mainloop, 1, NULL) < 0) { PULSE_CloseAudio(this); SDL_SetError("pa_mainloop_iterate() failed"); return(-1); } state = SDL_NAME(pa_context_get_state)(context); if (!PA_CONTEXT_IS_GOOD(state)) { PULSE_CloseAudio(this); SDL_SetError("Could not connect to PulseAudio"); return(-1); } } while (state != PA_CONTEXT_READY); stream = SDL_NAME(pa_stream_new)( context, "Simple DirectMedia Layer", &paspec, &pacmap ); if ( stream == NULL ) { PULSE_CloseAudio(this); SDL_SetError("Could not setup PulseAudio stream"); return(-1); } if (SDL_NAME(pa_stream_connect_playback)(stream, NULL, &paattr, flags, NULL, NULL) < 0) { PULSE_CloseAudio(this); SDL_SetError("Could not connect PulseAudio stream"); return(-1); } do { if (SDL_NAME(pa_mainloop_iterate)(mainloop, 1, NULL) < 0) { PULSE_CloseAudio(this); SDL_SetError("pa_mainloop_iterate() failed"); return(-1); } state = SDL_NAME(pa_stream_get_state)(stream); if (!PA_STREAM_IS_GOOD(state)) { PULSE_CloseAudio(this); SDL_SetError("Could not create to PulseAudio stream"); return(-1); } } while (state != PA_STREAM_READY); return(0); }
static int NAS_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) { AuElement elms[3]; int buffer_size; SDL_AudioFormat test_format, format; /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_memset(this->hidden, 0, (sizeof *this->hidden)); /* Try for a closest match on audio format */ format = 0; for (test_format = SDL_FirstAudioFormat(this->spec.format); !format && test_format;) { format = sdlformat_to_auformat(test_format); if (format == AuNone) { test_format = SDL_NextAudioFormat(); } } if (format == 0) { NAS_CloseDevice(this); return SDL_SetError("NAS: Couldn't find any hardware audio formats"); } this->spec.format = test_format; this->hidden->aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL); if (this->hidden->aud == 0) { NAS_CloseDevice(this); return SDL_SetError("NAS: Couldn't open connection to NAS server"); } this->hidden->dev = find_device(this, this->spec.channels); if ((this->hidden->dev == AuNone) || (!(this->hidden->flow = NAS_AuCreateFlow(this->hidden->aud, 0)))) { NAS_CloseDevice(this); return SDL_SetError("NAS: Couldn't find a fitting device on NAS server"); } buffer_size = this->spec.freq; if (buffer_size < 4096) buffer_size = 4096; if (buffer_size > 32768) buffer_size = 32768; /* So that the buffer won't get unmanageably big. */ /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(&this->spec); this2 = this->hidden; AuMakeElementImportClient(elms, this->spec.freq, format, this->spec.channels, AuTrue, buffer_size, buffer_size / 4, 0, NULL); AuMakeElementExportDevice(elms + 1, 0, this->hidden->dev, this->spec.freq, AuUnlimitedSamples, 0, NULL); NAS_AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, 2, elms, NULL); NAS_AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0, this->hidden->flow, event_handler, (AuPointer) NULL); NAS_AuStartFlow(this->hidden->aud, this->hidden->flow, NULL); /* Allocate mixing buffer */ this->hidden->mixlen = this->spec.size; this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); if (this->hidden->mixbuf == NULL) { NAS_CloseDevice(this); return SDL_OutOfMemory(); } SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); /* We're ready to rock and roll. :-) */ return 0; }
static int SNDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) { SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format); struct sio_par par; int status; this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc(sizeof(*this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_memset(this->hidden, 0, sizeof(*this->hidden)); this->hidden->mixlen = this->spec.size; /* !!! FIXME: SIO_DEVANY can be a specific device... */ if ((this->hidden->dev = SNDIO_sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL) { SNDIO_CloseDevice(this); return SDL_SetError("sio_open() failed"); } SNDIO_sio_initpar(&par); par.rate = this->spec.freq; par.pchan = this->spec.channels; par.round = this->spec.samples; par.appbufsz = par.round * 2; /* Try for a closest match on audio format */ status = -1; while (test_format && (status < 0)) { if (!SDL_AUDIO_ISFLOAT(test_format)) { par.le = SDL_AUDIO_ISLITTLEENDIAN(test_format) ? 1 : 0; par.sig = SDL_AUDIO_ISSIGNED(test_format) ? 1 : 0; par.bits = SDL_AUDIO_BITSIZE(test_format); if (SNDIO_sio_setpar(this->hidden->dev, &par) == 0) { continue; } if (SNDIO_sio_getpar(this->hidden->dev, &par) == 0) { SNDIO_CloseDevice(this); return SDL_SetError("sio_getpar() failed"); } if (par.bps != SIO_BPS(par.bits)) { continue; } if ((par.bits == 8 * par.bps) || (par.msb)) { status = 0; break; } } test_format = SDL_NextAudioFormat(); } if (status < 0) { SNDIO_CloseDevice(this); return SDL_SetError("sndio: Couldn't find any hardware audio formats"); } if ((par.bps == 4) && (par.sig) && (par.le)) this->spec.format = AUDIO_S32LSB; else if ((par.bps == 4) && (par.sig) && (!par.le)) this->spec.format = AUDIO_S32MSB; else if ((par.bps == 2) && (par.sig) && (par.le)) this->spec.format = AUDIO_S16LSB; else if ((par.bps == 2) && (par.sig) && (!par.le)) this->spec.format = AUDIO_S16MSB; else if ((par.bps == 2) && (!par.sig) && (par.le)) this->spec.format = AUDIO_U16LSB; else if ((par.bps == 2) && (!par.sig) && (!par.le)) this->spec.format = AUDIO_U16MSB; else if ((par.bps == 1) && (par.sig)) this->spec.format = AUDIO_S8; else if ((par.bps == 1) && (!par.sig)) this->spec.format = AUDIO_U8; else { SNDIO_CloseDevice(this); return SDL_SetError("sndio: Got unsupported hardware audio format."); } this->spec.freq = par.rate; this->spec.channels = par.pchan; this->spec.samples = par.round; /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(&this->spec); /* Allocate mixing buffer */ this->hidden->mixlen = this->spec.size; this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); if (this->hidden->mixbuf == NULL) { SNDIO_CloseDevice(this); return SDL_OutOfMemory(); } SDL_memset(this->hidden->mixbuf, this->spec.silence, this->hidden->mixlen); if (!SNDIO_sio_start(this->hidden->dev)) { return SDL_SetError("sio_start() failed"); } /* We're ready to rock and roll. :-) */ return 0; }
static int PAUDIO_OpenDevice(_THIS, const char *devname, int iscapture) { const char *workaround = SDL_getenv("SDL_DSP_NOSELECT"); char audiodev[1024]; const char *err = NULL; int format; int bytes_per_sample; SDL_AudioFormat test_format; audio_init paud_init; audio_buffer paud_bufinfo; audio_status paud_status; audio_control paud_control; audio_change paud_change; int fd = -1; /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_memset(this->hidden, 0, (sizeof *this->hidden)); /* Open the audio device */ fd = OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0); this->hidden->audio_fd = fd; if (fd < 0) { PAUDIO_CloseDevice(this); return SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); } /* * We can't set the buffer size - just ask the device for the maximum * that we can have. */ if (ioctl(fd, AUDIO_BUFFER, &paud_bufinfo) < 0) { PAUDIO_CloseDevice(this); return SDL_SetError("Couldn't get audio buffer information"); } if (this->spec.channels > 1) this->spec.channels = 2; else this->spec.channels = 1; /* * Fields in the audio_init structure: * * Ignored by us: * * paud.loadpath[LOAD_PATH]; * DSP code to load, MWave chip only? * paud.slot_number; * slot number of the adapter * paud.device_id; * adapter identification number * * Input: * * paud.srate; * the sampling rate in Hz * paud.bits_per_sample; * 8, 16, 32, ... * paud.bsize; * block size for this rate * paud.mode; * ADPCM, PCM, MU_LAW, A_LAW, SOURCE_MIX * paud.channels; * 1=mono, 2=stereo * paud.flags; * FIXED - fixed length data * * LEFT_ALIGNED, RIGHT_ALIGNED (var len only) * * TWOS_COMPLEMENT - 2's complement data * * SIGNED - signed? comment seems wrong in sys/audio.h * * BIG_ENDIAN * paud.operation; * PLAY, RECORD * * Output: * * paud.flags; * PITCH - pitch is supported * * INPUT - input is supported * * OUTPUT - output is supported * * MONITOR - monitor is supported * * VOLUME - volume is supported * * VOLUME_DELAY - volume delay is supported * * BALANCE - balance is supported * * BALANCE_DELAY - balance delay is supported * * TREBLE - treble control is supported * * BASS - bass control is supported * * BESTFIT_PROVIDED - best fit returned * * LOAD_CODE - DSP load needed * paud.rc; * NO_PLAY - DSP code can't do play requests * * NO_RECORD - DSP code can't do record requests * * INVALID_REQUEST - request was invalid * * CONFLICT - conflict with open's flags * * OVERLOADED - out of DSP MIPS or memory * paud.position_resolution; * smallest increment for position */ paud_init.srate = this->spec.freq; paud_init.mode = PCM; paud_init.operation = PLAY; paud_init.channels = this->spec.channels; /* Try for a closest match on audio format */ format = 0; for (test_format = SDL_FirstAudioFormat(this->spec.format); !format && test_format;) { #ifdef DEBUG_AUDIO fprintf(stderr, "Trying format 0x%4.4x\n", test_format); #endif switch (test_format) { case AUDIO_U8: bytes_per_sample = 1; paud_init.bits_per_sample = 8; paud_init.flags = TWOS_COMPLEMENT | FIXED; format = 1; break; case AUDIO_S8: bytes_per_sample = 1; paud_init.bits_per_sample = 8; paud_init.flags = SIGNED | TWOS_COMPLEMENT | FIXED; format = 1; break; case AUDIO_S16LSB: bytes_per_sample = 2; paud_init.bits_per_sample = 16; paud_init.flags = SIGNED | TWOS_COMPLEMENT | FIXED; format = 1; break; case AUDIO_S16MSB: bytes_per_sample = 2; paud_init.bits_per_sample = 16; paud_init.flags = BIG_ENDIAN | SIGNED | TWOS_COMPLEMENT | FIXED; format = 1; break; case AUDIO_U16LSB: bytes_per_sample = 2; paud_init.bits_per_sample = 16; paud_init.flags = TWOS_COMPLEMENT | FIXED; format = 1; break; case AUDIO_U16MSB: bytes_per_sample = 2; paud_init.bits_per_sample = 16; paud_init.flags = BIG_ENDIAN | TWOS_COMPLEMENT | FIXED; format = 1; break; default: break; } if (!format) { test_format = SDL_NextAudioFormat(); } } if (format == 0) { #ifdef DEBUG_AUDIO fprintf(stderr, "Couldn't find any hardware audio formats\n"); #endif PAUDIO_CloseDevice(this); return SDL_SetError("Couldn't find any hardware audio formats"); } this->spec.format = test_format; /* * We know the buffer size and the max number of subsequent writes * that can be pending. If more than one can pend, allow the application * to do something like double buffering between our write buffer and * the device's own buffer that we are filling with write() anyway. * * We calculate this->spec.samples like this because * SDL_CalculateAudioSpec() will give put paud_bufinfo.write_buf_cap * (or paud_bufinfo.write_buf_cap/2) into this->spec.size in return. */ if (paud_bufinfo.request_buf_cap == 1) { this->spec.samples = paud_bufinfo.write_buf_cap / bytes_per_sample / this->spec.channels; } else { this->spec.samples = paud_bufinfo.write_buf_cap / bytes_per_sample / this->spec.channels / 2; } paud_init.bsize = bytes_per_sample * this->spec.channels; SDL_CalculateAudioSpec(&this->spec); /* * The AIX paud device init can't modify the values of the audio_init * structure that we pass to it. So we don't need any recalculation * of this stuff and no reinit call as in linux dsp code. * * /dev/paud supports all of the encoding formats, so we don't need * to do anything like reopening the device, either. */ if (ioctl(fd, AUDIO_INIT, &paud_init) < 0) { switch (paud_init.rc) { case 1: err = "Couldn't set audio format: DSP can't do play requests"; break; case 2: err = "Couldn't set audio format: DSP can't do record requests"; break; case 4: err = "Couldn't set audio format: request was invalid"; break; case 5: err = "Couldn't set audio format: conflict with open's flags"; break; case 6: err = "Couldn't set audio format: out of DSP MIPS or memory"; break; default: err = "Couldn't set audio format: not documented in sys/audio.h"; break; } } if (err != NULL) { PAUDIO_CloseDevice(this); return SDL_SetError("Paudio: %s", err); } /* Allocate mixing buffer */ this->hidden->mixlen = this->spec.size; this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); if (this->hidden->mixbuf == NULL) { PAUDIO_CloseDevice(this); return SDL_OutOfMemory(); } SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); /* * Set some paramters: full volume, first speaker that we can find. * Ignore the other settings for now. */ paud_change.input = AUDIO_IGNORE; /* the new input source */ paud_change.output = OUTPUT_1; /* EXTERNAL_SPEAKER,INTERNAL_SPEAKER,OUTPUT_1 */ paud_change.monitor = AUDIO_IGNORE; /* the new monitor state */ paud_change.volume = 0x7fffffff; /* volume level [0-0x7fffffff] */ paud_change.volume_delay = AUDIO_IGNORE; /* the new volume delay */ paud_change.balance = 0x3fffffff; /* the new balance */ paud_change.balance_delay = AUDIO_IGNORE; /* the new balance delay */ paud_change.treble = AUDIO_IGNORE; /* the new treble state */ paud_change.bass = AUDIO_IGNORE; /* the new bass state */ paud_change.pitch = AUDIO_IGNORE; /* the new pitch state */ paud_control.ioctl_request = AUDIO_CHANGE; paud_control.request_info = (char *) &paud_change; if (ioctl(fd, AUDIO_CONTROL, &paud_control) < 0) { #ifdef DEBUG_AUDIO fprintf(stderr, "Can't change audio display settings\n"); #endif } /* * Tell the device to expect data. Actual start will wait for * the first write() call. */ paud_control.ioctl_request = AUDIO_START; paud_control.position = 0; if (ioctl(fd, AUDIO_CONTROL, &paud_control) < 0) { PAUDIO_CloseDevice(this); #ifdef DEBUG_AUDIO fprintf(stderr, "Can't start audio play\n"); #endif return SDL_SetError("Can't start audio play"); } /* Check to see if we need to use select() workaround */ if (workaround != NULL) { this->hidden->frame_ticks = (float) (this->spec.samples * 1000) / this->spec.freq; this->hidden->next_frame = SDL_GetTicks() + this->hidden->frame_ticks; } /* We're ready to rock and roll. :-) */ return 0; }
static int OBSD_OpenAudio(_THIS, SDL_AudioSpec *spec) { char audiodev[64]; Uint16 format; audio_info_t info; AUDIO_INITINFO(&info); SDL_CalculateAudioSpec(spec); #ifdef USE_TIMER_SYNC frame_ticks = 0.0; #endif audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0); if(audio_fd < 0) { SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); return(-1); } info.mode = AUMODE_PLAY; if(ioctl(audio_fd, AUDIO_SETINFO, &info) < 0) { SDL_SetError("Couldn't put device into play mode"); return(-1); } mixbuf = NULL; AUDIO_INITINFO(&info); for (format = SDL_FirstAudioFormat(spec->format); format; format = SDL_NextAudioFormat()) { switch(format) { case AUDIO_U8: info.play.encoding = AUDIO_ENCODING_ULINEAR; info.play.precision = 8; break; case AUDIO_S8: info.play.encoding = AUDIO_ENCODING_SLINEAR; info.play.precision = 8; break; case AUDIO_S16LSB: info.play.encoding = AUDIO_ENCODING_SLINEAR_LE; info.play.precision = 16; break; case AUDIO_S16MSB: info.play.encoding = AUDIO_ENCODING_SLINEAR_BE; info.play.precision = 16; break; case AUDIO_U16LSB: info.play.encoding = AUDIO_ENCODING_ULINEAR_LE; info.play.precision = 16; break; case AUDIO_U16MSB: info.play.encoding = AUDIO_ENCODING_ULINEAR_BE; info.play.precision = 16; break; default: continue; } if (ioctl(audio_fd, AUDIO_SETINFO, &info) == 0) break; } if(!format) { SDL_SetError("No supported encoding for 0x%x", spec->format); return(-1); } spec->format = format; AUDIO_INITINFO(&info); info.play.channels = spec->channels; if (ioctl(audio_fd, AUDIO_SETINFO, &info) == -1) spec->channels = 1; AUDIO_INITINFO(&info); info.play.sample_rate = spec->freq; info.blocksize = spec->size; info.hiwat = 5; info.lowat = 3; (void)ioctl(audio_fd, AUDIO_SETINFO, &info); (void)ioctl(audio_fd, AUDIO_GETINFO, &info); spec->freq = info.play.sample_rate; mixlen = spec->size; mixbuf = (Uint8*)SDL_AllocAudioMem(mixlen); if(mixbuf == NULL) { return(-1); } SDL_memset(mixbuf, spec->silence, spec->size); parent = getpid(); #ifdef DEBUG_AUDIO OBSD_Status(this); #endif return(0); }
static int PULSEAUDIO_OpenDevice(_THIS, const char *devname, int iscapture) { struct SDL_PrivateAudioData *h = NULL; Uint16 test_format = 0; pa_sample_spec paspec; pa_buffer_attr paattr; pa_channel_map pacmap; pa_stream_flags_t flags = 0; int state = 0; /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_memset(this->hidden, 0, (sizeof *this->hidden)); h = this->hidden; paspec.format = PA_SAMPLE_INVALID; /* Try for a closest match on audio format */ for (test_format = SDL_FirstAudioFormat(this->spec.format); (paspec.format == PA_SAMPLE_INVALID) && test_format;) { #ifdef DEBUG_AUDIO fprintf(stderr, "Trying format 0x%4.4x\n", test_format); #endif switch (test_format) { case AUDIO_U8: paspec.format = PA_SAMPLE_U8; break; case AUDIO_S16LSB: paspec.format = PA_SAMPLE_S16LE; break; case AUDIO_S16MSB: paspec.format = PA_SAMPLE_S16BE; break; case AUDIO_S32LSB: paspec.format = PA_SAMPLE_S32LE; break; case AUDIO_S32MSB: paspec.format = PA_SAMPLE_S32BE; break; case AUDIO_F32LSB: paspec.format = PA_SAMPLE_FLOAT32LE; break; case AUDIO_F32MSB: paspec.format = PA_SAMPLE_FLOAT32BE; break; default: paspec.format = PA_SAMPLE_INVALID; break; } if (paspec.format == PA_SAMPLE_INVALID) { test_format = SDL_NextAudioFormat(); } } if (paspec.format == PA_SAMPLE_INVALID) { PULSEAUDIO_CloseDevice(this); return SDL_SetError("Couldn't find any hardware audio formats"); } this->spec.format = test_format; /* Calculate the final parameters for this audio specification */ #ifdef PA_STREAM_ADJUST_LATENCY this->spec.samples /= 2; /* Mix in smaller chunck to avoid underruns */ #endif SDL_CalculateAudioSpec(&this->spec); /* Allocate mixing buffer */ h->mixlen = this->spec.size; h->mixbuf = (Uint8 *) SDL_AllocAudioMem(h->mixlen); if (h->mixbuf == NULL) { PULSEAUDIO_CloseDevice(this); return SDL_OutOfMemory(); } SDL_memset(h->mixbuf, this->spec.silence, this->spec.size); paspec.channels = this->spec.channels; paspec.rate = this->spec.freq; /* Reduced prebuffering compared to the defaults. */ #ifdef PA_STREAM_ADJUST_LATENCY /* 2x original requested bufsize */ paattr.tlength = h->mixlen * 4; paattr.prebuf = -1; paattr.maxlength = -1; /* -1 can lead to pa_stream_writable_size() >= mixlen never being true */ paattr.minreq = h->mixlen; flags = PA_STREAM_ADJUST_LATENCY; #else paattr.tlength = h->mixlen*2; paattr.prebuf = h->mixlen*2; paattr.maxlength = h->mixlen*2; paattr.minreq = h->mixlen; #endif /* The SDL ALSA output hints us that we use Windows' channel mapping */ /* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */ PULSEAUDIO_pa_channel_map_init_auto(&pacmap, this->spec.channels, PA_CHANNEL_MAP_WAVEEX); /* Set up a new main loop */ if (!(h->mainloop = PULSEAUDIO_pa_mainloop_new())) { PULSEAUDIO_CloseDevice(this); return SDL_SetError("pa_mainloop_new() failed"); } h->mainloop_api = PULSEAUDIO_pa_mainloop_get_api(h->mainloop); h->context = PULSEAUDIO_pa_context_new(h->mainloop_api, getAppName()); if (!h->context) { PULSEAUDIO_CloseDevice(this); return SDL_SetError("pa_context_new() failed"); } /* Connect to the PulseAudio server */ if (PULSEAUDIO_pa_context_connect(h->context, NULL, 0, NULL) < 0) { PULSEAUDIO_CloseDevice(this); return SDL_SetError("Could not setup connection to PulseAudio"); } do { if (PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) { PULSEAUDIO_CloseDevice(this); return SDL_SetError("pa_mainloop_iterate() failed"); } state = PULSEAUDIO_pa_context_get_state(h->context); if (!PA_CONTEXT_IS_GOOD(state)) { PULSEAUDIO_CloseDevice(this); return SDL_SetError("Could not connect to PulseAudio"); } } while (state != PA_CONTEXT_READY); h->stream = PULSEAUDIO_pa_stream_new( h->context, "Simple DirectMedia Layer", /* stream description */ &paspec, /* sample format spec */ &pacmap /* channel map */ ); if (h->stream == NULL) { PULSEAUDIO_CloseDevice(this); return SDL_SetError("Could not set up PulseAudio stream"); } if (PULSEAUDIO_pa_stream_connect_playback(h->stream, NULL, &paattr, flags, NULL, NULL) < 0) { PULSEAUDIO_CloseDevice(this); return SDL_SetError("Could not connect PulseAudio stream"); } do { if (PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) { PULSEAUDIO_CloseDevice(this); return SDL_SetError("pa_mainloop_iterate() failed"); } state = PULSEAUDIO_pa_stream_get_state(h->stream); if (!PA_STREAM_IS_GOOD(state)) { PULSEAUDIO_CloseDevice(this); return SDL_SetError("Could not create to PulseAudio stream"); } } while (state != PA_STREAM_READY); /* We're ready to rock and roll. :-) */ return 0; }
static int ARTSC_OpenAudio(_THIS, SDL_AudioSpec *spec) { int bits, frag_spec; Uint16 test_format, format; /* Reset the timer synchronization flag */ frame_ticks = 0.0; mixbuf = NULL; /* Try for a closest match on audio format */ format = 0; bits = 0; for ( test_format = SDL_FirstAudioFormat(spec->format); ! format && test_format; ) { #ifdef DEBUG_AUDIO fprintf(stderr, "Trying format 0x%4.4x\n", test_format); #endif switch ( test_format ) { case AUDIO_U8: bits = 8; format = 1; break; case AUDIO_S16LSB: bits = 16; format = 1; break; default: format = 0; break; } if ( ! format ) { test_format = SDL_NextAudioFormat(); } } if ( format == 0 ) { SDL_SetError("Couldn't find any hardware audio formats"); return(-1); } spec->format = test_format; stream = arts_play_stream(spec->freq, bits, spec->channels, "SDL"); /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(spec); /* Determine the power of two of the fragment size */ for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec ); if ( (0x01<<frag_spec) != spec->size ) { SDL_SetError("Fragment size must be a power of two"); return(-1); } frag_spec |= 0x00020000; /* two fragments, for low latency */ #ifdef ARTS_P_PACKET_SETTINGS arts_stream_set(stream, ARTS_P_PACKET_SETTINGS, frag_spec); #else arts_stream_set(stream, ARTS_P_PACKET_SIZE, frag_spec&0xffff); arts_stream_set(stream, ARTS_P_PACKET_COUNT, frag_spec>>16); #endif spec->size = arts_stream_get(stream, ARTS_P_PACKET_SIZE); /* Allocate mixing buffer */ mixlen = spec->size; mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); if ( mixbuf == NULL ) { return(-1); } memset(mixbuf, spec->silence, spec->size); /* Get the parent process id (we're the parent of the audio thread) */ parent = getpid(); /* We're ready to rock and roll. :-) */ return(0); }
static int BSDAUDIO_OpenDevice(_THIS, const char *devname, int iscapture) { const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT); SDL_AudioFormat format = 0; audio_info_t info; /* We don't care what the devname is...we'll try to open anything. */ /* ...but default to first name in the list... */ if (devname == NULL) { devname = SDL_GetAudioDeviceName(0, iscapture); if (devname == NULL) { SDL_SetError("No such audio device"); return 0; } } /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { SDL_OutOfMemory(); return 0; } SDL_memset(this->hidden, 0, (sizeof *this->hidden)); /* Open the audio device */ this->hidden->audio_fd = open(devname, flags, 0); if (this->hidden->audio_fd < 0) { SDL_SetError("Couldn't open %s: %s", devname, strerror(errno)); return 0; } AUDIO_INITINFO(&info); /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(&this->spec); /* Set to play mode */ info.mode = AUMODE_PLAY; if (ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info) < 0) { BSDAUDIO_CloseDevice(this); SDL_SetError("Couldn't put device into play mode"); return 0; } AUDIO_INITINFO(&info); for (format = SDL_FirstAudioFormat(this->spec.format); format; format = SDL_NextAudioFormat()) { switch (format) { case AUDIO_U8: info.play.encoding = AUDIO_ENCODING_ULINEAR; info.play.precision = 8; break; case AUDIO_S8: info.play.encoding = AUDIO_ENCODING_SLINEAR; info.play.precision = 8; break; case AUDIO_S16LSB: info.play.encoding = AUDIO_ENCODING_SLINEAR_LE; info.play.precision = 16; break; case AUDIO_S16MSB: info.play.encoding = AUDIO_ENCODING_SLINEAR_BE; info.play.precision = 16; break; case AUDIO_U16LSB: info.play.encoding = AUDIO_ENCODING_ULINEAR_LE; info.play.precision = 16; break; case AUDIO_U16MSB: info.play.encoding = AUDIO_ENCODING_ULINEAR_BE; info.play.precision = 16; break; default: continue; } if (ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info) == 0) { break; } } if (!format) { BSDAUDIO_CloseDevice(this); SDL_SetError("No supported encoding for 0x%x", this->spec.format); return 0; } this->spec.format = format; AUDIO_INITINFO(&info); info.play.channels = this->spec.channels; if (ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info) == -1) { this->spec.channels = 1; } AUDIO_INITINFO(&info); info.play.sample_rate = this->spec.freq; info.blocksize = this->spec.size; info.hiwat = 5; info.lowat = 3; (void) ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info); (void) ioctl(this->hidden->audio_fd, AUDIO_GETINFO, &info); this->spec.freq = info.play.sample_rate; /* Allocate mixing buffer */ this->hidden->mixlen = this->spec.size; this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); if (this->hidden->mixbuf == NULL) { BSDAUDIO_CloseDevice(this); SDL_OutOfMemory(); return 0; } SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); BSDAUDIO_Status(this); /* We're ready to rock and roll. :-) */ return (0); }
static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec) { int status; snd_pcm_hw_params_t *hwparams; snd_pcm_sw_params_t *swparams; snd_pcm_format_t format; snd_pcm_uframes_t frames; Uint16 test_format; /* Open the audio device */ /* Name of device should depend on # channels in spec */ status = SDL_NAME(snd_pcm_open)(&pcm_handle, get_audio_device(spec->channels), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK); if ( status < 0 ) { SDL_SetError("Couldn't open audio device: %s", SDL_NAME(snd_strerror)(status)); return(-1); } /* Figure out what the hardware is capable of */ snd_pcm_hw_params_alloca(&hwparams); status = SDL_NAME(snd_pcm_hw_params_any)(pcm_handle, hwparams); if ( status < 0 ) { SDL_SetError("Couldn't get hardware config: %s", SDL_NAME(snd_strerror)(status)); ALSA_CloseAudio(this); return(-1); } /* SDL only uses interleaved sample output */ status = SDL_NAME(snd_pcm_hw_params_set_access)(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED); if ( status < 0 ) { SDL_SetError("Couldn't set interleaved access: %s", SDL_NAME(snd_strerror)(status)); ALSA_CloseAudio(this); return(-1); } /* Try for a closest match on audio format */ status = -1; for ( test_format = SDL_FirstAudioFormat(spec->format); test_format && (status < 0); ) { switch ( test_format ) { case AUDIO_U8: format = SND_PCM_FORMAT_U8; break; case AUDIO_S8: format = SND_PCM_FORMAT_S8; break; case AUDIO_S16LSB: format = SND_PCM_FORMAT_S16_LE; break; case AUDIO_S16MSB: format = SND_PCM_FORMAT_S16_BE; break; case AUDIO_U16LSB: format = SND_PCM_FORMAT_U16_LE; break; case AUDIO_U16MSB: format = SND_PCM_FORMAT_U16_BE; break; default: format = 0; break; } if ( format != 0 ) { status = SDL_NAME(snd_pcm_hw_params_set_format)(pcm_handle, hwparams, format); } if ( status < 0 ) { test_format = SDL_NextAudioFormat(); } } if ( status < 0 ) { SDL_SetError("Couldn't find any hardware audio formats"); ALSA_CloseAudio(this); return(-1); } spec->format = test_format; /* Set the number of channels */ status = SDL_NAME(snd_pcm_hw_params_set_channels)(pcm_handle, hwparams, spec->channels); if ( status < 0 ) { status = SDL_NAME(snd_pcm_hw_params_get_channels)(hwparams); if ( (status <= 0) || (status > 2) ) { SDL_SetError("Couldn't set audio channels"); ALSA_CloseAudio(this); return(-1); } spec->channels = status; } /* Set the audio rate */ status = SDL_NAME(snd_pcm_hw_params_set_rate_near)(pcm_handle, hwparams, spec->freq, NULL); if ( status < 0 ) { SDL_SetError("Couldn't set audio frequency: %s", SDL_NAME(snd_strerror)(status)); ALSA_CloseAudio(this); return(-1); } spec->freq = status; /* Set the buffer size, in samples */ frames = spec->samples; frames = SDL_NAME(snd_pcm_hw_params_set_period_size_near)(pcm_handle, hwparams, frames, NULL); spec->samples = frames; SDL_NAME(snd_pcm_hw_params_set_periods_near)(pcm_handle, hwparams, 2, NULL); /* "set" the hardware with the desired parameters */ status = SDL_NAME(snd_pcm_hw_params)(pcm_handle, hwparams); if ( status < 0 ) { SDL_SetError("Couldn't set hardware audio parameters: %s", SDL_NAME(snd_strerror)(status)); ALSA_CloseAudio(this); return(-1); } /* This is useful for debugging... */ /* { snd_pcm_sframes_t bufsize; int fragments; bufsize = SDL_NAME(snd_pcm_hw_params_get_period_size)(hwparams); fragments = SDL_NAME(snd_pcm_hw_params_get_periods)(hwparams); fprintf(stderr, "ALSA: bufsize = %ld, fragments = %d\n", bufsize, fragments); } */ /* Set the software parameters */ snd_pcm_sw_params_alloca(&swparams); status = SDL_NAME(snd_pcm_sw_params_current)(pcm_handle, swparams); if ( status < 0 ) { SDL_SetError("Couldn't get software config: %s", SDL_NAME(snd_strerror)(status)); ALSA_CloseAudio(this); return(-1); } status = SDL_NAME(snd_pcm_sw_params_set_start_threshold)(pcm_handle, swparams, 0); if ( status < 0 ) { SDL_SetError("Couldn't set start threshold: %s", SDL_NAME(snd_strerror)(status)); ALSA_CloseAudio(this); return(-1); } status = SDL_NAME(snd_pcm_sw_params_set_avail_min)(pcm_handle, swparams, frames); if ( status < 0 ) { SDL_SetError("Couldn't set avail min: %s", SDL_NAME(snd_strerror)(status)); ALSA_CloseAudio(this); return(-1); } status = SDL_NAME(snd_pcm_sw_params)(pcm_handle, swparams); if ( status < 0 ) { SDL_SetError("Couldn't set software audio parameters: %s", SDL_NAME(snd_strerror)(status)); ALSA_CloseAudio(this); return(-1); } /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(spec); /* Allocate mixing buffer */ mixlen = spec->size; mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); if ( mixbuf == NULL ) { ALSA_CloseAudio(this); return(-1); } SDL_memset(mixbuf, spec->silence, spec->size); /* Get the parent process id (we're the parent of the audio thread) */ parent = getpid(); /* Switch to blocking mode for playback */ SDL_NAME(snd_pcm_nonblock)(pcm_handle, 0); /* We're ready to rock and roll. :-) */ return(0); }
static int NAS_OpenAudio(_THIS, SDL_AudioSpec *spec) { AuElement elms[3]; int buffer_size; Uint16 test_format, format; this->hidden->mixbuf = NULL; /* Try for a closest match on audio format */ format = 0; for ( test_format = SDL_FirstAudioFormat(spec->format); ! format && test_format; ) { format = sdlformat_to_auformat(test_format); if (format == AuNone) { test_format = SDL_NextAudioFormat(); } } if ( format == 0 ) { SDL_SetError("Couldn't find any hardware audio formats"); return(-1); } spec->format = test_format; this->hidden->aud = AuOpenServer("", 0, NULL, 0, NULL, NULL); if (this->hidden->aud == 0) { SDL_SetError("Couldn't open connection to NAS server"); return (-1); } this->hidden->dev = find_device(this, spec->channels); if ((this->hidden->dev == AuNone) || (!(this->hidden->flow = AuCreateFlow(this->hidden->aud, NULL)))) { AuCloseServer(this->hidden->aud); this->hidden->aud = 0; SDL_SetError("Couldn't find a fitting playback device on NAS server"); return (-1); } buffer_size = spec->freq; if (buffer_size < 4096) buffer_size = 4096; if (buffer_size > 32768) buffer_size = 32768; /* So that the buffer won't get unmanageably big. */ /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(spec); this2 = this->hidden; AuMakeElementImportClient(elms, spec->freq, format, spec->channels, AuTrue, buffer_size, buffer_size / 4, 0, NULL); AuMakeElementExportDevice(elms+1, 0, this->hidden->dev, spec->freq, AuUnlimitedSamples, 0, NULL); AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, 2, elms, NULL); AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0, this->hidden->flow, event_handler, (AuPointer) NULL); AuStartFlow(this->hidden->aud, this->hidden->flow, NULL); /* Allocate mixing buffer */ this->hidden->mixlen = spec->size; this->hidden->mixbuf = (Uint8 *)SDL_AllocAudioMem(this->hidden->mixlen); if ( this->hidden->mixbuf == NULL ) { return(-1); } memset(this->hidden->mixbuf, spec->silence, spec->size); /* Get the parent process id (we're the parent of the audio thread) */ this->hidden->parent = getpid(); /* We're ready to rock and roll. :-) */ return(0); }
static int PULSE_OpenAudio(_THIS, SDL_AudioSpec *spec) { Uint16 test_format; pa_sample_spec paspec; pa_buffer_attr paattr; pa_channel_map pacmap; paspec.format = PA_SAMPLE_INVALID; for ( test_format = SDL_FirstAudioFormat(spec->format); test_format; ) { switch ( test_format ) { case AUDIO_U8: paspec.format = PA_SAMPLE_U8; break; case AUDIO_S16LSB: paspec.format = PA_SAMPLE_S16LE; break; case AUDIO_S16MSB: paspec.format = PA_SAMPLE_S16BE; break; } if ( paspec.format != PA_SAMPLE_INVALID ) break; } if (paspec.format == PA_SAMPLE_INVALID ) { SDL_SetError("Couldn't find any suitable audio formats"); return(-1); } spec->format = test_format; paspec.channels = spec->channels; paspec.rate = spec->freq; /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(spec); /* Allocate mixing buffer */ mixlen = spec->size; mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); if ( mixbuf == NULL ) { return(-1); } SDL_memset(mixbuf, spec->silence, spec->size); /* Reduced prebuffering compared to the defaults. */ paattr.tlength = mixlen; paattr.minreq = mixlen; paattr.fragsize = mixlen; paattr.prebuf = mixlen; paattr.maxlength = mixlen * 4; /* The SDL ALSA output hints us that we use Windows' channel mapping */ /* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */ SDL_NAME(pa_channel_map_init_auto)( &pacmap, spec->channels, PA_CHANNEL_MAP_WAVEEX); /* Connect to the PulseAudio server */ stream = SDL_NAME(pa_simple_new)( SDL_getenv("PASERVER"), /* server */ get_progname(), /* application name */ PA_STREAM_PLAYBACK, /* playback mode */ SDL_getenv("PADEVICE"), /* device on the server */ "Simple DirectMedia Layer", /* stream description */ &paspec, /* sample format spec */ &pacmap, /* channel map */ &paattr, /* buffering attributes */ NULL /* error code */ ); if ( stream == NULL ) { PULSE_CloseAudio(this); SDL_SetError("Could not connect to PulseAudio"); return(-1); } /* Get the parent process id (we're the parent of the audio thread) */ parent = getpid(); return(0); }
static int AL_OpenAudio(_THIS, SDL_AudioSpec *spec) { ALconfig audio_config; #ifdef OLD_IRIX_AUDIO long audio_param[2]; #else ALpv audio_param; #endif int width; /* Determine the audio parameters from the AudioSpec */ switch ( spec->format & 0xFF ) { case 8: { /* Signed 8 bit audio data */ spec->format = AUDIO_S8; width = AL_SAMPLE_8; } break; case 16: { /* Signed 16 bit audio data */ spec->format = AUDIO_S16MSB; width = AL_SAMPLE_16; } break; default: { SDL_SetError("Unsupported audio format"); return(-1); } } /* Update the fragment size as size in bytes */ SDL_CalculateAudioSpec(spec); /* Set output frequency */ #ifdef OLD_IRIX_AUDIO audio_param[0] = AL_OUTPUT_RATE; audio_param[1] = spec->freq; if( ALsetparams(AL_DEFAULT_DEVICE, audio_param, 2) < 0 ) { #else audio_param.param = AL_RATE; audio_param.value.i = spec->freq; if( alSetParams(AL_DEFAULT_OUTPUT, &audio_param, 1) < 0 ) { #endif SDL_SetError("alSetParams failed"); return(-1); } /* Open the audio port with the requested frequency */ audio_port = NULL; audio_config = alNewConfig(); if ( audio_config && (alSetSampFmt(audio_config, AL_SAMPFMT_TWOSCOMP) >= 0) && (alSetWidth(audio_config, width) >= 0) && (alSetQueueSize(audio_config, spec->samples*2) >= 0) && (alSetChannels(audio_config, spec->channels) >= 0) ) { audio_port = alOpenPort("SDL audio", "w", audio_config); } alFreeConfig(audio_config); if( audio_port == NULL ) { SDL_SetError("Unable to open audio port"); return(-1); } /* Allocate mixing buffer */ mixbuf = (Uint8 *)SDL_AllocAudioMem(spec->size); if ( mixbuf == NULL ) { SDL_OutOfMemory(); return(-1); } memset(mixbuf, spec->silence, spec->size); /* We're ready to rock and roll. :-) */ return(0); }
static int NTO_OpenAudio(_THIS, SDL_AudioSpec* spec) { int rval; int format; Uint16 test_format; int found; audio_handle = NULL; this->enabled = 0; if (pcm_buf != NULL) { SDL_FreeAudioMem(pcm_buf); pcm_buf = NULL; } /* initialize channel transfer parameters to default */ NTO_InitAudioParams(&cparams); /* Open the audio device */ rval = snd_pcm_open_preferred(&audio_handle, &cardno, &deviceno, OPEN_FLAGS); if (rval < 0) { SDL_SetError("NTO_OpenAudio(): snd_pcm_open failed: %s\n", snd_strerror(rval)); return (-1); } if (!NTO_CheckBuggyCards(this, QSA_MMAP_WORKAROUND)) { /* enable count status parameter */ if ((rval = snd_pcm_plugin_set_disable(audio_handle, PLUGIN_DISABLE_MMAP)) < 0) { SDL_SetError("snd_pcm_plugin_set_disable failed: %s\n", snd_strerror(rval)); return (-1); } } /* Try for a closest match on audio format */ format = 0; /* can't use format as SND_PCM_SFMT_U8 = 0 in nto */ found = 0; for (test_format=SDL_FirstAudioFormat(spec->format); !found ;) { /* if match found set format to equivalent ALSA format */ switch (test_format) { case AUDIO_U8: format = SND_PCM_SFMT_U8; found = 1; break; case AUDIO_S8: format = SND_PCM_SFMT_S8; found = 1; break; case AUDIO_S16LSB: format = SND_PCM_SFMT_S16_LE; found = 1; break; case AUDIO_S16MSB: format = SND_PCM_SFMT_S16_BE; found = 1; break; case AUDIO_U16LSB: format = SND_PCM_SFMT_U16_LE; found = 1; break; case AUDIO_U16MSB: format = SND_PCM_SFMT_U16_BE; found = 1; break; default: break; } if (!found) { test_format = SDL_NextAudioFormat(); } } /* assumes test_format not 0 on success */ if (test_format == 0) { SDL_SetError("NTO_OpenAudio(): Couldn't find any hardware audio formats"); return (-1); } spec->format = test_format; /* Set the audio format */ cparams.format.format = format; /* Set mono or stereo audio (currently only two channels supported) */ cparams.format.voices = spec->channels; /* Set rate */ cparams.format.rate = spec->freq; /* Setup the transfer parameters according to cparams */ rval = snd_pcm_plugin_params(audio_handle, &cparams); if (rval < 0) { SDL_SetError("NTO_OpenAudio(): snd_pcm_channel_params failed: %s\n", snd_strerror(rval)); return (-1); } /* Make sure channel is setup right one last time */ SDL_memset(&csetup, 0x00, sizeof(csetup)); csetup.channel = SND_PCM_CHANNEL_PLAYBACK; if (snd_pcm_plugin_setup(audio_handle, &csetup) < 0) { SDL_SetError("NTO_OpenAudio(): Unable to setup playback channel\n"); return -1; } /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(spec); pcm_len = spec->size; if (pcm_len==0) { pcm_len = csetup.buf.block.frag_size * spec->channels * (snd_pcm_format_width(format)/8); } /* Allocate memory to the audio buffer and initialize with silence (Note that buffer size must be a multiple of fragment size, so find closest multiple) */ pcm_buf = (Uint8*)SDL_AllocAudioMem(pcm_len); if (pcm_buf == NULL) { SDL_SetError("NTO_OpenAudio(): pcm buffer allocation failed\n"); return (-1); } SDL_memset(pcm_buf, spec->silence, pcm_len); /* get the file descriptor */ if ((audio_fd = snd_pcm_file_descriptor(audio_handle, SND_PCM_CHANNEL_PLAYBACK)) < 0) { SDL_SetError("NTO_OpenAudio(): snd_pcm_file_descriptor failed with error code: %s\n", snd_strerror(rval)); return (-1); } /* Trigger audio playback */ rval = snd_pcm_plugin_prepare(audio_handle, SND_PCM_CHANNEL_PLAYBACK); if (rval < 0) { SDL_SetError("snd_pcm_plugin_prepare failed: %s\n", snd_strerror(rval)); return (-1); } this->enabled = 1; /* Get the parent process id (we're the parent of the audio thread) */ parent = getpid(); /* We're really ready to rock and roll. :-) */ return (0); }