Fix looping on loop points
[openal-soft.git] / Alc / null.c
blob15efee1dbdcd8436fc5a09d6846e8f9fa628c23e
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 volatile int killNow;
34 ALvoid *thread;
35 } null_data;
38 static const ALCchar nullDevice[] = "No Output";
40 static ALuint NullProc(ALvoid *ptr)
42 ALCdevice *Device = (ALCdevice*)ptr;
43 null_data *data = (null_data*)Device->ExtraData;
44 ALuint now, start;
45 ALuint64 avail, done;
46 const ALuint restTime = (ALuint64)Device->UpdateSize * 1000 /
47 Device->Frequency / 2;
49 done = 0;
50 start = timeGetTime();
51 while(!data->killNow && Device->Connected)
53 now = timeGetTime();
55 avail = (ALuint64)(now-start) * Device->Frequency / 1000;
56 if(avail < done)
58 /* Timer wrapped. Add the remainder of the cycle to the available
59 * count and reset the number of samples done */
60 avail += (ALuint64)0xFFFFFFFFu*Device->Frequency/1000 - done;
61 done = 0;
63 if(avail-done < Device->UpdateSize)
65 Sleep(restTime);
66 continue;
69 while(avail-done >= Device->UpdateSize)
71 aluMixData(Device, data->buffer, Device->UpdateSize);
72 done += Device->UpdateSize;
76 return 0;
79 static ALCboolean null_open_playback(ALCdevice *device, const ALCchar *deviceName)
81 null_data *data;
83 if(!deviceName)
84 deviceName = nullDevice;
85 else if(strcmp(deviceName, nullDevice) != 0)
86 return ALC_FALSE;
88 data = (null_data*)calloc(1, sizeof(*data));
90 device->szDeviceName = strdup(deviceName);
91 device->ExtraData = data;
92 return ALC_TRUE;
95 static void null_close_playback(ALCdevice *device)
97 null_data *data = (null_data*)device->ExtraData;
99 free(data);
100 device->ExtraData = NULL;
103 static ALCboolean null_reset_playback(ALCdevice *device)
105 null_data *data = (null_data*)device->ExtraData;
107 data->size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans,
108 device->FmtType);
109 data->buffer = malloc(data->size);
110 if(!data->buffer)
112 AL_PRINT("buffer malloc failed\n");
113 return ALC_FALSE;
115 SetDefaultWFXChannelOrder(device);
117 data->thread = StartThread(NullProc, device);
118 if(data->thread == NULL)
120 free(data->buffer);
121 data->buffer = NULL;
122 return ALC_FALSE;
125 return ALC_TRUE;
128 static void null_stop_playback(ALCdevice *device)
130 null_data *data = (null_data*)device->ExtraData;
132 if(!data->thread)
133 return;
135 data->killNow = 1;
136 StopThread(data->thread);
137 data->thread = NULL;
139 data->killNow = 0;
141 free(data->buffer);
142 data->buffer = NULL;
146 static ALCboolean null_open_capture(ALCdevice *device, const ALCchar *deviceName)
148 (void)device;
149 (void)deviceName;
150 return ALC_FALSE;
154 BackendFuncs null_funcs = {
155 null_open_playback,
156 null_close_playback,
157 null_reset_playback,
158 null_stop_playback,
159 null_open_capture,
160 NULL,
161 NULL,
162 NULL,
163 NULL,
164 NULL
167 void alc_null_init(BackendFuncs *func_list)
169 *func_list = null_funcs;
172 void alc_null_deinit(void)
176 void alc_null_probe(int type)
178 if(type == DEVICE_PROBE)
179 AppendDeviceList(nullDevice);
180 else if(type == ALL_DEVICE_PROBE)
181 AppendAllDeviceList(nullDevice);