-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.c
1354 lines (1132 loc) · 36.4 KB
/
sound.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
/*
* sound.c - General code for the sound interface.
*
* Written by
* Teemu Rantanen <tvr@cs.hut.fi>
*
* Resource and cmdline code by
* Ettore Perazzoli <ettore@comm2000.it>
*
* This file is part of VICE, the Versatile Commodore Emulator.
* See README for copyright notice.
*
* This program 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 Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*
*/
#include "vice.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include "archdep.h"
#include "clkguard.h"
#include "cmdline.h"
#include "debug.h"
#include "fixpoint.h"
#include "lib.h"
#include "log.h"
#include "machine.h"
#include "maincpu.h"
#include "resources.h"
#include "sound.h"
#include "translate.h"
#include "types.h"
#include "uiapi.h"
#include "util.h"
#include "vsync.h"
static log_t sound_log = LOG_ERR;
/* ------------------------------------------------------------------------- */
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* ------------------------------------------------------------------------- */
/* Resource handling -- Added by Ettore 98-04-26. */
/* FIXME: We need sanity checks! And do we really need all of these
`sound_close()' calls? */
static int playback_enabled; /* app_resources.sound */
static int sample_rate; /* app_resources.soundSampleRate */
static char *device_name = NULL; /* app_resources.soundDeviceName */
static char *device_arg = NULL; /* app_resources.soundDeviceArg */
static char *recorddevice_name = NULL;/* app_resources.soundDeviceName */
static char *recorddevice_arg = NULL; /* app_resources.soundDeviceArg */
static int buffer_size; /* app_resources.soundBufferSize */
static int suspend_time; /* app_resources.soundSuspendTime */
static int speed_adjustment_setting; /* app_resources.soundSpeedAdjustment */
static int volume;
static int fragment_size;
/* divisors for fragment size calculation */
static int fragment_divisor[] = {
4, /* 5ms */
2, /* 10 ms */
1 /* 20 ms */
};
/* I need this to serialize close_sound and enablesound/sound_open in
the OS/2 Multithreaded environment */
static int sdev_open = FALSE;
/* I need this to serialize close_sound and enablesound/sound_open in
the OS/2 Multithreaded environment */
int sound_state_changed;
int sid_state_changed;
/* Sample based or cycle based sound engine. */
static int cycle_based = 0;
static int set_playback_enabled(int val, void *param)
{
if (val)
vsync_disable_timer();
playback_enabled = val;
sound_machine_enable(playback_enabled);
return 0;
}
static int set_sample_rate(int val, void *param)
{
sample_rate = val;
sound_state_changed = TRUE;
return 0;
}
static int set_device_name(const char *val, void *param)
{
util_string_set(&device_name, val);
sound_state_changed = TRUE;
return 0;
}
static int set_device_arg(const char *val, void *param)
{
util_string_set(&device_arg, val);
sound_state_changed = TRUE;
return 0;
}
static int set_recorddevice_name(const char *val, void *param)
{
util_string_set(&recorddevice_name, val);
sound_state_changed = TRUE;
return 0;
}
static int set_recorddevice_arg(const char *val, void *param)
{
util_string_set(&recorddevice_arg, val);
sound_state_changed = TRUE;
return 0;
}
static int set_buffer_size(int val, void *param)
{
buffer_size = val;
sound_state_changed = TRUE;
return 0;
}
static int set_fragment_size(int val, void *param)
{
fragment_size = val;
sound_state_changed = TRUE;
return 0;
}
static int set_suspend_time(int val, void *param)
{
suspend_time = val;
if (suspend_time < 0)
suspend_time = 0;
sound_state_changed = TRUE;
return 0;
}
static int set_speed_adjustment_setting(int val, void *param)
{
speed_adjustment_setting = val;
return 0;
}
static int set_volume(int val, void *param)
{
volume = val;
if (volume < 0)
volume = 0;
if (volume > 100)
volume = 100;
ui_display_volume(volume);
return 0;
}
static const resource_string_t resources_string[] = {
{ "SoundDeviceName", "", RES_EVENT_NO, NULL,
&device_name, set_device_name, NULL },
{ "SoundDeviceArg", "", RES_EVENT_NO, NULL,
&device_arg, set_device_arg, NULL },
{ "SoundRecordDeviceName", "", RES_EVENT_STRICT, (resource_value_t)"",
&recorddevice_name, set_recorddevice_name, NULL },
{ "SoundRecordDeviceArg", "", RES_EVENT_NO, NULL,
&recorddevice_arg, set_recorddevice_arg, NULL },
{ NULL }
};
static const resource_int_t resources_int[] = {
{ "Sound", 1, RES_EVENT_SAME, NULL,
(void *)&playback_enabled, set_playback_enabled, NULL },
{ "SoundSampleRate", SOUND_SAMPLE_RATE, RES_EVENT_NO, NULL,
(void *)&sample_rate, set_sample_rate, NULL },
{ "SoundBufferSize", SOUND_SAMPLE_BUFFER_SIZE, RES_EVENT_NO, NULL,
(void *)&buffer_size, set_buffer_size, NULL },
{ "SoundFragmentSize", ARCHDEP_SOUND_FRAGMENT_SIZE, RES_EVENT_NO, NULL,
(void *)&fragment_size, set_fragment_size, NULL },
{ "SoundSuspendTime", 0, RES_EVENT_NO, NULL,
(void *)&suspend_time, set_suspend_time, NULL },
{ "SoundSpeedAdjustment", SOUND_ADJUST_FLEXIBLE, RES_EVENT_NO, NULL,
(void *)&speed_adjustment_setting, set_speed_adjustment_setting, NULL },
{ "SoundVolume", 100, RES_EVENT_NO, NULL,
(void *)&volume, set_volume, NULL },
{ NULL }
};
int sound_resources_init(void)
{
if (resources_register_string(resources_string) < 0)
return -1;
return resources_register_int(resources_int);
}
void sound_resources_shutdown(void)
{
lib_free(device_name);
lib_free(device_arg);
lib_free(recorddevice_name);
lib_free(recorddevice_arg);
}
/* ------------------------------------------------------------------------- */
static const cmdline_option_t cmdline_options[] = {
{ "-sound", SET_RESOURCE, 0,
NULL, NULL, "Sound", (resource_value_t)1,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_ENABLE_SOUND_PLAYBACK,
NULL, NULL },
{ "+sound", SET_RESOURCE, 0,
NULL, NULL, "Sound", (resource_value_t)0,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_DISABLE_SOUND_PLAYBACK,
NULL, NULL },
{ "-soundrate", SET_RESOURCE, 1,
NULL, NULL, "SoundSampleRate", NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_VALUE, IDCLS_SET_SAMPLE_RATE_VALUE_HZ,
NULL, NULL },
{ "-soundbufsize", SET_RESOURCE, 1,
NULL, NULL, "SoundBufferSize", NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_VALUE, IDCLS_SET_SOUND_BUFFER_SIZE_MSEC,
NULL, NULL },
{ "-soundfragsize", SET_RESOURCE, 1,
NULL, NULL, "SoundFragmentSize", NULL,
USE_PARAM_STRING, USE_DESCRIPTION_STRING,
IDCLS_P_VALUE, IDCLS_SET_SOUND_BUFFER_SIZE_MSEC,
T_("value"), T_("Set sound fragment size (0 = small, 1 = medium, 2 = large)") },
{ "-sounddev", SET_RESOURCE, 1,
NULL, NULL, "SoundDeviceName", NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_NAME, IDCLS_SPECIFY_SOUND_DRIVER,
NULL, NULL },
{ "-soundarg", SET_RESOURCE, 1,
NULL, NULL, "SoundDeviceArg", NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_ARGS, IDCLS_SPECIFY_SOUND_DRIVER_PARAM,
NULL, NULL },
{ "-soundrecdev", SET_RESOURCE, 1,
NULL, NULL, "SoundRecordDeviceName", NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_NAME, IDCLS_SPECIFY_RECORDING_SOUND_DRIVER,
NULL, NULL },
{ "-soundrecarg", SET_RESOURCE, 1,
NULL, NULL, "SoundRecordDeviceArg", NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_ARGS, IDCLS_SPECIFY_REC_SOUND_DRIVER_PARAM,
NULL, NULL },
{ "-soundsync", SET_RESOURCE, 1,
NULL, NULL, "SoundSpeedAdjustment", NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_SYNC, IDCLS_SET_SOUND_SPEED_ADJUST,
NULL, NULL },
{ NULL }
};
int sound_cmdline_options_init(void)
{
return cmdline_register_options(cmdline_options);
}
/* ------------------------------------------------------------------------- */
/* timing constants */
static unsigned int cycles_per_sec;
static unsigned int cycles_per_rfsh;
static double rfsh_per_sec;
/* Speed in percent, tracks relative_speed from vsync.c */
static int speed_percent;
/* Flag: Is warp mode enabled? */
static int warp_mode_enabled;
typedef struct
{
/* Number of sound channels (for multiple SIDs) */
int channels;
/* sid itself */
sound_t *psid[SOUND_CHANNELS_MAX];
/* number of clocks between each sample. used value */
soundclk_t clkstep;
/* number of clocks between each sample. original value */
soundclk_t origclkstep;
/* factor between those two clksteps */
soundclk_t clkfactor;
/* time of last sample generated */
soundclk_t fclk;
/* time of last write to sid. used for pdev->dump() */
CLOCK wclk;
/* time of last call to sound_run_sound() */
CLOCK lastclk;
/* sample buffer */
SWORD buffer[SOUND_CHANNELS_MAX * SOUND_BUFSIZE];
/* sample buffer pointer */
int bufptr;
/* pointer to playback device structure in use */
sound_device_t *playdev;
/* pointer to playback device structure in use */
sound_device_t *recdev;
/* number of samples in a fragment */
int fragsize;
/* number of fragments in kernel buffer */
int fragnr;
/* number of samples in kernel buffer */
int bufsize;
/* constants related to adjusting sound */
int prevused;
int prevfill;
/* is the device suspended? */
int issuspended;
SWORD lastsample[SOUND_CHANNELS_MAX];
} snddata_t;
static snddata_t snddata;
/* device registration code */
static sound_device_t *sound_devices[32];
static char *devlist;
int sound_register_device(sound_device_t *pdevice)
{
const int max = sizeof(sound_devices) / sizeof(sound_devices[0]);
int i;
char *tmplist;
for (i = 0; sound_devices[i] && i < max; i++);
if (i < max) {
sound_devices[i] = pdevice;
tmplist = lib_msprintf("%s %s", devlist, pdevice->name);
lib_free(devlist);
devlist = tmplist;
} else {
log_error(sound_log, "available sound devices exceed VICEs storage");
}
return 0;
}
unsigned int sound_device_num(void)
{
const unsigned int max = sizeof(sound_devices) / sizeof(sound_devices[0]);
unsigned int i;
for (i = 0; sound_devices[i] && i < max; i++);
return i;
}
const char *sound_device_name(unsigned int num)
{
return sound_devices[num]->name;
}
/* code to disable sid for a given number of seconds if needed */
static time_t disabletime;
static void suspendsound(const char *reason)
{
disabletime = time(0);
log_warning(sound_log, "suspend, disabling sound for %d secs (%s)",
suspend_time, reason);
sound_state_changed = TRUE;
}
static void enablesound(void)
{
time_t diff;
if (!disabletime)
return;
diff = time(0) - disabletime;
if (diff < 0 || diff >= (time_t)suspend_time) {
disabletime = 0;
}
}
/* close sid device and show error dialog */
static int sound_error(const char *msg)
{
sound_close();
if (console_mode || vsid_mode) {
log_message(sound_log, "%s", msg);
} else {
char *txt = lib_msprintf("Sound: %s", msg);
ui_error(txt);
lib_free(txt);
}
playback_enabled = 0;
if (!console_mode)
ui_update_menus();
return 1;
}
/* Fill buffer with last sample.
rise < 0 : attenuation
rise == 0 : constant value
rise > 0 : gain
*/
static void fill_buffer(int size, int rise)
{
int c, i;
SWORD *p;
p = lib_malloc(size * sizeof(SWORD) * snddata.channels);
if (!p)
return;
for (c = 0; c < snddata.channels; c++) {
for (i = 0; i < size; i++) {
double factor;
if (rise < 0)
factor = (double)(size - i) / size;
else
if (rise > 0)
factor = (double)i / size;
else
factor = 1.0;
p[i * snddata.channels + c] = (SWORD)(snddata.lastsample[c]
* factor);
}
}
i = snddata.playdev->write(p, size * snddata.channels);
lib_free(p);
if (i)
sound_error(translate_text(IDGS_WRITE_TO_SOUND_DEVICE_FAILED));
}
/* open SID engine */
static int sid_open(void)
{
int c;
for (c = 0; c < snddata.channels; c++) {
if (!(snddata.psid[c] = sound_machine_open(c))) {
return sound_error(translate_text(IDGS_CANNOT_OPEN_SID_ENGINE));
}
}
return 0;
}
/* initialize SID engine */
static int sid_init(void)
{
int c, speed, speed_factor;
/* Special handling for cycle based as opposed to sample based sound
engines. reSID is cycle based. */
cycle_based = sound_machine_cycle_based();
/* "No limit" doesn't make sense for cycle based sound engines,
which have a fixed sampling rate. */
speed_factor = speed_percent ? speed_percent : 100;
speed = sample_rate * 100 / speed_factor;
for (c = 0; c < snddata.channels; c++) {
if (!sound_machine_init(snddata.psid[c], speed, cycles_per_sec)) {
return sound_error(translate_text(IDGS_CANNOT_INIT_SID_ENGINE));
}
}
snddata.clkstep = SOUNDCLK_CONSTANT(cycles_per_sec) / sample_rate;
snddata.origclkstep = snddata.clkstep;
snddata.clkfactor = SOUNDCLK_CONSTANT(1.0);
snddata.fclk = SOUNDCLK_CONSTANT(maincpu_clk);
snddata.wclk = maincpu_clk;
snddata.lastclk = maincpu_clk;
return 0;
}
/* close SID engine */
static void sid_close(void)
{
int c;
for (c = 0; c < snddata.channels; c++) {
if (snddata.psid[c]) {
sound_machine_close(snddata.psid[c]);
snddata.psid[c] = NULL;
}
}
}
sound_t *sound_get_psid(unsigned int channel)
{
return snddata.psid[channel];
}
/* open sound device */
int sound_open(void)
{
int c, i, j;
sound_device_t *pdev, *rdev;
char *playname, *recname;
char *playparam, *recparam;
int speed;
int fragsize;
int fragnr;
double bufsize;
if (suspend_time > 0 && disabletime)
return 1;
/* Opening the sound device and initializing the sound engine
might take some time. */
vsync_suspend_speed_eval();
/* Second SID. */
snddata.channels = sound_machine_channels();
playname = device_name;
if (playname && playname[0] == '\0')
playname = NULL;
playparam = device_arg;
if (playparam && playparam[0] == '\0')
playparam = NULL;
recname = recorddevice_name;
if (recname && recname[0] == '\0')
recname = NULL;
recparam = recorddevice_arg;
if (recparam && recparam[0] == '\0')
recparam = NULL;
/* Calculate buffer size in seconds. */
bufsize = ((buffer_size < 1 || buffer_size > 1000)
? SOUND_SAMPLE_BUFFER_SIZE : buffer_size) / 1000.0;
speed = (sample_rate < 8000 || sample_rate > 96000)
? SOUND_SAMPLE_RATE : sample_rate;
/* Calculate reasonable fragments. Target is 2 fragments per frame,
* which gives a reasonable number of fillable audio chunks to avoid
* ugly situation where a full frame refresh needs to occur before more
* audio is generated. It also improves the estimate of optimal frame
* length for vsync, which is closely tied to audio and uses the fragment
* information to calculate it. */
fragsize = speed / ((rfsh_per_sec < 1.0) ? 1 : ((int)rfsh_per_sec))
/ fragment_divisor[fragment_size];
for (i = 1; 1 << i < fragsize; i++);
fragsize = 1 << i;
fragnr = (int)((speed * bufsize + fragsize - 1) / fragsize);
if (fragnr < 3)
fragnr = 3;
for (i = 0; (pdev = sound_devices[i]); i++) {
if (!playname || (pdev->name && !strcasecmp(playname, pdev->name)))
break;
}
if (pdev) {
if (pdev->init) {
int channels_cap = snddata.channels;
if (pdev->init(playparam, &speed, &fragsize, &fragnr, &channels_cap)) {
char *err;
err = lib_msprintf(translate_text(IDGS_INIT_FAILED_FOR_DEVICE_S),
pdev->name);
sound_error(err);
lib_free(err);
return 1;
}
if (channels_cap != snddata.channels) {
log_warning(sound_log,
"sound device lacks stereo capability");
snddata.channels = 1;
}
}
snddata.issuspended = 0;
for (c = 0; c < snddata.channels; c++) {
snddata.lastsample[c] = 0;
}
snddata.playdev = pdev;
snddata.fragsize = fragsize;
snddata.fragnr = fragnr;
snddata.bufsize = fragsize*fragnr;
snddata.bufptr = 0;
log_message(sound_log,
"Opened device `%s', speed %dHz, fragment size %dms, buffer size %dms%s",
pdev->name, speed,
(int)(1000.0 * fragsize / speed),
(int)(1000.0 * snddata.bufsize / speed),
snddata.channels > 1 ? ", stereo" : "");
sample_rate = speed;
if (sid_open() != 0 || sid_init() != 0) {
return 1;
}
sid_state_changed = FALSE;
/* Set warp mode for non-realtime sound devices in vsid mode. */
if (vsid_mode && !pdev->bufferspace) {
resources_set_int("WarpMode", 1);
}
/* Fill up the sound hardware buffer. */
if (pdev->bufferspace) {
/* Fill to bufsize - fragsize. */
j = pdev->bufferspace() - snddata.fragsize;
if (j > 0) {
/* Whole fragments. */
j -= j % snddata.fragsize;
fill_buffer(j, 0);
}
}
} else {
char *err = lib_msprintf(translate_text(IDGS_DEVICE_S_NOT_FOUND_SUPPORT),
playname);
sound_error(err);
lib_free(err);
return 1;
}
/* now the playback sound device is open */
sdev_open = TRUE;
sound_state_changed = FALSE;
for (i = 0; (rdev = sound_devices[i]); i++) {
if (recname && rdev->name && !strcasecmp(recname, rdev->name))
break;
}
if (recname && rdev == NULL)
ui_error(translate_text(IDGS_RECORD_DEVICE_S_NOT_EXIST), recname);
if (rdev) {
if (rdev == pdev) {
ui_error(translate_text(IDGS_RECORD_DIFFERENT_PLAYBACK));
resources_set_string("SoundRecordDeviceName", "");
return 0;
}
if (rdev->bufferspace != NULL) {
ui_error(translate_text(IDGS_WARNING_RECORDING_REALTIME));
}
if (rdev->init) {
int channels_cap = snddata.channels;
if (rdev->init(recparam, &speed, &fragsize, &fragnr, &channels_cap)) {
ui_error(translate_text(IDGS_INIT_FAILED_FOR_DEVICE_S),
rdev->name);
resources_set_string("SoundRecordDeviceName", "");
return 0;
}
if (sample_rate != speed
|| snddata.fragsize != fragsize
|| snddata.fragnr != fragnr
|| snddata.channels != channels_cap)
{
ui_error(translate_text(IDGS_RECORD_NOT_SUPPORT_SOUND_PAR));
rdev->close();
resources_set_string("SoundRecordDeviceName", "");
} else {
snddata.recdev = rdev;
log_message(sound_log,
"Opened recording device device `%s'", rdev->name);
}
}
}
return 0;
}
/* close sid */
void sound_close(void)
{
if (snddata.playdev) {
log_message(sound_log, "Closing device `%s'", snddata.playdev->name);
if (snddata.playdev->close)
snddata.playdev->close();
snddata.playdev = NULL;
}
if (snddata.recdev) {
log_message(sound_log, "Closing recording device `%s'", snddata.recdev->name);
if (snddata.recdev->close)
snddata.recdev->close();
snddata.recdev = NULL;
}
sid_close();
snddata.prevused = snddata.prevfill = 0;
sdev_open = FALSE;
sound_state_changed = FALSE;
/* Closing the sound device might take some time, and displaying
UI dialogs certainly does. */
vsync_suspend_speed_eval();
}
/* run sid */
static int sound_run_sound(void)
{
int nr = 0, c, i;
int delta_t = 0;
SWORD *bufferptr;
/* XXX: implement the exact ... */
if (!playback_enabled || (suspend_time > 0 && disabletime))
return 1;
if (!snddata.playdev) {
i = sound_open();
if (i)
return i;
}
#ifdef __riscos
/* RISC OS vidc device uses a different approach... */
SoundMachineReady = 1;
if (SoundThreadActive != 0)
return 0;
#endif
/* Handling of cycle based sound engines. */
if (cycle_based) {
for (c = 0; c < snddata.channels; c++) {
delta_t = maincpu_clk - snddata.lastclk;
bufferptr = snddata.buffer + snddata.bufptr * snddata.channels + c;
nr = sound_machine_calculate_samples(snddata.psid[c],
bufferptr,
SOUND_BUFSIZE - snddata.bufptr,
snddata.channels,
&delta_t);
if (volume < 100) {
for (i = 0; i < (nr * snddata.channels); i ++)
bufferptr[i] = (volume!=0) ?
(bufferptr[i]/(100 / volume)) : 0;
}
if (delta_t) {
return sound_error(translate_text(IDGS_SOUND_BUFFER_OVERFLOW_CYCLE));
}
}
} else {
/* Handling of sample based sound engines. */
nr = (int)((SOUNDCLK_CONSTANT(maincpu_clk) - snddata.fclk)
/ snddata.clkstep);
if (!nr)
return 0;
if (snddata.bufptr + nr > SOUND_BUFSIZE) {
return sound_error(translate_text(IDGS_SOUND_BUFFER_OVERFLOW));
}
for (c = 0; c < snddata.channels; c++) {
bufferptr = snddata.buffer + snddata.bufptr * snddata.channels + c;
sound_machine_calculate_samples(snddata.psid[c],
bufferptr,
nr,
snddata.channels,
&delta_t);
if (volume < 100) {
for (i = 0; i < (nr * snddata.channels); i ++)
bufferptr[i] = (volume != 0) ?
(bufferptr[i] / (100 / volume)) : 0;
}
}
snddata.fclk += nr * snddata.clkstep;
}
snddata.bufptr += nr;
snddata.lastclk = maincpu_clk;
return 0;
}
/* reset sid */
void sound_reset(void)
{
int c;
snddata.fclk = SOUNDCLK_CONSTANT(maincpu_clk);
snddata.wclk = maincpu_clk;
snddata.lastclk = maincpu_clk;
snddata.bufptr = 0; /* ugly hack! */
for (c = 0; c < snddata.channels; c++) {
if (snddata.psid[c])
sound_machine_reset(snddata.psid[c], maincpu_clk);
}
}
static void prevent_clk_overflow_callback(CLOCK sub, void *data)
{
int c;
snddata.lastclk -= sub;
snddata.fclk -= SOUNDCLK_CONSTANT(sub);
snddata.wclk -= sub;
for (c = 0; c < snddata.channels; c++) {
if (snddata.psid[c])
sound_machine_prevent_clk_overflow(snddata.psid[c], sub);
}
}
#ifdef __riscos
void sound_synthesize(SWORD *buffer, int length)
{
/* Handling of cycle based sound engines. */
if (cycle_based) {
/* FIXME: This is not implemented yet. A possible solution is
to make the main thread call sound_run at shorter intervals,
and reduce the responsibility of the sound thread to only
flush the sample buffer. On the other hand if sound_run were
called at shorter intervals the sound thread would probably
not be necessary at all. */
snddata.lastclk = maincpu_clk;
}
/* Handling of sample based sound engines. */
else {
int delta_t = 0;
int c;
for (c = 0; c < snddata.channels; c++) {
sound_machine_calculate_samples(snddata.psid[c], buffer + c,
length, snddata.channels,
&delta_t);
}
snddata.fclk += length * snddata.clkstep;
}
}
#endif
/* flush all generated samples from buffer to sounddevice. adjust sid runspeed
to match real running speed of program */
#if defined(__MSDOS__) || defined(__riscos)
int sound_flush()
#else
double sound_flush()
#endif
{
int c, i, nr, space = 0, used;
if (!playback_enabled) {
if (sdev_open)
sound_close();
return 0;
}
if (sound_state_changed) {
if (sdev_open)
sound_close();
sound_state_changed = FALSE;
}
if (suspend_time > 0)
enablesound();
if (sound_run_sound())
return 0;
if (sid_state_changed) {
if (sid_init() != 0) {
return 0;
}
sid_state_changed = FALSE;
}
if (warp_mode_enabled
&& snddata.recdev == NULL) {
snddata.bufptr = 0;
return 0;
}
sound_resume();
if (snddata.playdev->flush) {
char *state = sound_machine_dump_state(snddata.psid[0]);
i = snddata.playdev->flush(state);
lib_free(state);
if (i) {
sound_error(translate_text(IDGS_CANNOT_FLUSH));
return 0;
}
}
/* Calculate the number of samples to flush - whole fragments. */
nr = snddata.bufptr -
snddata.bufptr % snddata.fragsize;
if (!nr)
return 0;
/* adjust speed */
if (snddata.playdev->bufferspace) {
space = snddata.playdev->bufferspace();
if (space < 0 || space > snddata.bufsize) {
log_warning(sound_log, "fragment problems %d %d",
space, snddata.bufsize);
sound_error(translate_text(IDGS_FRAGMENT_PROBLEMS));
return 0;
}
/* we only write complete fragments, sound drivers that can tell
* better accuracy aren't utilized at this stage. */
space -= space % snddata.fragsize;
used = snddata.bufsize - space;
/* buffer emptied during vsync? Looks like underrun. */
if (used < snddata.fragsize) {
int j;
static time_t prev;
time_t now;
if (suspend_time > 0) {
now = time(0);
if (now == prev) {
suspendsound("buffer overruns");
return 0;
}
prev = now;
}
/* Calculate unused space in buffer, accounting for data we are
* about to write. */
j = snddata.bufsize - nr;
/* Fill up sound hardware buffer. */
if (j > 0) {
fill_buffer(j, 0);
}
snddata.prevfill = j;
/* Fresh start for vsync. */
#ifndef DEBUG