Don't limit IMA4 decoding to 2 channels
[openal-soft.git] / Alc / solaris.c
blobd07bac0be06261e90a74dcd141888fad0667179d
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>
33 #include "alMain.h"
34 #include "AL/al.h"
35 #include "AL/alc.h"
37 #include <sys/audioio.h>
40 static const ALCchar solaris_device[] = "Solaris Default";
42 typedef struct {
43 int fd;
44 volatile int killNow;
45 ALvoid *thread;
47 ALubyte *mix_data;
48 int data_size;
49 } solaris_data;
52 static ALuint SolarisProc(ALvoid *ptr)
54 ALCdevice *pDevice = (ALCdevice*)ptr;
55 solaris_data *data = (solaris_data*)pDevice->ExtraData;
56 int remaining = 0;
57 ALint frameSize;
58 int wrote;
60 SetRTPriority();
62 frameSize = aluFrameSizeFromFormat(pDevice->Format);
64 while(!data->killNow && pDevice->Connected)
66 ALint len = data->data_size;
67 ALubyte *WritePtr = data->mix_data;
69 aluMixData(pDevice, WritePtr, len/frameSize);
70 while(len > 0 && !data->killNow)
72 wrote = write(data->fd, WritePtr, len);
73 if(wrote < 0)
75 if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
77 AL_PRINT("write failed: %s\n", strerror(errno));
78 aluHandleDisconnect(pDevice);
79 break;
82 Sleep(1);
83 continue;
86 len -= wrote;
87 WritePtr += wrote;
91 return 0;
95 static ALCboolean solaris_open_playback(ALCdevice *device, const ALCchar *deviceName)
97 char driver[64];
98 solaris_data *data;
100 strncpy(driver, GetConfigValue("solaris", "device", "/dev/audio"), sizeof(driver)-1);
101 driver[sizeof(driver)-1] = 0;
103 if(!deviceName)
104 deviceName = solaris_device;
105 else if(strcmp(deviceName, solaris_device) != 0)
106 return ALC_FALSE;
108 data = (solaris_data*)calloc(1, sizeof(solaris_data));
109 data->killNow = 0;
111 data->fd = open(driver, O_WRONLY);
112 if(data->fd == -1)
114 free(data);
115 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
116 return ALC_FALSE;
119 device->szDeviceName = strdup(deviceName);
120 device->ExtraData = data;
121 return ALC_TRUE;
124 static void solaris_close_playback(ALCdevice *device)
126 solaris_data *data = (solaris_data*)device->ExtraData;
128 close(data->fd);
129 free(data);
130 device->ExtraData = NULL;
133 static ALCboolean solaris_reset_playback(ALCdevice *device)
135 solaris_data *data = (solaris_data*)device->ExtraData;
136 audio_info_t info;
137 ALuint frameSize;
138 int numChannels;
140 AUDIO_INITINFO(&info);
142 switch(aluBytesFromFormat(device->Format))
144 case 1:
145 info.play.precision = 8;
146 info.play.encoding = AUDIO_ENCODING_LINEAR8;
147 break;
148 case 4:
149 switch(numChannels)
151 case 1: device->Format = AL_FORMAT_MONO16; break;
152 case 2: device->Format = AL_FORMAT_STEREO16; break;
153 case 4: device->Format = AL_FORMAT_QUAD16; break;
154 case 6: device->Format = AL_FORMAT_51CHN16; break;
155 case 7: device->Format = AL_FORMAT_61CHN16; break;
156 case 8: device->Format = AL_FORMAT_71CHN16; break;
158 /* fall-through */
159 case 2:
160 info.play.precision = 16;
161 info.play.encoding = AUDIO_ENCODING_LINEAR;
162 break;
163 default:
164 AL_PRINT("Unknown format: 0x%x\n", device->Format);
165 return ALC_FALSE;
168 numChannels = aluChannelsFromFormat(device->Format);
169 info.play.sample_rate = device->Frequency;
170 info.play.channels = numChannels;
172 frameSize = numChannels * aluBytesFromFormat(device->Format);
173 info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize;
175 if(ioctl(data->fd, AUDIO_SETINFO, &info) < 0)
177 AL_PRINT("ioctl failed: %s\n", strerror(errno));
178 return ALC_FALSE;
181 if(aluChannelsFromFormat(device->Format) != info.play.channels)
183 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), info.play.channels);
184 return ALC_FALSE;
187 if(!((info.play.precision == 8 && aluBytesFromFormat(device->Format) == 1) ||
188 (info.play.precision == 16 && aluBytesFromFormat(device->Format) == 2)))
190 AL_PRINT("Could not set %d-bit output, got %d\n", aluBytesFromFormat(device->Format)*8, info.play.precision);
191 return ALC_FALSE;
194 device->Frequency = info.play.sample_rate;
195 device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1;
197 data->data_size = device->UpdateSize * frameSize;
198 data->mix_data = calloc(1, data->data_size);
200 SetDefaultChannelOrder(device);
202 data->thread = StartThread(SolarisProc, device);
203 if(data->thread == NULL)
205 free(data->mix_data);
206 data->mix_data = NULL;
207 return ALC_FALSE;
210 return ALC_TRUE;
213 static void solaris_stop_playback(ALCdevice *device)
215 solaris_data *data = (solaris_data*)device->ExtraData;
217 if(!data->thread)
218 return;
220 data->killNow = 1;
221 StopThread(data->thread);
222 data->thread = NULL;
224 data->killNow = 0;
226 free(data->mix_data);
227 data->mix_data = NULL;
231 static ALCboolean solaris_open_capture(ALCdevice *device, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
233 (void)device;
234 (void)deviceName;
235 (void)frequency;
236 (void)format;
237 (void)SampleSize;
238 return ALC_FALSE;
241 static void solaris_close_capture(ALCdevice *device)
243 (void)device;
246 static void solaris_start_capture(ALCdevice *pDevice)
248 (void)pDevice;
251 static void solaris_stop_capture(ALCdevice *pDevice)
253 (void)pDevice;
256 static void solaris_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
258 (void)pDevice;
259 (void)pBuffer;
260 (void)lSamples;
263 static ALCuint solaris_available_samples(ALCdevice *pDevice)
265 (void)pDevice;
266 return 0;
270 BackendFuncs solaris_funcs = {
271 solaris_open_playback,
272 solaris_close_playback,
273 solaris_reset_playback,
274 solaris_stop_playback,
275 solaris_open_capture,
276 solaris_close_capture,
277 solaris_start_capture,
278 solaris_stop_capture,
279 solaris_capture_samples,
280 solaris_available_samples
283 void alc_solaris_init(BackendFuncs *func_list)
285 *func_list = solaris_funcs;
288 void alc_solaris_deinit(void)
292 void alc_solaris_probe(int type)
294 #ifdef HAVE_STAT
295 struct stat buf;
296 if(stat(GetConfigValue("solaris", "device", "/dev/audio"), &buf) != 0)
297 return;
298 #endif
300 if(type == DEVICE_PROBE)
301 AppendDeviceList(solaris_device);
302 else if(type == ALL_DEVICE_PROBE)
303 AppendAllDeviceList(solaris_device);