forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
audio_driver.c
1025 lines (858 loc) · 27.7 KB
/
audio_driver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2016 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <retro_assert.h>
#include <lists/string_list.h>
#include <conversion/float_to_s16.h>
#include <conversion/s16_to_float.h>
#include "audio_driver.h"
#include "audio_resampler_driver.h"
#include "../record/record_driver.h"
#include "audio_thread_wrapper.h"
#include "../command.h"
#include "../configuration.h"
#include "../retroarch.h"
#include "../runloop.h"
#include "../performance_counters.h"
#include "../verbosity.h"
#include "../list_special.h"
#ifndef AUDIO_BUFFER_FREE_SAMPLES_COUNT
#define AUDIO_BUFFER_FREE_SAMPLES_COUNT (8 * 1024)
#endif
struct audio_driver_input_data
{
float *data;
size_t data_ptr;
struct
{
size_t size;
size_t nonblock_size;
size_t block_size;
} chunk;
struct
{
float input;
bool control;
struct
{
double original;
double current;
} source_ratio;
} audio_rate;
bool use_float;
struct
{
int16_t *buf;
size_t ptr;
size_t size;
} rewind;
rarch_dsp_filter_t *dsp;
size_t driver_buffer_size;
float volume_gain;
struct
{
float *buf;
int16_t *conv_buf;
} output_samples;
struct
{
unsigned buf[AUDIO_BUFFER_FREE_SAMPLES_COUNT];
uint64_t count;
} free_samples;
};
static const audio_driver_t *audio_drivers[] = {
#ifdef HAVE_ALSA
&audio_alsa,
#if !defined(__QNX__) && defined(HAVE_THREADS)
&audio_alsathread,
#endif
#endif
#if defined(HAVE_OSS) || defined(HAVE_OSS_BSD)
&audio_oss,
#endif
#ifdef HAVE_RSOUND
&audio_rsound,
#endif
#ifdef HAVE_COREAUDIO
&audio_coreaudio,
#endif
#ifdef HAVE_AL
&audio_openal,
#endif
#ifdef HAVE_SL
&audio_opensl,
#endif
#ifdef HAVE_ROAR
&audio_roar,
#endif
#ifdef HAVE_JACK
&audio_jack,
#endif
#if defined(HAVE_SDL) || defined(HAVE_SDL2)
&audio_sdl,
#endif
#ifdef HAVE_XAUDIO
&audio_xa,
#endif
#ifdef HAVE_DSOUND
&audio_dsound,
#endif
#ifdef HAVE_PULSE
&audio_pulse,
#endif
#ifdef __CELLOS_LV2__
&audio_ps3,
#endif
#ifdef XENON
&audio_xenon360,
#endif
#ifdef GEKKO
&audio_gx,
#endif
#ifdef EMSCRIPTEN
&audio_rwebaudio,
#endif
#if defined(PSP) || defined(VITA)
&audio_psp,
#endif
#ifdef _3DS
&audio_ctr_csnd,
&audio_ctr_dsp,
#endif
&audio_null,
NULL,
};
static struct audio_driver_input_data audio_driver_data;
static struct retro_audio_callback audio_callback;
static struct string_list *audio_driver_devices_list = NULL;
static struct retro_perf_counter resampler_proc = {0};
static const rarch_resampler_t *audio_driver_resampler = NULL;
static void *audio_driver_resampler_data = NULL;
static bool audio_driver_active = false;
static bool audio_driver_data_own = false;
static const audio_driver_t *current_audio = NULL;
static void *audio_driver_context_audio_data = NULL;
/**
* compute_audio_buffer_statistics:
*
* Computes audio buffer statistics.
*
**/
static void compute_audio_buffer_statistics(void)
{
unsigned i, low_water_size, high_water_size, avg, stddev;
float avg_filled, deviation;
uint64_t accum = 0;
uint64_t accum_var = 0;
unsigned low_water_count = 0;
unsigned high_water_count = 0;
unsigned samples = MIN(
audio_driver_data.free_samples.count,
AUDIO_BUFFER_FREE_SAMPLES_COUNT);
if (samples < 3)
return;
for (i = 1; i < samples; i++)
accum += audio_driver_data.free_samples.buf[i];
avg = accum / (samples - 1);
for (i = 1; i < samples; i++)
{
int diff = avg - audio_driver_data.free_samples.buf[i];
accum_var += diff * diff;
}
stddev = (unsigned)sqrt((double)accum_var / (samples - 2));
avg_filled = 1.0f - (float)avg / audio_driver_data.driver_buffer_size;
deviation = (float)stddev / audio_driver_data.driver_buffer_size;
low_water_size = audio_driver_data.driver_buffer_size * 3 / 4;
high_water_size = audio_driver_data.driver_buffer_size / 4;
for (i = 1; i < samples; i++)
{
if (audio_driver_data.free_samples.buf[i] >= low_water_size)
low_water_count++;
else if (audio_driver_data.free_samples.buf[i] <= high_water_size)
high_water_count++;
}
RARCH_LOG("Average audio buffer saturation: %.2f %%, standard deviation (percentage points): %.2f %%.\n",
avg_filled * 100.0, deviation * 100.0);
RARCH_LOG("Amount of time spent close to underrun: %.2f %%. Close to blocking: %.2f %%.\n",
(100.0 * low_water_count) / (samples - 1),
(100.0 * high_water_count) / (samples - 1));
}
/**
* audio_driver_find_handle:
* @idx : index of driver to get handle to.
*
* Returns: handle to audio driver at index. Can be NULL
* if nothing found.
**/
const void *audio_driver_find_handle(int idx)
{
const void *drv = audio_drivers[idx];
if (!drv)
return NULL;
return drv;
}
/**
* audio_driver_find_ident:
* @idx : index of driver to get handle to.
*
* Returns: Human-readable identifier of audio driver at index. Can be NULL
* if nothing found.
**/
const char *audio_driver_find_ident(int idx)
{
const audio_driver_t *drv = audio_drivers[idx];
if (!drv)
return NULL;
return drv->ident;
}
/**
* config_get_audio_driver_options:
*
* Get an enumerated list of all audio driver names, separated by '|'.
*
* Returns: string listing of all audio driver names, separated by '|'.
**/
const char *config_get_audio_driver_options(void)
{
return char_list_new_special(STRING_LIST_AUDIO_DRIVERS, NULL);
}
static bool uninit_audio(void)
{
settings_t *settings = config_get_ptr();
if (current_audio && current_audio->free)
{
if (audio_driver_context_audio_data)
current_audio->free(audio_driver_context_audio_data);
audio_driver_context_audio_data = NULL;
}
if (audio_driver_data.output_samples.conv_buf)
free(audio_driver_data.output_samples.conv_buf);
audio_driver_data.output_samples.conv_buf = NULL;
audio_driver_data.data_ptr = 0;
if (audio_driver_data.rewind.buf)
free(audio_driver_data.rewind.buf);
audio_driver_data.rewind.buf = NULL;
audio_driver_data.rewind.size = 0;
if (!settings->audio.enable)
{
audio_driver_unset_active();
return false;
}
audio_driver_deinit_resampler();
if (audio_driver_data.data)
free(audio_driver_data.data);
audio_driver_data.data = NULL;
if (audio_driver_data.output_samples.buf)
free(audio_driver_data.output_samples.buf);
audio_driver_data.output_samples.buf = NULL;
command_event(CMD_EVENT_DSP_FILTER_DEINIT, NULL);
compute_audio_buffer_statistics();
return true;
}
static bool audio_driver_init_internal(bool audio_cb_inited)
{
size_t outsamples_max, max_bufsamples = AUDIO_CHUNK_SIZE_NONBLOCKING * 2;
settings_t *settings = config_get_ptr();
convert_s16_to_float_init_simd();
convert_float_to_s16_init_simd();
/* Accomodate rewind since at some point we might have two full buffers. */
outsamples_max = max_bufsamples * AUDIO_MAX_RATIO *
settings->slowmotion_ratio;
audio_driver_data.output_samples.conv_buf =
(int16_t*)malloc(outsamples_max * sizeof(int16_t));
/* Used for recording even if audio isn't enabled. */
retro_assert(audio_driver_data.output_samples.conv_buf != NULL);
if (!audio_driver_data.output_samples.conv_buf)
goto error;
audio_driver_data.chunk.block_size = AUDIO_CHUNK_SIZE_BLOCKING;
audio_driver_data.chunk.nonblock_size = AUDIO_CHUNK_SIZE_NONBLOCKING;
audio_driver_data.chunk.size = audio_driver_data.chunk.block_size;
/* Needs to be able to hold full content of a full max_bufsamples
* in addition to its own. */
audio_driver_data.rewind.buf = (int16_t*)malloc
(max_bufsamples * sizeof(int16_t));
retro_assert(audio_driver_data.rewind.buf != NULL);
if (!audio_driver_data.rewind.buf)
goto error;
audio_driver_data.rewind.size = max_bufsamples;
if (!settings->audio.enable)
{
audio_driver_unset_active();
return false;
}
audio_driver_find_driver();
#ifdef HAVE_THREADS
if (audio_cb_inited)
{
RARCH_LOG("Starting threaded audio driver ...\n");
if (!audio_init_thread(
¤t_audio,
&audio_driver_context_audio_data,
*settings->audio.device ? settings->audio.device : NULL,
settings->audio.out_rate, settings->audio.latency,
current_audio))
{
RARCH_ERR("Cannot open threaded audio driver ... Exiting ...\n");
retroarch_fail(1, "audio_driver_init_internal()");
}
}
else
#endif
{
audio_driver_context_audio_data =
current_audio->init(*settings->audio.device ?
settings->audio.device : NULL,
settings->audio.out_rate, settings->audio.latency);
}
if (!audio_driver_context_audio_data)
{
RARCH_ERR("Failed to initialize audio driver. Will continue without audio.\n");
audio_driver_unset_active();
}
audio_driver_data.use_float = false;
if ( audio_driver_is_active()
&& current_audio->use_float(audio_driver_context_audio_data))
audio_driver_data.use_float = true;
if (!settings->audio.sync && audio_driver_is_active())
{
command_event(CMD_EVENT_AUDIO_SET_NONBLOCKING_STATE, NULL);
audio_driver_data.chunk.size = audio_driver_data.chunk.nonblock_size;
}
if (audio_driver_data.audio_rate.input <= 0.0f)
{
/* Should never happen. */
RARCH_WARN("Input rate is invalid (%.3f Hz). Using output rate (%u Hz).\n",
audio_driver_data.audio_rate.input, settings->audio.out_rate);
audio_driver_data.audio_rate.input = settings->audio.out_rate;
}
audio_driver_data.audio_rate.source_ratio.original =
audio_driver_data.audio_rate.source_ratio.current =
(double)settings->audio.out_rate / audio_driver_data.audio_rate.input;
if (!audio_driver_init_resampler())
{
RARCH_ERR("Failed to initialize resampler \"%s\".\n",
settings->audio.resampler);
audio_driver_unset_active();
}
audio_driver_data.data = (float*)
malloc(max_bufsamples * sizeof(float));
retro_assert(audio_driver_data.data != NULL);
if (!audio_driver_data.data)
goto error;
audio_driver_data.data_ptr = 0;
retro_assert(settings->audio.out_rate <
audio_driver_data.audio_rate.input * AUDIO_MAX_RATIO);
audio_driver_data.output_samples.buf = (float*)
malloc(outsamples_max * sizeof(float));
retro_assert(audio_driver_data.output_samples.buf != NULL);
if (!audio_driver_data.output_samples.buf)
goto error;
audio_driver_data.audio_rate.control = false;
if (
!audio_cb_inited
&& audio_driver_is_active()
&& settings->audio.rate_control
)
{
/* Audio rate control requires write_avail
* and buffer_size to be implemented. */
if (current_audio->buffer_size)
{
audio_driver_data.driver_buffer_size =
current_audio->buffer_size(audio_driver_context_audio_data);
audio_driver_data.audio_rate.control = true;
}
else
RARCH_WARN("Audio rate control was desired, but driver does not support needed features.\n");
}
command_event(CMD_EVENT_DSP_FILTER_INIT, NULL);
audio_driver_data.free_samples.count = 0;
/* Threaded driver is initially stopped. */
if (
audio_driver_is_active()
&& !settings->audio.mute_enable
&& audio_cb_inited
)
audio_driver_start();
return true;
error:
return audio_driver_deinit();
}
/*
* audio_driver_readjust_input_rate:
*
* Readjust the audio input rate.
*/
static void audio_driver_readjust_input_rate(void)
{
settings_t *settings = config_get_ptr();
unsigned write_idx = audio_driver_data.free_samples.count++ &
(AUDIO_BUFFER_FREE_SAMPLES_COUNT - 1);
int half_size = audio_driver_data.driver_buffer_size / 2;
int avail =
current_audio->write_avail(audio_driver_context_audio_data);
int delta_mid = avail - half_size;
double direction = (double)delta_mid / half_size;
double adjust = 1.0 + settings->audio.rate_control_delta * direction;
#if 0
RARCH_LOG_OUTPUT("Audio buffer is %u%% full\n",
(unsigned)(100 - (avail * 100) / audio_driver_data.driver_buffer_size));
#endif
audio_driver_data.free_samples.buf[write_idx] = avail;
audio_driver_data.audio_rate.source_ratio.current =
audio_driver_data.audio_rate.source_ratio.original * adjust;
#if 0
RARCH_LOG_OUTPUT("New rate: %lf, Orig rate: %lf\n",
audio_driver_data.audio_rate.source_ratio.current,
audio_driver_data.audio_rate.source_ratio.original);
#endif
}
void audio_driver_set_nonblocking_state(bool enable)
{
settings_t *settings = config_get_ptr();
if (
audio_driver_is_active()
&& audio_driver_context_audio_data
)
current_audio->set_nonblock_state(audio_driver_context_audio_data,
settings->audio.sync ? enable : true);
audio_driver_data.chunk.size = enable ?
audio_driver_data.chunk.nonblock_size :
audio_driver_data.chunk.block_size;
}
/**
* audio_driver_flush:
* @data : pointer to audio buffer.
* @right : amount of samples to write.
*
* Writes audio samples to audio driver. Will first
* perform DSP processing (if enabled) and resampling.
*
* Returns: true (1) if audio samples were written to the audio
* driver, false (0) in case of an error.
**/
static bool audio_driver_flush(const int16_t *data, size_t samples)
{
static struct retro_perf_counter audio_convert_s16 = {0};
static struct retro_perf_counter audio_convert_float = {0};
static struct retro_perf_counter audio_dsp = {0};
struct resampler_data src_data = {0};
struct rarch_dsp_data dsp_data = {0};
const void *output_data = NULL;
unsigned output_frames = 0;
size_t output_size = sizeof(float);
settings_t *settings = config_get_ptr();
recording_push_audio(data, samples);
if (runloop_ctl(RUNLOOP_CTL_IS_PAUSED, NULL) || settings->audio.mute_enable)
return true;
if (!audio_driver_is_active())
return false;
if (!audio_driver_data.data)
return false;
performance_counter_init(&audio_convert_s16, "audio_convert_s16");
performance_counter_start(&audio_convert_s16);
convert_s16_to_float(audio_driver_data.data, data, samples,
audio_driver_data.volume_gain);
performance_counter_stop(&audio_convert_s16);
src_data.data_in = audio_driver_data.data;
src_data.input_frames = samples >> 1;
dsp_data.input = audio_driver_data.data;
dsp_data.input_frames = samples >> 1;
if (audio_driver_data.dsp)
{
performance_counter_init(&audio_dsp, "audio_dsp");
performance_counter_start(&audio_dsp);
rarch_dsp_filter_process(audio_driver_data.dsp, &dsp_data);
performance_counter_stop(&audio_dsp);
if (dsp_data.output)
{
src_data.data_in = dsp_data.output;
src_data.input_frames = dsp_data.output_frames;
}
}
src_data.data_out = audio_driver_data.output_samples.buf;
if (audio_driver_data.audio_rate.control)
audio_driver_readjust_input_rate();
src_data.ratio = audio_driver_data.audio_rate.source_ratio.current;
if (runloop_ctl(RUNLOOP_CTL_IS_SLOWMOTION, NULL))
src_data.ratio *= settings->slowmotion_ratio;
audio_driver_process_resampler(&src_data);
output_data = audio_driver_data.output_samples.buf;
output_frames = src_data.output_frames;
if (!audio_driver_data.use_float)
{
performance_counter_init(&audio_convert_float, "audio_convert_float");
performance_counter_start(&audio_convert_float);
convert_float_to_s16(audio_driver_data.output_samples.conv_buf,
(const float*)output_data, output_frames * 2);
performance_counter_stop(&audio_convert_float);
output_data = audio_driver_data.output_samples.conv_buf;
output_size = sizeof(int16_t);
}
if (current_audio->write(audio_driver_context_audio_data,
output_data, output_frames * output_size * 2) < 0)
{
audio_driver_unset_active();
return false;
}
return true;
}
/**
* audio_driver_sample:
* @left : value of the left audio channel.
* @right : value of the right audio channel.
*
* Audio sample render callback function.
**/
void audio_driver_sample(int16_t left, int16_t right)
{
audio_driver_data.output_samples.conv_buf[audio_driver_data.data_ptr++] = left;
audio_driver_data.output_samples.conv_buf[audio_driver_data.data_ptr++] = right;
if (audio_driver_data.data_ptr < audio_driver_data.chunk.size)
return;
audio_driver_flush(audio_driver_data.output_samples.conv_buf,
audio_driver_data.data_ptr);
audio_driver_data.data_ptr = 0;
}
/**
* audio_driver_sample_batch:
* @data : pointer to audio buffer.
* @frames : amount of audio frames to push.
*
* Batched audio sample render callback function.
*
* Returns: amount of frames sampled. Will be equal to @frames
* unless @frames exceeds (AUDIO_CHUNK_SIZE_NONBLOCKING / 2).
**/
size_t audio_driver_sample_batch(const int16_t *data, size_t frames)
{
if (frames > (AUDIO_CHUNK_SIZE_NONBLOCKING >> 1))
frames = AUDIO_CHUNK_SIZE_NONBLOCKING >> 1;
audio_driver_flush(data, frames << 1);
return frames;
}
/**
* audio_driver_sample_rewind:
* @left : value of the left audio channel.
* @right : value of the right audio channel.
*
* Audio sample render callback function (rewind version).
* This callback function will be used instead of
* audio_driver_sample when rewinding is activated.
**/
void audio_driver_sample_rewind(int16_t left, int16_t right)
{
audio_driver_data.rewind.buf[--audio_driver_data.rewind.ptr] = right;
audio_driver_data.rewind.buf[--audio_driver_data.rewind.ptr] = left;
}
/**
* audio_driver_sample_batch_rewind:
* @data : pointer to audio buffer.
* @frames : amount of audio frames to push.
*
* Batched audio sample render callback function (rewind version).
*
* This callback function will be used instead of
* audio_driver_sample_batch when rewinding is activated.
*
* Returns: amount of frames sampled. Will be equal to @frames
* unless @frames exceeds (AUDIO_CHUNK_SIZE_NONBLOCKING / 2).
**/
size_t audio_driver_sample_batch_rewind(const int16_t *data, size_t frames)
{
size_t i;
size_t samples = frames << 1;
for (i = 0; i < samples; i++)
audio_driver_data.rewind.buf[--audio_driver_data.rewind.ptr] = data[i];
return frames;
}
void audio_driver_set_volume_gain(float gain)
{
audio_driver_data.volume_gain = gain;
}
void audio_driver_dsp_filter_free(void)
{
if (audio_driver_data.dsp)
rarch_dsp_filter_free(audio_driver_data.dsp);
audio_driver_data.dsp = NULL;
}
void audio_driver_dsp_filter_init(const char *device)
{
audio_driver_data.dsp = rarch_dsp_filter_new(
device, audio_driver_data.audio_rate.input);
if (!audio_driver_data.dsp)
RARCH_ERR("[DSP]: Failed to initialize DSP filter \"%s\".\n", device);
}
void audio_driver_set_buffer_size(size_t bufsize)
{
audio_driver_data.driver_buffer_size = bufsize;
}
void audio_driver_monitor_adjust_system_rates(void)
{
float timing_skew;
settings_t *settings = config_get_ptr();
const struct retro_system_timing *info = NULL;
struct retro_system_av_info *av_info = video_viewport_get_system_av_info();
if (av_info)
info = (const struct retro_system_timing*)&av_info->timing;
if (!info || info->sample_rate <= 0.0)
return;
timing_skew = fabs(1.0f - info->fps / settings->video.refresh_rate);
audio_driver_data.audio_rate.input = info->sample_rate;
if (timing_skew <= settings->audio.max_timing_skew)
audio_driver_data.audio_rate.input *= (settings->video.refresh_rate / info->fps);
RARCH_LOG("Set audio input rate to: %.2f Hz.\n",
audio_driver_data.audio_rate.input);
}
void audio_driver_setup_rewind(void)
{
unsigned i;
/* Push audio ready to be played. */
audio_driver_data.rewind.ptr = audio_driver_data.rewind.size;
for (i = 0; i < audio_driver_data.data_ptr; i += 2)
{
audio_driver_data.rewind.buf[--audio_driver_data.rewind.ptr] =
audio_driver_data.output_samples.conv_buf[i + 1];
audio_driver_data.rewind.buf[--audio_driver_data.rewind.ptr] =
audio_driver_data.output_samples.conv_buf[i + 0];
}
audio_driver_data.data_ptr = 0;
}
bool audio_driver_find_driver(void)
{
int i;
driver_ctx_info_t drv;
settings_t *settings = config_get_ptr();
drv.label = "audio_driver";
drv.s = settings->audio.driver;
driver_ctl(RARCH_DRIVER_CTL_FIND_INDEX, &drv);
i = drv.len;
if (i >= 0)
current_audio = (const audio_driver_t*)audio_driver_find_handle(i);
else
{
unsigned d;
RARCH_ERR("Couldn't find any audio driver named \"%s\"\n",
settings->audio.driver);
RARCH_LOG_OUTPUT("Available audio drivers are:\n");
for (d = 0; audio_driver_find_handle(d); d++)
RARCH_LOG_OUTPUT("\t%s\n", audio_driver_find_ident(d));
RARCH_WARN("Going to default to first audio driver...\n");
current_audio = (const audio_driver_t*)audio_driver_find_handle(0);
if (!current_audio)
retroarch_fail(1, "audio_driver_find()");
}
return true;
}
void audio_driver_deinit_resampler(void)
{
rarch_resampler_freep(&audio_driver_resampler,
&audio_driver_resampler_data);
}
bool audio_driver_free_devices_list(void)
{
if (!current_audio || !current_audio->device_list_free
|| !audio_driver_context_audio_data)
return false;
current_audio->device_list_free(audio_driver_context_audio_data,
audio_driver_devices_list);
audio_driver_devices_list = NULL;
return true;
}
bool audio_driver_new_devices_list(void)
{
if (!current_audio || !current_audio->device_list_new
|| !audio_driver_context_audio_data)
return false;
audio_driver_devices_list = (struct string_list*)
current_audio->device_list_new(audio_driver_context_audio_data);
if (!audio_driver_devices_list)
return false;
return true;
}
bool audio_driver_init(void)
{
return audio_driver_init_internal(audio_callback.callback != NULL);
}
bool audio_driver_get_devices_list(void **data)
{
struct string_list**ptr = (struct string_list**)data;
if (!ptr)
return false;
*ptr = audio_driver_devices_list;
return true;
}
bool audio_driver_init_resampler(void)
{
settings_t *settings = config_get_ptr();
return rarch_resampler_realloc(
&audio_driver_resampler_data,
&audio_driver_resampler,
settings->audio.resampler,
audio_driver_data.audio_rate.source_ratio.original);
}
void audio_driver_process_resampler(struct resampler_data *data)
{
performance_counter_init(&resampler_proc, "resampler_proc");
performance_counter_start(&resampler_proc);
rarch_resampler_process(audio_driver_resampler,
audio_driver_resampler_data, data);
performance_counter_stop(&resampler_proc);
}
bool audio_driver_deinit(void)
{
audio_driver_free_devices_list();
if (!uninit_audio())
return false;
return true;
}
bool audio_driver_set_callback(const void *data)
{
const struct retro_audio_callback *cb = (const struct retro_audio_callback*)data;
#ifdef HAVE_NETPLAY
global_t *global = global_get_ptr();
#endif
if (recording_driver_get_data_ptr()) /* A/V sync is a must. */
return false;
#ifdef HAVE_NETPLAY
if (global->netplay.enable)
return false;
#endif
if (cb)
audio_callback = *cb;
return true;
}
bool audio_driver_enable_callback(void)
{
if (!audio_driver_has_callback())
return false;
if (audio_callback.set_state)
audio_callback.set_state(true);
return true;
}
bool audio_driver_disable_callback(void)
{
if (!audio_driver_has_callback())
return false;
if (audio_callback.set_state)
audio_callback.set_state(false);
return true;
}
/* Sets audio monitor rate to new value. */
void audio_driver_monitor_set_rate(void)
{
settings_t *settings = config_get_ptr();
double new_src_ratio = (double)settings->audio.out_rate /
audio_driver_data.audio_rate.input;
audio_driver_data.audio_rate.source_ratio.original = new_src_ratio;
audio_driver_data.audio_rate.source_ratio.current = new_src_ratio;
}
bool audio_driver_callback(void)
{
if (!audio_driver_has_callback())
return false;
if (audio_callback.callback)
audio_callback.callback();
return true;
}
bool audio_driver_has_callback(void)
{
return audio_callback.callback;
}
bool audio_driver_toggle_mute(void)
{
settings_t *settings = config_get_ptr();
if (!audio_driver_context_audio_data)
return false;
if (!audio_driver_is_active())
return false;
settings->audio.mute_enable = !settings->audio.mute_enable;
if (settings->audio.mute_enable)
command_event(CMD_EVENT_AUDIO_STOP, NULL);
else if (!command_event(CMD_EVENT_AUDIO_START, NULL))
{
audio_driver_unset_active();
return false;
}
return true;
}
bool audio_driver_start(void)
{
if (!current_audio || !current_audio->start
|| !audio_driver_context_audio_data)
return false;
return current_audio->start(audio_driver_context_audio_data);
}
bool audio_driver_stop(void)
{
if (!current_audio || !current_audio->stop
|| !audio_driver_context_audio_data)
return false;
return current_audio->stop(audio_driver_context_audio_data);
}
void audio_driver_unset_callback(void)
{
audio_callback.callback = NULL;
audio_callback.set_state = NULL;
}
bool audio_driver_alive(void)
{
if (!current_audio || !current_audio->alive
|| !audio_driver_context_audio_data)
return false;
return current_audio->alive(audio_driver_context_audio_data);
}
void audio_driver_frame_is_reverse(void)
{
/* We just rewound. Flush rewind audio buffer. */
audio_driver_flush(
audio_driver_data.rewind.buf + audio_driver_data.rewind.ptr,
audio_driver_data.rewind.size - audio_driver_data.rewind.ptr);
}
void audio_driver_destroy_data(void)
{
audio_driver_context_audio_data = NULL;
}
void audio_driver_set_own_driver(void)
{
audio_driver_data_own = true;
}
void audio_driver_unset_own_driver(void)
{
audio_driver_data_own = false;
}
bool audio_driver_owns_driver(void)