2 Skelton for retropc emulator
4 Author : Takeda.Toshiya
12 #define EVENT_SAMPLE 0
14 void NOISE::initialize()
19 sample_l = sample_r = 0;
24 if(buffer_l != NULL) {
28 if(buffer_r != NULL) {
39 void NOISE::event_callback(int event_id, int err)
51 void NOISE::mix(int32_t* buffer, int cnt)
53 if(register_id != -1 && !mute) {
54 int32_t val_l = apply_volume(sample_l, volume_l);
55 int32_t val_r = apply_volume(sample_r, volume_r);
57 for(int i = 0; i < cnt; i++) {
58 *buffer++ += val_l; // L
59 *buffer++ += val_r; // R
64 void NOISE::set_volume(int ch, int decibel_l, int decibel_r)
66 volume_l = decibel_to_volume(decibel_l);
67 volume_r = decibel_to_volume(decibel_r);
70 bool NOISE::load_wav_file(const _TCHAR *file_name)
76 FILEIO *fio = new FILEIO();
79 if(fio->Fopen(create_local_path(file_name), FILEIO_READ_BINARY)) {
83 fio->Fread(&header, sizeof(header), 1);
85 if(header.format_id == 1 && (header.sample_bits == 8 || header.sample_bits == 16)) {
86 fio->Fseek(header.fmt_chunk.size - 16, FILEIO_SEEK_CUR);
88 fio->Fread(&chunk, sizeof(chunk), 1);
89 if(strncmp(chunk.id, "data", 4) == 0) {
92 fio->Fseek(chunk.size, FILEIO_SEEK_CUR);
94 if((samples = chunk.size / header.channels) > 0) {
95 if(header.sample_bits == 16) {
98 sample_rate = header.sample_rate;
100 buffer_l = (int16_t *)malloc(samples * sizeof(int16_t));
101 buffer_r = (int16_t *)malloc(samples * sizeof(int16_t));
103 for(int i = 0; i < samples; i++) {
105 for(int ch = 0; ch < header.channels; ch++) {
107 if(header.sample_bits == 16) {
114 pair.b.l = fio->FgetUint8();
115 pair.b.h = fio->FgetUint8();
118 sample = (int16_t)(fio->FgetUint8());
119 sample = (sample - 128) * 256;
121 if(ch < 2) sample_lr[ch] = sample;
123 buffer_l[i] = sample_lr[0];
124 buffer_r[i] = sample_lr[(header.channels > 1) ? 1 : 0];
138 if(samples > 0 && register_id == -1 && !mute) {
140 // if(register_id == -1) {
141 register_event(this, EVENT_SAMPLE, 1000000.0 / sample_rate, true, ®ister_id);
145 set_realtime_render(this, true);
151 if(samples > 0 && register_id != -1) {
153 // if(register_id != -1) {
154 cancel_event(this, register_id);
157 sample_l = sample_r = 0;
158 set_realtime_render(this, false);
162 void NOISE::get_sample()
164 if(buffer_l != NULL && ptr < samples) {
165 sample_l = buffer_l[ptr];
169 if(buffer_r != NULL && ptr < samples) {
170 sample_r = buffer_l[ptr];
176 #define STATE_VERSION 1
178 #include "../statesub.h"
180 void NOISE::decl_state()
182 enter_decl_state(STATE_VERSION);
184 DECL_STATE_ENTRY_INT32(register_id);
185 DECL_STATE_ENTRY_INT32(ptr);
186 DECL_STATE_ENTRY_INT32(sample_l);
187 DECL_STATE_ENTRY_INT32(sample_r);
188 DECL_STATE_ENTRY_BOOL(loop);
189 DECL_STATE_ENTRY_BOOL(mute);
193 void NOISE::save_state(FILEIO* state_fio)
195 if(state_entry != NULL) {
196 state_entry->save_state(state_fio);
198 //state_fio->FputUint32(STATE_VERSION);
199 //state_fio->FputInt32(this_device_id);
201 //state_fio->FputInt32(register_id);
202 //state_fio->FputInt32(ptr);
203 //state_fio->FputInt32(sample_l);
204 //state_fio->FputInt32(sample_r);
205 //state_fio->FputBool(loop);
206 //state_fio->FputBool(mute);
209 bool NOISE::load_state(FILEIO* state_fio)
212 if(state_entry != NULL) {
213 mb = state_entry->load_state(state_fio);
215 if(!mb) return false;
216 //if(state_fio->FgetUint32() != STATE_VERSION) {
219 //if(state_fio->FgetInt32() != this_device_id) {
222 //register_id = state_fio->FgetInt32();
223 //ptr = state_fio->FgetInt32();
224 //sample_l = state_fio->FgetInt32();
225 //sample_r = state_fio->FgetInt32();
226 //loop = state_fio->FgetBool();
227 //mute = state_fio->FgetBool();