null.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include "alMain.h"
  23. #include "AL/al.h"
  24. #include "AL/alc.h"
  25. typedef struct {
  26. ALvoid *buffer;
  27. ALuint size;
  28. volatile int killNow;
  29. ALvoid *thread;
  30. } null_data;
  31. static const ALCchar nullDevice[] = "No Output";
  32. static ALuint NullProc(ALvoid *ptr)
  33. {
  34. ALCdevice *Device = (ALCdevice*)ptr;
  35. null_data *data = (null_data*)Device->ExtraData;
  36. ALuint now, start;
  37. ALuint64 avail, done;
  38. const ALuint restTime = ((ALuint)((ALuint64)Device->UpdateSize * 1000 /
  39. Device->Frequency)) / 2;
  40. done = 0;
  41. start = timeGetTime();
  42. while(!data->killNow && Device->Connected)
  43. {
  44. now = timeGetTime();
  45. avail = (ALuint64)(now-start) * Device->Frequency / 1000;
  46. if(avail < done)
  47. {
  48. /* Timer wrapped. Add the remainder of the cycle to the available
  49. * count and reset the number of samples done */
  50. avail += (ALuint64)0xFFFFFFFFu*Device->Frequency/1000 - done;
  51. done = 0;
  52. }
  53. if(avail-done < Device->UpdateSize)
  54. {
  55. Sleep(restTime);
  56. continue;
  57. }
  58. while(avail-done >= Device->UpdateSize)
  59. {
  60. aluMixData(Device, data->buffer, Device->UpdateSize);
  61. done += Device->UpdateSize;
  62. }
  63. }
  64. return 0;
  65. }
  66. static ALCboolean null_open_playback(ALCdevice *device, const ALCchar *deviceName)
  67. {
  68. null_data *data;
  69. if(!deviceName)
  70. deviceName = nullDevice;
  71. else if(strcmp(deviceName, nullDevice) != 0)
  72. return ALC_FALSE;
  73. data = (null_data*)calloc(1, sizeof(*data));
  74. device->szDeviceName = strdup(deviceName);
  75. device->ExtraData = data;
  76. return ALC_TRUE;
  77. }
  78. static void null_close_playback(ALCdevice *device)
  79. {
  80. null_data *data = (null_data*)device->ExtraData;
  81. free(data);
  82. device->ExtraData = NULL;
  83. }
  84. static ALCboolean null_reset_playback(ALCdevice *device)
  85. {
  86. null_data *data = (null_data*)device->ExtraData;
  87. data->size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans,
  88. device->FmtType);
  89. data->buffer = malloc(data->size);
  90. if(!data->buffer)
  91. {
  92. AL_PRINT("buffer malloc failed\n");
  93. return ALC_FALSE;
  94. }
  95. SetDefaultWFXChannelOrder(device);
  96. data->thread = StartThread(NullProc, device);
  97. if(data->thread == NULL)
  98. {
  99. free(data->buffer);
  100. data->buffer = NULL;
  101. return ALC_FALSE;
  102. }
  103. return ALC_TRUE;
  104. }
  105. static void null_stop_playback(ALCdevice *device)
  106. {
  107. null_data *data = (null_data*)device->ExtraData;
  108. if(!data->thread)
  109. return;
  110. data->killNow = 1;
  111. StopThread(data->thread);
  112. data->thread = NULL;
  113. data->killNow = 0;
  114. free(data->buffer);
  115. data->buffer = NULL;
  116. }
  117. static ALCboolean null_open_capture(ALCdevice *device, const ALCchar *deviceName)
  118. {
  119. (void)device;
  120. (void)deviceName;
  121. return ALC_FALSE;
  122. }
  123. BackendFuncs null_funcs = {
  124. null_open_playback,
  125. null_close_playback,
  126. null_reset_playback,
  127. null_stop_playback,
  128. null_open_capture,
  129. NULL,
  130. NULL,
  131. NULL,
  132. NULL,
  133. NULL
  134. };
  135. void alc_null_init(BackendFuncs *func_list)
  136. {
  137. *func_list = null_funcs;
  138. }
  139. void alc_null_deinit(void)
  140. {
  141. }
  142. void alc_null_probe(int type)
  143. {
  144. if(type == DEVICE_PROBE)
  145. AppendDeviceList(nullDevice);
  146. else if(type == ALL_DEVICE_PROBE)
  147. AppendAllDeviceList(nullDevice);
  148. }