- int frag = 0, bsize, buffers;
-
- // if no settings change, we don't need to do anything
- if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;
-
- if (sounddev > 0) close(sounddev);
- sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
- if (sounddev == -1)
- printf("open(\"/dev/dsp\") failed with %i\n", errno);
-
- ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
- ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
- ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
- // calculate buffer size
- buffers = 16;
- bsize = rate / 32;
- if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding
- while ((bsize>>=1)) frag++;
- frag |= buffers<<16; // 16 buffers
- ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
- printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",
- rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));
-
- s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;