update license in source code itself
[libpicofe.git] / linux / sndout_oss.c
CommitLineData
f89d8471 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
b3972d82 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
23static int sounddev = -1, mixerdev = -1;
dd59a74b 24static int can_write_safe;
25
f679add7 26#define FRAG_COUNT 4
b3972d82 27
28int 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
b188c2b6 40void 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
f679add7 50int sndout_oss_start(int rate, int stereo, int frames_in_frag)
b3972d82 51{
f679add7 52 static int s_oldrate = 0, s_oldstereo = 0;
dd59a74b 53 int frag, bsize, bits, ret;
b3972d82 54
b188c2b6 55 // GP2X: if no settings change, we don't need to do anything,
56 // since audio is never stopped there
f679add7 57 if (sounddev >= 0 && rate == s_oldrate && s_oldstereo == stereo)
b3972d82 58 return 0;
59
b188c2b6 60 sndout_oss_stop();
d747e9d6 61 sounddev = open("/dev/dsp", O_WRONLY);
b3972d82 62 if (sounddev == -1)
63 {
64 perror("open(\"/dev/dsp\")");
d747e9d6 65 sounddev = open("/dev/dsp1", O_WRONLY);
5f7b5155 66 if (sounddev == -1) {
67 perror("open(\"/dev/dsp1\")");
68 return -1;
69 }
b3972d82 70 }
71
f679add7 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;
dd59a74b 76
77 for (frag = 0; bsize; bsize >>= 1, frag++)
78 ;
b3972d82 79
f679add7 80 frag |= FRAG_COUNT << 16; // fragment count
dd59a74b 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
b188c2b6 94#ifdef __GP2X__
dd59a74b 95 // not sure if this is still needed (avoiding driver bugs?)
b3972d82 96 usleep(192*1024);
b188c2b6 97#endif
b3972d82 98
dd59a74b 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));
b3972d82 101
f679add7 102 s_oldrate = rate; s_oldstereo = stereo;
dd59a74b 103 can_write_safe = 0;
b3972d82 104 return 0;
105}
106
b3972d82 107int sndout_oss_write(const void *buff, int len)
108{
109 return write(sounddev, buff, len);
110}
111
a86e9a3e 112#include "../plat.h"
a33164ff 113
114/* not really non-blocking, just detects if blocking occurs
115 * and starts skipping writes in case it does. */
116int 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
dd59a74b 148int 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
f679add7 163 // have enough bytes to write + 1 frag
164 return bi.bytes - bi.fragsize >= bytes ? 1 : 0;
dd59a74b 165}
b3972d82 166
167void sndout_oss_sync(void)
168{
169 ioctl(sounddev, SOUND_PCM_SYNC, 0);
170}
171
b3972d82 172void 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
b3972d82 181void sndout_oss_exit(void)
182{
183 if (sounddev >= 0) close(sounddev); sounddev = -1;
184 if (mixerdev >= 0) close(mixerdev); mixerdev = -1;
185}
186