changeset 740:25c9e9d39997

Fix LFO counter update speed and implement amplitude modulation
author Michael Pavone <pavone@retrodev.com>
date Thu, 28 May 2015 00:11:15 -0700
parents 2317bdca03b4
children 85c98a222fea
files ym2612.c
diffstat 1 files changed, 16 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/ym2612.c	Wed May 27 20:53:21 2015 -0700
+++ b/ym2612.c	Thu May 28 00:11:15 2015 -0700
@@ -108,6 +108,8 @@
 };
 int16_t lfo_pm_table[128 * 32 * 8];
 
+uint16_t ams_shift[] = {8, 3, 1, 0};
+
 #define MAX_ENVELOPE 0xFFC
 #define YM_DIVIDER 2
 #define CYCLE_NEVER 0xFFFFFFFF
@@ -268,16 +270,16 @@
 					context->timer_b = context->timer_b_load;
 				}
 			}
-		}
-		//Update LFO
-		if (context->lfo_enable) {
-			if (context->lfo_counter) {
-				context->lfo_counter--;
-			} else {
-				context->lfo_counter = lfo_timer_values[context->lfo_freq];
-				context->lfo_am_step += 2;
-				context->lfo_am_step &= 0xFE;
-				context->lfo_pm_step = context->lfo_am_step / 8;
+			//Update LFO
+			if (context->lfo_enable) {
+				if (context->lfo_counter) {
+					context->lfo_counter--;
+				} else {
+					context->lfo_counter = lfo_timer_values[context->lfo_freq];
+					context->lfo_am_step += 2;
+					context->lfo_am_step &= 0xFE;
+					context->lfo_pm_step = context->lfo_am_step / 8;
+				}
 			}
 		}
 		//Update Envelope Generator
@@ -427,6 +429,10 @@
 				break;
 			}
 			uint16_t env = operator->envelope + operator->total_level;
+			if (operator->am) {
+				uint16_t base_am = (context->lfo_am_step & 0x80 ? context->lfo_am_step : ~context->lfo_am_step) & 0x7E;
+				env += base_am >> ams_shift[chan->ams];
+			}
 			if (env > MAX_ENVELOPE) {
 				env = MAX_ENVELOPE;
 			}