wwviaudio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. (C) Copyright 2007,2008, Stephen M. Cameron.
  3. This file is part of wordwarvi.
  4. wordwarvi is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. wordwarvi is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with wordwarvi; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #ifndef WWVIAUDIO_STUBS_ONLY
  17. #include <stdio.h>
  18. #include <stdint.h>
  19. #include <limits.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #define WWVIAUDIO_DEFINE_GLOBALS
  26. #include "wwviaudio.h"
  27. #undef WWVIAUDIO_DEFINE_GLOBALS
  28. #include <portaudio.h>
  29. #include <ccan/ogg_to_pcm/ogg_to_pcm.h>
  30. #define FRAMES_PER_BUFFER (1024)
  31. static PaStream *stream = NULL;
  32. static int audio_paused = 0;
  33. static int music_playing = 1;
  34. static int sound_working = 0;
  35. static int nomusic = 0;
  36. static int sound_effects_on = 1;
  37. static int sound_device = -1; /* default sound device for port audio. */
  38. static int max_concurrent_sounds = 0;
  39. static int max_sound_clips = 0;
  40. /* Pause all audio output, output silence. */
  41. void wwviaudio_pause_audio(void)
  42. {
  43. audio_paused = 1;
  44. }
  45. /* Resume playing audio previously paused. */
  46. void wwviaudio_resume_audio(void)
  47. {
  48. audio_paused = 0;
  49. }
  50. /* Silence the music channel */
  51. void wwviaudio_silence_music(void)
  52. {
  53. music_playing = 0;
  54. }
  55. /* Resume volume on the music channel. */
  56. void wwviaudio_resume_music(void)
  57. {
  58. music_playing = 1;
  59. }
  60. void wwviaudio_toggle_music(void)
  61. {
  62. music_playing = !music_playing;
  63. }
  64. /* Silence the sound effects. */
  65. void wwviaudio_silence_sound_effects(void)
  66. {
  67. sound_effects_on = 0;
  68. }
  69. /* Resume volume on the sound effects. */
  70. void wwviaudio_resume_sound_effects(void)
  71. {
  72. sound_effects_on = 1;
  73. }
  74. void wwviaudio_toggle_sound_effects(void)
  75. {
  76. sound_effects_on = !sound_effects_on;
  77. }
  78. void wwviaudio_set_nomusic(void)
  79. {
  80. nomusic = 1;
  81. }
  82. static struct sound_clip {
  83. int active;
  84. int nsamples;
  85. int pos;
  86. int16_t *sample;
  87. } *clip = NULL;
  88. static struct sound_clip *audio_queue = NULL;
  89. int wwviaudio_read_ogg_clip(int clipnum, char *filename)
  90. {
  91. uint64_t nframes;
  92. char filebuf[PATH_MAX];
  93. struct stat statbuf;
  94. int samplesize, sample_rate;
  95. int nchannels;
  96. int rc;
  97. if (clipnum >= max_sound_clips || clipnum < 0)
  98. return -1;
  99. strncpy(filebuf, filename, PATH_MAX);
  100. rc = stat(filebuf, &statbuf);
  101. if (rc != 0) {
  102. snprintf(filebuf, PATH_MAX, "%s", filename);
  103. rc = stat(filebuf, &statbuf);
  104. if (rc != 0) {
  105. fprintf(stderr, "stat('%s') failed.\n", filebuf);
  106. return -1;
  107. }
  108. }
  109. /*
  110. printf("Reading sound file: '%s'\n", filebuf);
  111. printf("frames = %lld\n", sfinfo.frames);
  112. printf("samplerate = %d\n", sfinfo.samplerate);
  113. printf("channels = %d\n", sfinfo.channels);
  114. printf("format = %d\n", sfinfo.format);
  115. printf("sections = %d\n", sfinfo.sections);
  116. printf("seekable = %d\n", sfinfo.seekable);
  117. */
  118. if (clip[clipnum].sample != NULL)
  119. /* overwriting a previously read clip... */
  120. free(clip[clipnum].sample);
  121. rc = ogg_to_pcm(filebuf, &clip[clipnum].sample, &samplesize,
  122. &sample_rate, &nchannels, &nframes);
  123. if (clip[clipnum].sample == NULL) {
  124. printf("Can't get memory for sound data for %llu frames in %s\n",
  125. nframes, filebuf);
  126. goto error;
  127. }
  128. if (rc != 0) {
  129. fprintf(stderr, "Error: ogg_to_pcm('%s') failed.\n",
  130. filebuf);
  131. goto error;
  132. }
  133. clip[clipnum].nsamples = (int) nframes;
  134. if (clip[clipnum].nsamples < 0)
  135. clip[clipnum].nsamples = 0;
  136. return 0;
  137. error:
  138. return -1;
  139. }
  140. /* This routine will be called by the PortAudio engine when audio is needed.
  141. ** It may called at interrupt level on some machines so don't do anything
  142. ** that could mess up the system like calling malloc() or free().
  143. */
  144. static int patestCallback(const void *inputBuffer, void *outputBuffer,
  145. unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo,
  146. PaStreamCallbackFlags statusFlags, __attribute__ ((unused)) void *userData )
  147. {
  148. int i, j, sample, count;
  149. float *out = NULL;
  150. float output;
  151. out = (float*) outputBuffer;
  152. output = 0.0;
  153. count = 0;
  154. if (audio_paused) {
  155. /* output silence when paused and
  156. * don't advance any sound slot pointers
  157. */
  158. for (i = 0; i < framesPerBuffer; i++)
  159. *out++ = (float) 0;
  160. return 0;
  161. }
  162. for (i = 0; i < framesPerBuffer; i++) {
  163. output = 0.0;
  164. count = 0;
  165. for (j = 0; j < max_concurrent_sounds; j++) {
  166. if (!audio_queue[j].active ||
  167. audio_queue[j].sample == NULL)
  168. continue;
  169. sample = i + audio_queue[j].pos;
  170. count++;
  171. if (sample >= audio_queue[j].nsamples) {
  172. audio_queue[j].active = 0;
  173. continue;
  174. }
  175. if (j != WWVIAUDIO_MUSIC_SLOT && sound_effects_on)
  176. output += (float) audio_queue[j].sample[sample] * 0.5 / (float) (INT16_MAX);
  177. else if (j == WWVIAUDIO_MUSIC_SLOT && music_playing)
  178. output += (float) audio_queue[j].sample[sample] / (float) (INT16_MAX);
  179. }
  180. *out++ = (float) output / 2.0;
  181. }
  182. for (i = 0; i < max_concurrent_sounds; i++) {
  183. if (!audio_queue[i].active)
  184. continue;
  185. audio_queue[i].pos += framesPerBuffer;
  186. if (audio_queue[i].pos >= audio_queue[i].nsamples)
  187. audio_queue[i].active = 0;
  188. }
  189. return 0; /* we're never finished */
  190. }
  191. static void decode_paerror(PaError rc)
  192. {
  193. if (rc == paNoError)
  194. return;
  195. fprintf(stderr, "An error occurred while using the portaudio stream\n");
  196. fprintf(stderr, "Error number: %d\n", rc);
  197. fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(rc));
  198. }
  199. static void wwviaudio_terminate_portaudio(PaError rc)
  200. {
  201. Pa_Terminate();
  202. decode_paerror(rc);
  203. }
  204. int wwviaudio_initialize_portaudio(int maximum_concurrent_sounds, int maximum_sound_clips)
  205. {
  206. PaStreamParameters outparams;
  207. PaError rc;
  208. PaDeviceIndex device_count;
  209. max_concurrent_sounds = maximum_concurrent_sounds;
  210. max_sound_clips = maximum_sound_clips;
  211. audio_queue = malloc(max_concurrent_sounds * sizeof(audio_queue[0]));
  212. clip = malloc(max_sound_clips * sizeof(clip[0]));
  213. if (audio_queue == NULL || clip == NULL)
  214. return -1;
  215. memset(audio_queue, 0, sizeof(audio_queue[0]) * max_concurrent_sounds);
  216. memset(clip, 0, sizeof(clip[0]) * max_sound_clips);
  217. rc = Pa_Initialize();
  218. if (rc != paNoError)
  219. goto error;
  220. device_count = Pa_GetDeviceCount();
  221. printf("Portaudio reports %d sound devices.\n", device_count);
  222. if (device_count == 0) {
  223. printf("There will be no audio.\n");
  224. goto error;
  225. rc = 0;
  226. }
  227. sound_working = 1;
  228. outparams.device = Pa_GetDefaultOutputDevice(); /* default output device */
  229. printf("Portaudio says the default device is: %d\n", outparams.device);
  230. if (sound_device != -1) {
  231. if (sound_device >= device_count)
  232. fprintf(stderr, "wordwarvi: Invalid sound device "
  233. "%d specified, ignoring.\n", sound_device);
  234. else
  235. outparams.device = sound_device;
  236. printf("Using sound device %d\n", outparams.device);
  237. }
  238. if (outparams.device < 0 && device_count > 0) {
  239. printf("Hmm, that's strange, portaudio says the default device is %d,\n"
  240. " but there are %d devices\n",
  241. outparams.device, device_count);
  242. printf("I think we'll just skip sound for now.\n");
  243. printf("You might try the '--sounddevice' option and see if that helps.\n");
  244. sound_working = 0;
  245. return -1;
  246. }
  247. outparams.channelCount = 1; /* mono output */
  248. outparams.sampleFormat = paFloat32; /* 32 bit floating point output */
  249. outparams.suggestedLatency =
  250. Pa_GetDeviceInfo(outparams.device)->defaultLowOutputLatency;
  251. outparams.hostApiSpecificStreamInfo = NULL;
  252. rc = Pa_OpenStream(&stream,
  253. NULL, /* no input */
  254. &outparams, WWVIAUDIO_SAMPLE_RATE, FRAMES_PER_BUFFER,
  255. paNoFlag, /* paClipOff, */ /* we won't output out of range samples so don't bother clipping them */
  256. patestCallback, NULL /* cookie */);
  257. if (rc != paNoError)
  258. goto error;
  259. if ((rc = Pa_StartStream(stream)) != paNoError)
  260. goto error;
  261. return rc;
  262. error:
  263. wwviaudio_terminate_portaudio(rc);
  264. return rc;
  265. }
  266. void wwviaudio_stop_portaudio(void)
  267. {
  268. int i, rc;
  269. if (!sound_working)
  270. return;
  271. if ((rc = Pa_StopStream(stream)) != paNoError)
  272. goto error;
  273. rc = Pa_CloseStream(stream);
  274. error:
  275. wwviaudio_terminate_portaudio(rc);
  276. if (audio_queue) {
  277. free(audio_queue);
  278. audio_queue = NULL;
  279. max_concurrent_sounds = 0;
  280. }
  281. if (clip) {
  282. for (i = 0; i < max_sound_clips; i++) {
  283. if (clip[i].sample)
  284. free(clip[i].sample);
  285. }
  286. free(clip);
  287. clip = NULL;
  288. max_sound_clips = 0;
  289. }
  290. return;
  291. }
  292. static int wwviaudio_add_sound_to_slot(int which_sound, int which_slot)
  293. {
  294. int i;
  295. if (!sound_working)
  296. return 0;
  297. if (nomusic && which_slot == WWVIAUDIO_MUSIC_SLOT)
  298. return 0;
  299. if (which_slot != WWVIAUDIO_ANY_SLOT) {
  300. if (audio_queue[which_slot].active)
  301. audio_queue[which_slot].active = 0;
  302. audio_queue[which_slot].pos = 0;
  303. audio_queue[which_slot].nsamples = 0;
  304. /* would like to put a memory barrier here. */
  305. audio_queue[which_slot].sample = clip[which_sound].sample;
  306. audio_queue[which_slot].nsamples = clip[which_sound].nsamples;
  307. /* would like to put a memory barrier here. */
  308. audio_queue[which_slot].active = 1;
  309. return which_slot;
  310. }
  311. for (i=1;i<max_concurrent_sounds;i++) {
  312. if (audio_queue[i].active == 0) {
  313. audio_queue[i].nsamples = clip[which_sound].nsamples;
  314. audio_queue[i].pos = 0;
  315. audio_queue[i].sample = clip[which_sound].sample;
  316. audio_queue[i].active = 1;
  317. break;
  318. }
  319. }
  320. return (i >= max_concurrent_sounds) ? -1 : i;
  321. }
  322. int wwviaudio_add_sound(int which_sound)
  323. {
  324. return wwviaudio_add_sound_to_slot(which_sound, WWVIAUDIO_ANY_SLOT);
  325. }
  326. int wwviaudio_play_music(int which_sound)
  327. {
  328. return wwviaudio_add_sound_to_slot(which_sound, WWVIAUDIO_MUSIC_SLOT);
  329. }
  330. void wwviaudio_add_sound_low_priority(int which_sound)
  331. {
  332. /* adds a sound if there are at least 5 empty sound slots. */
  333. int i;
  334. int empty_slots = 0;
  335. int last_slot;
  336. if (!sound_working)
  337. return;
  338. last_slot = -1;
  339. for (i = 1; i < max_concurrent_sounds; i++)
  340. if (audio_queue[i].active == 0) {
  341. last_slot = i;
  342. empty_slots++;
  343. if (empty_slots >= 5)
  344. break;
  345. }
  346. if (empty_slots < 5)
  347. return;
  348. i = last_slot;
  349. if (audio_queue[i].active == 0) {
  350. audio_queue[i].nsamples = clip[which_sound].nsamples;
  351. audio_queue[i].pos = 0;
  352. audio_queue[i].sample = clip[which_sound].sample;
  353. audio_queue[i].active = 1;
  354. }
  355. return;
  356. }
  357. void wwviaudio_cancel_sound(int queue_entry)
  358. {
  359. if (!sound_working)
  360. return;
  361. audio_queue[queue_entry].active = 0;
  362. }
  363. void wwviaudio_cancel_music(void)
  364. {
  365. wwviaudio_cancel_sound(WWVIAUDIO_MUSIC_SLOT);
  366. }
  367. void wwviaudio_cancel_all_sounds(void)
  368. {
  369. int i;
  370. if (!sound_working)
  371. return;
  372. for (i = 0; i < max_concurrent_sounds; i++)
  373. audio_queue[i].active = 0;
  374. }
  375. int wwviaudio_set_sound_device(int device)
  376. {
  377. sound_device = device;
  378. return 0;
  379. }
  380. #else /* stubs only... */
  381. int wwviaudio_initialize_portaudio() { return 0; }
  382. void wwviaudio_stop_portaudio() { return; }
  383. void wwviaudio_set_nomusic() { return; }
  384. int wwviaudio_read_ogg_clip(int clipnum, char *filename) { return 0; }
  385. void wwviaudio_pause_audio() { return; }
  386. void wwviaudio_resume_audio() { return; }
  387. void wwviaudio_silence_music() { return; }
  388. void wwviaudio_resume_music() { return; }
  389. void wwviaudio_silence_sound_effects() { return; }
  390. void wwviaudio_resume_sound_effects() { return; }
  391. void wwviaudio_toggle_sound_effects() { return; }
  392. int wwviaudio_play_music(int which_sound) { return 0; }
  393. void wwviaudio_cancel_music() { return; }
  394. void wwviaudio_toggle_music() { return; }
  395. int wwviaudio_add_sound(int which_sound) { return 0; }
  396. void wwviaudio_add_sound_low_priority(int which_sound) { return; }
  397. void wwviaudio_cancel_sound(int queue_entry) { return; }
  398. void wwviaudio_cancel_all_sounds() { return; }
  399. int wwviaudio_set_sound_device(int device) { return 0; }
  400. #endif