audio_driver.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  1. /**
  2. * RetroArch - A frontend for libretro.
  3. * Copyright (C) 2010-2014 - Hans-Kristian Arntzen
  4. * Copyright (C) 2011-2017 - Daniel De Matteis
  5. *
  6. * RetroArch is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU General Public License as published by the Free
  8. * Software Foundation, either version 3 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * RetroArch is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with RetroArch. If not, see <http://www.gnu.org/licenses/>.
  18. **/
  19. #include <math.h>
  20. #include "audio_driver.h"
  21. #include <string/stdstring.h>
  22. #include <encodings/utf.h>
  23. #include <clamping.h>
  24. #include <memalign.h>
  25. #include <audio/conversion/float_to_s16.h>
  26. #include <audio/conversion/s16_to_float.h>
  27. #ifdef HAVE_AUDIOMIXER
  28. #include <audio/audio_mixer.h>
  29. #include "../tasks/task_audio_mixer.h"
  30. #endif
  31. #ifdef HAVE_DSP_FILTER
  32. #include <audio/dsp_filter.h>
  33. #endif
  34. #include <lists/dir_list.h>
  35. #ifdef HAVE_THREADS
  36. #include "audio_thread_wrapper.h"
  37. #endif
  38. #ifdef HAVE_MENU
  39. #include "../menu/menu_driver.h"
  40. #endif
  41. #ifdef HAVE_NETWORKING
  42. #include "../network/netplay/netplay.h"
  43. #endif
  44. #include "../configuration.h"
  45. #include "../driver.h"
  46. #include "../frontend/frontend_driver.h"
  47. #include "../retroarch.h"
  48. #include "../list_special.h"
  49. #include "../file_path_special.h"
  50. #include "../record/record_driver.h"
  51. #include "../tasks/task_content.h"
  52. #include "../verbosity.h"
  53. #define MENU_SOUND_FORMATS "ogg|mod|xm|s3m|mp3|flac|wav"
  54. /* Converts decibels to voltage gain. returns voltage gain value. */
  55. #define DB_TO_GAIN(db) (powf(10.0f, (db) / 20.0f))
  56. audio_driver_t audio_null = {
  57. NULL, /* init */
  58. NULL, /* write */
  59. NULL, /* stop */
  60. NULL, /* start */
  61. NULL, /* alive */
  62. NULL, /* set_nonblock_state */
  63. NULL, /* free */
  64. NULL, /* use_float */
  65. "null",
  66. NULL,
  67. NULL,
  68. NULL, /* write_avail */
  69. NULL
  70. };
  71. audio_driver_t *audio_drivers[] = {
  72. #ifdef HAVE_ALSA
  73. &audio_alsa,
  74. #if !defined(__QNX__) && !defined(MIYOO) && defined(HAVE_THREADS)
  75. &audio_alsathread,
  76. #endif
  77. #endif
  78. #ifdef HAVE_TINYALSA
  79. &audio_tinyalsa,
  80. #endif
  81. #if defined(HAVE_AUDIOIO)
  82. &audio_audioio,
  83. #endif
  84. #if defined(HAVE_OSS) || defined(HAVE_OSS_BSD)
  85. &audio_oss,
  86. #endif
  87. #ifdef HAVE_RSOUND
  88. &audio_rsound,
  89. #endif
  90. #ifdef HAVE_COREAUDIO
  91. &audio_coreaudio,
  92. #endif
  93. #ifdef HAVE_COREAUDIO3
  94. &audio_coreaudio3,
  95. #endif
  96. #ifdef HAVE_AL
  97. &audio_openal,
  98. #endif
  99. #ifdef HAVE_SL
  100. &audio_opensl,
  101. #endif
  102. #ifdef HAVE_ROAR
  103. &audio_roar,
  104. #endif
  105. #ifdef HAVE_JACK
  106. &audio_jack,
  107. #endif
  108. #ifdef HAVE_WASAPI
  109. &audio_wasapi,
  110. #endif
  111. #ifdef HAVE_XAUDIO
  112. &audio_xa,
  113. #endif
  114. #ifdef HAVE_DSOUND
  115. &audio_dsound,
  116. #endif
  117. #if defined(HAVE_SDL) || defined(HAVE_SDL2)
  118. &audio_sdl,
  119. #endif
  120. #ifdef HAVE_PULSE
  121. &audio_pulse,
  122. #endif
  123. #if defined(__PSL1GHT__) || defined(__PS3__)
  124. &audio_ps3,
  125. #endif
  126. #ifdef XENON
  127. &audio_xenon360,
  128. #endif
  129. #ifdef GEKKO
  130. &audio_gx,
  131. #endif
  132. #ifdef WIIU
  133. &audio_ax,
  134. #endif
  135. #ifdef EMSCRIPTEN
  136. &audio_rwebaudio,
  137. #endif
  138. #if defined(PSP) || defined(VITA) || defined(ORBIS)
  139. &audio_psp,
  140. #endif
  141. #if defined(PS2)
  142. &audio_ps2,
  143. #endif
  144. #ifdef _3DS
  145. &audio_ctr_csnd,
  146. &audio_ctr_dsp,
  147. #ifdef HAVE_THREADS
  148. &audio_ctr_dsp_thread,
  149. #endif
  150. #endif
  151. #ifdef SWITCH
  152. &audio_switch,
  153. &audio_switch_thread,
  154. #ifdef HAVE_LIBNX
  155. &audio_switch_libnx_audren,
  156. &audio_switch_libnx_audren_thread,
  157. #endif
  158. #endif
  159. &audio_null,
  160. NULL,
  161. };
  162. static audio_driver_state_t audio_driver_st = {0}; /* double alignment */
  163. /**************************************/
  164. audio_driver_state_t *audio_state_get_ptr(void)
  165. {
  166. return &audio_driver_st;
  167. }
  168. /**
  169. * config_get_audio_driver_options:
  170. *
  171. * Get an enumerated list of all audio driver names, separated by '|'.
  172. *
  173. * Returns: string listing of all audio driver names, separated by '|'.
  174. **/
  175. const char *config_get_audio_driver_options(void)
  176. {
  177. return char_list_new_special(STRING_LIST_AUDIO_DRIVERS, NULL);
  178. }
  179. #ifdef HAVE_TRANSLATE
  180. /* TODO/FIXME - Doesn't currently work. Fix this. */
  181. bool audio_driver_is_ai_service_speech_running(void)
  182. {
  183. #ifdef HAVE_AUDIOMIXER
  184. enum audio_mixer_state res = audio_driver_mixer_get_stream_state(10);
  185. bool ret = (res == AUDIO_STREAM_STATE_NONE) || (res == AUDIO_STREAM_STATE_STOPPED);
  186. if (!ret)
  187. return true;
  188. #endif
  189. return false;
  190. }
  191. #endif
  192. static enum resampler_quality audio_driver_get_resampler_quality(
  193. settings_t *settings)
  194. {
  195. if (settings)
  196. return (enum resampler_quality)settings->uints.audio_resampler_quality;
  197. return RESAMPLER_QUALITY_DONTCARE;
  198. }
  199. static bool audio_driver_free_devices_list(void)
  200. {
  201. audio_driver_state_t *audio_st = &audio_driver_st;
  202. if (
  203. !audio_st->current_audio
  204. || !audio_st->current_audio->device_list_free
  205. || !audio_st->context_audio_data)
  206. return false;
  207. audio_st->current_audio->device_list_free(
  208. audio_st->context_audio_data,
  209. audio_st->devices_list);
  210. audio_st->devices_list = NULL;
  211. return true;
  212. }
  213. #ifdef DEBUG
  214. static void report_audio_buffer_statistics(void)
  215. {
  216. audio_statistics_t audio_stats;
  217. audio_stats.samples = 0;
  218. audio_stats.average_buffer_saturation = 0.0f;
  219. audio_stats.std_deviation_percentage = 0.0f;
  220. audio_stats.close_to_underrun = 0.0f;
  221. audio_stats.close_to_blocking = 0.0f;
  222. if (!audio_compute_buffer_statistics(&audio_stats))
  223. return;
  224. RARCH_LOG("[Audio]: Average audio buffer saturation: %.2f %%,"
  225. " standard deviation (percentage points): %.2f %%.\n"
  226. "[Audio]: Amount of time spent close to underrun: %.2f %%."
  227. " Close to blocking: %.2f %%.\n",
  228. audio_stats.average_buffer_saturation,
  229. audio_stats.std_deviation_percentage,
  230. audio_stats.close_to_underrun,
  231. audio_stats.close_to_blocking);
  232. }
  233. #endif
  234. static void audio_driver_deinit_resampler(void)
  235. {
  236. audio_driver_state_t *audio_st = &audio_driver_st;
  237. if (audio_st->resampler && audio_st->resampler_data)
  238. audio_st->resampler->free(audio_st->resampler_data);
  239. audio_st->resampler = NULL;
  240. audio_st->resampler_data = NULL;
  241. audio_st->resampler_ident[0] = '\0';
  242. audio_st->resampler_quality = RESAMPLER_QUALITY_DONTCARE;
  243. }
  244. static bool audio_driver_deinit_internal(bool audio_enable)
  245. {
  246. audio_driver_state_t *audio_st = &audio_driver_st;
  247. if ( audio_st->current_audio
  248. && audio_st->current_audio->free)
  249. {
  250. if (audio_st->context_audio_data)
  251. audio_st->current_audio->free(audio_st->context_audio_data);
  252. audio_st->context_audio_data = NULL;
  253. }
  254. if (audio_st->output_samples_conv_buf)
  255. memalign_free(audio_st->output_samples_conv_buf);
  256. audio_st->output_samples_conv_buf = NULL;
  257. if (audio_st->input_data)
  258. memalign_free(audio_st->input_data);
  259. audio_st->input_data = NULL;
  260. audio_st->data_ptr = 0;
  261. #ifdef HAVE_REWIND
  262. if (audio_st->rewind_buf)
  263. memalign_free(audio_st->rewind_buf);
  264. audio_st->rewind_buf = NULL;
  265. audio_st->rewind_size = 0;
  266. #endif
  267. if (!audio_enable)
  268. {
  269. audio_st->flags &= ~AUDIO_FLAG_ACTIVE;
  270. return false;
  271. }
  272. audio_driver_deinit_resampler();
  273. if (audio_st->output_samples_buf)
  274. memalign_free(audio_st->output_samples_buf);
  275. audio_st->output_samples_buf = NULL;
  276. #ifdef HAVE_DSP_FILTER
  277. audio_driver_dsp_filter_free();
  278. #endif
  279. #ifdef DEBUG
  280. report_audio_buffer_statistics();
  281. #endif
  282. return true;
  283. }
  284. #ifdef HAVE_AUDIOMIXER
  285. static void audio_driver_mixer_deinit(void)
  286. {
  287. unsigned i;
  288. audio_driver_st.flags &= ~AUDIO_FLAG_MIXER_ACTIVE;
  289. for (i = 0; i < AUDIO_MIXER_MAX_SYSTEM_STREAMS; i++)
  290. {
  291. audio_driver_mixer_stop_stream(i);
  292. audio_driver_mixer_remove_stream(i);
  293. }
  294. audio_mixer_done();
  295. }
  296. #endif
  297. bool audio_driver_deinit(void)
  298. {
  299. settings_t *settings = config_get_ptr();
  300. #ifdef HAVE_AUDIOMIXER
  301. audio_driver_mixer_deinit();
  302. #endif
  303. audio_driver_free_devices_list();
  304. return audio_driver_deinit_internal(
  305. settings->bools.audio_enable);
  306. }
  307. bool audio_driver_find_driver(
  308. void *settings_data,
  309. const char *prefix,
  310. bool verbosity_enabled)
  311. {
  312. settings_t *settings = (settings_t*)settings_data;
  313. int i = (int)driver_find_index(
  314. "audio_driver",
  315. settings->arrays.audio_driver);
  316. if (i >= 0)
  317. audio_driver_st.current_audio = (const audio_driver_t*)
  318. audio_drivers[i];
  319. else
  320. {
  321. const audio_driver_t *tmp = NULL;
  322. if (verbosity_enabled)
  323. {
  324. unsigned d;
  325. RARCH_ERR("Couldn't find any %s named \"%s\"\n", prefix,
  326. settings->arrays.audio_driver);
  327. RARCH_LOG_OUTPUT("Available %ss are:\n", prefix);
  328. for (d = 0; audio_drivers[d]; d++)
  329. {
  330. if (audio_drivers[d])
  331. RARCH_LOG_OUTPUT("\t%s\n", audio_drivers[d]->ident);
  332. }
  333. RARCH_WARN("Going to default to first %s...\n", prefix);
  334. }
  335. tmp = (const audio_driver_t*)audio_drivers[0];
  336. if (!tmp)
  337. return false;
  338. audio_driver_st.current_audio = tmp;
  339. }
  340. return true;
  341. }
  342. /**
  343. * audio_driver_flush:
  344. * @data : pointer to audio buffer.
  345. * @right : amount of samples to write.
  346. *
  347. * Writes audio samples to audio driver. Will first
  348. * perform DSP processing (if enabled) and resampling.
  349. **/
  350. static void audio_driver_flush(
  351. audio_driver_state_t *audio_st,
  352. float slowmotion_ratio,
  353. bool audio_fastforward_mute,
  354. const int16_t *data, size_t samples,
  355. bool is_slowmotion, bool is_fastforward)
  356. {
  357. struct resampler_data src_data;
  358. float audio_volume_gain = (audio_st->mute_enable ||
  359. (audio_fastforward_mute && is_fastforward))
  360. ? 0.0f
  361. : audio_st->volume_gain;
  362. src_data.data_out = NULL;
  363. src_data.output_frames = 0;
  364. convert_s16_to_float(audio_st->input_data, data, samples,
  365. audio_volume_gain);
  366. src_data.data_in = audio_st->input_data;
  367. src_data.input_frames = samples >> 1;
  368. #ifdef HAVE_DSP_FILTER
  369. if (audio_st->dsp)
  370. {
  371. struct retro_dsp_data dsp_data;
  372. dsp_data.input = NULL;
  373. dsp_data.input_frames = 0;
  374. dsp_data.output = NULL;
  375. dsp_data.output_frames = 0;
  376. dsp_data.input = audio_st->input_data;
  377. dsp_data.input_frames = (unsigned)(samples >> 1);
  378. retro_dsp_filter_process(audio_st->dsp, &dsp_data);
  379. if (dsp_data.output)
  380. {
  381. src_data.data_in = dsp_data.output;
  382. src_data.input_frames = dsp_data.output_frames;
  383. }
  384. }
  385. #endif
  386. src_data.data_out = audio_st->output_samples_buf;
  387. if (audio_st->flags & AUDIO_FLAG_CONTROL)
  388. {
  389. /* Readjust the audio input rate. */
  390. int half_size = (int)(audio_st->buffer_size / 2);
  391. int avail =
  392. (int)audio_st->current_audio->write_avail(
  393. audio_st->context_audio_data);
  394. int delta_mid = avail - half_size;
  395. double direction = (double)delta_mid / half_size;
  396. double adjust = 1.0 +
  397. audio_st->rate_control_delta * direction;
  398. unsigned write_idx =
  399. audio_st->free_samples_count++ &
  400. (AUDIO_BUFFER_FREE_SAMPLES_COUNT - 1);
  401. audio_st->free_samples_buf[write_idx] = avail;
  402. audio_st->source_ratio_current =
  403. audio_st->source_ratio_original * adjust;
  404. #if 0
  405. if (verbosity_is_enabled())
  406. {
  407. RARCH_LOG_OUTPUT("[Audio]: Audio buffer is %u%% full\n",
  408. (unsigned)(100 - (avail * 100) /
  409. audio_st->buffer_size));
  410. RARCH_LOG_OUTPUT("[Audio]: New rate: %lf, Orig rate: %lf\n",
  411. audio_st->source_ratio_current,
  412. audio_st->source_ratio_original);
  413. }
  414. #endif
  415. }
  416. src_data.ratio = audio_st->source_ratio_current;
  417. if (is_slowmotion)
  418. src_data.ratio *= slowmotion_ratio;
  419. if (is_fastforward && config_get_ptr()->bools.audio_fastforward_speedup) {
  420. const retro_time_t flush_time = cpu_features_get_time_usec();
  421. if (audio_st->last_flush_time > 0) {
  422. /* What we should see if the speed was 1.0x, converted to microsecs */
  423. const double expected_flush_delta =
  424. (src_data.input_frames / audio_st->input * 1000000);
  425. /* Exponential moving average of the last AUDIO_FF_EXP_AVG_SAMPLES
  426. samples. This helps make sure pitches are recognizable by avoiding
  427. too much variance flush-to-flush.
  428. It's not needed to avoid crackling (the generated waves are going to
  429. be continuous either way), but it's important to avoid time
  430. compression and decompression every single frame, which would make
  431. sounds irrecognizable.
  432. https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average */
  433. const retro_time_t n = AUDIO_FF_EXP_AVG_SAMPLES;
  434. audio_st->avg_flush_delta = audio_st->avg_flush_delta * (n - 1) / n +
  435. (flush_time - audio_st->last_flush_time) / n;
  436. /* How much does the avg_flush_delta deviate from the delta at 1.0x speed? */
  437. src_data.ratio *=
  438. MAX(AUDIO_MIN_RATIO,
  439. MIN(AUDIO_MAX_RATIO,
  440. audio_st->avg_flush_delta / expected_flush_delta));
  441. }
  442. audio_st->last_flush_time = flush_time;
  443. }
  444. audio_st->resampler->process(
  445. audio_st->resampler_data, &src_data);
  446. #ifdef HAVE_AUDIOMIXER
  447. if (audio_st->flags & AUDIO_FLAG_MIXER_ACTIVE)
  448. {
  449. bool override = true;
  450. float mixer_gain = 0.0f;
  451. bool audio_driver_mixer_mute_enable = audio_st->mixer_mute_enable;
  452. if (!audio_driver_mixer_mute_enable)
  453. {
  454. if (audio_st->mixer_volume_gain == 1.0f)
  455. override = false;
  456. mixer_gain = audio_st->mixer_volume_gain;
  457. }
  458. audio_mixer_mix(audio_st->output_samples_buf,
  459. src_data.output_frames, mixer_gain, override);
  460. }
  461. #endif
  462. {
  463. const void *output_data = audio_st->output_samples_buf;
  464. unsigned output_frames = (unsigned)src_data.output_frames;
  465. if (audio_st->flags & AUDIO_FLAG_USE_FLOAT)
  466. output_frames *= sizeof(float);
  467. else
  468. {
  469. convert_float_to_s16(audio_st->output_samples_conv_buf,
  470. (const float*)output_data, output_frames * 2);
  471. output_data = audio_st->output_samples_conv_buf;
  472. output_frames *= sizeof(int16_t);
  473. }
  474. audio_st->current_audio->write(audio_st->context_audio_data,
  475. output_data, output_frames * 2);
  476. }
  477. }
  478. #ifdef HAVE_AUDIOMIXER
  479. audio_mixer_stream_t *audio_driver_mixer_get_stream(unsigned i)
  480. {
  481. if (i > (AUDIO_MIXER_MAX_SYSTEM_STREAMS-1))
  482. return NULL;
  483. return &audio_driver_st.mixer_streams[i];
  484. }
  485. const char *audio_driver_mixer_get_stream_name(unsigned i)
  486. {
  487. if (i > (AUDIO_MIXER_MAX_SYSTEM_STREAMS-1))
  488. return msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NOT_AVAILABLE);
  489. if (!string_is_empty(audio_driver_st.mixer_streams[i].name))
  490. return audio_driver_st.mixer_streams[i].name;
  491. return msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NOT_AVAILABLE);
  492. }
  493. #endif
  494. bool audio_driver_init_internal(
  495. void *settings_data,
  496. bool audio_cb_inited)
  497. {
  498. unsigned new_rate = 0;
  499. float *samples_buf = NULL;
  500. settings_t *settings = (settings_t*)settings_data;
  501. size_t max_bufsamples = AUDIO_CHUNK_SIZE_NONBLOCKING * 2;
  502. bool audio_enable = settings->bools.audio_enable;
  503. bool audio_sync = settings->bools.audio_sync;
  504. bool audio_rate_control = settings->bools.audio_rate_control;
  505. float slowmotion_ratio = settings->floats.slowmotion_ratio;
  506. unsigned setting_audio_latency = settings->uints.audio_latency;
  507. unsigned runloop_audio_latency = runloop_state_get_ptr()->audio_latency;
  508. unsigned audio_latency = (runloop_audio_latency > setting_audio_latency) ?
  509. runloop_audio_latency : setting_audio_latency;
  510. #ifdef HAVE_REWIND
  511. int16_t *rewind_buf = NULL;
  512. #endif
  513. /* Accomodate rewind since at some point we might have two full buffers. */
  514. size_t outsamples_max = AUDIO_CHUNK_SIZE_NONBLOCKING * 2 * AUDIO_MAX_RATIO * slowmotion_ratio;
  515. int16_t *conv_buf = (int16_t*)memalign_alloc(64, outsamples_max * sizeof(int16_t));
  516. float *audio_buf = (float*)memalign_alloc(64, AUDIO_CHUNK_SIZE_NONBLOCKING * 2 * sizeof(float));
  517. bool verbosity_enabled = verbosity_is_enabled();
  518. convert_s16_to_float_init_simd();
  519. convert_float_to_s16_init_simd();
  520. if (!conv_buf || !audio_buf)
  521. goto error;
  522. memset(audio_buf, 0, AUDIO_CHUNK_SIZE_NONBLOCKING * 2 * sizeof(float));
  523. audio_driver_st.input_data = audio_buf;
  524. audio_driver_st.output_samples_conv_buf = conv_buf;
  525. audio_driver_st.chunk_block_size = AUDIO_CHUNK_SIZE_BLOCKING;
  526. audio_driver_st.chunk_nonblock_size = AUDIO_CHUNK_SIZE_NONBLOCKING;
  527. audio_driver_st.chunk_size = audio_driver_st.chunk_block_size;
  528. #ifdef HAVE_REWIND
  529. /* Needs to be able to hold full content of a full max_bufsamples
  530. * in addition to its own. */
  531. if (!(rewind_buf = (int16_t*)memalign_alloc(64, max_bufsamples * sizeof(int16_t))))
  532. goto error;
  533. audio_driver_st.rewind_buf = rewind_buf;
  534. audio_driver_st.rewind_size = max_bufsamples;
  535. #endif
  536. if (!audio_enable)
  537. {
  538. audio_driver_st.flags &= ~AUDIO_FLAG_ACTIVE;
  539. return false;
  540. }
  541. else
  542. audio_driver_st.flags |= AUDIO_FLAG_ACTIVE;
  543. if (!(audio_driver_find_driver(settings,
  544. "audio driver", verbosity_enabled)))
  545. {
  546. RARCH_ERR("Failed to initialize audio driver.\n");
  547. return false;
  548. }
  549. if (!audio_driver_st.current_audio || !audio_driver_st.current_audio->init)
  550. {
  551. RARCH_ERR("Failed to initialize audio driver. Will continue without audio.\n");
  552. audio_driver_st.flags &= ~AUDIO_FLAG_ACTIVE;
  553. return false;
  554. }
  555. #ifdef HAVE_THREADS
  556. if (audio_cb_inited)
  557. {
  558. RARCH_LOG("[Audio]: Starting threaded audio driver ...\n");
  559. if (!audio_init_thread(
  560. &audio_driver_st.current_audio,
  561. &audio_driver_st.context_audio_data,
  562. *settings->arrays.audio_device
  563. ? settings->arrays.audio_device : NULL,
  564. settings->uints.audio_output_sample_rate, &new_rate,
  565. audio_latency,
  566. settings->uints.audio_block_frames,
  567. audio_driver_st.current_audio))
  568. {
  569. RARCH_ERR("Cannot open threaded audio driver ... Exiting ...\n");
  570. return false;
  571. }
  572. }
  573. else
  574. #endif
  575. {
  576. audio_driver_st.context_audio_data =
  577. audio_driver_st.current_audio->init(*settings->arrays.audio_device ?
  578. settings->arrays.audio_device : NULL,
  579. settings->uints.audio_output_sample_rate,
  580. audio_latency,
  581. settings->uints.audio_block_frames,
  582. &new_rate);
  583. }
  584. if (new_rate != 0)
  585. configuration_set_int(settings, settings->uints.audio_output_sample_rate, new_rate);
  586. if (!audio_driver_st.context_audio_data)
  587. {
  588. RARCH_ERR("Failed to initialize audio driver. Will continue without audio.\n");
  589. audio_driver_st.flags &= ~AUDIO_FLAG_ACTIVE;
  590. }
  591. audio_driver_st.flags &= ~AUDIO_FLAG_USE_FLOAT;
  592. if ( (audio_driver_st.flags & AUDIO_FLAG_ACTIVE)
  593. && audio_driver_st.current_audio->use_float(
  594. audio_driver_st.context_audio_data))
  595. audio_driver_st.flags |= AUDIO_FLAG_USE_FLOAT;
  596. if ( !audio_sync
  597. && (audio_driver_st.flags & AUDIO_FLAG_ACTIVE))
  598. {
  599. if ( (audio_driver_st.flags & AUDIO_FLAG_ACTIVE)
  600. && audio_driver_st.context_audio_data)
  601. audio_driver_st.current_audio->set_nonblock_state(
  602. audio_driver_st.context_audio_data, true);
  603. audio_driver_st.chunk_size =
  604. audio_driver_st.chunk_nonblock_size;
  605. }
  606. if (audio_driver_st.input <= 0.0f)
  607. {
  608. /* Should never happen. */
  609. RARCH_WARN("[Audio]: Input rate is invalid (%.3f Hz)."
  610. " Using output rate (%u Hz).\n",
  611. audio_driver_st.input, settings->uints.audio_output_sample_rate);
  612. audio_driver_st.input = settings->uints.audio_output_sample_rate;
  613. }
  614. audio_driver_st.source_ratio_original =
  615. audio_driver_st.source_ratio_current =
  616. (double)settings->uints.audio_output_sample_rate / audio_driver_st.input;
  617. if (!string_is_empty(settings->arrays.audio_resampler))
  618. strlcpy(audio_driver_st.resampler_ident,
  619. settings->arrays.audio_resampler,
  620. sizeof(audio_driver_st.resampler_ident));
  621. else
  622. audio_driver_st.resampler_ident[0] = '\0';
  623. audio_driver_st.resampler_quality =
  624. audio_driver_get_resampler_quality(settings);
  625. if (!retro_resampler_realloc(
  626. &audio_driver_st.resampler_data,
  627. &audio_driver_st.resampler,
  628. audio_driver_st.resampler_ident,
  629. audio_driver_st.resampler_quality,
  630. audio_driver_st.source_ratio_original))
  631. {
  632. RARCH_ERR("Failed to initialize resampler \"%s\".\n",
  633. audio_driver_st.resampler_ident);
  634. audio_driver_st.flags &= ~AUDIO_FLAG_ACTIVE;
  635. }
  636. audio_driver_st.data_ptr = 0;
  637. if (!(samples_buf = (float*)memalign_alloc(64, outsamples_max * sizeof(float))))
  638. goto error;
  639. audio_driver_st.output_samples_buf = (float*)samples_buf;
  640. audio_driver_st.flags &= ~AUDIO_FLAG_CONTROL;
  641. if (
  642. !audio_cb_inited
  643. && (audio_driver_st.flags & AUDIO_FLAG_ACTIVE)
  644. && (audio_rate_control)
  645. )
  646. {
  647. /* Audio rate control requires write_avail
  648. * and buffer_size to be implemented. */
  649. if (audio_driver_st.current_audio->buffer_size)
  650. {
  651. audio_driver_st.buffer_size =
  652. audio_driver_st.current_audio->buffer_size(
  653. audio_driver_st.context_audio_data);
  654. audio_driver_st.flags |= AUDIO_FLAG_CONTROL;
  655. }
  656. else
  657. RARCH_WARN("[Audio]: Rate control was desired, but driver does not support needed features.\n");
  658. }
  659. command_event(CMD_EVENT_DSP_FILTER_INIT, NULL);
  660. audio_driver_st.free_samples_count = 0;
  661. #ifdef HAVE_AUDIOMIXER
  662. audio_mixer_init(settings->uints.audio_output_sample_rate);
  663. #endif
  664. /* Threaded driver is initially stopped. */
  665. if (
  666. (audio_driver_st.flags & AUDIO_FLAG_ACTIVE)
  667. && audio_cb_inited
  668. )
  669. audio_driver_start(false);
  670. return true;
  671. error:
  672. return audio_driver_deinit();
  673. }
  674. void audio_driver_sample(int16_t left, int16_t right)
  675. {
  676. uint32_t runloop_flags;
  677. audio_driver_state_t *audio_st = &audio_driver_st;
  678. recording_state_t *recording_st = NULL;
  679. if (audio_st->flags & AUDIO_FLAG_SUSPENDED)
  680. return;
  681. audio_st->output_samples_conv_buf[audio_st->data_ptr++] = left;
  682. audio_st->output_samples_conv_buf[audio_st->data_ptr++] = right;
  683. if (audio_st->data_ptr < audio_st->chunk_size)
  684. return;
  685. runloop_flags = runloop_get_flags();
  686. recording_st = recording_state_get_ptr();
  687. if ( recording_st->data &&
  688. recording_st->driver &&
  689. recording_st->driver->push_audio)
  690. {
  691. struct record_audio_data ffemu_data;
  692. ffemu_data.data = audio_st->output_samples_conv_buf;
  693. ffemu_data.frames = audio_st->data_ptr / 2;
  694. recording_st->driver->push_audio(recording_st->data, &ffemu_data);
  695. }
  696. if (!( (runloop_flags & RUNLOOP_FLAG_PAUSED)
  697. || !(audio_st->flags & AUDIO_FLAG_ACTIVE)
  698. || !(audio_st->output_samples_buf)))
  699. audio_driver_flush(audio_st,
  700. config_get_ptr()->floats.slowmotion_ratio,
  701. config_get_ptr()->bools.audio_fastforward_mute,
  702. audio_st->output_samples_conv_buf,
  703. audio_st->data_ptr,
  704. runloop_flags & RUNLOOP_FLAG_SLOWMOTION,
  705. runloop_flags & RUNLOOP_FLAG_FASTMOTION);
  706. audio_st->data_ptr = 0;
  707. }
  708. size_t audio_driver_sample_batch(const int16_t *data, size_t frames)
  709. {
  710. uint32_t runloop_flags;
  711. size_t frames_remaining = frames;
  712. recording_state_t *record_st = recording_state_get_ptr();
  713. audio_driver_state_t *audio_st = &audio_driver_st;
  714. if ((audio_st->flags & AUDIO_FLAG_SUSPENDED) || (frames < 1))
  715. return frames;
  716. runloop_flags = runloop_get_flags();
  717. /* We want to run this loop at least once, so use a
  718. * do...while (do...while has only a single conditional
  719. * jump, as opposed to for and while which have a
  720. * conditional jump and an unconditional jump). Note,
  721. * however, that this is only relevant for compilers
  722. * that are poor at optimisation... */
  723. do
  724. {
  725. size_t frames_to_write =
  726. (frames_remaining > (AUDIO_CHUNK_SIZE_NONBLOCKING >> 1)) ?
  727. (AUDIO_CHUNK_SIZE_NONBLOCKING >> 1) : frames_remaining;
  728. if ( record_st->data
  729. && record_st->driver
  730. && record_st->driver->push_audio)
  731. {
  732. struct record_audio_data ffemu_data;
  733. ffemu_data.data = data;
  734. ffemu_data.frames = frames_to_write;
  735. record_st->driver->push_audio(record_st->data, &ffemu_data);
  736. }
  737. if (!( (runloop_flags & RUNLOOP_FLAG_PAUSED)
  738. || !(audio_st->flags & AUDIO_FLAG_ACTIVE)
  739. || !(audio_st->output_samples_buf)))
  740. audio_driver_flush(audio_st,
  741. config_get_ptr()->floats.slowmotion_ratio,
  742. config_get_ptr()->bools.audio_fastforward_mute,
  743. data,
  744. frames_to_write << 1,
  745. runloop_flags & RUNLOOP_FLAG_SLOWMOTION,
  746. runloop_flags & RUNLOOP_FLAG_FASTMOTION);
  747. frames_remaining -= frames_to_write;
  748. data += frames_to_write << 1;
  749. }
  750. while (frames_remaining > 0);
  751. return frames;
  752. }
  753. #ifdef HAVE_REWIND
  754. void audio_driver_sample_rewind(int16_t left, int16_t right)
  755. {
  756. audio_driver_state_t *audio_st = &audio_driver_st;
  757. if (audio_st->rewind_ptr == 0)
  758. return;
  759. audio_st->rewind_buf[--audio_st->rewind_ptr] = right;
  760. audio_st->rewind_buf[--audio_st->rewind_ptr] = left;
  761. }
  762. size_t audio_driver_sample_batch_rewind(
  763. const int16_t *data, size_t frames)
  764. {
  765. size_t i;
  766. audio_driver_state_t *audio_st = &audio_driver_st;
  767. size_t samples = frames << 1;
  768. for (i = 0; i < samples; i++)
  769. {
  770. if (audio_st->rewind_ptr < 1)
  771. break;
  772. audio_st->rewind_buf[--audio_st->rewind_ptr] = data[i];
  773. }
  774. return frames;
  775. }
  776. #endif
  777. #ifdef HAVE_DSP_FILTER
  778. void audio_driver_dsp_filter_free(void)
  779. {
  780. audio_driver_state_t *audio_st = &audio_driver_st;
  781. if (audio_st->dsp)
  782. retro_dsp_filter_free(audio_st->dsp);
  783. audio_st->dsp = NULL;
  784. }
  785. bool audio_driver_dsp_filter_init(const char *device)
  786. {
  787. retro_dsp_filter_t *audio_driver_dsp = NULL;
  788. struct string_list *plugs = NULL;
  789. #if defined(HAVE_DYLIB) && !defined(HAVE_FILTERS_BUILTIN)
  790. char ext_name[32];
  791. char basedir[256];
  792. ext_name[0] = '\0';
  793. fill_pathname_basedir(basedir, device, sizeof(basedir));
  794. if (!frontend_driver_get_core_extension(ext_name, sizeof(ext_name)))
  795. return false;
  796. if (!(plugs = dir_list_new(basedir, ext_name, false, true, false, false)))
  797. return false;
  798. #endif
  799. audio_driver_dsp = retro_dsp_filter_new(
  800. device, plugs, audio_driver_st.input);
  801. if (!audio_driver_dsp)
  802. return false;
  803. audio_driver_st.dsp = audio_driver_dsp;
  804. return true;
  805. }
  806. #endif
  807. void audio_driver_set_buffer_size(size_t bufsize)
  808. {
  809. audio_driver_st.buffer_size = bufsize;
  810. }
  811. float audio_driver_monitor_adjust_system_rates(
  812. double input_sample_rate,
  813. double input_fps,
  814. float video_refresh_rate,
  815. unsigned video_swap_interval,
  816. float audio_max_timing_skew)
  817. {
  818. float inp_sample_rate = input_sample_rate;
  819. float target_video_sync_rate = video_refresh_rate
  820. / (float)video_swap_interval;
  821. float timing_skew =
  822. fabs(1.0f - input_fps / target_video_sync_rate);
  823. if (timing_skew <= audio_max_timing_skew)
  824. return (inp_sample_rate * target_video_sync_rate / input_fps);
  825. return inp_sample_rate;
  826. }
  827. #ifdef HAVE_REWIND
  828. void audio_driver_setup_rewind(void)
  829. {
  830. unsigned i;
  831. audio_driver_state_t *audio_st = &audio_driver_st;
  832. /* Push audio ready to be played. */
  833. audio_st->rewind_ptr = audio_st->rewind_size;
  834. for (i = 0; i < audio_st->data_ptr; i += 2)
  835. {
  836. if (audio_st->rewind_ptr > 0)
  837. audio_st->rewind_buf[--audio_st->rewind_ptr] =
  838. audio_st->output_samples_conv_buf[i + 1];
  839. if (audio_st->rewind_ptr > 0)
  840. audio_st->rewind_buf[--audio_st->rewind_ptr] =
  841. audio_st->output_samples_conv_buf[i + 0];
  842. }
  843. audio_st->data_ptr = 0;
  844. }
  845. #endif
  846. bool audio_driver_get_devices_list(void **data)
  847. {
  848. struct string_list**ptr = (struct string_list**)data;
  849. if (!ptr)
  850. return false;
  851. *ptr = audio_driver_st.devices_list;
  852. return true;
  853. }
  854. #ifdef HAVE_AUDIOMIXER
  855. bool audio_driver_mixer_extension_supported(const char *ext)
  856. {
  857. unsigned i;
  858. struct string_list str_list;
  859. union string_list_elem_attr attr;
  860. bool ret = false;
  861. attr.i = 0;
  862. if (!string_list_initialize(&str_list))
  863. return false;
  864. #ifdef HAVE_STB_VORBIS
  865. string_list_append(&str_list, "ogg", attr);
  866. #endif
  867. #ifdef HAVE_IBXM
  868. string_list_append(&str_list, "mod", attr);
  869. string_list_append(&str_list, "s3m", attr);
  870. string_list_append(&str_list, "xm", attr);
  871. #endif
  872. #ifdef HAVE_DR_FLAC
  873. string_list_append(&str_list, "flac", attr);
  874. #endif
  875. #ifdef HAVE_DR_MP3
  876. string_list_append(&str_list, "mp3", attr);
  877. #endif
  878. string_list_append(&str_list, "wav", attr);
  879. for (i = 0; i < str_list.size; i++)
  880. {
  881. const char *str_ext = str_list.elems[i].data;
  882. if (string_is_equal_noncase(str_ext, ext))
  883. {
  884. ret = true;
  885. break;
  886. }
  887. }
  888. string_list_deinitialize(&str_list);
  889. return ret;
  890. }
  891. static int audio_mixer_find_index(
  892. audio_mixer_sound_t *sound)
  893. {
  894. unsigned i;
  895. for (i = 0; i < AUDIO_MIXER_MAX_SYSTEM_STREAMS; i++)
  896. {
  897. audio_mixer_sound_t *handle = audio_driver_st.mixer_streams[i].handle;
  898. if (handle == sound)
  899. return i;
  900. }
  901. return -1;
  902. }
  903. static void audio_mixer_play_stop_cb(
  904. audio_mixer_sound_t *sound, unsigned reason)
  905. {
  906. int idx = audio_mixer_find_index(sound);
  907. switch (reason)
  908. {
  909. case AUDIO_MIXER_SOUND_FINISHED:
  910. audio_mixer_destroy(sound);
  911. if (idx >= 0)
  912. {
  913. unsigned i = (unsigned)idx;
  914. if (!string_is_empty(audio_driver_st.mixer_streams[i].name))
  915. free(audio_driver_st.mixer_streams[i].name);
  916. audio_driver_st.mixer_streams[i].name = NULL;
  917. audio_driver_st.mixer_streams[i].state = AUDIO_STREAM_STATE_NONE;
  918. audio_driver_st.mixer_streams[i].volume = 0.0f;
  919. audio_driver_st.mixer_streams[i].buf = NULL;
  920. audio_driver_st.mixer_streams[i].stop_cb = NULL;
  921. audio_driver_st.mixer_streams[i].handle = NULL;
  922. audio_driver_st.mixer_streams[i].voice = NULL;
  923. }
  924. break;
  925. case AUDIO_MIXER_SOUND_STOPPED:
  926. break;
  927. case AUDIO_MIXER_SOUND_REPEATED:
  928. break;
  929. }
  930. }
  931. static void audio_mixer_menu_stop_cb(
  932. audio_mixer_sound_t *sound, unsigned reason)
  933. {
  934. int idx = audio_mixer_find_index(sound);
  935. switch (reason)
  936. {
  937. case AUDIO_MIXER_SOUND_FINISHED:
  938. if (idx >= 0)
  939. {
  940. unsigned i = (unsigned)idx;
  941. audio_driver_st.mixer_streams[i].state = AUDIO_STREAM_STATE_STOPPED;
  942. audio_driver_st.mixer_streams[i].volume = 0.0f;
  943. }
  944. break;
  945. case AUDIO_MIXER_SOUND_STOPPED:
  946. case AUDIO_MIXER_SOUND_REPEATED:
  947. break;
  948. }
  949. }
  950. static void audio_mixer_play_stop_sequential_cb(
  951. audio_mixer_sound_t *sound, unsigned reason)
  952. {
  953. int idx = audio_mixer_find_index(sound);
  954. switch (reason)
  955. {
  956. case AUDIO_MIXER_SOUND_FINISHED:
  957. audio_mixer_destroy(sound);
  958. if (idx >= 0)
  959. {
  960. unsigned i = (unsigned)idx;
  961. if (!string_is_empty(audio_driver_st.mixer_streams[i].name))
  962. free(audio_driver_st.mixer_streams[i].name);
  963. if (i < AUDIO_MIXER_MAX_STREAMS)
  964. audio_driver_st.mixer_streams[i].stream_type = AUDIO_STREAM_TYPE_USER;
  965. else
  966. audio_driver_st.mixer_streams[i].stream_type = AUDIO_STREAM_TYPE_SYSTEM;
  967. audio_driver_st.mixer_streams[i].name = NULL;
  968. audio_driver_st.mixer_streams[i].state = AUDIO_STREAM_STATE_NONE;
  969. audio_driver_st.mixer_streams[i].volume = 0.0f;
  970. audio_driver_st.mixer_streams[i].buf = NULL;
  971. audio_driver_st.mixer_streams[i].stop_cb = NULL;
  972. audio_driver_st.mixer_streams[i].handle = NULL;
  973. audio_driver_st.mixer_streams[i].voice = NULL;
  974. i++;
  975. for (; i < AUDIO_MIXER_MAX_SYSTEM_STREAMS; i++)
  976. {
  977. if (audio_driver_st.mixer_streams[i].state
  978. == AUDIO_STREAM_STATE_STOPPED)
  979. {
  980. audio_driver_mixer_play_stream_sequential(i);
  981. break;
  982. }
  983. }
  984. }
  985. break;
  986. case AUDIO_MIXER_SOUND_STOPPED:
  987. case AUDIO_MIXER_SOUND_REPEATED:
  988. break;
  989. }
  990. }
  991. static bool audio_driver_mixer_get_free_stream_slot(
  992. unsigned *id, enum audio_mixer_stream_type type)
  993. {
  994. unsigned i = AUDIO_MIXER_MAX_STREAMS;
  995. unsigned count = AUDIO_MIXER_MAX_SYSTEM_STREAMS;
  996. if (type == AUDIO_STREAM_TYPE_USER)
  997. {
  998. i = 0;
  999. count = AUDIO_MIXER_MAX_STREAMS;
  1000. }
  1001. for (; i < count; i++)
  1002. {
  1003. if (audio_driver_st.mixer_streams[i].state == AUDIO_STREAM_STATE_NONE)
  1004. {
  1005. *id = i;
  1006. return true;
  1007. }
  1008. }
  1009. return false;
  1010. }
  1011. bool audio_driver_mixer_add_stream(audio_mixer_stream_params_t *params)
  1012. {
  1013. unsigned free_slot = 0;
  1014. audio_mixer_voice_t *voice = NULL;
  1015. audio_mixer_sound_t *handle = NULL;
  1016. audio_mixer_stop_cb_t stop_cb = audio_mixer_play_stop_cb;
  1017. bool looped = (params->state == AUDIO_STREAM_STATE_PLAYING_LOOPED);
  1018. void *buf = NULL;
  1019. if (params->stream_type == AUDIO_STREAM_TYPE_NONE)
  1020. return false;
  1021. switch (params->slot_selection_type)
  1022. {
  1023. case AUDIO_MIXER_SLOT_SELECTION_MANUAL:
  1024. free_slot = params->slot_selection_idx;
  1025. /* If we are using a manually specified
  1026. * slot, must free any existing stream
  1027. * before assigning the new one */
  1028. audio_driver_mixer_stop_stream(free_slot);
  1029. audio_driver_mixer_remove_stream(free_slot);
  1030. break;
  1031. case AUDIO_MIXER_SLOT_SELECTION_AUTOMATIC:
  1032. default:
  1033. if (!audio_driver_mixer_get_free_stream_slot(
  1034. &free_slot, params->stream_type))
  1035. return false;
  1036. break;
  1037. }
  1038. if (params->state == AUDIO_STREAM_STATE_NONE)
  1039. return false;
  1040. if (!(buf = malloc(params->bufsize)))
  1041. return false;
  1042. memcpy(buf, params->buf, params->bufsize);
  1043. switch (params->type)
  1044. {
  1045. case AUDIO_MIXER_TYPE_WAV:
  1046. handle = audio_mixer_load_wav(buf, (int32_t)params->bufsize,
  1047. audio_driver_st.resampler_ident,
  1048. audio_driver_st.resampler_quality);
  1049. /* WAV is a special case - input buffer is not
  1050. * free()'d when sound playback is complete (it is
  1051. * converted to a PCM buffer, which is free()'d instead),
  1052. * so have to do it here */
  1053. free(buf);
  1054. buf = NULL;
  1055. break;
  1056. case AUDIO_MIXER_TYPE_OGG:
  1057. handle = audio_mixer_load_ogg(buf, (int32_t)params->bufsize);
  1058. break;
  1059. case AUDIO_MIXER_TYPE_MOD:
  1060. handle = audio_mixer_load_mod(buf, (int32_t)params->bufsize);
  1061. break;
  1062. case AUDIO_MIXER_TYPE_FLAC:
  1063. #ifdef HAVE_DR_FLAC
  1064. handle = audio_mixer_load_flac(buf, (int32_t)params->bufsize);
  1065. #endif
  1066. break;
  1067. case AUDIO_MIXER_TYPE_MP3:
  1068. #ifdef HAVE_DR_MP3
  1069. handle = audio_mixer_load_mp3(buf, (int32_t)params->bufsize);
  1070. #endif
  1071. break;
  1072. case AUDIO_MIXER_TYPE_NONE:
  1073. break;
  1074. }
  1075. if (!handle)
  1076. {
  1077. free(buf);
  1078. return false;
  1079. }
  1080. switch (params->state)
  1081. {
  1082. case AUDIO_STREAM_STATE_PLAYING_SEQUENTIAL:
  1083. stop_cb = audio_mixer_play_stop_sequential_cb;
  1084. /* fall-through */
  1085. case AUDIO_STREAM_STATE_PLAYING_LOOPED:
  1086. case AUDIO_STREAM_STATE_PLAYING:
  1087. voice = audio_mixer_play(handle, looped, params->volume,
  1088. audio_driver_st.resampler_ident,
  1089. audio_driver_st.resampler_quality, stop_cb);
  1090. break;
  1091. default:
  1092. break;
  1093. }
  1094. audio_driver_st.flags |= AUDIO_FLAG_MIXER_ACTIVE;
  1095. audio_driver_st.mixer_streams[free_slot].name =
  1096. !string_is_empty(params->basename) ? strdup(params->basename) : NULL;
  1097. audio_driver_st.mixer_streams[free_slot].buf = buf;
  1098. audio_driver_st.mixer_streams[free_slot].handle = handle;
  1099. audio_driver_st.mixer_streams[free_slot].voice = voice;
  1100. audio_driver_st.mixer_streams[free_slot].stream_type = params->stream_type;
  1101. audio_driver_st.mixer_streams[free_slot].type = params->type;
  1102. audio_driver_st.mixer_streams[free_slot].state = params->state;
  1103. audio_driver_st.mixer_streams[free_slot].volume = params->volume;
  1104. audio_driver_st.mixer_streams[free_slot].stop_cb = stop_cb;
  1105. return true;
  1106. }
  1107. enum audio_mixer_state audio_driver_mixer_get_stream_state(unsigned i)
  1108. {
  1109. if (i >= AUDIO_MIXER_MAX_SYSTEM_STREAMS)
  1110. return AUDIO_STREAM_STATE_NONE;
  1111. return audio_driver_st.mixer_streams[i].state;
  1112. }
  1113. static void audio_driver_mixer_play_stream_internal(
  1114. unsigned i, unsigned type)
  1115. {
  1116. if (i >= AUDIO_MIXER_MAX_SYSTEM_STREAMS)
  1117. return;
  1118. switch (audio_driver_st.mixer_streams[i].state)
  1119. {
  1120. case AUDIO_STREAM_STATE_STOPPED:
  1121. audio_driver_st.mixer_streams[i].voice =
  1122. audio_mixer_play(audio_driver_st.mixer_streams[i].handle,
  1123. (type == AUDIO_STREAM_STATE_PLAYING_LOOPED) ? true : false,
  1124. 1.0f, audio_driver_st.resampler_ident,
  1125. audio_driver_st.resampler_quality,
  1126. audio_driver_st.mixer_streams[i].stop_cb);
  1127. audio_driver_st.mixer_streams[i].state = (enum audio_mixer_state)type;
  1128. break;
  1129. case AUDIO_STREAM_STATE_PLAYING:
  1130. case AUDIO_STREAM_STATE_PLAYING_LOOPED:
  1131. case AUDIO_STREAM_STATE_PLAYING_SEQUENTIAL:
  1132. case AUDIO_STREAM_STATE_NONE:
  1133. break;
  1134. }
  1135. }
  1136. static void audio_driver_load_menu_bgm_callback(retro_task_t *task,
  1137. void *task_data, void *user_data, const char *error)
  1138. {
  1139. uint8_t flags = content_get_flags();
  1140. if (!(flags & CONTENT_ST_FLAG_IS_INITED))
  1141. audio_driver_mixer_play_menu_sound_looped(AUDIO_MIXER_SYSTEM_SLOT_BGM);
  1142. }
  1143. void audio_driver_load_system_sounds(void)
  1144. {
  1145. char basename_noext[256];
  1146. char sounds_path[PATH_MAX_LENGTH];
  1147. char sounds_fallback_path[PATH_MAX_LENGTH];
  1148. settings_t *settings = config_get_ptr();
  1149. const char *dir_assets = settings->paths.directory_assets;
  1150. const bool audio_enable_menu = settings->bools.audio_enable_menu;
  1151. const bool audio_enable_menu_ok = audio_enable_menu && settings->bools.audio_enable_menu_ok;
  1152. const bool audio_enable_menu_cancel = audio_enable_menu && settings->bools.audio_enable_menu_cancel;
  1153. const bool audio_enable_menu_notice = audio_enable_menu && settings->bools.audio_enable_menu_notice;
  1154. const bool audio_enable_menu_bgm = audio_enable_menu && settings->bools.audio_enable_menu_bgm;
  1155. const bool audio_enable_menu_scroll = audio_enable_menu && settings->bools.audio_enable_menu_scroll;
  1156. const bool audio_enable_cheevo_unlock = settings->bools.cheevos_unlock_sound_enable;
  1157. const char *path_ok = NULL;
  1158. const char *path_cancel = NULL;
  1159. const char *path_notice = NULL;
  1160. const char *path_notice_back = NULL;
  1161. const char *path_bgm = NULL;
  1162. const char *path_cheevo_unlock = NULL;
  1163. const char *path_up = NULL;
  1164. const char *path_down = NULL;
  1165. struct string_list *list = NULL;
  1166. struct string_list *list_fallback = NULL;
  1167. unsigned i = 0;
  1168. if (!audio_enable_menu && !audio_enable_cheevo_unlock)
  1169. goto end;
  1170. sounds_path[0] = basename_noext[0] ='\0';
  1171. fill_pathname_join_special(
  1172. sounds_fallback_path,
  1173. dir_assets,
  1174. "sounds",
  1175. sizeof(sounds_fallback_path));
  1176. fill_pathname_application_special(
  1177. sounds_path,
  1178. sizeof(sounds_path),
  1179. APPLICATION_SPECIAL_DIRECTORY_ASSETS_SOUNDS);
  1180. list = dir_list_new(sounds_path, MENU_SOUND_FORMATS, false, false, false, false);
  1181. list_fallback = dir_list_new(sounds_fallback_path, MENU_SOUND_FORMATS, false, false, false, false);
  1182. if (!list)
  1183. {
  1184. list = list_fallback;
  1185. list_fallback = NULL;
  1186. }
  1187. if (!list || list->size == 0)
  1188. goto end;
  1189. if (list_fallback && list_fallback->size > 0)
  1190. {
  1191. for (i = 0; i < list_fallback->size; i++)
  1192. {
  1193. if (list->size == 0 || !string_list_find_elem(list, list_fallback->elems[i].data))
  1194. {
  1195. union string_list_elem_attr attr = {0};
  1196. string_list_append(list, list_fallback->elems[i].data, attr);
  1197. }
  1198. }
  1199. }
  1200. for (i = 0; i < list->size; i++)
  1201. {
  1202. const char *path = list->elems[i].data;
  1203. const char *ext = path_get_extension(path);
  1204. if (audio_driver_mixer_extension_supported(ext))
  1205. {
  1206. basename_noext[0] = '\0';
  1207. fill_pathname_base(basename_noext, path, sizeof(basename_noext));
  1208. path_remove_extension(basename_noext);
  1209. if (string_is_equal_noncase(basename_noext, "ok"))
  1210. path_ok = path;
  1211. else if (string_is_equal_noncase(basename_noext, "cancel"))
  1212. path_cancel = path;
  1213. else if (string_is_equal_noncase(basename_noext, "notice"))
  1214. path_notice = path;
  1215. else if (string_is_equal_noncase(basename_noext, "notice_back"))
  1216. path_notice_back = path;
  1217. else if (string_is_equal_noncase(basename_noext, "bgm"))
  1218. path_bgm = path;
  1219. else if (string_is_equal_noncase(basename_noext, "unlock"))
  1220. path_cheevo_unlock = path;
  1221. else if (string_is_equal_noncase(basename_noext, "up"))
  1222. path_up = path;
  1223. else if (string_is_equal_noncase(basename_noext, "down"))
  1224. path_down = path;
  1225. }
  1226. }
  1227. if (path_ok && audio_enable_menu_ok)
  1228. task_push_audio_mixer_load(path_ok, NULL, NULL, true, AUDIO_MIXER_SLOT_SELECTION_MANUAL, AUDIO_MIXER_SYSTEM_SLOT_OK);
  1229. if (path_cancel && audio_enable_menu_cancel)
  1230. task_push_audio_mixer_load(path_cancel, NULL, NULL, true, AUDIO_MIXER_SLOT_SELECTION_MANUAL, AUDIO_MIXER_SYSTEM_SLOT_CANCEL);
  1231. if (audio_enable_menu_notice) {
  1232. if (path_notice)
  1233. task_push_audio_mixer_load(path_notice, NULL, NULL, true, AUDIO_MIXER_SLOT_SELECTION_MANUAL, AUDIO_MIXER_SYSTEM_SLOT_NOTICE);
  1234. if (path_notice_back)
  1235. task_push_audio_mixer_load(path_notice_back, NULL, NULL, true, AUDIO_MIXER_SLOT_SELECTION_MANUAL, AUDIO_MIXER_SYSTEM_SLOT_NOTICE_BACK);
  1236. }
  1237. if (path_bgm && audio_enable_menu_bgm)
  1238. task_push_audio_mixer_load(path_bgm, audio_driver_load_menu_bgm_callback, NULL, true, AUDIO_MIXER_SLOT_SELECTION_MANUAL, AUDIO_MIXER_SYSTEM_SLOT_BGM);
  1239. if (path_cheevo_unlock && audio_enable_cheevo_unlock)
  1240. task_push_audio_mixer_load(path_cheevo_unlock, NULL, NULL, true, AUDIO_MIXER_SLOT_SELECTION_MANUAL, AUDIO_MIXER_SYSTEM_SLOT_ACHIEVEMENT_UNLOCK);
  1241. if (audio_enable_menu_scroll)
  1242. {
  1243. if (path_up)
  1244. task_push_audio_mixer_load(path_up, NULL, NULL, true, AUDIO_MIXER_SLOT_SELECTION_MANUAL, AUDIO_MIXER_SYSTEM_SLOT_UP);
  1245. if (path_down)
  1246. task_push_audio_mixer_load(path_down, NULL, NULL, true, AUDIO_MIXER_SLOT_SELECTION_MANUAL, AUDIO_MIXER_SYSTEM_SLOT_DOWN);
  1247. }
  1248. end:
  1249. if (list)
  1250. string_list_free(list);
  1251. if (list_fallback)
  1252. string_list_free(list_fallback);
  1253. }
  1254. void audio_driver_mixer_play_stream(unsigned i)
  1255. {
  1256. audio_driver_st.mixer_streams[i].stop_cb = audio_mixer_play_stop_cb;
  1257. audio_driver_mixer_play_stream_internal(i, AUDIO_STREAM_STATE_PLAYING);
  1258. }
  1259. void audio_driver_mixer_play_menu_sound_looped(unsigned i)
  1260. {
  1261. audio_driver_st.mixer_streams[i].stop_cb = audio_mixer_menu_stop_cb;
  1262. audio_driver_mixer_play_stream_internal(i, AUDIO_STREAM_STATE_PLAYING_LOOPED);
  1263. }
  1264. void audio_driver_mixer_play_menu_sound(unsigned i)
  1265. {
  1266. audio_driver_st.mixer_streams[i].stop_cb = audio_mixer_menu_stop_cb;
  1267. audio_driver_mixer_stop_stream(i);
  1268. audio_driver_mixer_play_stream_internal(i, AUDIO_STREAM_STATE_PLAYING);
  1269. }
  1270. void audio_driver_mixer_play_scroll_sound(bool direction_up)
  1271. {
  1272. settings_t *settings = config_get_ptr();
  1273. bool audio_enable_menu = settings->bools.audio_enable_menu;
  1274. bool audio_enable_menu_scroll = settings->bools.audio_enable_menu_scroll;
  1275. if (audio_enable_menu && audio_enable_menu_scroll)
  1276. audio_driver_mixer_play_menu_sound(direction_up ? AUDIO_MIXER_SYSTEM_SLOT_UP : AUDIO_MIXER_SYSTEM_SLOT_DOWN);
  1277. }
  1278. void audio_driver_mixer_play_stream_looped(unsigned i)
  1279. {
  1280. audio_driver_st.mixer_streams[i].stop_cb = audio_mixer_play_stop_cb;
  1281. audio_driver_mixer_play_stream_internal(i, AUDIO_STREAM_STATE_PLAYING_LOOPED);
  1282. }
  1283. void audio_driver_mixer_play_stream_sequential(unsigned i)
  1284. {
  1285. audio_driver_st.mixer_streams[i].stop_cb = audio_mixer_play_stop_sequential_cb;
  1286. audio_driver_mixer_play_stream_internal(i, AUDIO_STREAM_STATE_PLAYING_SEQUENTIAL);
  1287. }
  1288. float audio_driver_mixer_get_stream_volume(unsigned i)
  1289. {
  1290. if (i >= AUDIO_MIXER_MAX_SYSTEM_STREAMS)
  1291. return 0.0f;
  1292. return audio_driver_st.mixer_streams[i].volume;
  1293. }
  1294. void audio_driver_mixer_set_stream_volume(unsigned i, float vol)
  1295. {
  1296. audio_mixer_voice_t *voice = NULL;
  1297. if (i >= AUDIO_MIXER_MAX_SYSTEM_STREAMS)
  1298. return;
  1299. audio_driver_st.mixer_streams[i].volume = vol;
  1300. voice =
  1301. audio_driver_st.mixer_streams[i].voice;
  1302. if (voice)
  1303. audio_mixer_voice_set_volume(voice, DB_TO_GAIN(vol));
  1304. }
  1305. void audio_driver_mixer_stop_stream(unsigned i)
  1306. {
  1307. if (i >= AUDIO_MIXER_MAX_SYSTEM_STREAMS)
  1308. return;
  1309. switch (audio_driver_st.mixer_streams[i].state)
  1310. {
  1311. case AUDIO_STREAM_STATE_PLAYING:
  1312. case AUDIO_STREAM_STATE_PLAYING_LOOPED:
  1313. case AUDIO_STREAM_STATE_PLAYING_SEQUENTIAL:
  1314. {
  1315. audio_mixer_voice_t *voice = audio_driver_st.mixer_streams[i].voice;
  1316. if (voice)
  1317. audio_mixer_stop(voice);
  1318. audio_driver_st.mixer_streams[i].state = AUDIO_STREAM_STATE_STOPPED;
  1319. audio_driver_st.mixer_streams[i].volume = 1.0f;
  1320. }
  1321. break;
  1322. case AUDIO_STREAM_STATE_STOPPED:
  1323. case AUDIO_STREAM_STATE_NONE:
  1324. break;
  1325. }
  1326. }
  1327. void audio_driver_mixer_remove_stream(unsigned i)
  1328. {
  1329. if (i >= AUDIO_MIXER_MAX_SYSTEM_STREAMS)
  1330. return;
  1331. switch (audio_driver_st.mixer_streams[i].state)
  1332. {
  1333. case AUDIO_STREAM_STATE_PLAYING:
  1334. case AUDIO_STREAM_STATE_PLAYING_LOOPED:
  1335. case AUDIO_STREAM_STATE_PLAYING_SEQUENTIAL:
  1336. audio_driver_mixer_stop_stream(i);
  1337. /* fall-through */
  1338. case AUDIO_STREAM_STATE_STOPPED:
  1339. {
  1340. audio_mixer_sound_t *handle = audio_driver_st.mixer_streams[i].handle;
  1341. if (handle)
  1342. audio_mixer_destroy(handle);
  1343. if (!string_is_empty(audio_driver_st.mixer_streams[i].name))
  1344. free(audio_driver_st.mixer_streams[i].name);
  1345. audio_driver_st.mixer_streams[i].state = AUDIO_STREAM_STATE_NONE;
  1346. audio_driver_st.mixer_streams[i].stop_cb = NULL;
  1347. audio_driver_st.mixer_streams[i].volume = 0.0f;
  1348. audio_driver_st.mixer_streams[i].handle = NULL;
  1349. audio_driver_st.mixer_streams[i].voice = NULL;
  1350. audio_driver_st.mixer_streams[i].name = NULL;
  1351. }
  1352. break;
  1353. case AUDIO_STREAM_STATE_NONE:
  1354. break;
  1355. }
  1356. }
  1357. bool audio_driver_mixer_toggle_mute(void)
  1358. {
  1359. audio_driver_st.mixer_mute_enable =
  1360. !audio_driver_st.mixer_mute_enable;
  1361. return true;
  1362. }
  1363. #endif
  1364. bool audio_driver_enable_callback(void)
  1365. {
  1366. if (!audio_driver_st.callback.callback)
  1367. return false;
  1368. if (audio_driver_st.callback.set_state)
  1369. audio_driver_st.callback.set_state(true);
  1370. return true;
  1371. }
  1372. bool audio_driver_disable_callback(void)
  1373. {
  1374. if (!audio_driver_st.callback.callback)
  1375. return false;
  1376. if (audio_driver_st.callback.set_state)
  1377. audio_driver_st.callback.set_state(false);
  1378. return true;
  1379. }
  1380. bool audio_driver_callback(void)
  1381. {
  1382. settings_t *settings = config_get_ptr();
  1383. uint32_t runloop_flags = runloop_get_flags();
  1384. bool runloop_paused = runloop_flags & RUNLOOP_FLAG_PAUSED;
  1385. #ifdef HAVE_MENU
  1386. #ifdef HAVE_NETWORKING
  1387. bool core_paused = runloop_paused ||
  1388. ( settings->bools.menu_pause_libretro
  1389. && (menu_state_get_ptr()->flags & MENU_ST_FLAG_ALIVE)
  1390. && netplay_driver_ctl(RARCH_NETPLAY_CTL_ALLOW_PAUSE, NULL));
  1391. #else
  1392. bool core_paused = runloop_paused ||
  1393. (settings->bools.menu_pause_libretro
  1394. && (menu_state_get_ptr()->flags & MENU_ST_FLAG_ALIVE));
  1395. #endif
  1396. #else
  1397. bool core_paused = runloop_paused;
  1398. #endif
  1399. if (!audio_driver_st.callback.callback)
  1400. return false;
  1401. if (!core_paused && audio_driver_st.callback.callback)
  1402. audio_driver_st.callback.callback();
  1403. return true;
  1404. }
  1405. bool audio_driver_has_callback(void)
  1406. {
  1407. return audio_driver_st.callback.callback != NULL;
  1408. }
  1409. static INLINE bool audio_driver_alive(void)
  1410. {
  1411. audio_driver_state_t *audio_st = &audio_driver_st;
  1412. if ( audio_st->current_audio
  1413. && audio_st->current_audio->alive
  1414. && audio_st->context_audio_data)
  1415. return audio_st->current_audio->alive(audio_st->context_audio_data);
  1416. return false;
  1417. }
  1418. bool audio_driver_start(bool is_shutdown)
  1419. {
  1420. audio_driver_state_t *audio_st = &audio_driver_st;
  1421. if (
  1422. !audio_st->current_audio
  1423. || !audio_st->current_audio->start
  1424. || !audio_st->context_audio_data)
  1425. goto error;
  1426. if (!audio_st->current_audio->start(
  1427. audio_st->context_audio_data, is_shutdown))
  1428. goto error;
  1429. return true;
  1430. error:
  1431. RARCH_ERR("%s\n",
  1432. msg_hash_to_str(MSG_FAILED_TO_START_AUDIO_DRIVER));
  1433. audio_driver_st.flags &= ~AUDIO_FLAG_ACTIVE;
  1434. return false;
  1435. }
  1436. bool audio_driver_stop(void)
  1437. {
  1438. if ( !audio_driver_st.current_audio
  1439. || !audio_driver_st.current_audio->stop
  1440. || !audio_driver_st.context_audio_data
  1441. || !audio_driver_alive()
  1442. )
  1443. return false;
  1444. return audio_driver_st.current_audio->stop(
  1445. audio_driver_st.context_audio_data);
  1446. }
  1447. #ifdef HAVE_REWIND
  1448. void audio_driver_frame_is_reverse(void)
  1449. {
  1450. audio_driver_state_t *audio_st = &audio_driver_st;
  1451. recording_state_t *recording_st = recording_state_get_ptr();
  1452. uint32_t runloop_flags = runloop_get_flags();
  1453. /* We just rewound. Flush rewind audio buffer. */
  1454. if ( recording_st->data &&
  1455. recording_st->driver &&
  1456. recording_st->driver->push_audio)
  1457. {
  1458. struct record_audio_data ffemu_data;
  1459. ffemu_data.data = audio_st->rewind_buf +
  1460. audio_st->rewind_ptr;
  1461. ffemu_data.frames = (audio_st->rewind_size -
  1462. audio_st->rewind_ptr) / 2;
  1463. recording_st->driver->push_audio(
  1464. recording_st->data,
  1465. &ffemu_data);
  1466. }
  1467. if (!(
  1468. (runloop_flags & RUNLOOP_FLAG_PAUSED)
  1469. || !(audio_st->flags & AUDIO_FLAG_ACTIVE)
  1470. || !(audio_st->output_samples_buf)))
  1471. if (!(audio_st->flags & AUDIO_FLAG_SUSPENDED))
  1472. {
  1473. settings_t *settings = config_get_ptr();
  1474. audio_driver_flush(audio_st,
  1475. settings->floats.slowmotion_ratio,
  1476. settings->bools.audio_fastforward_mute,
  1477. audio_st->rewind_buf +
  1478. audio_st->rewind_ptr,
  1479. audio_st->rewind_size -
  1480. audio_st->rewind_ptr,
  1481. runloop_flags & RUNLOOP_FLAG_SLOWMOTION,
  1482. runloop_flags & RUNLOOP_FLAG_FASTMOTION);
  1483. }
  1484. }
  1485. #endif
  1486. void audio_set_float(enum audio_action action, float val)
  1487. {
  1488. switch (action)
  1489. {
  1490. case AUDIO_ACTION_VOLUME_GAIN:
  1491. audio_driver_st.volume_gain = DB_TO_GAIN(val);
  1492. break;
  1493. case AUDIO_ACTION_MIXER_VOLUME_GAIN:
  1494. #ifdef HAVE_AUDIOMIXER
  1495. audio_driver_st.mixer_volume_gain = DB_TO_GAIN(val);
  1496. #endif
  1497. break;
  1498. case AUDIO_ACTION_RATE_CONTROL_DELTA:
  1499. audio_driver_st.rate_control_delta = val;
  1500. break;
  1501. case AUDIO_ACTION_NONE:
  1502. default:
  1503. break;
  1504. }
  1505. }
  1506. float *audio_get_float_ptr(enum audio_action action)
  1507. {
  1508. switch (action)
  1509. {
  1510. case AUDIO_ACTION_RATE_CONTROL_DELTA:
  1511. return &audio_driver_st.rate_control_delta;
  1512. case AUDIO_ACTION_NONE:
  1513. default:
  1514. break;
  1515. }
  1516. return NULL;
  1517. }
  1518. bool *audio_get_bool_ptr(enum audio_action action)
  1519. {
  1520. switch (action)
  1521. {
  1522. case AUDIO_ACTION_MIXER_MUTE_ENABLE:
  1523. #ifdef HAVE_AUDIOMIXER
  1524. return &audio_driver_st.mixer_mute_enable;
  1525. #else
  1526. break;
  1527. #endif
  1528. case AUDIO_ACTION_MUTE_ENABLE:
  1529. return &audio_driver_st.mute_enable;
  1530. case AUDIO_ACTION_NONE:
  1531. default:
  1532. break;
  1533. }
  1534. return NULL;
  1535. }
  1536. bool audio_compute_buffer_statistics(audio_statistics_t *stats)
  1537. {
  1538. unsigned i, low_water_size, high_water_size, avg, stddev;
  1539. uint64_t accum = 0;
  1540. uint64_t accum_var = 0;
  1541. unsigned low_water_count = 0;
  1542. unsigned high_water_count = 0;
  1543. audio_driver_state_t *audio_st = &audio_driver_st;
  1544. unsigned samples = MIN(
  1545. (unsigned)audio_st->free_samples_count,
  1546. AUDIO_BUFFER_FREE_SAMPLES_COUNT);
  1547. if (samples < 3)
  1548. return false;
  1549. stats->samples = (unsigned)
  1550. audio_st->free_samples_count;
  1551. #ifdef WARPUP
  1552. /* uint64 to double not implemented, fair chance
  1553. * signed int64 to double doesn't exist either */
  1554. /* https://forums.libretro.com/t/unsupported-platform-help/13903/ */
  1555. (void)stddev;
  1556. #elif defined(_MSC_VER) && _MSC_VER <= 1200
  1557. /* FIXME: error C2520: conversion from unsigned __int64
  1558. * to double not implemented, use signed __int64 */
  1559. (void)stddev;
  1560. #else
  1561. for (i = 1; i < samples; i++)
  1562. accum += audio_st->free_samples_buf[i];
  1563. avg = (unsigned)accum / (samples - 1);
  1564. for (i = 1; i < samples; i++)
  1565. {
  1566. int diff = avg - audio_st->free_samples_buf[i];
  1567. accum_var += diff * diff;
  1568. }
  1569. stddev = (unsigned)
  1570. sqrt((double)accum_var / (samples - 2));
  1571. stats->average_buffer_saturation = (1.0f - (float)avg
  1572. / audio_st->buffer_size) * 100.0;
  1573. stats->std_deviation_percentage = ((float)stddev
  1574. / audio_st->buffer_size) * 100.0;
  1575. #endif
  1576. low_water_size = (unsigned)(audio_st->buffer_size * 3 / 4);
  1577. high_water_size = (unsigned)(audio_st->buffer_size / 4);
  1578. for (i = 1; i < samples; i++)
  1579. {
  1580. if (audio_st->free_samples_buf[i] >= low_water_size)
  1581. low_water_count++;
  1582. else if (audio_st->free_samples_buf[i] <= high_water_size)
  1583. high_water_count++;
  1584. }
  1585. stats->close_to_underrun = (100.0f * low_water_count) / (samples - 1);
  1586. stats->close_to_blocking = (100.0f * high_water_count) / (samples - 1);
  1587. return true;
  1588. }
  1589. #ifdef HAVE_MENU
  1590. void audio_driver_menu_sample(void)
  1591. {
  1592. static int16_t samples_buf[1024] = {0};
  1593. settings_t *settings = config_get_ptr();
  1594. video_driver_state_t *video_st = video_state_get_ptr();
  1595. uint32_t runloop_flags = runloop_get_flags();
  1596. recording_state_t *recording_st = recording_state_get_ptr();
  1597. struct retro_system_av_info *av_info = &video_st->av_info;
  1598. const struct retro_system_timing *info =
  1599. (const struct retro_system_timing*)&av_info->timing;
  1600. unsigned sample_count = (info->sample_rate / info->fps) * 2;
  1601. audio_driver_state_t *audio_st = &audio_driver_st;
  1602. bool check_flush = !(
  1603. (runloop_flags & RUNLOOP_FLAG_PAUSED)
  1604. || !(audio_st->flags & AUDIO_FLAG_ACTIVE)
  1605. || !audio_st->output_samples_buf);
  1606. if ((audio_st->flags & AUDIO_FLAG_SUSPENDED))
  1607. check_flush = false;
  1608. while (sample_count > 1024)
  1609. {
  1610. if ( recording_st->data &&
  1611. recording_st->driver &&
  1612. recording_st->driver->push_audio)
  1613. {
  1614. struct record_audio_data ffemu_data;
  1615. ffemu_data.data = samples_buf;
  1616. ffemu_data.frames = 1024 / 2;
  1617. recording_st->driver->push_audio(
  1618. recording_st->data, &ffemu_data);
  1619. }
  1620. if (check_flush)
  1621. audio_driver_flush(audio_st,
  1622. settings->floats.slowmotion_ratio,
  1623. settings->bools.audio_fastforward_mute,
  1624. samples_buf,
  1625. 1024,
  1626. runloop_flags & RUNLOOP_FLAG_SLOWMOTION,
  1627. runloop_flags & RUNLOOP_FLAG_FASTMOTION);
  1628. sample_count -= 1024;
  1629. }
  1630. if ( recording_st->data &&
  1631. recording_st->driver &&
  1632. recording_st->driver->push_audio)
  1633. {
  1634. struct record_audio_data ffemu_data;
  1635. ffemu_data.data = samples_buf;
  1636. ffemu_data.frames = sample_count / 2;
  1637. recording_st->driver->push_audio(
  1638. recording_st->data, &ffemu_data);
  1639. }
  1640. if (check_flush)
  1641. audio_driver_flush(audio_st,
  1642. settings->floats.slowmotion_ratio,
  1643. settings->bools.audio_fastforward_mute,
  1644. samples_buf,
  1645. sample_count,
  1646. runloop_flags & RUNLOOP_FLAG_SLOWMOTION,
  1647. runloop_flags & RUNLOOP_FLAG_FASTMOTION);
  1648. }
  1649. #endif