| 1 | /* sound output via OSS */ |
| 2 | #include <stdio.h> |
| 3 | #include <sys/types.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <sys/ioctl.h> |
| 7 | #include <sys/soundcard.h> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | #include "sndout_oss.h" |
| 11 | |
| 12 | static int sounddev = -1, mixerdev = -1; |
| 13 | static int can_write_safe; |
| 14 | |
| 15 | #define FRAG_COUNT 4 |
| 16 | |
| 17 | int sndout_oss_init(void) |
| 18 | { |
| 19 | if (mixerdev >= 0) close(mixerdev); |
| 20 | mixerdev = open("/dev/mixer", O_RDWR); |
| 21 | if (mixerdev == -1) |
| 22 | { |
| 23 | perror("open(\"/dev/mixer\")"); |
| 24 | } |
| 25 | |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | void sndout_oss_stop(void) |
| 30 | { |
| 31 | if (sounddev < 0) |
| 32 | return; |
| 33 | |
| 34 | ioctl(sounddev, SOUND_PCM_SYNC, 0); |
| 35 | close(sounddev); |
| 36 | sounddev = -1; |
| 37 | } |
| 38 | |
| 39 | int sndout_oss_start(int rate, int stereo, int frames_in_frag) |
| 40 | { |
| 41 | static int s_oldrate = 0, s_oldstereo = 0; |
| 42 | int frag, bsize, bits, ret; |
| 43 | |
| 44 | // GP2X: if no settings change, we don't need to do anything, |
| 45 | // since audio is never stopped there |
| 46 | if (sounddev >= 0 && rate == s_oldrate && s_oldstereo == stereo) |
| 47 | return 0; |
| 48 | |
| 49 | sndout_oss_stop(); |
| 50 | sounddev = open("/dev/dsp", O_WRONLY); |
| 51 | if (sounddev == -1) |
| 52 | { |
| 53 | perror("open(\"/dev/dsp\")"); |
| 54 | sounddev = open("/dev/dsp1", O_WRONLY); |
| 55 | if (sounddev == -1) { |
| 56 | perror("open(\"/dev/dsp1\")"); |
| 57 | return -1; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // try to fit frames_in_frag frames worth of data in fragment |
| 62 | // ignore mono because it's unlikely to be used and |
| 63 | // both GP2X and Wiz mixes mono to stereo anyway. |
| 64 | bsize = (frames_in_frag * rate / 50) * 4; |
| 65 | |
| 66 | for (frag = 0; bsize; bsize >>= 1, frag++) |
| 67 | ; |
| 68 | |
| 69 | frag |= FRAG_COUNT << 16; // fragment count |
| 70 | ret = ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag); |
| 71 | if (ret < 0) |
| 72 | perror("SNDCTL_DSP_SETFRAGMENT failed"); |
| 73 | |
| 74 | bits = 16; |
| 75 | ret = ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo); |
| 76 | if (ret == 0) |
| 77 | ret = ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits); |
| 78 | if (ret == 0) |
| 79 | ret = ioctl(sounddev, SNDCTL_DSP_SPEED, &rate); |
| 80 | if (ret < 0) |
| 81 | perror("failed to set audio format"); |
| 82 | |
| 83 | #ifdef __GP2X__ |
| 84 | // not sure if this is still needed (avoiding driver bugs?) |
| 85 | usleep(192*1024); |
| 86 | #endif |
| 87 | |
| 88 | printf("sndout_oss_start: %d/%dbit/%s, %d buffers of %i bytes\n", |
| 89 | rate, bits, stereo ? "stereo" : "mono", frag >> 16, 1 << (frag & 0xffff)); |
| 90 | |
| 91 | s_oldrate = rate; s_oldstereo = stereo; |
| 92 | can_write_safe = 0; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int sndout_oss_write(const void *buff, int len) |
| 97 | { |
| 98 | return write(sounddev, buff, len); |
| 99 | } |
| 100 | |
| 101 | #include "../common/plat.h" |
| 102 | |
| 103 | /* not really non-blocking, just detects if blocking occurs |
| 104 | * and starts skipping writes in case it does. */ |
| 105 | int sndout_oss_write_nb(const void *buff, int len) |
| 106 | { |
| 107 | static int lag_counter, skip_counter; |
| 108 | unsigned int t; |
| 109 | int ret; |
| 110 | |
| 111 | if (lag_counter > 2) { |
| 112 | // skip writes if audio starts blocking |
| 113 | lag_counter = 0; |
| 114 | skip_counter = FRAG_COUNT; |
| 115 | } |
| 116 | |
| 117 | if (skip_counter > 0) { |
| 118 | skip_counter--; |
| 119 | return len; |
| 120 | } |
| 121 | |
| 122 | t = plat_get_ticks_ms(); |
| 123 | ret = sndout_oss_write(buff, len); |
| 124 | t = plat_get_ticks_ms() - t; |
| 125 | if (t > 1) { |
| 126 | // this shouldn't really happen, most likely audio is out of sync |
| 127 | lag_counter++; |
| 128 | if (lag_counter > 2) |
| 129 | printf("audio lag %u\n", t); |
| 130 | } |
| 131 | else |
| 132 | lag_counter = 0; |
| 133 | |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | int sndout_oss_can_write(int bytes) |
| 138 | { |
| 139 | audio_buf_info bi; |
| 140 | int ret; |
| 141 | |
| 142 | #ifdef __GP2X__ |
| 143 | // note: SNDCTL_DSP_GETOSPACE crashes F100 kernel for some reason |
| 144 | // if called too early, so we work around here |
| 145 | if (can_write_safe++ < 8) |
| 146 | return 1; |
| 147 | #endif |
| 148 | ret = ioctl(sounddev, SNDCTL_DSP_GETOSPACE, &bi); |
| 149 | if (ret < 0) |
| 150 | return 1; |
| 151 | |
| 152 | // have enough bytes to write + 1 frag |
| 153 | return bi.bytes - bi.fragsize >= bytes ? 1 : 0; |
| 154 | } |
| 155 | |
| 156 | void sndout_oss_sync(void) |
| 157 | { |
| 158 | ioctl(sounddev, SOUND_PCM_SYNC, 0); |
| 159 | } |
| 160 | |
| 161 | void sndout_oss_setvol(int l, int r) |
| 162 | { |
| 163 | if (mixerdev < 0) return; |
| 164 | |
| 165 | l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r; |
| 166 | l<<=8; l|=r; |
| 167 | ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/ |
| 168 | } |
| 169 | |
| 170 | void sndout_oss_exit(void) |
| 171 | { |
| 172 | if (sounddev >= 0) close(sounddev); sounddev = -1; |
| 173 | if (mixerdev >= 0) close(mixerdev); mixerdev = -1; |
| 174 | } |
| 175 | |