You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
8.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "audio.h"
  2. #include <windows.h>
  3. #define BUFFER_SIZE 0x10000
  4. #define NUM_BUFFERS 8 /* total 512k roughly 2.5 sec of CD quality sound */
  5. static void wait_for_buffer(WAVEHDR* hdr, HANDLE hEvent);
  6. static void drain_audio(struct audio_output_struct *ao);
  7. void ereturn( int eno, const char* estr )
  8. {
  9. #ifdef DEBUG
  10. printf("Error(%d):%s\n", eno, estr);
  11. #endif
  12. }
  13. /* Buffer ring queue state */
  14. struct queue_state
  15. {
  16. WAVEHDR buffer_headers[NUM_BUFFERS];
  17. /* The next buffer to be filled and put in playback */
  18. int next_buffer;
  19. /* Buffer playback completion event */
  20. HANDLE play_done_event;
  21. HWAVEOUT waveout;
  22. };
  23. static int open_audio( audio_output_t *ao )
  24. {
  25. struct queue_state* state;
  26. int i;
  27. MMRESULT res;
  28. WAVEFORMATEX out_fmt;
  29. UINT dev_id;
  30. if(!ao) return -1;
  31. if(ao->rate == -1) return 0;
  32. /* Allocate queue state struct for this device */
  33. state = calloc(1, sizeof(struct queue_state));
  34. if(!state) return -1;
  35. ao->userptr = state;
  36. state->play_done_event = CreateEvent(0,FALSE,FALSE,0);
  37. if(state->play_done_event == INVALID_HANDLE_VALUE) return -1;
  38. /* FIXME: real device enumeration by capabilities? */
  39. dev_id = WAVE_MAPPER; /* probably does the same thing */
  40. ao->device = "WaveMapper";
  41. /* FIXME: support for smth besides MPG123_ENC_SIGNED_16? */
  42. out_fmt.wFormatTag = WAVE_FORMAT_PCM;
  43. out_fmt.wBitsPerSample = 16;
  44. out_fmt.nChannels = ao->channels;
  45. out_fmt.nSamplesPerSec = ao->rate;
  46. out_fmt.nBlockAlign = out_fmt.nChannels*out_fmt.wBitsPerSample/8;
  47. out_fmt.nAvgBytesPerSec = out_fmt.nBlockAlign*out_fmt.nSamplesPerSec;
  48. out_fmt.cbSize = 0;
  49. res = waveOutOpen(&state->waveout, dev_id, &out_fmt,
  50. (DWORD_PTR)state->play_done_event, 0, CALLBACK_EVENT);
  51. switch(res)
  52. {
  53. case MMSYSERR_NOERROR:
  54. break;
  55. case MMSYSERR_ALLOCATED:
  56. ereturn(-1, "Audio output device is already allocated.");
  57. case MMSYSERR_NODRIVER:
  58. ereturn(-1, "No device driver is present.");
  59. case MMSYSERR_NOMEM:
  60. ereturn(-1, "Unable to allocate or lock memory.");
  61. case WAVERR_BADFORMAT:
  62. ereturn(-1, "Unsupported waveform-audio format.");
  63. default:
  64. ereturn(-1, "Unable to open wave output device.");
  65. }
  66. /* Reset event from the "device open" message */
  67. ResetEvent(state->play_done_event);
  68. /* Allocate playback buffers */
  69. for(i = 0; i < NUM_BUFFERS; i++)
  70. if(!(state->buffer_headers[i].lpData = (LPSTR)malloc(BUFFER_SIZE)))
  71. {
  72. ereturn(-1, "Out of memory for playback buffers.");
  73. }
  74. else
  75. {
  76. /* Tell waveOutPrepareHeader the maximum value of dwBufferLength
  77. we will ever send */
  78. state->buffer_headers[i].dwBufferLength = BUFFER_SIZE;
  79. state->buffer_headers[i].dwFlags = 0;
  80. res = waveOutPrepareHeader(state->waveout, &state->buffer_headers[i], sizeof(WAVEHDR));
  81. if(res != MMSYSERR_NOERROR) ereturn(-1, "Can't write to audio output device (prepare).");
  82. /* set the current size of the buffer to 0 */
  83. state->buffer_headers[i].dwBufferLength = 0;
  84. /* set flags to unprepared - must reset this to WHDR_PREPARED before calling write */
  85. state->buffer_headers[i].dwFlags = 0;
  86. }
  87. return 0;
  88. }
  89. static void wait_for_buffer(WAVEHDR* hdr, HANDLE hEvent)
  90. {
  91. /* At this point there are several possible states:
  92. 1) Empty or partial buffer (unqueued) - dwFlags == 0
  93. 2) Buffer queued or being played - dwFlags == WHDR_PREPARED | WHDR_INQUEUE
  94. 3) Buffer unqueued and finished being played - dwFlags == WHDR_PREPARED | WHDR_DONE
  95. 4) Buffer removed from queue, but not yet marked as done - dwFlags == WHDR_PREPARED
  96. */
  97. /* Check buffer header and wait if it's being played. */
  98. if (hdr->dwFlags & WHDR_PREPARED)
  99. {
  100. while(!(hdr->dwFlags & WHDR_DONE))
  101. {
  102. /*debug1("waiting for buffer %i...", state->next_buffer);*/
  103. /* Waits for *a* buffer to finish. May not be the one we
  104. want, so check again */
  105. WaitForSingleObject(hEvent, INFINITE);
  106. }
  107. hdr->dwFlags = 0;
  108. hdr->dwBufferLength = 0;
  109. }
  110. }
  111. static int get_audio_formats(struct audio_output_struct *ao)
  112. {
  113. /* FIXME: support for smth besides MPG123_ENC_SIGNED_16? */
  114. return AUDIO_TYPE_SIGNED_16;
  115. }
  116. /* Stores audio data to the fixed size buffers and pushes them into the playback queue.
  117. I have one grief with that: The last piece of a track may not reach the output,
  118. only full buffers sent... But we don't get smooth audio otherwise. */
  119. static int write_audio(struct audio_output_struct *ao, unsigned char *buf, int len)
  120. {
  121. struct queue_state* state;
  122. MMRESULT res;
  123. WAVEHDR* hdr;
  124. int rest_len; /* Input data bytes left for next recursion. */
  125. int bufill; /* Bytes we stuff into buffer now. */
  126. if(!ao || !ao->userptr) return -1;
  127. if(!buf || len <= 0) return 0;
  128. state = (struct queue_state*)ao->userptr;
  129. hdr = &state->buffer_headers[state->next_buffer];
  130. wait_for_buffer(hdr, state->play_done_event);
  131. /* Now see how much we want to stuff in and then stuff it in. */
  132. bufill = BUFFER_SIZE - hdr->dwBufferLength;
  133. if(len < bufill) bufill = len;
  134. rest_len = len - bufill;
  135. memcpy(hdr->lpData + hdr->dwBufferLength, buf, bufill);
  136. hdr->dwBufferLength += bufill;
  137. if(hdr->dwBufferLength == BUFFER_SIZE)
  138. { /* Send the buffer out when it's full. */
  139. hdr->dwFlags |= WHDR_PREPARED;
  140. res = waveOutWrite(state->waveout, hdr, sizeof(WAVEHDR));
  141. if(res != MMSYSERR_NOERROR) ereturn(-1, "Can't write to audio output device.");
  142. /* Cycle to the next buffer in the ring queue */
  143. state->next_buffer = (state->next_buffer + 1) % NUM_BUFFERS;
  144. }
  145. /* I'd like to propagate error codes or something... but there are no catchable surprises left.
  146. Anyhow: Here is the recursion that makes ravenexp happy;-) */
  147. if(rest_len && write_audio(ao, buf + bufill, rest_len) < 0) /* Write the rest. */
  148. return -1;
  149. else
  150. return len;
  151. }
  152. /* Flush means abort any pending playback */
  153. static void flush_audio(struct audio_output_struct *ao)
  154. {
  155. struct queue_state* state;
  156. WAVEHDR* hdr;
  157. if(!ao || !ao->userptr) return;
  158. state = (struct queue_state*)ao->userptr;
  159. /* Cancel any buffers in queue. Ignore errors since we are void and
  160. can't return them anyway */
  161. waveOutReset(state->waveout);
  162. /* Discard any partial buffer */
  163. hdr = &state->buffer_headers[state->next_buffer];
  164. /* If WHDR_PREPARED is not set, this is (potentially) a partial buffer */
  165. if (!(hdr->dwFlags & WHDR_PREPARED))
  166. hdr->dwBufferLength = 0;
  167. /* Finish processing the buffers */
  168. drain_audio(ao);
  169. }
  170. /* output final buffer (if any) */
  171. static void write_final_buffer(struct queue_state *state)
  172. {
  173. WAVEHDR* hdr;
  174. hdr = &state->buffer_headers[state->next_buffer];
  175. if((!(hdr->dwFlags & WHDR_PREPARED)) && (hdr->dwBufferLength != 0))
  176. {
  177. hdr->dwFlags |= WHDR_PREPARED;
  178. /* ignore any errors */
  179. waveOutWrite(state->waveout, hdr, sizeof(WAVEHDR));
  180. /* Cycle to the next buffer in the ring queue */
  181. state->next_buffer = (state->next_buffer + 1) % NUM_BUFFERS;
  182. }
  183. }
  184. /* Note: I tried to fix this stuff without testing.
  185. There were some obvious errors in the code.
  186. Someone run this on a win32 machine! -- ThOr */
  187. static void drain_audio(struct audio_output_struct *ao)
  188. {
  189. int i, z;
  190. struct queue_state* state;
  191. if(!ao || !ao->userptr) return;
  192. state = (struct queue_state*)ao->userptr;
  193. /* output final buffer (if any) */
  194. write_final_buffer(state);
  195. /* I _think_ I understood how this should work. -- ThOr */
  196. z = state->next_buffer;
  197. for(i = 0; i < NUM_BUFFERS; i++)
  198. {
  199. wait_for_buffer(&state->buffer_headers[z], state->play_done_event);
  200. z = (z + 1) % NUM_BUFFERS;
  201. }
  202. }
  203. static int close_audio(struct audio_output_struct *ao)
  204. {
  205. int i;
  206. struct queue_state* state;
  207. if(!ao || !ao->userptr) return -1;
  208. state = (struct queue_state*)ao->userptr;
  209. /* wait for all active buffers to complete */
  210. drain_audio(ao);
  211. CloseHandle(state->play_done_event);
  212. for(i = 0; i < NUM_BUFFERS; i++)
  213. {
  214. state->buffer_headers[i].dwFlags |= WHDR_PREPARED;
  215. waveOutUnprepareHeader(state->waveout, &state->buffer_headers[i], sizeof(WAVEHDR));
  216. free(state->buffer_headers[i].lpData);
  217. }
  218. waveOutClose(state->waveout);
  219. free(ao->userptr);
  220. ao->userptr = 0;
  221. return 0;
  222. }
  223. static int getbuffersize()
  224. {
  225. return BUFFER_SIZE;
  226. }
  227. int init_audio(audio_output_t* ao)
  228. {
  229. if(!ao) return -1;
  230. /* Set callbacks */
  231. ao->open = open_audio;
  232. ao->flush = flush_audio;
  233. ao->write = write_audio;
  234. ao->get_formats = get_audio_formats;
  235. ao->close = close_audio;
  236. ao->getbuffersize = getbuffersize;
  237. /* Success */
  238. return 0;
  239. }