qt.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #include "qThreadWrapper.h"
  26. #include "qAudioOutputWrapper.h"
  27. static const ALCchar qt_device[] = "Qt Default";
  28. typedef struct
  29. {
  30. Thread thread;
  31. AudioOutput audio;
  32. AudioBuffer buffer;
  33. volatile int running;
  34. } QtData;
  35. #ifdef _SYMBIAN
  36. static void* tick_function(void* arg)
  37. {
  38. ALCdevice* device = (ALCdevice*)arg;
  39. QtData* data = (QtData*)device->ExtraData;
  40. if (!data->running)
  41. {
  42. return NULL;
  43. }
  44. if (!isAudioOutputReady(data->audio))
  45. {
  46. return NULL;
  47. }
  48. int bufferSizeInBytes = getAudioBufferPeriodSize(data->audio);
  49. int bufferSizeInSamples = bufferSizeInBytes / aluFrameSizeFromFormat(device->Format);
  50. char* buffer = (char*)calloc(bufferSizeInBytes, sizeof(char));
  51. int bytesFree = getAudioBufferFreeSize(data->audio);
  52. int chunks = bytesFree / bufferSizeInBytes;
  53. while (chunks)
  54. {
  55. aluMixData(device, buffer, bufferSizeInSamples);
  56. writeAudioBuffer(data->buffer, buffer, bufferSizeInBytes);
  57. --chunks;
  58. }
  59. free(buffer);
  60. return NULL;
  61. }
  62. #else
  63. static void* thread_function(void* arg)
  64. {
  65. ALCdevice* device = (ALCdevice*)arg;
  66. QtData* data = (QtData*)device->ExtraData;
  67. while (!isAudioOutputReady(data->audio))
  68. {
  69. }
  70. AL_PRINT("QAudioOutput ready.");
  71. int bufferSizeInBytes = getAudioBufferPeriodSize(data->audio);
  72. int bufferSizeInSamples = bufferSizeInBytes / aluFrameSizeFromFormat(device->Format);
  73. char* buffer = (char*)calloc(bufferSizeInBytes, sizeof(char));
  74. while (data->running)
  75. {
  76. int bytesFree = getAudioBufferFreeSize(data->audio);
  77. int chunks = bytesFree / bufferSizeInBytes;
  78. while (chunks)
  79. {
  80. aluMixData(device, buffer, bufferSizeInSamples);
  81. writeAudioBuffer(data->buffer, buffer, bufferSizeInBytes);
  82. --chunks;
  83. }
  84. sleepThread(data->thread, 500);
  85. }
  86. free(buffer);
  87. return NULL;
  88. }
  89. #endif
  90. static ALCboolean qt_open_playback(ALCdevice *device, const ALCchar *deviceName)
  91. {
  92. QtData* data;
  93. if (!deviceName)
  94. {
  95. deviceName = qt_device;
  96. }
  97. else if (strcmp(deviceName, qt_device) != 0)
  98. {
  99. return ALC_FALSE;
  100. }
  101. data = (QtData*)calloc(1, sizeof(*data));
  102. device->szDeviceName = strdup(deviceName);
  103. device->ExtraData = data;
  104. return ALC_TRUE;
  105. }
  106. static void qt_close_playback(ALCdevice *device)
  107. {
  108. QtData* data = (QtData*)device->ExtraData;
  109. if (data != NULL)
  110. {
  111. free(data);
  112. device->ExtraData = NULL;
  113. }
  114. }
  115. static ALCboolean qt_reset_playback(ALCdevice *device)
  116. {
  117. QtData* data = (QtData*)device->ExtraData;
  118. if (aluChannelsFromFormat(device->Format) >= 2)
  119. {
  120. device->Format = aluBytesFromFormat(device->Format) >= 2 ? AL_FORMAT_STEREO16 : AL_FORMAT_STEREO8;
  121. }
  122. else
  123. {
  124. device->Format = aluBytesFromFormat(device->Format) >= 2 ? AL_FORMAT_MONO16 : AL_FORMAT_MONO8;
  125. }
  126. SetDefaultChannelOrder(device);
  127. int frequency = device->Frequency;
  128. int channels = aluChannelsFromFormat(device->Format);
  129. int sampleSize = aluBytesFromFormat(device->Format) == 1 ? 8 : 16;
  130. AudioOutput audioOutput = createAudioOutput(frequency, channels, sampleSize);
  131. if (!audioOutput)
  132. {
  133. AL_PRINT("Failed to create AudioOutput!");
  134. return ALC_FALSE;
  135. }
  136. AudioBuffer audioBuffer = startAudioOutput(audioOutput);
  137. if (!audioBuffer)
  138. {
  139. AL_PRINT("Failed to start AudioOutput!");
  140. return ALC_FALSE;
  141. }
  142. data->audio = audioOutput;
  143. data->buffer = audioBuffer;
  144. #ifdef _SYMBIAN
  145. data->thread = createThread(tick_function, device);
  146. #else
  147. data->thread = createThread(thread_function, device);
  148. #endif
  149. data->running = 1;
  150. return ALC_TRUE;
  151. }
  152. static void qt_stop_playback(ALCdevice *device)
  153. {
  154. QtData* data = (QtData*)device->ExtraData;
  155. if (data->running)
  156. {
  157. data->running = 0;
  158. destroyThread(data->thread);
  159. }
  160. stopAudioOutput(data->audio);
  161. destroyAudioOutput(data->audio);
  162. }
  163. static ALCboolean qt_open_capture(ALCdevice *pDevice, const ALCchar *deviceName)
  164. {
  165. (void)pDevice;
  166. (void)deviceName;
  167. return ALC_FALSE;
  168. }
  169. static void qt_close_capture(ALCdevice *pDevice)
  170. {
  171. (void)pDevice;
  172. }
  173. static void qt_start_capture(ALCdevice *pDevice)
  174. {
  175. (void)pDevice;
  176. }
  177. static void qt_stop_capture(ALCdevice *pDevice)
  178. {
  179. (void)pDevice;
  180. }
  181. static void qt_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
  182. {
  183. (void)pDevice;
  184. (void)pBuffer;
  185. (void)lSamples;
  186. }
  187. static ALCuint qt_available_samples(ALCdevice *pDevice)
  188. {
  189. (void)pDevice;
  190. return 0;
  191. }
  192. static const BackendFuncs qt_funcs = {
  193. qt_open_playback,
  194. qt_close_playback,
  195. qt_reset_playback,
  196. qt_stop_playback,
  197. qt_open_capture,
  198. qt_close_capture,
  199. qt_start_capture,
  200. qt_stop_capture,
  201. qt_capture_samples,
  202. qt_available_samples
  203. };
  204. void alc_qt_init(BackendFuncs *func_list)
  205. {
  206. *func_list = qt_funcs;
  207. }
  208. void alc_qt_deinit(void)
  209. {
  210. }
  211. void alc_qt_probe(int type)
  212. {
  213. if (type == DEVICE_PROBE)
  214. {
  215. AppendDeviceList(qt_device);
  216. }
  217. else if (type == ALL_DEVICE_PROBE)
  218. {
  219. AppendAllDeviceList(qt_device);
  220. }
  221. }