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)) {
84 pair16_t __sample_bits;
89 fio->Fread(&header, sizeof(header), 1);
90 __fmt_id.set_2bytes_le_from(header.format_id);
91 __sample_bits.set_2bytes_le_from(header.sample_bits);
92 __chunk_size.set_4bytes_le_from(header.fmt_chunk.size);
93 __channels.set_2bytes_le_from(header.channels);
94 __sample_rate.set_4bytes_le_from(header.sample_rate);
96 if((__fmt_id.w == 1) && ((__sample_bits.w == 8) || (__sample_bits.w == 16))) {
97 fio->Fseek(__chunk_size.d - 16, FILEIO_SEEK_CUR);
100 if(fio->Fread(&chunk, sizeof(chunk), 1) != 1) {
104 __chunk_size.set_4bytes_le_from(chunk.size);
105 if(strncmp(chunk.id, "data", 4) == 0) {
108 fio->Fseek(__chunk_size.d, FILEIO_SEEK_CUR);
110 __chunk_size.set_4bytes_le_from(chunk.size);
112 buffer_l = buffer_r = NULL;
117 if((samples = __chunk_size.d / __channels.w) > 0) {
118 if(__sample_bits.w == 16) {
121 sample_rate = __sample_rate.d;
123 buffer_l = (int16_t *)malloc(samples * sizeof(int16_t));
124 buffer_r = (int16_t *)malloc(samples * sizeof(int16_t));
126 for(int i = 0; i < samples; i++) {
128 for(int ch = 0; ch < __channels.sw; ch++) {
130 if(__sample_bits.w == 16) {
137 pair.b.l = fio->FgetUint8();
138 pair.b.h = fio->FgetUint8();
141 sample = (int16_t)(fio->FgetUint8());
142 sample = (sample - 128) * 256;
144 if(ch < 2) sample_lr[ch] = sample;
146 buffer_l[i] = sample_lr[0];
147 buffer_r[i] = sample_lr[(__channels.w > 1) ? 1 : 0];
153 result = load_wav_to_stereo((void *)fio, &buffer_l, &buffer_r, &sample_rate, &samples);
164 if(samples > 0 && register_id == -1 && !mute) {
166 // if(register_id == -1) {
167 register_event(this, EVENT_SAMPLE, 1000000.0 / sample_rate, true, ®ister_id);
171 set_realtime_render(this, true);
177 if(samples > 0 && register_id != -1) {
179 // if(register_id != -1) {
180 cancel_event(this, register_id);
183 sample_l = sample_r = 0;
184 set_realtime_render(this, false);
188 void NOISE::get_sample()
190 if(buffer_l != NULL && ptr < samples) {
191 sample_l = buffer_l[ptr];
195 if(buffer_r != NULL && ptr < samples) {
196 sample_r = buffer_r[ptr];
202 #define STATE_VERSION 1
204 #include "../statesub.h"
206 void NOISE::decl_state()
208 enter_decl_state(STATE_VERSION);
210 DECL_STATE_ENTRY_INT32(register_id);
211 DECL_STATE_ENTRY_INT32(ptr);
212 DECL_STATE_ENTRY_INT32(sample_l);
213 DECL_STATE_ENTRY_INT32(sample_r);
214 DECL_STATE_ENTRY_BOOL(loop);
215 DECL_STATE_ENTRY_BOOL(mute);
219 void NOISE::save_state(FILEIO* state_fio)
221 if(state_entry != NULL) {
222 state_entry->save_state(state_fio);
224 //state_fio->FputUint32(STATE_VERSION);
225 //state_fio->FputInt32(this_device_id);
227 //state_fio->FputInt32(register_id);
228 //state_fio->FputInt32(ptr);
229 //state_fio->FputInt32(sample_l);
230 //state_fio->FputInt32(sample_r);
231 //state_fio->FputBool(loop);
232 //state_fio->FputBool(mute);
235 bool NOISE::load_state(FILEIO* state_fio)
238 if(state_entry != NULL) {
239 mb = state_entry->load_state(state_fio);
241 if(!mb) return false;
242 //if(state_fio->FgetUint32() != STATE_VERSION) {
245 //if(state_fio->FgetInt32() != this_device_id) {
248 //register_id = state_fio->FgetInt32();
249 //ptr = state_fio->FgetInt32();
250 //sample_l = state_fio->FgetInt32();
251 //sample_r = state_fio->FgetInt32();
252 //loop = state_fio->FgetBool();
253 //mute = state_fio->FgetBool();