OSDN Git Service

7bdfbd7344774457a5793350de22624890c7f141
[csp-qt/common_source_project-fm7.git] / source / src / vm / beep.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.22 -
6
7         [ beep ]
8 */
9
10 #include "beep.h"
11
12 void BEEP::reset()
13 {
14         touch_sound();
15         signal = true;
16         count = 0;
17         on = mute = false;
18 }
19
20 void BEEP::write_signal(int id, uint32_t data, uint32_t mask)
21 {
22         if(id == SIG_BEEP_ON) {
23                 touch_sound();
24                 on = ((data & mask) != 0);
25         } else if(id == SIG_BEEP_MUTE) {
26                 touch_sound();
27                 mute = ((data & mask) != 0);
28         }
29 }
30
31 void BEEP::mix(int32_t* buffer, int cnt)
32 {
33         if(on && !mute) {
34                 for(int i = 0; i < cnt; i++) {
35                         int sample = (count < 1024) ? (gen_vol * (count - 512)) / 512 : gen_vol;
36                         int vol_l = apply_volume(sample, volume_l);
37                         int vol_r = apply_volume(sample, volume_r);
38                         *buffer++ += signal ? vol_l : -vol_l; // L
39                         *buffer++ += signal ? vol_r : -vol_r; // R
40                         if((count -= 1024) < 0) {
41                                 count += diff;
42                                 signal = !signal;
43                         }
44                 }
45         }
46 }
47
48 void BEEP::set_volume(int ch, int decibel_l, int decibel_r)
49 {
50         volume_l = decibel_to_volume(decibel_l);
51         volume_r = decibel_to_volume(decibel_r);
52 }
53
54 void BEEP::initialize_sound(int rate, double frequency, int volume)
55 {
56         gen_rate = rate;
57         gen_vol = volume;
58         set_frequency(frequency);
59 }
60
61 void BEEP::set_frequency(double frequency)
62 {
63         diff = (int)(1024.0 * gen_rate / frequency / 2.0 + 0.5);
64 }
65
66 #define STATE_VERSION   1
67
68 bool BEEP::process_state(FILEIO* state_fio, bool loading)
69 {
70         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
71                 return false;
72         }
73         if(!state_fio->StateCheckInt32(this_device_id)) {
74                 return false;
75         }
76         state_fio->StateBool(signal);
77         state_fio->StateInt32(count);
78         state_fio->StateBool(on);
79         state_fio->StateBool(mute);
80         return true;
81 }