Add an example program showing how to apply reverb to a source
[openal-soft.git] / Alc / backends / solaris.c
blobb84bb839e18a37c4033d3636b5c208e029722540
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <memory.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <math.h>
34 #include "alMain.h"
35 #include "alu.h"
37 #include <sys/audioio.h>
40 static const ALCchar solaris_device[] = "Solaris Default";
42 static const char *solaris_driver = "/dev/audio";
44 typedef struct {
45 int fd;
46 volatile int killNow;
47 ALvoid *thread;
49 ALubyte *mix_data;
50 int data_size;
51 } solaris_data;
54 static ALuint SolarisProc(ALvoid *ptr)
56 ALCdevice *Device = (ALCdevice*)ptr;
57 solaris_data *data = (solaris_data*)Device->ExtraData;
58 ALint frameSize;
59 int wrote;
61 SetRTPriority();
63 frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
65 while(!data->killNow && Device->Connected)
67 ALint len = data->data_size;
68 ALubyte *WritePtr = data->mix_data;
70 aluMixData(Device, WritePtr, len/frameSize);
71 while(len > 0 && !data->killNow)
73 wrote = write(data->fd, WritePtr, len);
74 if(wrote < 0)
76 if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
78 ERR("write failed: %s\n", strerror(errno));
79 aluHandleDisconnect(Device);
80 break;
83 Sleep(1);
84 continue;
87 len -= wrote;
88 WritePtr += wrote;
92 return 0;
96 static ALCenum solaris_open_playback(ALCdevice *device, const ALCchar *deviceName)
98 solaris_data *data;
100 if(!deviceName)
101 deviceName = solaris_device;
102 else if(strcmp(deviceName, solaris_device) != 0)
103 return ALC_INVALID_VALUE;
105 data = (solaris_data*)calloc(1, sizeof(solaris_data));
106 data->killNow = 0;
108 data->fd = open(solaris_driver, O_WRONLY);
109 if(data->fd == -1)
111 free(data);
112 ERR("Could not open %s: %s\n", solaris_driver, strerror(errno));
113 return ALC_INVALID_VALUE;
116 device->DeviceName = strdup(deviceName);
117 device->ExtraData = data;
118 return ALC_NO_ERROR;
121 static void solaris_close_playback(ALCdevice *device)
123 solaris_data *data = (solaris_data*)device->ExtraData;
125 close(data->fd);
126 free(data);
127 device->ExtraData = NULL;
130 static ALCboolean solaris_reset_playback(ALCdevice *device)
132 solaris_data *data = (solaris_data*)device->ExtraData;
133 audio_info_t info;
134 ALuint frameSize;
135 int numChannels;
137 AUDIO_INITINFO(&info);
139 info.play.sample_rate = device->Frequency;
141 if(device->FmtChans != DevFmtMono)
142 device->FmtChans = DevFmtStereo;
143 numChannels = ChannelsFromDevFmt(device->FmtChans);
144 info.play.channels = numChannels;
146 switch(device->FmtType)
148 case DevFmtByte:
149 info.play.precision = 8;
150 info.play.encoding = AUDIO_ENCODING_LINEAR;
151 break;
152 case DevFmtUByte:
153 info.play.precision = 8;
154 info.play.encoding = AUDIO_ENCODING_LINEAR8;
155 break;
156 case DevFmtUShort:
157 case DevFmtInt:
158 case DevFmtUInt:
159 case DevFmtFloat:
160 device->FmtType = DevFmtShort;
161 /* fall-through */
162 case DevFmtShort:
163 info.play.precision = 16;
164 info.play.encoding = AUDIO_ENCODING_LINEAR;
165 break;
168 frameSize = numChannels * BytesFromDevFmt(device->FmtType);
169 info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize;
171 if(ioctl(data->fd, AUDIO_SETINFO, &info) < 0)
173 ERR("ioctl failed: %s\n", strerror(errno));
174 return ALC_FALSE;
177 if(ChannelsFromDevFmt(device->FmtChans) != info.play.channels)
179 ERR("Could not set %d channels, got %d instead\n", ChannelsFromDevFmt(device->FmtChans), info.play.channels);
180 return ALC_FALSE;
183 if(!((info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8 && device->FmtType == DevFmtUByte) ||
184 (info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtByte) ||
185 (info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtShort) ||
186 (info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtInt)))
188 ERR("Could not set %s samples, got %d (0x%x)\n", DevFmtTypeString(device->FmtType),
189 info.play.precision, info.play.encoding);
190 return ALC_FALSE;
193 device->Frequency = info.play.sample_rate;
194 device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1;
196 SetDefaultChannelOrder(device);
198 return ALC_TRUE;
201 static ALCboolean solaris_start_playback(ALCdevice *device)
203 solaris_data *data = (solaris_data*)device->ExtraData;
205 data->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
206 data->mix_data = calloc(1, data->data_size);
208 data->thread = StartThread(SolarisProc, device);
209 if(data->thread == NULL)
211 free(data->mix_data);
212 data->mix_data = NULL;
213 return ALC_FALSE;
216 return ALC_TRUE;
219 static void solaris_stop_playback(ALCdevice *device)
221 solaris_data *data = (solaris_data*)device->ExtraData;
223 if(!data->thread)
224 return;
226 data->killNow = 1;
227 StopThread(data->thread);
228 data->thread = NULL;
230 data->killNow = 0;
231 if(ioctl(data->fd, AUDIO_DRAIN) < 0)
232 ERR("Error draining device: %s\n", strerror(errno));
234 free(data->mix_data);
235 data->mix_data = NULL;
239 static const BackendFuncs solaris_funcs = {
240 solaris_open_playback,
241 solaris_close_playback,
242 solaris_reset_playback,
243 solaris_start_playback,
244 solaris_stop_playback,
245 NULL,
246 NULL,
247 NULL,
248 NULL,
249 NULL,
250 NULL,
251 ALCdevice_LockDefault,
252 ALCdevice_UnlockDefault,
253 ALCdevice_GetLatencyDefault
256 ALCboolean alc_solaris_init(BackendFuncs *func_list)
258 ConfigValueStr("solaris", "device", &solaris_driver);
260 *func_list = solaris_funcs;
261 return ALC_TRUE;
264 void alc_solaris_deinit(void)
268 void alc_solaris_probe(enum DevProbe type)
270 switch(type)
272 case ALL_DEVICE_PROBE:
274 #ifdef HAVE_STAT
275 struct stat buf;
276 if(stat(solaris_driver, &buf) == 0)
277 #endif
278 AppendAllDevicesList(solaris_device);
280 break;
282 case CAPTURE_DEVICE_PROBE:
283 break;