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