1 /***************************************************************************
2
3 speaker.c
4 Sound driver to emulate a simple speaker,
5 driven by one or more output bits
6
7 ****************************************************************************/
8 #include "driver.h"
9
10 static INT16 default_levels[2] = {0,32767};
11
12 struct speaker
13 {
14 int channel;
15 INT16 *levels;
16 int num_levels;
17 int level;
18 int mixing_level;
19 };
20
21 static struct Speaker_interface *intf;
22 static struct speaker speaker[MAX_SPEAKER];
23
speaker_sh_init(int which,int speaker_num_levels,INT16 * speaker_levels)24 void speaker_sh_init(int which, int speaker_num_levels, INT16 *speaker_levels)
25 {
26 struct speaker *sp = &speaker[which];
27 sp->levels = speaker_levels;
28 sp->num_levels = speaker_num_levels;
29 }
30
speaker_sound_update(int param,INT16 * buffer,int length)31 static void speaker_sound_update(int param, INT16 *buffer, int length)
32 {
33 struct speaker *sp = &speaker[param];
34 int volume = sp->levels[sp->level] * sp->mixing_level / 100;
35
36 while( length-- > 0 )
37 *buffer++ = volume;
38 }
39
speaker_sh_start(const struct MachineSound * msound)40 int speaker_sh_start(const struct MachineSound *msound)
41 {
42 int i;
43
44 intf = msound->sound_interface;
45
46 for( i = 0; i < intf->num; i++ )
47 {
48 char buf[32];
49 struct speaker *sp = &speaker[i];
50 sp->mixing_level = intf->mixing_level[i];
51 if( intf->num > 1 )
52 sprintf(buf, "Speaker #%d", i+1);
53 else
54 strcpy(buf, "Speaker");
55 sp->channel = stream_init(buf, sp->mixing_level, Machine->sample_rate, 0, speaker_sound_update);
56 if( sp->channel == -1 )
57 return 1;
58 sp->num_levels = 2;
59 sp->levels = default_levels;
60 sp->level = 0;
61 }
62 return 0;
63 }
64
speaker_sh_stop(void)65 void speaker_sh_stop(void)
66 {
67 /* nothing */
68 }
69
speaker_sh_update(void)70 void speaker_sh_update(void)
71 {
72 int i;
73 for( i = 0; i < intf->num; i++ )
74 stream_update(speaker[i].channel, 0);
75 }
76
speaker_level_w(int which,int new_level)77 void speaker_level_w(int which, int new_level)
78 {
79 struct speaker *sp = &speaker[which];
80
81 if( new_level < 0 )
82 new_level = 0;
83 else
84 if( new_level >= sp->num_levels )
85 new_level = sp->num_levels - 1;
86
87 if( new_level == sp->level )
88 return;
89
90 /* force streams.c to update sound until this point in time now */
91 stream_update(sp->channel, 0);
92
93 sp->level = new_level;
94 }
95
96
97