Fixed bug 5080 - SDL_netbsdaudio: Always use the device's preferred frequency Nia Alarie The NetBSD kernel's audio resampling code is much simpler and lower quality than libsamplerate. Presumably, if SDL always performs I/O on the audio device in its native frequency, we can avoid resampling audio in the kernel and let SDL do it with libsamplerate instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
diff --git a/src/audio/netbsd/SDL_netbsdaudio.c b/src/audio/netbsd/SDL_netbsdaudio.c
index ad9f3bc..6abf253 100644
--- a/src/audio/netbsd/SDL_netbsdaudio.c
+++ b/src/audio/netbsd/SDL_netbsdaudio.c
@@ -205,7 +205,7 @@ static int
NETBSDAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
{
SDL_AudioFormat format = 0;
- audio_info_t info;
+ audio_info_t info, hwinfo;
struct audio_prinfo *prinfo = iscapture ? &info.record : &info.play;
/* We don't care what the devname is...we'll try to open anything. */
@@ -233,7 +233,20 @@ NETBSDAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
AUDIO_INITINFO(&info);
+#ifdef AUDIO_GETFORMAT /* Introduced in NetBSD 9.0 */
+ if (ioctl(this->hidden->audio_fd, AUDIO_GETFORMAT, &hwinfo) != -1) {
+ /*
+ * Use the device's native sample rate so the kernel doesn't have to
+ * resample.
+ */
+ this->spec.freq = iscapture ?
+ hwinfo.record.sample_rate : hwinfo.play.sample_rate;
+ }
+#endif
+
prinfo->encoding = AUDIO_ENCODING_NONE;
+ prinfo->sample_rate = this->spec.freq;
+ prinfo->channels = this->spec.channels;
for (format = SDL_FirstAudioFormat(this->spec.format); format;) {
switch (format) {
@@ -280,23 +293,19 @@ NETBSDAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
return SDL_SetError("No supported encoding for 0x%x", this->spec.format);
}
- this->spec.format = format;
-
- /* Calculate spec parameters based on our chosen format */
- SDL_CalculateAudioSpec(&this->spec);
-
- info.mode = iscapture ? AUMODE_RECORD : AUMODE_PLAY;
- info.blocksize = this->spec.size;
info.hiwat = 5;
info.lowat = 3;
- prinfo->sample_rate = this->spec.freq;
- prinfo->channels = this->spec.channels;
(void) ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info);
(void) ioctl(this->hidden->audio_fd, AUDIO_GETINFO, &info);
+
+ /* Final spec used for the device. */
+ this->spec.format = format;
this->spec.freq = prinfo->sample_rate;
this->spec.channels = prinfo->channels;
+ SDL_CalculateAudioSpec(&this->spec);
+
if (!iscapture) {
/* Allocate mixing buffer */
this->hidden->mixlen = this->spec.size;