OSDN Git Service

9412f564dff4096a8381d0ac0971d069db3fa2c3
[csp-qt/common_source_project-fm7.git] / source / src / vm / datarec.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.18 -
6
7         [ data recorder ]
8 */
9
10 #include "datarec.h"
11 //#include "event.h"
12 #include "noise.h"
13
14 #define EVENT_SIGNAL    0
15 #define EVENT_SOUND     1
16
17 //#ifndef DATAREC_FAST_FWD_SPEED
18 //#define DATAREC_FAST_FWD_SPEED        10
19 //#endif
20 //#ifndef DATAREC_FAST_REW_SPEED
21 //#define DATAREC_FAST_REW_SPEED        10
22 //#endif
23
24 void DATAREC::initialize()
25 {
26         DEVICE::initialize();
27         __DATAREC_SOUND = osd->check_feature(_T("DATAREC_SOUND"));
28         if(osd->check_feature(_T("DATAREC_PCM_VOLUME"))) {
29                 pcm_max_vol = (int)osd->get_feature_int_value(_T("DATAREC_PCM_VOLUME"));
30         }
31         __DATAREC_SOUND_LEFT  = osd->check_feature(_T("DATAREC_SOUND_LEFT"));
32         __DATAREC_SOUND_RIGHT = osd->check_feature(_T("DATAREC_SOUND_RIGHT"));
33
34         __DATAREC_FAST_FWD_SPEED = 10.0;
35         if(osd->check_feature(_T("DATAREC_FAST_FWD_SPEED"))) {
36                 __DATAREC_FAST_FWD_SPEED = osd->get_feature_double_value(_T("DATAREC_FAST_FWD_SPEED"));
37         }
38         __DATAREC_FAST_REW_SPEED = 10.0;
39         if(osd->check_feature(_T("DATAREC_FAST_REW_SPEED"))) {
40                 __DATAREC_FAST_REW_SPEED = osd->get_feature_double_value(_T("DATAREC_FAST_REW_SPEED"));
41         }
42         __TYPE_MZ80B = false;
43         if(osd->check_feature(_T("_MZ80B")))  __TYPE_MZ80B = true;
44         if(osd->check_feature(_T("_MZ2000"))) __TYPE_MZ80B = true;
45         if(osd->check_feature(_T("_MZ2200"))) __TYPE_MZ80B = true;
46         play_fio = new FILEIO();
47         rec_fio = new FILEIO();
48         
49         memset(rec_file_path, 0x00, sizeof(rec_file_path));
50         play = rec = remote = trigger = false;
51         ff_rew = 0;
52         in_signal = out_signal = false;
53         register_id = -1;
54         realtime = false;
55         
56         buffer = buffer_bak = NULL;
57 //#ifdef DATAREC_SOUND
58         sound_buffer = NULL;
59 //#endif
60         apss_buffer = NULL;
61         buffer_ptr = buffer_length = 0;
62         is_wav = is_tap = is_t77 = false;
63         ave_hi_freq = 0;
64         
65         pcm_changed = 0;
66         pcm_last_vol_l = pcm_last_vol_r = 0;
67 //#ifdef DATAREC_SOUND
68         sound_last_vol_l = sound_last_vol_r = 0;
69 //#endif
70         
71         // initialize noise
72         if(d_noise_play != NULL) {
73                 d_noise_play->set_device_name(_T("Noise Player (CMT Play)"));
74                 d_noise_play->load_wav_file(_T("RELAY_ON.WAV"));
75                 d_noise_play->set_mute(!config.sound_noise_cmt);
76         }
77         if(d_noise_stop != NULL) {
78                 d_noise_stop->set_device_name(_T("Noise Player (CMT Stop)"));
79                 d_noise_stop->load_wav_file(_T("RELAYOFF.WAV"));
80                 d_noise_stop->set_mute(!config.sound_noise_cmt);
81         }
82         if(d_noise_fast != NULL) {
83                 d_noise_fast->set_device_name(_T("Noise Player (CMT Fast)"));
84                 d_noise_fast->load_wav_file(_T("FAST_FWD.WAV"));
85                 d_noise_fast->set_loop(true);
86                 d_noise_fast->set_mute(!config.sound_noise_cmt);
87         }
88         
89         // skip frames
90         signal_changed = 0;
91         register_frame_event(this);
92 }
93
94 void DATAREC::reset()
95 {
96         touch_sound();
97         // TRY: Not close even resetting.20180630 K.O
98         // ToDo: Resume tape-counter after re-initializing VM.
99         //      close_tape();
100         set_remote(false);
101
102         update_event();
103         
104         write_signals(&outputs_ear, 0);
105         in_signal = false;
106
107         pcm_prev_clock = get_current_clock();
108         pcm_positive_clocks = pcm_negative_clocks = 0;
109
110 }
111
112 void DATAREC::release()
113 {
114         close_file();
115         delete play_fio;
116         delete rec_fio;
117         DEVICE::release();
118 }
119
120 void DATAREC::write_signal(int id, uint32_t data, uint32_t mask)
121 {
122         bool signal = ((data & mask) != 0);
123         
124         if(id == SIG_DATAREC_MIC) {
125                 if(out_signal != signal) {
126                         touch_sound();
127                         if(rec && remote) {
128                                 if(out_signal) {
129                                         pcm_positive_clocks += get_passed_clock(pcm_prev_clock);
130                                 } else {
131                                         pcm_negative_clocks += get_passed_clock(pcm_prev_clock);
132                                 }
133                                 pcm_prev_clock = get_current_clock();
134                                 pcm_changed = 2;
135                                 signal_changed++;
136                         }
137                         if(prev_clock != 0) {
138                                 if(out_signal) {
139                                         positive_clocks += get_passed_clock(prev_clock);
140                                 } else {
141                                         negative_clocks += get_passed_clock(prev_clock);
142                                 }
143                                 prev_clock = get_current_clock();
144                         }
145                         out_signal = signal;
146                 }
147         } else if(id == SIG_DATAREC_REMOTE) {
148                 touch_sound();
149                 set_remote(signal);
150         } else if(id == SIG_DATAREC_TRIG) {
151                 // L->H: remote signal is switched
152                 if(signal && !trigger) {
153                         touch_sound();
154                         set_remote(!remote);
155                 }
156                 trigger = signal;
157         }
158 }
159
160 void DATAREC::event_frame()
161 {
162         if(pcm_changed) {
163                 pcm_changed--;
164         }
165 //#ifdef DATAREC_SOUND
166         if(__DATAREC_SOUND) {
167                 if(remote && (play || rec) && ff_rew == 0 && signal_changed > 10 && !config.sound_play_tape && sound_sample == 0) {
168                         request_skip_frames();
169                 }
170         } else {
171 //#else
172                 if(remote && (play || rec) && ff_rew == 0 && signal_changed > 10 && !config.sound_play_tape) {
173 //#endif
174                         request_skip_frames();
175                 }
176         }
177         signal_changed = 0;
178 }
179
180 void DATAREC::event_callback(int event_id, int err)
181 {
182         if(event_id == EVENT_SIGNAL) {
183                 if(play) {
184                         if(ff_rew > 0) {
185                                 my_stprintf_s(message, 1024, _T("Fast Forward (%d %%)"), get_tape_position());
186                         } else if(ff_rew < 0) {
187                                 my_stprintf_s(message, 1024, _T("Fast Rewind (%d %%)"), get_tape_position());
188                         } else {
189                                 my_stprintf_s(message, 1024, _T("Play (%d %%)"), get_tape_position());
190                         }
191                         bool signal = in_signal;
192                         if(is_wav) {
193                                 if(buffer_ptr >= 0 && buffer_ptr < buffer_length) {
194                                         if(buffer != NULL) {
195                                                 signal = ((buffer[buffer_ptr] & 0x80) != 0);
196                                         } else {
197                                                 signal = false;
198                                         }
199 //#ifdef DATAREC_SOUND
200                                         if(__DATAREC_SOUND) {
201                                                 if(sound_buffer != NULL && ff_rew == 0) {
202                                                         sound_sample = sound_buffer[buffer_ptr];
203                                                 } else {
204                                                         sound_sample = 0;
205                                                 }
206                                         }
207 //#endif
208                                 }
209                                 if(ff_rew < 0) {
210                                         if((buffer_ptr = max(buffer_ptr - 1, 0)) == 0) {
211                                                 set_remote(false);      // top of tape
212                                                 signal = false;
213                                         }
214                                 } else {
215                                         if((buffer_ptr = min(buffer_ptr + 1, buffer_length)) == buffer_length) {
216                                                 set_remote(false);      // end of tape
217                                                 signal = false;
218                                         }
219                                 }
220                                 update_event();
221                         } else {
222                                 if(ff_rew < 0) {
223                                         if((buffer_bak != NULL) && (buffer != NULL)) {
224                                                 my_memcpy(buffer, buffer_bak, buffer_length);
225                                         }
226                                         buffer_ptr = 0;
227                                         set_remote(false);      // top of tape
228                                 } else {
229                                         if(buffer != NULL) {
230                                                 while(buffer_ptr < buffer_length) {
231                                                         if((buffer[buffer_ptr] & 0x7f) == 0) {
232                                                                 if(++buffer_ptr == buffer_length) {
233                                                                         set_remote(false);      // end of tape
234                                                                         signal = false;
235                                                                         break;
236                                                                 }
237                                                                 signal = ((buffer[buffer_ptr] & 0x80) != 0);
238                                                         } else {
239                                                                 signal = ((buffer[buffer_ptr] & 0x80) != 0);
240                                                                 uint8_t tmp = buffer[buffer_ptr];
241                                                                 buffer[buffer_ptr] = (tmp & 0x80) | ((tmp & 0x7f) - 1);
242                                                                 break;
243                                                         }
244                                                 }
245                                         } else {
246                                                 set_remote(false);      // end of tape
247                                                 signal = false;
248                                         }
249                                 }
250                         }
251                         // notify the signal is changed
252                         if(signal != in_signal) {
253                                 if(in_signal) {
254                                         pcm_positive_clocks += get_passed_clock(pcm_prev_clock);
255                                 } else {
256                                         pcm_negative_clocks += get_passed_clock(pcm_prev_clock);
257                                 }
258                                 pcm_prev_clock = get_current_clock();
259                                 pcm_changed = 2;
260                                 in_signal = signal;
261                                 signal_changed++;
262                                 touch_sound();
263                                 write_signals(&outputs_ear, in_signal ? 0xffffffff : 0);
264                         }
265                         // chek apss state
266                         if(apss_buffer != NULL) {
267                                 int ptr = (apss_ptr++) % (sample_rate * 2);
268                                 if(apss_buffer[ptr]) {
269                                         apss_count--;
270                                 }
271                                 if(in_signal) {
272                                         apss_count++;
273                                 }
274                                 apss_buffer[ptr] = in_signal;
275                                 
276                                 if(apss_ptr >= sample_rate * 2) {
277                                         double rate = (double)apss_count / (double)(sample_rate * 2);
278                                         if(rate > 0.9 || rate < 0.1) {
279                                                 if(apss_signals) {
280                                                         if(apss_remain > 0) {
281                                                                 apss_remain--;
282                                                         } else if(apss_remain < 0) {
283                                                                 apss_remain++;
284                                                         }
285                                                         write_signals(&outputs_apss, 0xffffffff);
286                                                         apss_signals = false;
287                                                 }
288                                         } else {
289                                                 if(!apss_signals) {
290                                                         write_signals(&outputs_apss, 0);
291                                                         apss_signals = true;
292                                                 }
293                                         }
294                                 }
295                         }
296                 } else if(rec && buffer != NULL) {
297                         if(out_signal) {
298                                 positive_clocks += get_passed_clock(prev_clock);
299                         } else {
300                                 negative_clocks += get_passed_clock(prev_clock);
301                         }
302                         if(is_wav) {
303                                 if(positive_clocks != 0 || negative_clocks != 0) {
304                                         buffer[buffer_ptr] = (255 * positive_clocks) / (positive_clocks + negative_clocks);
305                                 } else {
306                                         buffer[buffer_ptr] = 0;
307                                 }
308                                 if(++buffer_ptr >= buffer_length) {
309                                         if(is_tap) {
310                                                 for(int i = 0; i < buffer_length; i += 8) {
311                                                         uint8_t val = 0;
312                                                         for(int j = 0, bit = 0x80; j < 8; j++, bit >>= 1) {
313                                                                 if(i + j < buffer_length && buffer[i + j] >= 0x80) {
314                                                                         val |= bit;
315                                                                 }
316                                                         }
317                                                         rec_fio->FputUint8(val);
318                                                 }
319                                         } else {
320                                                 rec_fio->Fwrite(buffer, buffer_length, 1);
321                                                 rec_fio->Fflush();
322                                         }
323                                         buffer_ptr = 0;
324                                 }
325                         } else if(is_t77) {
326                                 bool signal = (positive_clocks > negative_clocks);
327                                 uint16_t data = (buffer[buffer_ptr] << 8) | buffer[buffer_ptr + 1];
328                                 if(signal != ((data & 0x8000) != 0)) {
329                                         if((buffer_ptr += 2) >= buffer_length) {
330                                                 rec_fio->Fwrite(buffer, buffer_length, 1);
331                                                 rec_fio->Fflush();
332                                                 buffer_ptr = 0;
333                                         }
334                                         data = signal ? 0x8001 : 0x0001;
335                                 } else if((data & 0x7fff) < 0x7fff) {
336                                         data++;
337                                 }
338                                 buffer[buffer_ptr + 0] = data >> 8;
339                                 buffer[buffer_ptr + 1] = data & 0xff;
340                         } else {
341                                 bool prev_signal = ((buffer[buffer_ptr] & 0x80) != 0);
342                                 bool cur_signal = (positive_clocks > negative_clocks);
343                                 if(prev_signal != cur_signal || (buffer[buffer_ptr] & 0x7f) == 0x7f) {
344                                         if(++buffer_ptr >= buffer_length) {
345                                                 rec_fio->Fwrite(buffer, buffer_length, 1);
346                                                 rec_fio->Fflush();
347                                                 buffer_ptr = 0;
348                                         }
349                                         buffer[buffer_ptr] = cur_signal ? 0x80 : 0;
350                                 }
351                                 buffer[buffer_ptr]++;
352                         }
353                         prev_clock = get_current_clock();
354                         positive_clocks = negative_clocks = 0;
355                 }
356         }
357 }
358
359 void DATAREC::set_remote(bool value)
360 {
361         if(remote != value) {
362                 if(value) {
363                         if(d_noise_play != NULL) {
364                                 d_noise_play->play();
365                         }
366                         if(d_noise_fast != NULL && ff_rew != 0) {
367                                 d_noise_fast->play();
368                         }
369                 } else {
370                         if(d_noise_stop != NULL) {
371                                 d_noise_stop->play();
372                         }
373                         if(d_noise_fast != NULL) {
374                                 d_noise_fast->stop();
375                         }
376                 }
377                 remote = value;
378                 update_event();
379         }
380 }
381
382 void DATAREC::set_ff_rew(int value)
383 {
384         if(ff_rew != value) {
385                 if(register_id != -1) {
386                         cancel_event(this, register_id);
387                         register_id = -1;
388                 }
389                 if(value != 0) {
390                         if(d_noise_fast != NULL && remote) {
391                                 d_noise_fast->play();
392                         }
393                 } else {
394                         if(d_noise_fast != NULL) {
395                                 d_noise_fast->stop();
396                         }
397                 }
398                 ff_rew = value;
399                 apss_signals = false;
400                 update_event();
401         }
402 }
403
404 bool DATAREC::do_apss(int value)
405 {
406         bool result = false;
407         
408         if(play) {
409                 set_ff_rew(0);
410                 set_remote(true);
411                 set_ff_rew(value > 0 ? 1 : -1);
412                 apss_remain = value;
413                 
414                 while(apss_remain != 0 && remote) {
415                         event_callback(EVENT_SIGNAL, 0);
416                 }
417                 result = (apss_remain == 0);
418         }
419         
420         // stop cmt
421         set_remote(false);
422         set_ff_rew(0);
423         
424         if(value > 0) {
425                 if(play) {
426                         my_stprintf_s(message, 1024, _T("APSS Forward (%d %%)"), get_tape_position());
427                 } else {
428                         my_stprintf_s(message, 1024, _T("APSS Forward"));
429                 }
430         } else {
431                 if(play) {
432                         my_stprintf_s(message, 1024, _T("APSS Rewind (%d %%)"), get_tape_position());
433                 } else {
434                         my_stprintf_s(message, 1024, _T("APSS Rewind"));
435                 }
436         }
437         return result;
438 }
439
440 void DATAREC::update_event()
441 {
442         if(remote && (play || rec)) {
443                 if(register_id == -1) {
444                         if(ff_rew > 0) {
445                                 register_event(this, EVENT_SIGNAL, sample_usec / __DATAREC_FAST_FWD_SPEED, true, &register_id);
446                         } else if(ff_rew < 0) {
447                                 register_event(this, EVENT_SIGNAL, sample_usec / __DATAREC_FAST_REW_SPEED, true, &register_id);
448                         } else {
449                                 if(rec) {
450                                         my_stprintf_s(message, 1024, _T("Record"));
451                                 }
452                                 register_event(this, EVENT_SIGNAL, sample_usec, true, &register_id);
453                         }
454                         prev_clock = get_current_clock();
455                         positive_clocks = negative_clocks = 0;
456                 }
457         } else {
458                 if(register_id != -1) {
459                         cancel_event(this, register_id);
460                         register_id = -1;
461                         if(play) {
462                                 if(buffer_ptr >= buffer_length) {
463                                         my_stprintf_s(message, 1024, _T("Stop (End-of-Tape)"));
464                                 } else if(buffer_ptr <= 0) {
465                                         my_stprintf_s(message, 1024, _T("Stop (Beginning-of-Tape)"));
466                                 } else {
467                                         my_stprintf_s(message, 1024, _T("Stop (%d %%)"), get_tape_position());
468                                 }
469                         } else {
470                                 my_stprintf_s(message, 1024, _T("Stop"));
471                         }
472                 }
473                 prev_clock = 0;
474         }
475         
476         // update signals
477 //#ifdef DATAREC_SOUND
478         if(__DATAREC_SOUND) {
479                 if(!(play && remote)) {
480                         sound_sample = 0;
481                 }
482         }
483 //#endif
484         write_signals(&outputs_remote, remote ? 0xffffffff : 0);
485         write_signals(&outputs_rotate, (register_id != -1) ? 0xffffffff : 0);
486         write_signals(&outputs_end, (buffer_ptr == buffer_length) ? 0xffffffff : 0);
487         write_signals(&outputs_top, (buffer_ptr == 0) ? 0xffffffff : 0);
488         
489         update_realtime_render();
490 }
491
492 void DATAREC::update_realtime_render()
493 {
494         bool value = (remote && (play || rec) && ff_rew == 0 && config.sound_play_tape);
495         
496         if(realtime != value) {
497                 set_realtime_render(this, value);
498                 realtime = value;
499         }
500 }
501
502 bool DATAREC::play_tape(const _TCHAR* file_path)
503 {
504         close_tape();
505         
506         if(play_fio->Fopen(file_path, FILEIO_READ_BINARY)) {
507                 if(check_file_extension(play_fio->FilePath(), _T(".wav")) || check_file_extension(play_fio->FilePath(), _T(".mti"))) {
508                         // standard PCM wave file
509                         if((buffer_length = load_wav_image(0)) != 0) {
510                                 play = is_wav = true;
511                         }
512                 } else if(check_file_extension(play_fio->FilePath(), _T(".t77"))) {
513                         // FUJITSU FM-7 series tape image
514                         if((buffer_length = load_t77_image()) != 0) {
515                                 buffer = (uint8_t *)malloc(buffer_length);
516                                 load_t77_image();
517                                 play = is_wav = true;
518                         }
519                 } else if(check_file_extension(play_fio->FilePath(), _T(".tap"))) {
520                         // SHARP X1 series tape image
521                         if((buffer_length = load_tap_image()) != 0) {
522                                 buffer = (uint8_t *)malloc(buffer_length);
523                                 load_tap_image();
524                                 play = is_wav = true;
525                         }
526                 } else if(check_file_extension(play_fio->FilePath(), _T(".mzt")) || check_file_extension(play_fio->FilePath(), _T(".mzf")) || check_file_extension(play_fio->FilePath(), _T(".m12"))) {
527                         // SHARP MZ series tape image
528                         if((buffer_length = load_mzt_image()) != 0) {
529                                 buffer = (uint8_t *)malloc(buffer_length);
530                                 load_mzt_image();
531                                 play = is_wav = true;
532                         }
533                 } else if(check_file_extension(play_fio->FilePath(), _T(".mtw"))) {
534                         // skip mzt image
535                         uint8_t header[128];
536                         play_fio->Fread(header, sizeof(header), 1);
537                         uint16_t size = header[0x12] | (header[0x13] << 8);
538                         // load standard PCM wave file
539                         if((buffer_length = load_wav_image(sizeof(header) + size)) != 0) {
540                                 play = is_wav = true;
541                         }
542                 } else if(check_file_extension(play_fio->FilePath(), _T(".p6"))) {
543                         // NEC PC-6001/6601 series tape image
544                         if((buffer_length = load_p6_image(false)) != 0) {
545                                 buffer = (uint8_t *)malloc(buffer_length);
546                                 load_p6_image(false);
547                                 play = is_wav = true;
548                         }
549                 } else if(check_file_extension(play_fio->FilePath(), _T(".p6t"))) {
550                         // NEC PC-6001/6601 series tape image
551                         if((buffer_length = load_p6_image(true)) != 0) {
552                                 buffer = (uint8_t *)malloc(buffer_length);
553                                 load_p6_image(true);
554                                 play = is_wav = true;
555                         }
556                 } else if(check_file_extension(play_fio->FilePath(), _T(".bin"))) {
557                         // HITACH BASIC Master Jr tape image (bm2)
558                         if((buffer_length = load_bmjr_image()) != 0) {
559                                 buffer = (uint8_t *)malloc(buffer_length);
560                                 load_bmjr_image();
561                                 play = is_wav = true;
562                         }
563                 } else if(check_file_extension(play_fio->FilePath(), _T(".cas"))) {
564                         // standard cas image for my emulator
565                         if((buffer_length = load_cas_image()) != 0) {
566                                 buffer = (uint8_t *)malloc(buffer_length);
567                                 load_cas_image();
568                                 play = is_wav = true;
569                         }
570                 }
571                 play_fio->Fclose();
572         }
573         if(play) {
574                 if(!is_wav && buffer_length != 0) {
575                         buffer_bak = (uint8_t *)malloc(buffer_length);
576                         my_memcpy(buffer_bak, buffer, buffer_length);
577                 }
578                 
579                 // get the first signal
580                 bool signal = ((buffer[0] & 0x80) != 0);
581                 if(signal != in_signal) {
582                         touch_sound();
583                         write_signals(&outputs_ear, signal ? 0xffffffff : 0);
584                         in_signal = signal;
585                 }
586                 
587                 // initialize apss
588                 apss_buffer_length = sample_rate * 2;
589                 apss_buffer = (bool *)calloc(apss_buffer_length, 1);
590                 apss_ptr = apss_count = 0;
591                 apss_signals = false;
592                 write_signals(&outputs_apss, 0);
593                 
594                 update_event();
595         }
596         return play;
597 }
598
599 bool DATAREC::rec_tape(const _TCHAR* file_path)
600 {
601         close_tape();
602         
603         if(rec_fio->Fopen(file_path, FILEIO_READ_WRITE_NEW_BINARY)) {
604                 my_tcscpy_s(rec_file_path, _MAX_PATH, file_path);
605                 sample_rate = 48000;
606                 sample_usec = 1000000. / sample_rate;
607                 buffer_length = 1024 * 1024;
608                 buffer = (uint8_t *)calloc(buffer_length, 1);
609                 
610                 if(check_file_extension(file_path, _T(".wav"))) {
611                         // write wave header
612                         write_dummy_wav_header((void *)rec_fio);
613                         is_wav = true;
614                 } else if(check_file_extension(file_path, _T(".tap"))) {
615                         // write frequency
616                         rec_fio->FputUint32((uint32_t)sample_rate);
617                         is_wav = is_tap = true;
618                 } else if(check_file_extension(file_path, _T(".t77"))) {
619                         // 9us/sample
620                         sample_usec = 9;
621                         sample_rate = (int)(1000000.0 / sample_usec + 0.5);
622                         // write header
623                         rec_fio->Fwrite((void *)("XM7 TAPE IMAGE 0"), 16, 1);
624                         rec_fio->FputUint16(0); // marker
625                         is_t77 = true;
626                         // initialize buffer
627                         buffer[0] = out_signal ? 0x80 : 0;
628                         buffer[1] = 0;
629                 } else {
630                         // initialize buffer
631                         buffer[0] = out_signal ? 0x80 : 0;
632                 }
633                 rec = true;
634                 update_event();
635         }
636         return rec;
637 }
638
639 void DATAREC::close_tape()
640 {
641         touch_sound();
642         set_remote(false);
643         if(register_id >= 0) {
644                 cancel_event(this, register_id);
645                 register_id = -1;
646         }
647         close_file();
648         
649         play = rec = is_wav = is_tap = is_t77 = false;
650         buffer_ptr = buffer_length = 0;
651         ave_hi_freq = 0;
652         
653         update_event();
654         
655         // no sounds
656         write_signals(&outputs_ear, 0);
657         in_signal = false;
658 }
659
660 void DATAREC::close_file()
661 {
662         if(play_fio->IsOpened()) {
663                 play_fio->Fclose();
664         }
665         if(rec_fio->IsOpened()) {
666                 if(rec) {
667                         if(is_tap) {
668                                 for(int i = 0; i < buffer_ptr; i += 8) {
669                                         uint8_t val = 0;
670                                         for(int j = 0, bit = 0x80; j < 8; j++, bit >>= 1) {
671                                                 if(i + j < buffer_ptr && buffer[i + j] >= 0x80) {
672                                                         val |= bit;
673                                                 }
674                                         }
675                                         rec_fio->FputUint8(val);
676                                 }
677                         } else if(is_t77) {
678                                 rec_fio->Fwrite(buffer, buffer_ptr + 2, 1);
679                         } else if(is_wav) {
680                                 save_wav_image();
681                         } else {
682                                 rec_fio->Fwrite(buffer, buffer_ptr + 1, 1);
683                         }
684                 }
685                 rec_fio->Fflush();
686                 rec_fio->Fclose();
687         }
688         if(buffer != NULL) {
689                 free(buffer);
690                 buffer = NULL;
691         }
692         if(buffer_bak != NULL) {
693                 free(buffer_bak);
694                 buffer_bak = NULL;
695         }
696 //#ifdef DATAREC_SOUND
697         if(__DATAREC_SOUND) {
698                 if(sound_buffer != NULL) {
699                         free(sound_buffer);
700                         sound_buffer = NULL;
701                 }
702         }
703 //#endif
704         if(apss_buffer != NULL) {
705                 free(apss_buffer);
706                 apss_buffer = NULL;
707         }
708 }
709
710 // standard PCM wave file
711
712 void adjust_zero_position(int16_t *wav_buffer, int samples, int sample_rate)
713 {
714         int16_t *zero_buffer = (int16_t *)malloc(samples * sizeof(int16_t));
715         int width = (int)((double)sample_rate / 1000.0 + 0.5);
716         
717         for(int i = width; i < samples - width; i++) {
718                 int max_sample = -65536, min_sample = 65536;
719                 for(int j = -width; j < width; j++) {
720                         if(max_sample < (int)wav_buffer[i + j]) max_sample = (int)wav_buffer[i + j];
721                         if(min_sample > (int)wav_buffer[i + j]) min_sample = (int)wav_buffer[i + j];
722                 }
723                 if(max_sample - min_sample > 4096) {
724                         zero_buffer[i] = (int16_t)((max_sample + min_sample) / 2);
725                 } else {
726                         zero_buffer[i] = wav_buffer[i];
727                 }
728         }
729         for(int i = 0; i < samples; i++) {
730                 wav_buffer[i] -= zero_buffer[(i < width) ? width : (i < samples - width) ? i : (samples - width - 1)];
731         }
732         free(zero_buffer);
733 }
734 int DATAREC::load_wav_image(int offset)
735 {
736         // check wave header
737         wav_header_t header;
738         wav_chunk_t chunk;
739         pair32_t tmpval32;
740         pair16_t tmpval16;
741         
742         pair16_t __fmt_id;
743         pair16_t __sample_bits;
744         pair16_t __channels;
745         pair32_t __sample_rate;
746         pair32_t __chunk_size;
747         
748         play_fio->Fseek(offset, FILEIO_SEEK_SET);
749         play_fio->Fread(&header, sizeof(header), 1);
750         
751         __fmt_id.set_2bytes_le_from(header.format_id);
752         __sample_bits.set_2bytes_le_from(header.sample_bits);
753         
754         if((__fmt_id.u16 != 1) || !((__sample_bits.u16 == 8) || (__sample_bits.u16 == 16))) {
755                 return 0;
756         }
757         tmpval32.set_4bytes_le_from(header.fmt_chunk.size);
758         play_fio->Fseek(tmpval32.d - 16, FILEIO_SEEK_CUR);
759         while(1) {
760                 play_fio->Fread(&chunk, sizeof(chunk), 1);
761                 __chunk_size.set_4bytes_le_from(chunk.size);
762                 if(strncmp(chunk.id, "data", 4) == 0) {
763                         break;
764                 }
765
766                 play_fio->Fseek(tmpval32.d, FILEIO_SEEK_CUR);
767         }
768
769         __channels.set_2bytes_le_from(header.channels);
770         
771         int samples = (int)(__chunk_size.d / (uint32_t)(__channels.u16)), loaded_samples = 0;
772
773         if(__sample_bits.u16 == 16) {
774                 samples /= 2;
775         }
776         __sample_rate.set_4bytes_le_from(header.sample_rate);
777         sample_rate = __sample_rate.d;
778         sample_usec = 1000000. / (float)sample_rate;
779         
780         // load samples
781         if(samples > 0) {
782                 #define TMP_LENGTH (0x10000 * (uint32_t)(__channels.u16))
783                 
784                 uint8_t *tmp_buffer = (uint8_t *)malloc(TMP_LENGTH);
785                 play_fio->Fread(tmp_buffer, TMP_LENGTH, 1);
786                 
787                 #define GET_SAMPLE { \
788                         for(int ch = 0; ch < __channels.s16; ch++) { \
789                                 if(__sample_bits.u16 == 16) { \
790                                         union { \
791                                                 int16_t s16; \
792                                                 struct { \
793                                                         uint8_t l, h; \
794                                                 } b; \
795                                         } pair; \
796                                         pair.b.l = tmp_buffer[tmp_ptr++]; \
797                                         pair.b.h = tmp_buffer[tmp_ptr++]; \
798                                         sample[ch] = pair.s16; \
799                                 } else { \
800                                         sample[ch] = (tmp_buffer[tmp_ptr++] - 128) * 256; \
801                                 } \
802                         } \
803                         if(tmp_ptr == TMP_LENGTH) { \
804                                 play_fio->Fread(tmp_buffer, TMP_LENGTH, 1); \
805                                 tmp_ptr = 0; \
806                         } \
807                 }
808
809                 bool __t = false;
810                 if(__DATAREC_SOUND) {
811                    if(!config.wave_shaper[drive_num] || __channels.u16 > 1) {
812                            __t = true;
813                    }
814                 } else {
815                         if(!config.wave_shaper[drive_num]) {
816                                 __t = true;
817                         }
818                 }
819 //#ifdef DATAREC_SOUND
820 //              if(!config.wave_shaper[drive_num] || header.channels > 1) {
821 //#else
822 //              if(!config.wave_shaper[drive_num]) {
823 //#endif
824                 if(__t) {
825                         // load samples
826 //#ifdef DATAREC_SOUND
827                         if(__DATAREC_SOUND) {
828                                 if(__channels.u16 > 1) {
829                                         sound_buffer_length = samples * sizeof(int16_t);
830                                         sound_buffer = (int16_t *)malloc(sound_buffer_length);
831                                 }
832                         }
833 //#endif
834                         int16_t *wav_buffer = (int16_t *)malloc(samples * sizeof(int16_t));
835                         for(int i = 0, tmp_ptr = 0; i < samples; i++) {
836                                 int16_t sample[16];
837                                 GET_SAMPLE
838                                 int16_t sample_signal = sample[0];
839 //#ifdef DATAREC_SOUND
840                                 if(__DATAREC_SOUND) {
841                                         if(__channels.u16 > 1) {
842 //#ifdef DATAREC_SOUND_LEFT
843                                                 if(__DATAREC_SOUND_LEFT) {
844                                                         sample_signal = sample[1];
845                                                         sound_buffer[i] = sample[0];
846                                                 } else {
847 //#else
848                                                         sound_buffer[i] = sample[1];
849                                                 }
850 //#endif
851                                         }
852                                 }
853 //#endif
854                                 wav_buffer[i] = sample_signal;
855                         }
856                         adjust_zero_position(wav_buffer, samples, __sample_rate.d);
857                         
858                         // copy to dest buffer
859                         buffer = (uint8_t *)malloc(samples);
860                         bool prev_signal = false;
861                         int top_index = 0;
862                         int16_t max_sample = 0, min_sample = 0;
863                         
864                         for(int i = 0 /*, tmp_ptr = 0 */; i < samples; i++) {
865                                 int16_t sample_signal = wav_buffer[i];
866                                 bool signal = (sample_signal > 0);
867                                 
868                                 if(!prev_signal && signal) {
869                                         if(!(min_sample < -2048)) {
870                                                 for(int j = top_index; j < i; j++) {
871                                                         buffer[j] = 0;
872                                                 }
873                                         }
874                                 } else if(prev_signal && !signal) {
875                                         if(!(max_sample > 2048)) {
876                                                 for(int j = top_index; j < i; j++) {
877                                                         buffer[j] = 0;
878                                                 }
879                                         }
880                                 }
881                                 if(prev_signal != signal) {
882                                         top_index = i;
883                                         max_sample = min_sample = 0;
884                                 }
885                                 if(signal) {
886                                         if(max_sample < sample_signal) {
887                                                 max_sample = sample_signal;
888                                         }
889                                 } else {
890                                         if(min_sample > sample_signal) {
891                                                 min_sample = sample_signal;
892                                         }
893                                 }
894                                 buffer[i] = (signal ? 0xff : 0);
895                                 prev_signal = signal;
896                         }
897                         if(!prev_signal) {
898                                 if(!(min_sample < -2048)) {
899                                         for(int j = top_index; j < samples; j++) {
900                                                 buffer[j] = 0;
901                                         }
902                                 }
903                         } else {
904                                 if(!(max_sample > 2048)) {
905                                         for(int j = top_index; j < samples; j++) {
906                                                 buffer[j] = 0;
907                                         }
908                                 }
909                         }
910                         free(wav_buffer);
911                         
912                         loaded_samples = samples;
913                 } else {
914                         // load samples
915                         int16_t *wav_buffer = (int16_t *)malloc(samples * sizeof(int16_t));
916                         for(int i = 0, tmp_ptr = 0; i < samples; i++) {
917                                 int16_t sample[16];
918                                 GET_SAMPLE
919                                 wav_buffer[i] = sample[0];
920                         }
921                         adjust_zero_position(wav_buffer, samples, __sample_rate.d);
922                         
923                         // t=0 : get thresholds
924                         // t=1 : get number of samples
925                         // t=2 : load samples
926                         #define FREQ_SCALE 16
927                         int min_threshold = (int)(__sample_rate.d * FREQ_SCALE / 2400.0 / 2.0 / 3.0 + 0.5);
928                         int max_threshold = (int)(__sample_rate.d * FREQ_SCALE / 1200.0 / 2.0 * 3.0 + 0.5);
929                         int half_threshold, hi_count, lo_count;
930                         int *counts = (int *)calloc(max_threshold, sizeof(int));
931                         
932                         for(int t = 0; t < 3; t++) {
933                                 int count_positive = 0, count_negative = 0;
934                                 bool prev_signal = false;
935                                 
936                                 for(int i = 0; i < samples - 1; i++) {
937                                         int prev = wav_buffer[i], next = wav_buffer[i + 1];
938                                         double diff = (double)(next - prev) / FREQ_SCALE;
939                                         for(int j = 0; j < FREQ_SCALE; j++) {
940                                                 int sample = prev + (int)(diff * j + 0.5);
941                                                 bool signal = (sample > 0);
942                                                 
943                                                 if(!prev_signal && signal) {
944                                                         if(t == 0) {
945                                                                 if(count_positive < max_threshold && count_positive > min_threshold && count_negative > min_threshold) {
946                                                                         counts[count_positive]++;
947                                                                 }
948                                                         } else {
949                                                                 int count_p = count_positive / FREQ_SCALE;
950                                                                 int count_n = count_negative / FREQ_SCALE;
951                                                                 if(count_positive < max_threshold && count_positive > min_threshold && count_negative > min_threshold) {
952                                                                         count_p = (count_positive > half_threshold) ? hi_count : lo_count;
953                                                                         if(count_negative < max_threshold) {
954                                                                                 count_n = count_p;
955                                                                         }
956                                                                 }
957                                                                 if(buffer != NULL) {
958                                                                         for(int j = 0; j < count_p; j++) buffer[loaded_samples++] = 0xff;
959                                                                         for(int j = 0; j < count_n; j++) buffer[loaded_samples++] = 0x00;
960                                                                 } else {
961                                                                         loaded_samples += count_p + count_n;
962                                                                 }
963                                                         }
964                                                         count_positive = count_negative = 0;
965                                                 }
966                                                 if(signal) {
967                                                         count_positive++;
968                                                 } else {
969                                                         count_negative++;
970                                                 }
971                                                 prev_signal = signal;
972                                         }
973                                 }
974                                 if(t == 0) {
975                                         long sum_value = 0, sum_count = 0, half_tmp;
976                                         for(int i = 0; i < max_threshold; i++) {
977                                                 sum_value += i * counts[i];
978                                                 sum_count += counts[i];
979                                         }
980                                         // 1920 = 2400 * 0.6 + 1200 * 0.4
981                                         if(sum_count > 60 * 1920) {
982                                                 half_tmp = (int)((double)sum_value / (double)sum_count + 0.5);
983                                         } else {
984                                                 half_tmp = (int)(__sample_rate.d * FREQ_SCALE / 1920.0 / 2.0 + 0.5);
985                                         }
986                                         
987                                         sum_value = sum_count = 0;
988                                         for(int i = 0; i < half_tmp; i++) {
989                                                 sum_value += i * counts[i];
990                                                 sum_count += counts[i];
991                                         }
992                                         double lo_tmp = (double)sum_value / (double)sum_count;
993                                         
994                                         sum_value = sum_count = 0;
995                                         for(int i = half_tmp; i < half_tmp * 2; i++) {
996                                                 sum_value += i * counts[i];
997                                                 sum_count += counts[i];
998                                         }
999                                         double hi_tmp = (double)sum_value / (double)sum_count;
1000                                         
1001                                         half_threshold = (int)((lo_tmp + hi_tmp) / 2 + 0.5);
1002                                         min_threshold = (int)(2 * lo_tmp - half_threshold + 0.5);
1003                                         max_threshold = (int)(2 * hi_tmp - half_threshold + 0.5);
1004                                         lo_count = (int)(lo_tmp / FREQ_SCALE + 0.5);
1005                                         hi_count = (int)(hi_tmp / FREQ_SCALE + 0.5);
1006                                 } else {
1007                                         int count_p = count_positive / FREQ_SCALE;
1008                                         int count_n = count_negative / FREQ_SCALE;
1009                                         if(count_positive < max_threshold && count_positive > min_threshold && count_negative > min_threshold) {
1010                                                 count_p = (count_positive > half_threshold) ? hi_count : lo_count;
1011                                                 if(count_negative < max_threshold) {
1012                                                         count_n = count_p;
1013                                                 }
1014                                         }
1015                                         if(buffer != NULL) {
1016                                                 for(int j = 0; j < count_p; j++) buffer[loaded_samples++] = 0xff;
1017                                                 for(int j = 0; j < count_n; j++) buffer[loaded_samples++] = 0x00;
1018                                         } else {
1019                                                 loaded_samples += count_p + count_n;
1020                                         }
1021                                 }
1022                                 if(t == 1) {
1023                                         buffer = (uint8_t *)malloc(loaded_samples);
1024 //#ifdef DATAREC_SOUND
1025                                         if(__DATAREC_SOUND) {
1026                                                 if(__channels.u16 > 1) {
1027                                                         sound_buffer_length = loaded_samples * sizeof(int16_t);
1028                                                         sound_buffer = (int16_t *)malloc(sound_buffer_length);
1029                                                 }
1030                                         }
1031 //#endif
1032                                         loaded_samples = 0;
1033                                 }
1034                         }
1035                         free(counts);
1036                         free(wav_buffer);
1037                 }
1038                 free(tmp_buffer);
1039         }
1040         return loaded_samples;
1041 }
1042
1043
1044 void DATAREC::save_wav_image()
1045 {
1046         // write samples remained in buffer
1047         if(buffer_ptr > 0) {
1048                 rec_fio->Fwrite(buffer, buffer_ptr, 1);
1049                 rec_fio->Fflush();
1050         }
1051         uint32_t length = rec_fio->Ftell();
1052         
1053         wav_header_t wav_header;
1054         wav_chunk_t wav_chunk;
1055 #if 0
1056         pair32_t __riff_chunk_size;
1057         pair32_t __fmt_chunk_size;
1058         pair32_t __wav_chunk_size;
1059         pair16_t __fmt_id;
1060         pair16_t __channels;
1061         pair32_t __sample_rate;
1062         pair16_t __block_size;
1063         pair16_t __sample_bits;
1064
1065         __riff_chunk_size.d = length - 8;
1066         __fmt_chunk_size.d = 16;
1067         __fmt_id.u16 = 1;
1068         __channels.u16 = 1;
1069         __sample_rate.d = sample_rate;
1070         __block_size.u16 = 1;
1071         __sample_bits.u16 = 8;
1072         memcpy(wav_header.riff_chunk.id, "RIFF", 4);
1073         wav_header.riff_chunk.size = __riff_chunk_size.get_4bytes_le_to();
1074         
1075         memcpy(wav_header.wave, "WAVE", 4);
1076         memcpy(wav_header.fmt_chunk.id, "fmt ", 4);
1077         wav_header.fmt_chunk.size = __riff_chunk_size.get_4bytes_le_to();
1078         wav_header.format_id = __fmt_id.get_2bytes_le_to();
1079         wav_header.channels = __channels.get_2bytes_le_to();
1080         wav_header.sample_rate = __sample_rate.get_4bytes_le_to();
1081         wav_header.data_speed =  __sample_rate.get_4bytes_le_to();
1082         wav_header.block_size = __block_size.get_2bytes_le_to();
1083         wav_header.sample_bits = __sample_bits.get_2bytes_le_to();
1084         
1085         memcpy(wav_chunk.id, "data", 4);
1086         __wav_chunk_size.d = length - sizeof(wav_header) - sizeof(wav_chunk);
1087         wav_chunk.size = __wav_chunk_size.get_4bytes_le_to();
1088
1089         rec_fio->Fseek(0, FILEIO_SEEK_SET);
1090         rec_fio->Fwrite(&wav_header, sizeof(wav_header), 1);
1091         rec_fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
1092 #else
1093         if(set_wav_header(&wav_header, &wav_chunk, 1, sample_rate, 8, length)) {
1094                 rec_fio->Fseek(0, FILEIO_SEEK_SET);
1095                 rec_fio->Fwrite(&wav_header, sizeof(wav_header), 1);
1096                 rec_fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
1097         }
1098 #endif
1099 }
1100
1101 // FUJITSU FM-7 series tape image
1102
1103 #define T77_PUT_SIGNAL(signal, len) { \
1104         int remain = len; \
1105         while(remain > 0) { \
1106                 if(buffer != NULL) { \
1107                         buffer[ptr++] = (signal) ? 0xff : 0x00; \
1108                 } else { \
1109                         ptr++; \
1110                 } \
1111                 remain--; \
1112         } \
1113 }
1114
1115 int DATAREC::load_t77_image()
1116 {
1117         sample_usec = 9;
1118         sample_rate = (int)(1000000.0 / sample_usec + 0.5);
1119         
1120         // load t77 file
1121         uint8_t tmpbuf[17];
1122         int ptr = 0;
1123         int file_size = (int)play_fio->FileLength();
1124         
1125         if(file_size <= 0) {
1126                 return 0; // over 2GB
1127         }
1128         play_fio->Fseek(0, FILEIO_SEEK_SET);
1129         play_fio->Fread(tmpbuf, 16, 1);
1130         tmpbuf[16] = '\0';
1131         if(strcmp((char *)tmpbuf, "XM7 TAPE IMAGE 0") != 0) {
1132                 return 0;
1133         }
1134         file_size -= 16;
1135         
1136         while(file_size > 0) {
1137                 uint16_t h = play_fio->FgetUint8();
1138                 uint16_t l = play_fio->FgetUint8();
1139                 uint16_t v = h * 256 + l;
1140                 
1141                 if((file_size -= 2) < 0) {
1142                         break;
1143                 }
1144                 if(v & 0x7fff) {
1145                         T77_PUT_SIGNAL((h & 0x80) != 0, v & 0x7fff);
1146                 }
1147         }
1148         return ptr;
1149 }
1150
1151 // SHARP X1 series tape image
1152
1153 /*
1154         new tape file format for t-tune (from tape_fmt.txt)
1155
1156         offset:size :
1157         00H   :  4  : \8e¯\95Ê\83C\83\93\83f\83b\83N\83X "TAPE"
1158         04H   : 17  : \83e\81[\83v\82Ì\96¼\91O(asciiz)
1159         15H   :  5  : \83\8a\83U\81[\83u
1160         1AH   :  1  : \83\89\83C\83g\83v\83\8d\83e\83N\83g\83m\83b\83`(00H=\8f\91\82«\8d\9e\82Ý\89Â\81A10H=\8f\91\82«\8d\9e\82Ý\8bÖ\8e~\81j
1161         1BH   :  1  : \8bL\98^\83t\83H\81[\83}\83b\83g\82Ì\8eí\97Þ(01H=\92è\91¬\83T\83\93\83v\83\8a\83\93\83O\95û\96@\81j
1162         1CH   :  4  : \83T\83\93\83v\83\8a\83\93\83O\8eü\94g\90\94(\82g\82\9a\92P\88Ê\81j
1163         20H   :  4  : \83e\81[\83v\83f\81[\83^\82Ì\83T\83C\83Y\81i\83r\83b\83g\92P\88Ê\81j
1164         24H   :  4  : \83e\81[\83v\82Ì\88Ê\92u\81i\83r\83b\83g\92P\88Ê\81j
1165         28H   :  ?  : \83e\81[\83v\82Ì\83f\81[\83^
1166 */
1167
1168 int DATAREC::load_tap_image()
1169 {
1170         // get file size
1171         play_fio->Fseek(0, FILEIO_SEEK_END);
1172         //int file_size = play_fio->Ftell();
1173         play_fio->Fseek(0, FILEIO_SEEK_SET);
1174         
1175         // check header
1176         uint8_t header[4];
1177         play_fio->Fread(header, 4, 1);
1178         
1179         if(header[0] == 'T' && header[1] == 'A' && header[2] == 'P' && header[3] == 'E') {
1180                 // skip name, reserved, write protect notch
1181                 play_fio->Fseek(17 + 5 + 1, FILEIO_SEEK_CUR);
1182                 // format
1183                 if(play_fio->Fgetc() != 0x01) {
1184                         // unknown data format
1185                         return 0;
1186                 }
1187                 // sample rate
1188                 play_fio->Fread(header, 4, 1);
1189                 sample_rate = header[0] | (header[1] << 8) | (header[2] << 16) | (header[3] << 24);
1190                 sample_usec = 1000000. / sample_rate;
1191                 // data length
1192                 play_fio->Fread(header, 4, 1);
1193                 // play position
1194                 play_fio->Fread(header, 4, 1);
1195         } else {
1196                 // sample rate
1197                 sample_rate = header[0] | (header[1] << 8) | (header[2] << 16) | (header[3] << 24);
1198                 sample_usec = 1000000. / sample_rate;
1199         }
1200         
1201         // load samples
1202         int ptr = 0, data;
1203         while((data = play_fio->Fgetc()) != EOF) {
1204                 for(int i = 0, bit = 0x80; i < 8; i++, bit >>= 1) {
1205                         if(buffer != NULL) {
1206                                 buffer[ptr] = ((data & bit) != 0) ? 255 : 0;
1207                         }
1208                         ptr++;
1209                 }
1210         }
1211         return ptr;
1212 }
1213
1214 // SHARP MZ series tape image
1215
1216 //#define MZT_PUT_SIGNAL(signal, len) { \
1217 //      int remain = len; \
1218 //      while(remain > 0) { \
1219 //              if(buffer != NULL) { \
1220 //                      buffer[ptr++] = ((signal != 0) ? 0x80 : 0) | min(remain, 0x7f); \
1221 //              } else { \
1222 //                      ptr++; \
1223 //              } \
1224 //              remain -= min(remain, 0x7f); \
1225 //      } \
1226 //}
1227
1228 #define MZT_PUT_SIGNAL(signal, len) { \
1229         int remain = len; \
1230         while(remain > 0) { \
1231                 if(buffer != NULL) { \
1232                         buffer[ptr++] = (signal != 0) ? 0xff : 0; \
1233                 } else { \
1234                         ptr++; \
1235                 } \
1236                 remain--; \
1237         } \
1238 }
1239
1240 #define MZT_PUT_BIT(bit, len) { \
1241         for(int l = 0; l < (len); l++) { \
1242                 if(bit) { \
1243                         MZT_PUT_SIGNAL(1, len1); \
1244                         MZT_PUT_SIGNAL(0, len2); \
1245                 } else { \
1246                         MZT_PUT_SIGNAL(1, len3); \
1247                         MZT_PUT_SIGNAL(0, len4); \
1248                 } \
1249         } \
1250 }
1251
1252 #define MZT_PUT_BYTE(byte) { \
1253         MZT_PUT_BIT(1, 1); \
1254         for(int j = 0; j < 8; j++) { \
1255                 if((byte) & (0x80 >> j)) { \
1256                         MZT_PUT_BIT(1, 1); \
1257                         count++; \
1258                 } else { \
1259                         MZT_PUT_BIT(0, 1); \
1260                 } \
1261         } \
1262 }
1263
1264 #define MZT_PUT_BLOCK(buf, len) { \
1265         int count = 0; \
1266         for(int i = 0; i < (len); i++) { \
1267                 MZT_PUT_BYTE((buf)[i]); \
1268         } \
1269         uint8_t hi = (count >> 8) & 0xff; \
1270         uint8_t lo = (count >> 0) & 0xff; \
1271         MZT_PUT_BYTE(hi); \
1272         MZT_PUT_BYTE(lo); \
1273 }
1274
1275 int DATAREC::load_mzt_image()
1276 {
1277         int len1, len2, len3, len4;
1278         sample_rate = 48000;
1279         sample_usec = 1000000. / sample_rate;
1280         if(__TYPE_MZ80B) {
1281                 len1 = len2 = (int)(120.0 / 16.0 * sample_rate / 22050.0 + 0.5);
1282                 len3 = len4 = (int)(60.0 / 16.0 * sample_rate / 22050.0 + 0.5);
1283         } else {
1284                 len1 = (int)(24.0 * sample_rate / 48000.0 + 0.5);
1285                 len2 = (int)(29.0 * sample_rate / 48000.0 + 0.5);
1286                 len3 = (int)(11.0 * sample_rate / 48000.0 + 0.5);
1287                 len4 = (int)(15.0 * sample_rate / 48000.0 + 0.5);
1288         }
1289         
1290         
1291         // get file size
1292         play_fio->Fseek(0, FILEIO_SEEK_END);
1293         int file_size = play_fio->Ftell();
1294         play_fio->Fseek(0, FILEIO_SEEK_SET);
1295         
1296         // load mzt file
1297         int ptr = 0;
1298         while(file_size > 128) {
1299                 // load header
1300                 uint8_t header[128], ram[0x20000];
1301                 play_fio->Fread(header, sizeof(header), 1);
1302                 file_size -= sizeof(header);
1303                 
1304                 uint16_t size = header[0x12] | (header[0x13] << 8);
1305                 uint16_t offs = header[0x14] | (header[0x15] << 8);
1306                 memset(ram, 0, sizeof(ram));
1307                 play_fio->Fread(ram + offs, size, 1);
1308                 file_size -= size;
1309 //#if defined(_MZ80K) || defined(_MZ700) || defined(_MZ1200) || defined(_MZ1500)
1310 #if 0
1311                 // apply mz700win patch
1312                 if(header[0x40] == 'P' && header[0x41] == 'A' && header[0x42] == 'T' && header[0x43] == ':') {
1313                         int patch_ofs = 0x44;
1314                         for(; patch_ofs < 0x80; ) {
1315                                 uint16_t patch_addr = header[patch_ofs] | (header[patch_ofs + 1] << 8);
1316                                 patch_ofs += 2;
1317                                 if(patch_addr == 0xffff) {
1318                                         break;
1319                                 }
1320                                 int patch_len = header[patch_ofs++];
1321                                 for(int i = 0; i < patch_len; i++) {
1322                                         ram[patch_addr + i] = header[patch_ofs++];
1323                                 }
1324                         }
1325                         for(int i = 0x40; i < patch_ofs; i++) {
1326                                 header[i] = 0;
1327                         }
1328                 }
1329 #endif
1330                 // output to buffer
1331                 MZT_PUT_SIGNAL(0, sample_rate);
1332 //#if defined(_MZ80B) || defined(_MZ2000) || defined(_MZ2200)
1333                 if(__TYPE_MZ80B) {
1334                 // Bin2Wav Ver 0.03
1335                         MZT_PUT_BIT(0, 22000);
1336                         MZT_PUT_BIT(1, 40);
1337                         MZT_PUT_BIT(0, 41);
1338                         MZT_PUT_BLOCK(header, 128);
1339                         MZT_PUT_BIT(1, 1);
1340                         MZT_PUT_SIGNAL(1, (int)(22.0 * sample_rate / 22050.0 + 0.5));
1341                         MZT_PUT_SIGNAL(0, (int)(22.0 * sample_rate / 22050.0 + 0.5));
1342                         MZT_PUT_SIGNAL(0, sample_rate);
1343                         MZT_PUT_BIT(0, 11000);
1344                         MZT_PUT_BIT(1, 20);
1345                         MZT_PUT_BIT(0, 21);
1346                         MZT_PUT_BLOCK(ram + offs, size);
1347                         MZT_PUT_BIT(1, 1);
1348                 } else {
1349 //#else
1350                         // format info written in \8e\8e\8c±\82É\8fo\82éX1
1351                         MZT_PUT_BIT(0, 10000);
1352                         MZT_PUT_BIT(1, 40);
1353                         MZT_PUT_BIT(0, 40);
1354                         MZT_PUT_BIT(1, 1);
1355                         MZT_PUT_BLOCK(header, 128);
1356                         MZT_PUT_BIT(1, 1);
1357                         MZT_PUT_BIT(0, 256);
1358                         MZT_PUT_BLOCK(header, 128);
1359                         MZT_PUT_BIT(1, 1);
1360                         MZT_PUT_SIGNAL(0, sample_rate);
1361                         MZT_PUT_BIT(0, 10000);
1362                         MZT_PUT_BIT(1, 20);
1363                         MZT_PUT_BIT(0, 20);
1364                         MZT_PUT_BIT(1, 1);
1365                         MZT_PUT_BLOCK(ram + offs, size);
1366                         MZT_PUT_BIT(1, 1);
1367                 }
1368 //#endif
1369         }
1370         return ptr;
1371 }
1372
1373 // NEC PC-6001/6601 series tape image
1374
1375 #define P6_PUT_1200HZ() { \
1376         if(buffer != NULL) { \
1377                 for(int p = 0; p < 20; p++) buffer[ptr++] = 0xff; \
1378                 for(int p = 0; p < 20; p++) buffer[ptr++] = 0x00; \
1379         } else { \
1380                 ptr += 40; \
1381         } \
1382 }
1383
1384 #define P6_PUT_2400HZ() { \
1385         if(buffer != NULL) { \
1386                 for(int p = 0; p < 10; p++) buffer[ptr++] = 0xff; \
1387                 for(int p = 0; p < 10; p++) buffer[ptr++] = 0x00; \
1388         } else { \
1389                 ptr += 20; \
1390         } \
1391 }
1392
1393 #define P6_PUT_2400HZ_X2() { \
1394         if(buffer != NULL) { \
1395                 for(int p = 0; p < 10; p++) buffer[ptr++] = 0xff; \
1396                 for(int p = 0; p < 10; p++) buffer[ptr++] = 0x00; \
1397                 for(int p = 0; p < 10; p++) buffer[ptr++] = 0xff; \
1398                 for(int p = 0; p < 10; p++) buffer[ptr++] = 0x00; \
1399         } else { \
1400                 ptr += 40; \
1401         } \
1402 }
1403
1404 int DATAREC::load_p6_image(bool is_p6t)
1405 {
1406         sample_rate = 48000;
1407         sample_usec = 1000000. / sample_rate;
1408         
1409         int ptr = 0, remain = 0x10000, data;
1410         if(is_p6t) {
1411                 // get info block offset
1412                 play_fio->Fseek(-4, FILEIO_SEEK_END);
1413                 int length = play_fio->FgetInt32();
1414                 // check info block
1415                 play_fio->Fseek(length, FILEIO_SEEK_SET);
1416                 char id_p = play_fio->Fgetc();
1417                 char id_6 = play_fio->Fgetc();
1418                 uint8_t ver = play_fio->FgetUint8();
1419                 if(id_p == 'P' && id_6 == '6' && ver == 2) {
1420                         uint8_t blocks = play_fio->FgetUint8();
1421                         if(blocks >= 1) {
1422                                 play_fio->FgetUint8();
1423                                 play_fio->FgetUint8();
1424                                 play_fio->FgetUint8();
1425                                 uint16_t cmd = play_fio->FgetUint16();
1426                                 play_fio->Fseek(cmd, FILEIO_SEEK_CUR);
1427                                 uint16_t exp = play_fio->FgetUint16();
1428                                 play_fio->Fseek(exp, FILEIO_SEEK_CUR);
1429                                 // check 1st data block
1430                                 char id_t = play_fio->Fgetc();
1431                                 char id_i = play_fio->Fgetc();
1432                                 if(id_t == 'T' && id_i == 'I') {
1433                                         play_fio->FgetUint8();
1434                                         play_fio->Fseek(16, FILEIO_SEEK_CUR);
1435                                         uint16_t baud = play_fio->FgetUint16(); // 600 or 1200
1436                                         sample_rate = sample_rate * baud / 1200;
1437                                         sample_usec = 1000000. / sample_rate;
1438                                 }
1439                         }
1440                         remain = min(length, 0x10000);
1441                 }
1442         }
1443         play_fio->Fseek(0, FILEIO_SEEK_SET);
1444         
1445         for(int i = 0; i < 9600; i++) {
1446                 P6_PUT_2400HZ();
1447         }
1448         for(int i = 0; i < 16; i++) {
1449                 data = play_fio->Fgetc();
1450                 P6_PUT_1200HZ();
1451                 for(int j = 0; j < 8; j++) {
1452                         if(data & (1 << j)) {
1453                                 P6_PUT_2400HZ_X2();
1454                         } else {
1455                                 P6_PUT_1200HZ();
1456                         }
1457                 }
1458                 P6_PUT_2400HZ_X2();
1459                 P6_PUT_2400HZ_X2();
1460                 P6_PUT_2400HZ_X2();
1461                 remain--;
1462         }
1463 //      for(int i = 0; i < 1280; i++) {
1464         for(int i = 0; i < 2400; i++) {
1465                 P6_PUT_2400HZ();
1466         }
1467         while((data = play_fio->Fgetc()) != EOF && remain > 0) {
1468                 P6_PUT_1200HZ();
1469                 for(int j = 0; j < 8; j++) {
1470                         if(data & (1 << j)) {
1471                                 P6_PUT_2400HZ_X2();
1472                         } else {
1473                                 P6_PUT_1200HZ();
1474                         }
1475                 }
1476                 P6_PUT_2400HZ_X2();
1477                 P6_PUT_2400HZ_X2();
1478                 P6_PUT_2400HZ_X2();
1479                 remain--;
1480         }
1481 #if 1
1482         for(int i = 0; i < 16; i++) {
1483                 P6_PUT_1200HZ();
1484                 for(int j = 0; j < 8; j++) {
1485                         P6_PUT_1200HZ();
1486                 }
1487                 P6_PUT_2400HZ_X2();
1488                 P6_PUT_2400HZ_X2();
1489                 P6_PUT_2400HZ_X2();
1490         }
1491 #endif
1492         return ptr;
1493 }
1494
1495 // HITACH BASIC Master Jr tape image (bm2)
1496
1497 #define BMJR_PUT_1200HZ_X4() { \
1498         if(buffer != NULL) { \
1499                 for(int k = 0; k < 4; k++) { \
1500                         for(int p = 0; p < 20; p++) buffer[ptr++] = 0xff; \
1501                         for(int p = 0; p < 20; p++) buffer[ptr++] = 0x00; \
1502                 } \
1503         } else { \
1504                 ptr += 40 * 4; \
1505         } \
1506 }
1507
1508 #define BMJR_PUT_2400HZ_X8() { \
1509         if(buffer != NULL) { \
1510                 for(int k = 0; k < 8; k++) { \
1511                         for(int p = 0; p < 10; p++) buffer[ptr++] = 0xff; \
1512                         for(int p = 0; p < 10; p++) buffer[ptr++] = 0x00; \
1513                 } \
1514         } else { \
1515                 ptr += 20 * 8; \
1516         } \
1517 }
1518
1519 int DATAREC::load_bmjr_image()
1520 {
1521         sample_rate = 48000;
1522         sample_usec = 1000000. / sample_rate;
1523         
1524         play_fio->Fseek(0, FILEIO_SEEK_SET);
1525         
1526         int ptr = 0, data;
1527         while((data = play_fio->Fgetc()) != EOF) {
1528                 // start bit = 0
1529                 BMJR_PUT_1200HZ_X4();
1530                 // data bits
1531                 for(int j = 0; j < 8; j++) {
1532                         if(data & (1 << j)) {
1533                                 BMJR_PUT_2400HZ_X8();
1534                         } else {
1535                                 BMJR_PUT_1200HZ_X4();
1536                         }
1537                 }
1538                 // stop bits = 1,1
1539                 BMJR_PUT_2400HZ_X8();
1540                 BMJR_PUT_2400HZ_X8();
1541         }
1542         return ptr;
1543 }
1544
1545 // standard cas image for my emulator
1546
1547 static const uint8_t msx_cas_header[8] = {0x1f, 0xa6, 0xde, 0xba, 0xcc, 0x13, 0x7d, 0x74};
1548
1549 int DATAREC::load_cas_image()
1550 {
1551         sample_rate = 48000;
1552         sample_usec = 1000000. / sample_rate;
1553         
1554         // SORD m5 or NEC PC-6001 series cas image ?
1555         static const uint8_t momomomomomo[6] = {0xd3, 0xd3, 0xd3, 0xd3, 0xd3, 0xd3};
1556         uint8_t tmp_header[8];
1557         play_fio->Fseek(0, FILEIO_SEEK_SET);
1558         play_fio->Fread(tmp_header, sizeof(tmp_header), 1);
1559         
1560         if(memcmp(tmp_header, "SORDM5", 6) == 0) {
1561                 return load_m5_cas_image();
1562         } else if(memcmp(tmp_header, msx_cas_header, 8) == 0) {
1563                 return load_msx_cas_image();
1564         } else if(memcmp(tmp_header, momomomomomo, 6) == 0) {
1565                 return load_p6_image(false);
1566         }
1567         
1568         // this is the standard cas image for my emulator
1569         play_fio->Fseek(0, FILEIO_SEEK_SET);
1570         int ptr = 0, data;
1571         while((data = play_fio->Fgetc()) != EOF) {
1572                 for(int i = 0; i < (data & 0x7f); i++) {
1573                         if(buffer != NULL) {
1574                                 buffer[ptr] = (data & 0x80) ? 255 : 0;
1575                         }
1576                         ptr++;
1577                 }
1578         }
1579         return ptr;
1580 }
1581
1582 // SORD M5 tape image
1583
1584 #define M5_PUT_BIT(val, len) { \
1585         int remain = len; \
1586         while(remain > 0) { \
1587                 if(buffer != NULL) { \
1588                         buffer[ptr] = val ? 0 : 0xff; \
1589                 } \
1590                 ptr++; \
1591                 remain--; \
1592         } \
1593 }
1594
1595 #define M5_PUT_BYTE(data) { \
1596         for(int j = 0; j < 10; j++) { \
1597                 int bit = (j == 0) ? 1 : (j == 1) ? 0 : ((data >> (j - 2)) & 1); \
1598                 if(bit) { \
1599                         M5_PUT_BIT(0xff, 8); \
1600                         M5_PUT_BIT(0x00, 7); \
1601                 } else { \
1602                         M5_PUT_BIT(0xff, 16); \
1603                         M5_PUT_BIT(0x00, 14); \
1604                 } \
1605         } \
1606 }
1607
1608 int DATAREC::load_m5_cas_image()
1609 {
1610         play_fio->Fseek(16, FILEIO_SEEK_SET);
1611         int ptr = 0, block_type;
1612         
1613         while((block_type = play_fio->Fgetc()) != EOF) {
1614                 if(block_type != 'H' && block_type != 'D') {
1615                         return 0;
1616                 }
1617                 int block_size = play_fio->Fgetc();
1618                 
1619                 if(block_type == 'H') {
1620                         M5_PUT_BIT(0x00, 1);
1621                 }
1622                 for(int i = 0; i < (block_type == 'H' ? 945 : 59); i++) {
1623                         M5_PUT_BIT(0xff, 8);
1624                         M5_PUT_BIT(0x00, 7);
1625                 }
1626                 M5_PUT_BYTE(block_type);
1627                 M5_PUT_BYTE(block_size);
1628                 
1629                 for(int i = 0; i < ((block_size == 0) ? 0x101 : (block_size + 1)); i++) {
1630                         uint8_t data = play_fio->Fgetc();
1631                         M5_PUT_BYTE(data);
1632                 }
1633                 M5_PUT_BIT(0xff, 8);
1634                 M5_PUT_BIT(0x00, 7);
1635         }
1636         M5_PUT_BIT(0x00, 1);
1637         return ptr;
1638 }
1639
1640 // ASCII MSX tape image (fMSX)
1641 // MAME/MESS /src/lib/formats/fmsx_cas.c by Mr.Sean Young
1642
1643 #define CAS_PERIOD              (16)
1644 #define CAS_HEADER_PERIODS      (4000)
1645 #define CAS_EMPTY_PERIODS       (1000)
1646
1647 int DATAREC::load_msx_cas_image()
1648 {
1649         sample_rate = 22050;
1650         sample_usec = 1000000. / sample_rate;
1651         
1652         play_fio->Fseek(0, FILEIO_SEEK_END);
1653         int cas_size = play_fio->Ftell();
1654         uint8_t *bytes = (uint8_t *)malloc(cas_size);
1655         play_fio->Fseek(0, FILEIO_SEEK_SET);
1656         play_fio->Fread(bytes, cas_size, 1);
1657         
1658         int cas_pos, bit, state = 1, samples_pos, size, n, i, p;
1659         
1660         cas_pos = 0;
1661         samples_pos = 0;
1662         
1663         while(/*samples_pos < sample_count && */cas_pos < cas_size) {
1664                 /* Check if we need to output a header */
1665                 if(cas_pos + 8 < cas_size) {
1666                         if(!memcmp( bytes + cas_pos, msx_cas_header, 8)) {
1667                                 /* Write CAS_EMPTY_PERIODS of silence */
1668                                 n = CAS_EMPTY_PERIODS * CAS_PERIOD;
1669                                 while(n--) {
1670                                         if(buffer != NULL) {
1671                                                 buffer[samples_pos] = 0;
1672                                         }
1673                                         samples_pos++;
1674                                 }
1675                                 /* Write CAS_HEADER_PERIODS of header (high frequency) */
1676                                 for(i = 0; i < CAS_HEADER_PERIODS * 4 ; i++) {
1677                                         for(n = 0; n < CAS_PERIOD / 4; n++) {
1678                                                 if(buffer != NULL) {
1679                                                         buffer[samples_pos + n] = (state ? 0xff : 0);
1680                                                 }
1681                                         }
1682                                         samples_pos += CAS_PERIOD / 4 ;
1683                                         state = !state;
1684                                 }
1685                                 cas_pos += 8;
1686                         }
1687                 }
1688                 
1689                 for(i = 0; i <= 11; i++) {
1690                         if(i == 0) {
1691                                 bit = 0;
1692                         } else if(i < 9) {
1693                                 bit = (bytes[cas_pos] & (1 << (i - 1) ) );
1694                         } else {
1695                                 bit = 1;
1696                         }
1697                         
1698                         /* write this one bit */
1699                         for(n = 0; n < (bit ? 4 : 2); n++) {
1700                                 size = (bit ? CAS_PERIOD / 4 : CAS_PERIOD / 2);
1701                                 for(p = 0; p < size; p++) {
1702                                         if(buffer != NULL) {
1703                                                 buffer[samples_pos + p] = (state ? 0xff : 0);
1704                                         }
1705                                 }
1706                                 state = !state;
1707                                 samples_pos += size;
1708                         }
1709                 }
1710                 cas_pos++;
1711         }
1712         free(bytes);
1713         return samples_pos;
1714 }
1715
1716 void DATAREC::mix(int32_t* buffer, int cnt)
1717 {
1718         int32_t* buffer_tmp = buffer;
1719         
1720         if(config.sound_play_tape && pcm_changed && remote && (play || rec) && ff_rew == 0) {
1721                 bool signal = ((play && in_signal) || (rec && out_signal));
1722                 if(signal) {
1723                         pcm_positive_clocks += get_passed_clock(pcm_prev_clock);
1724                 } else {
1725                         pcm_negative_clocks += get_passed_clock(pcm_prev_clock);
1726                 }
1727                 int clocks = pcm_positive_clocks + pcm_negative_clocks;
1728                 int sample = clocks ? (pcm_max_vol * pcm_positive_clocks - pcm_max_vol * pcm_negative_clocks) / clocks : signal ? pcm_max_vol : -pcm_max_vol;
1729                 
1730                 pcm_last_vol_l = apply_volume(sample, pcm_volume_l);
1731                 pcm_last_vol_r = apply_volume(sample, pcm_volume_r);
1732                 
1733                 for(int i = 0; i < cnt; i++) {
1734                         *buffer++ += pcm_last_vol_l; // L
1735                         *buffer++ += pcm_last_vol_r; // R
1736                 }
1737         } else if(pcm_last_vol_l || pcm_last_vol_r) {
1738                 // suppress petite noise when go to mute
1739                 for(int i = 0; i < cnt; i++) {
1740                         *buffer++ += pcm_last_vol_l; // L
1741                         *buffer++ += pcm_last_vol_r; // R
1742                         
1743                         if(pcm_last_vol_l > 0) {
1744                                 pcm_last_vol_l--;
1745                         } else if(pcm_last_vol_l < 0) {
1746                                 pcm_last_vol_l++;
1747                         }
1748                         if(pcm_last_vol_r > 0) {
1749                                 pcm_last_vol_r--;
1750                         } else if(pcm_last_vol_r < 0) {
1751                                 pcm_last_vol_r++;
1752                         }
1753                 }
1754         }
1755         pcm_prev_clock = get_current_clock();
1756         pcm_positive_clocks = pcm_negative_clocks = 0;
1757         
1758 //#ifdef DATAREC_SOUND
1759         if(__DATAREC_SOUND) {
1760                 if(/*config.sound_play_tape && */remote && play && ff_rew == 0) {
1761                         sound_last_vol_l = apply_volume(sound_sample, sound_volume_l);
1762                         sound_last_vol_r = apply_volume(sound_sample, sound_volume_r);
1763                         buffer = buffer_tmp; // restore
1764                         for(int i = 0; i < cnt; i++) {
1765                                 *buffer += sound_last_vol_l; // L
1766                                 *buffer += sound_last_vol_r; // R
1767                         }
1768                 } else if(sound_last_vol_l || sound_last_vol_r) {
1769                         // suppress petite noise when go to mute
1770                         for(int i = 0; i < cnt; i++) {
1771                                 *buffer++ += sound_last_vol_l; // L
1772                                 *buffer++ += sound_last_vol_r; // R
1773                                 
1774                                 if(sound_last_vol_l > 0) {
1775                                         sound_last_vol_l--;
1776                                 } else if(sound_last_vol_l < 0) {
1777                                         sound_last_vol_l++;
1778                                 }
1779                                 if(sound_last_vol_r > 0) {
1780                                         sound_last_vol_r--;
1781                                 } else if(sound_last_vol_r < 0) {
1782                                         sound_last_vol_r++;
1783                                 }
1784                         }
1785                 }
1786         }
1787 //#endif
1788 }
1789
1790 void DATAREC::set_volume(int ch, int decibel_l, int decibel_r)
1791 {
1792         if(ch == 0) {
1793                 pcm_volume_l = decibel_to_volume(decibel_l);
1794                 pcm_volume_r = decibel_to_volume(decibel_r);
1795 //#ifdef DATAREC_SOUND
1796         } else if(ch == 1) {
1797                 if(__DATAREC_SOUND) {
1798                         sound_volume_l = decibel_to_volume(decibel_l);
1799                         sound_volume_r = decibel_to_volume(decibel_r);
1800                 }
1801 //#endif
1802         }
1803 }
1804
1805 double DATAREC::get_ave_hi_freq()
1806 {
1807         if(ave_hi_freq == 0 && play && is_wav) {
1808                 bool prev_signal = false;
1809                 int positive = 0, negative = 0, pulse_count = 0;
1810                 double sum = 0;
1811                 double base_usec = 1000000.0 / (double)sample_rate;
1812                 
1813                 for(int i=0; i < buffer_length; i++) {
1814                         bool next_signal = ((buffer[i] & 0x80) != 0);
1815                         if(!prev_signal && next_signal) {
1816                                 double usec = base_usec * (positive + negative);
1817                                 if(316.667 <= usec && usec < 516.667) {
1818                                         sum += usec;
1819                                         pulse_count += 1;
1820                                 } else if(633.333 <= usec && usec < 1033.333) {
1821                                         sum += usec;
1822                                         pulse_count += 2;
1823                                 }
1824                                 positive = negative = 0;
1825                         }
1826                         if(next_signal) {
1827                                 positive++;
1828                         } else {
1829                                 negative++;
1830                         }
1831                         prev_signal = next_signal;
1832                 }
1833                 if(sum != 0 && pulse_count != 0) {
1834                         double average = sum / pulse_count;
1835                         ave_hi_freq = 1000000.0 / average;
1836                 }
1837         }
1838         if(ave_hi_freq == 0) {
1839                 ave_hi_freq = 2400;
1840         }
1841         return ave_hi_freq;
1842 }
1843
1844 void DATAREC::update_config()
1845 {
1846         if(d_noise_play != NULL) {
1847                 d_noise_play->set_mute(!config.sound_noise_cmt);
1848         }
1849         if(d_noise_stop != NULL) {
1850                 d_noise_stop->set_mute(!config.sound_noise_cmt);
1851         }
1852         if(d_noise_fast != NULL) {
1853                 d_noise_fast->set_mute(!config.sound_noise_cmt);
1854         }
1855         update_realtime_render();
1856 }
1857
1858 #define STATE_VERSION   12
1859
1860 bool DATAREC::process_state(FILEIO* state_fio, bool loading)
1861 {
1862         int length_tmp;
1863
1864         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
1865                 return false;
1866         }
1867         if(!state_fio->StateCheckInt32(this_device_id)) {
1868                 return false;
1869         }
1870         // pre process
1871         if(loading) {
1872                 close_file();
1873         }
1874         state_fio->StateValue(play);
1875         state_fio->StateValue(rec);
1876         state_fio->StateValue(remote);
1877         state_fio->StateValue(trigger);
1878         state_fio->StateArray(rec_file_path, sizeof(rec_file_path), 1);
1879         if(loading) {
1880                 length_tmp = state_fio->FgetInt32_LE();
1881                 if(rec) {
1882                         rec_fio->Fopen(rec_file_path, FILEIO_READ_WRITE_NEW_BINARY);
1883                         while(length_tmp != 0) {
1884                                 uint8_t buffer_tmp[1024];
1885                                 int length_rw = min(length_tmp, (int)sizeof(buffer_tmp));
1886                                 state_fio->Fread(buffer_tmp, length_rw, 1);
1887                                 if(rec_fio->IsOpened()) {
1888                                         rec_fio->Fwrite(buffer_tmp, length_rw, 1);
1889                                 }
1890                                 length_tmp -= length_rw;
1891                         }
1892                 }
1893         } else {
1894                 if(rec && rec_fio->IsOpened()) {
1895                         length_tmp = (int)rec_fio->Ftell();
1896                         rec_fio->Fseek(0, FILEIO_SEEK_SET);
1897                         state_fio->FputInt32_LE(length_tmp);
1898                         while(length_tmp != 0) {
1899                                 uint8_t buffer_tmp[1024];
1900                                 int length_rw = min(length_tmp, (int)sizeof(buffer_tmp));
1901                                 rec_fio->Fread(buffer_tmp, length_rw, 1);
1902                                 state_fio->Fwrite(buffer_tmp, length_rw, 1);
1903                                 length_tmp -= length_rw;
1904                         }
1905                 } else {
1906                         state_fio->FputInt32_LE(0);
1907                 }
1908         }
1909         state_fio->StateInt32(ff_rew);
1910         state_fio->StateBool(in_signal);
1911         state_fio->StateBool(out_signal);
1912         state_fio->StateUint32(prev_clock);
1913         state_fio->StateInt32(positive_clocks);
1914         state_fio->StateInt32(negative_clocks);
1915         state_fio->StateInt32(signal_changed);
1916         state_fio->StateInt32(register_id);
1917         state_fio->StateBool(realtime);
1918         state_fio->StateInt32(sample_rate);
1919         state_fio->StateDouble(sample_usec);
1920         state_fio->StateInt32(buffer_ptr);
1921         if(loading) {
1922                 if((buffer_length = state_fio->FgetInt32_LE()) != 0) {
1923                         buffer = (uint8_t *)malloc(buffer_length);
1924                         state_fio->Fread(buffer, buffer_length, 1);
1925                 }
1926                 if((length_tmp = state_fio->FgetInt32_LE()) != 0) {
1927                         buffer_bak = (uint8_t *)malloc(length_tmp);
1928                         state_fio->Fread(buffer_bak, length_tmp, 1);
1929                 }
1930                 if(__DATAREC_SOUND) {
1931                         if((sound_buffer_length = state_fio->FgetInt32_LE()) != 0) {
1932                                 sound_buffer = (int16_t *)malloc(sound_buffer_length);
1933                                 state_fio->Fread(sound_buffer, sound_buffer_length, 1);
1934                         }
1935                 }
1936         } else {
1937                 if(buffer) {
1938                         state_fio->FputInt32_LE(buffer_length);
1939                         state_fio->Fwrite(buffer, buffer_length, 1);
1940                 } else {
1941                         state_fio->FputInt32_LE(0);
1942                 }
1943                 if(buffer_bak) {
1944                         state_fio->FputInt32_LE(buffer_length);
1945                         state_fio->Fwrite(buffer_bak, buffer_length, 1);
1946                 } else {
1947                         state_fio->FputInt32_LE(0);
1948                 }
1949                 if(__DATAREC_SOUND) {
1950                         if(sound_buffer) {
1951                                 state_fio->FputInt32_LE(sound_buffer_length);
1952                                 state_fio->Fwrite(sound_buffer, sound_buffer_length, 1);
1953                         } else {
1954                                 state_fio->FputInt32_LE(0);
1955                         }
1956                 }
1957         }
1958         if(__DATAREC_SOUND) {
1959                 state_fio->StateInt16(sound_sample);
1960         }
1961         state_fio->StateBool(is_wav);
1962         state_fio->StateBool(is_tap);
1963         state_fio->StateBool(is_t77);
1964         if(loading) {
1965                 if((apss_buffer_length = state_fio->FgetInt32_LE()) != 0) {
1966                         apss_buffer = (bool *)malloc(apss_buffer_length);
1967                         state_fio->Fread(apss_buffer, apss_buffer_length, 1);
1968                 }
1969         } else {
1970                 if(apss_buffer) {
1971                         state_fio->FputInt32_LE(apss_buffer_length);
1972                         state_fio->Fwrite(apss_buffer, apss_buffer_length, 1);
1973                 } else {
1974                         state_fio->FputInt32_LE(0);
1975                 }
1976         }
1977         state_fio->StateInt32(apss_ptr);
1978         state_fio->StateInt32(apss_count);
1979         state_fio->StateInt32(apss_remain);
1980         state_fio->StateBool(apss_signals);
1981         state_fio->StateInt32(pcm_changed);
1982         state_fio->StateUint32(pcm_prev_clock);
1983         state_fio->StateInt32(pcm_positive_clocks);
1984         state_fio->StateInt32(pcm_negative_clocks);
1985         
1986         // post process
1987         if(loading) {
1988                 pcm_last_vol_l = pcm_last_vol_r = 0;
1989                 if(__DATAREC_SOUND) {
1990                         sound_last_vol_l = sound_last_vol_r = 0;
1991                 }
1992         }
1993         return true;
1994 }