Better protect against negative attenuation
[openal-soft.git] / Alc / null.c
blobad3d3f6aacf8e46a6ef5b4c572579ff363e0cc87
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2010 by Chris Robinson
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 <stdlib.h>
24 #include "alMain.h"
25 #include "AL/al.h"
26 #include "AL/alc.h"
29 typedef struct {
30 ALvoid *buffer;
31 ALuint size;
33 ALuint startTime;
34 ALuint64 baseTime;
36 volatile int killNow;
37 ALvoid *thread;
38 } null_data;
41 static const ALCchar nullDevice[] = "No Output";
43 static ALuint NullProc(ALvoid *ptr)
45 ALCdevice *Device = (ALCdevice*)ptr;
46 null_data *data = (null_data*)Device->ExtraData;
47 ALuint now, start;
48 ALuint64 avail, done;
49 const ALuint restTime = (ALuint64)Device->UpdateSize * 1000 /
50 Device->Frequency / 2;
52 done = 0;
53 start = data->startTime;
54 while(!data->killNow && Device->Connected)
56 now = timeGetTime();
58 avail = (ALuint64)(now-start) * Device->Frequency / 1000;
59 if(avail < done)
61 AL_PRINT("Timer wrapped\n");
62 aluHandleDisconnect(Device);
63 break;
65 if(avail-done < Device->UpdateSize)
67 Sleep(restTime);
68 continue;
71 while(avail-done >= Device->UpdateSize)
73 aluMixData(Device, data->buffer, Device->UpdateSize);
74 done += Device->UpdateSize;
78 return 0;
81 static ALCboolean null_open_playback(ALCdevice *device, const ALCchar *deviceName)
83 null_data *data;
85 if(!deviceName)
86 deviceName = nullDevice;
87 else if(strcmp(deviceName, nullDevice) != 0)
88 return ALC_FALSE;
90 data = (null_data*)calloc(1, sizeof(*data));
92 device->szDeviceName = strdup(deviceName);
93 device->ExtraData = data;
94 return ALC_TRUE;
97 static void null_close_playback(ALCdevice *device)
99 null_data *data = (null_data*)device->ExtraData;
101 free(data);
102 device->ExtraData = NULL;
105 static ALCboolean null_reset_playback(ALCdevice *device)
107 null_data *data = (null_data*)device->ExtraData;
109 data->size = device->UpdateSize * aluFrameSizeFromFormat(device->Format);
110 data->buffer = malloc(data->size);
111 if(!data->buffer)
113 AL_PRINT("buffer malloc failed\n");
114 return ALC_FALSE;
116 SetDefaultWFXChannelOrder(device);
118 device->TimeRes = 1000000;
120 data->startTime = timeGetTime();
121 data->thread = StartThread(NullProc, device);
122 if(data->thread == NULL)
124 free(data->buffer);
125 data->buffer = NULL;
126 return ALC_FALSE;
129 return ALC_TRUE;
132 static void null_stop_playback(ALCdevice *device)
134 null_data *data = (null_data*)device->ExtraData;
135 ALuint ext;
137 if(!data->thread)
138 return;
140 data->killNow = 1;
141 StopThread(data->thread);
142 data->thread = NULL;
144 data->killNow = 0;
146 ext = timeGetTime() - data->startTime;
147 data->baseTime += (ALuint64)ext * 1000000;
149 free(data->buffer);
150 data->buffer = NULL;
154 static ALCboolean null_open_capture(ALCdevice *device, const ALCchar *deviceName)
156 (void)device;
157 (void)deviceName;
158 return ALC_FALSE;
161 static ALuint64 null_get_time(ALCdevice *Device)
163 null_data *data = (null_data*)Device->ExtraData;
164 ALuint ext = 0;
165 if(data->thread)
166 ext = timeGetTime() - data->startTime;
167 return data->baseTime + ((ALuint64)ext * 1000000);
171 BackendFuncs null_funcs = {
172 null_open_playback,
173 null_close_playback,
174 null_reset_playback,
175 null_stop_playback,
176 null_open_capture,
177 NULL,
178 NULL,
179 NULL,
180 NULL,
181 NULL,
182 null_get_time
185 void alc_null_init(BackendFuncs *func_list)
187 *func_list = null_funcs;
190 void alc_null_deinit(void)
194 void alc_null_probe(int type)
196 if(type == DEVICE_PROBE)
197 AppendDeviceList(nullDevice);
198 else if(type == ALL_DEVICE_PROBE)
199 AppendAllDeviceList(nullDevice);