comparison ym2612.c @ 1555:6ce36c3f250b

More audio refactoring in preparation for allowing proper sync to video with dynamic audio rate control
author Michael Pavone <pavone@retrodev.com>
date Fri, 30 Mar 2018 00:37:08 -0700
parents ce1f93be0104
children 4637ab86be8c
comparison
equal deleted inserted replaced
1554:87350caf6dab 1555:6ce36c3f250b
114 wave_finalize(log_context->channels[i].logfile); 114 wave_finalize(log_context->channels[i].logfile);
115 } 115 }
116 } 116 }
117 log_context = NULL; 117 log_context = NULL;
118 } 118 }
119 #define BUFFER_INC_RES 0x40000000UL
120 119
121 void ym_adjust_master_clock(ym2612_context * context, uint32_t master_clock) 120 void ym_adjust_master_clock(ym2612_context * context, uint32_t master_clock)
122 { 121 {
123 uint64_t old_inc = context->buffer_inc; 122 render_audio_adjust_clock(context->audio, master_clock, context->clock_inc * NUM_OPERATORS);
124 context->buffer_inc = ((BUFFER_INC_RES * (uint64_t)context->sample_rate) / (uint64_t)master_clock) * (uint64_t)context->clock_inc * NUM_OPERATORS;
125 } 123 }
126 124
127 #ifdef __ANDROID__ 125 #ifdef __ANDROID__
128 #define log2(x) (log(x)/log(2)) 126 #define log2(x) (log(x)/log(2))
129 #endif 127 #endif
161 context->operators[i].envelope = MAX_ENVELOPE; 159 context->operators[i].envelope = MAX_ENVELOPE;
162 context->operators[i].env_phase = PHASE_RELEASE; 160 context->operators[i].env_phase = PHASE_RELEASE;
163 } 161 }
164 } 162 }
165 163
166 void ym_init(ym2612_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t sample_limit, uint32_t options, uint32_t lowpass_cutoff) 164 void ym_init(ym2612_context * context, uint32_t master_clock, uint32_t clock_div, uint32_t options)
167 { 165 {
168 static uint8_t registered_finalize; 166 static uint8_t registered_finalize;
169 dfopen(debug_file, "ym_debug.txt", "w"); 167 dfopen(debug_file, "ym_debug.txt", "w");
170 memset(context, 0, sizeof(*context)); 168 memset(context, 0, sizeof(*context));
171 context->audio = render_audio_source(2);
172 context->audio_buffer = render_audio_source_buffer(context->audio);
173 context->sample_rate = sample_rate;
174 context->clock_inc = clock_div * 6; 169 context->clock_inc = clock_div * 6;
175 ym_adjust_master_clock(context, master_clock); 170 context->audio = render_audio_source(master_clock, context->clock_inc * NUM_OPERATORS, 2);
176
177 double rc = (1.0 / (double)lowpass_cutoff) / (2.0 * M_PI);
178 double dt = 1.0 / ((double)master_clock / (double)(context->clock_inc * NUM_OPERATORS));
179 double alpha = dt / (dt + rc);
180 context->lowpass_alpha = (int32_t)(((double)0x10000) * alpha);
181
182 context->sample_limit = sample_limit*2;
183 171
184 //some games seem to expect that the LR flags start out as 1 172 //some games seem to expect that the LR flags start out as 1
185 for (int i = 0; i < NUM_CHANNELS; i++) { 173 for (int i = 0; i < NUM_CHANNELS; i++) {
186 if (options & YM_OPT_WAVE_LOG) { 174 if (options & YM_OPT_WAVE_LOG) {
187 char fname[64]; 175 char fname[64];
189 FILE * f = context->channels[i].logfile = fopen(fname, "wb"); 177 FILE * f = context->channels[i].logfile = fopen(fname, "wb");
190 if (!f) { 178 if (!f) {
191 fprintf(stderr, "Failed to open WAVE log file %s for writing\n", fname); 179 fprintf(stderr, "Failed to open WAVE log file %s for writing\n", fname);
192 continue; 180 continue;
193 } 181 }
194 if (!wave_init(f, sample_rate, 16, 1)) { 182 if (!wave_init(f, master_clock / (context->clock_inc * NUM_OPERATORS), 16, 1)) {
195 fclose(f); 183 fclose(f);
196 context->channels[i].logfile = NULL; 184 context->channels[i].logfile = NULL;
197 } 185 }
198 } 186 }
199 } 187 }
602 } 590 }
603 context->current_op++; 591 context->current_op++;
604 if (context->current_op == NUM_OPERATORS) { 592 if (context->current_op == NUM_OPERATORS) {
605 context->current_op = 0; 593 context->current_op = 0;
606 594
607 context->buffer_fraction += context->buffer_inc;
608 int16_t left = 0, right = 0; 595 int16_t left = 0, right = 0;
609 for (int i = 0; i < NUM_CHANNELS; i++) { 596 for (int i = 0; i < NUM_CHANNELS; i++) {
610 int16_t value = context->channels[i].output; 597 int16_t value = context->channels[i].output;
611 if (value > 0x1FE0) { 598 if (value > 0x1FE0) {
612 value = 0x1FE0; 599 value = 0x1FE0;
616 value &= 0x3FE0; 603 value &= 0x3FE0;
617 if (value & 0x2000) { 604 if (value & 0x2000) {
618 value |= 0xC000; 605 value |= 0xC000;
619 } 606 }
620 } 607 }
621 if (context->channels[i].logfile && context->buffer_fraction > BUFFER_INC_RES) { 608 if (context->channels[i].logfile) {
622 fwrite(&value, sizeof(value), 1, context->channels[i].logfile); 609 fwrite(&value, sizeof(value), 1, context->channels[i].logfile);
623 } 610 }
624 if (context->channels[i].lr & 0x80) { 611 if (context->channels[i].lr & 0x80) {
625 left += (value * YM_VOLUME_MULTIPLIER) / YM_VOLUME_DIVIDER; 612 left += (value * YM_VOLUME_MULTIPLIER) / YM_VOLUME_DIVIDER;
626 } 613 }
627 if (context->channels[i].lr & 0x40) { 614 if (context->channels[i].lr & 0x40) {
628 right += (value * YM_VOLUME_MULTIPLIER) / YM_VOLUME_DIVIDER; 615 right += (value * YM_VOLUME_MULTIPLIER) / YM_VOLUME_DIVIDER;
629 } 616 }
630 } 617 }
631 int32_t tmp = left * context->lowpass_alpha + context->last_left * (0x10000 - context->lowpass_alpha); 618 render_put_stereo_sample(context->audio, left, right);
632 left = tmp >> 16;
633 tmp = right * context->lowpass_alpha + context->last_right * (0x10000 - context->lowpass_alpha);
634 right = tmp >> 16;
635 while (context->buffer_fraction > BUFFER_INC_RES) {
636 context->buffer_fraction -= BUFFER_INC_RES;
637
638 int64_t tmp = context->last_left * ((context->buffer_fraction << 16) / context->buffer_inc);
639 tmp += left * (0x10000 - ((context->buffer_fraction << 16) / context->buffer_inc));
640 context->audio_buffer[context->buffer_pos] = tmp >> 16;
641
642 tmp = context->last_right * ((context->buffer_fraction << 16) / context->buffer_inc);
643 tmp += right * (0x10000 - ((context->buffer_fraction << 16) / context->buffer_inc));
644 context->audio_buffer[context->buffer_pos+1] = tmp >> 16;
645
646 context->buffer_pos += 2;
647 if (context->buffer_pos == context->sample_limit) {
648 if (!headless) {
649 context->audio_buffer = render_audio_ready(context->audio);
650 context->buffer_pos = 0;
651 }
652 }
653 }
654 context->last_left = left;
655 context->last_right = right;
656 } 619 }
657 620
658 } 621 }
659 if (context->current_cycle >= context->write_cycle + (context->busy_cycles * context->clock_inc / 6)) { 622 if (context->current_cycle >= context->write_cycle + (context->busy_cycles * context->clock_inc / 6)) {
660 context->status &= 0x7F; 623 context->status &= 0x7F;