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 bool NOISE::process_state(FILEIO* state_fio, bool loading)
206 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
209 if(!state_fio->StateCheckInt32(this_device_id)) {
212 state_fio->StateInt32(register_id);
213 state_fio->StateInt32(ptr);
214 state_fio->StateInt32(sample_l);
215 state_fio->StateInt32(sample_r);
216 state_fio->StateBool(loop);
217 state_fio->StateBool(mute);