null.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. volatile int killNow;
  27. ALvoid *thread;
  28. } null_data;
  29. static const ALCchar nullDevice[] = "No Output";
  30. static ALuint NullProc(ALvoid *ptr)
  31. {
  32. ALCdevice *Device = (ALCdevice*)ptr;
  33. null_data *data = (null_data*)Device->ExtraData;
  34. ALuint now, start;
  35. ALuint64 avail, done;
  36. const ALuint restTime = (ALuint64)Device->UpdateSize * 1000 /
  37. Device->Frequency / 2;
  38. done = 0;
  39. start = timeGetTime();
  40. while(!data->killNow && Device->Connected)
  41. {
  42. now = timeGetTime();
  43. avail = (ALuint64)(now-start) * Device->Frequency / 1000;
  44. if(avail < done)
  45. {
  46. /* Timer wrapped (50 days???). Add the remainder of the cycle to
  47. * the available count and reset the number of samples done */
  48. avail += ((ALuint64)1<<32)*Device->Frequency/1000 - done;
  49. done = 0;
  50. }
  51. if(avail-done < Device->UpdateSize)
  52. {
  53. Sleep(restTime);
  54. continue;
  55. }
  56. while(avail-done >= Device->UpdateSize)
  57. {
  58. aluMixData(Device, NULL, Device->UpdateSize);
  59. done += Device->UpdateSize;
  60. }
  61. }
  62. return 0;
  63. }
  64. static ALCenum null_open_playback(ALCdevice *device, const ALCchar *deviceName)
  65. {
  66. null_data *data;
  67. if(!deviceName)
  68. deviceName = nullDevice;
  69. else if(strcmp(deviceName, nullDevice) != 0)
  70. return ALC_INVALID_VALUE;
  71. data = (null_data*)calloc(1, sizeof(*data));
  72. device->szDeviceName = strdup(deviceName);
  73. device->ExtraData = data;
  74. return ALC_NO_ERROR;
  75. }
  76. static void null_close_playback(ALCdevice *device)
  77. {
  78. null_data *data = (null_data*)device->ExtraData;
  79. free(data);
  80. device->ExtraData = NULL;
  81. }
  82. static ALCboolean null_reset_playback(ALCdevice *device)
  83. {
  84. SetDefaultWFXChannelOrder(device);
  85. return ALC_TRUE;
  86. }
  87. static ALCboolean null_start_playback(ALCdevice *device)
  88. {
  89. null_data *data = (null_data*)device->ExtraData;
  90. data->thread = StartThread(NullProc, device);
  91. if(data->thread == NULL)
  92. return ALC_FALSE;
  93. return ALC_TRUE;
  94. }
  95. static void null_stop_playback(ALCdevice *device)
  96. {
  97. null_data *data = (null_data*)device->ExtraData;
  98. if(!data->thread)
  99. return;
  100. data->killNow = 1;
  101. StopThread(data->thread);
  102. data->thread = NULL;
  103. data->killNow = 0;
  104. }
  105. static const BackendFuncs null_funcs = {
  106. null_open_playback,
  107. null_close_playback,
  108. null_reset_playback,
  109. null_start_playback,
  110. null_stop_playback,
  111. NULL,
  112. NULL,
  113. NULL,
  114. NULL,
  115. NULL,
  116. NULL
  117. };
  118. ALCboolean alc_null_init(BackendFuncs *func_list)
  119. {
  120. *func_list = null_funcs;
  121. return ALC_TRUE;
  122. }
  123. void alc_null_deinit(void)
  124. {
  125. }
  126. void alc_null_probe(enum DevProbe type)
  127. {
  128. switch(type)
  129. {
  130. case ALL_DEVICE_PROBE:
  131. AppendAllDeviceList(nullDevice);
  132. break;
  133. case CAPTURE_DEVICE_PROBE:
  134. break;
  135. }
  136. }