Synth envelope release button reference problem

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So I am implementing a synth envelop on an ATMega328p and am using a button reference in the envelope function. Each time a different note-button is pressed, the corresponding button should be referenced to the envelope function for releasing.

The problem is that, everything works as intended if I change the starting value of that reference in its declaration, but it does not change through assigning a value to it in my main code.

 

uint8_t		envelopeStage[2] = {4, 4};		//Tracks the current stage of the envelope
bool&		envButton = buttonState[0];		//The button envelope tracks for releasing
uint16_t	millisecsEnvelope[2] = {0, 0};	//Timer for the envelope incremented in ms

void buttonAction(volatile const uint8_t* PINX, const uint8_t PINXno)
{
	if (PINX == buttonPort[0] && PINXno == buttonPin[0])
	{
		UDR0 = 'A';
		envButton = buttonState[0];  //Does not work!!!!
		play();
	}
	else if (PINX == buttonPort[1] && PINXno == buttonPin[1])
	{
		UDR0 = 'B';
		envButton = buttonState[1];  //Does not work!!!!
		play();
	}
	else if (PINX == buttonPort[2] && PINXno == buttonPin[2])
	{
		UDR0 = 'C';
		envButton = buttonState[2];  //Does not work!!!!
		play();
	}
	else if (PINX == buttonPort[3] && PINXno == buttonPin[3])
	{
		UDR0 = 'D';
		envButton = buttonState[3];  //Does not work!!!!
		play();
	}
}

void doEnvelope(uint8_t oscillator, bool& buttonState)
{
	waitForSample();
	
	switch (envelopeStage[oscillator])
	{
		case 0:
		if (buttonState && ((millisecsEnvelope[oscillator] << 4) / attack[oscillator] < 255))
		{
			volume[oscillator] = (millisecsEnvelope[oscillator] << 4) / attack[oscillator];		// millisecsEnvelope[oscillator] / (attack/16)
		}
		else if (buttonState)
		{
			volume[oscillator] = 255;
			++envelopeStage[oscillator];
			millisecsEnvelope[oscillator] = 0;
		}
		else
		{
			++envelopeStage[oscillator];
			millisecsEnvelope[oscillator] = 0;
		}
		break;
	
		case 1:
		if (buttonState && volume[oscillator] > sustain[oscillator])
		{
			volume[oscillator] = 255 - ((millisecsEnvelope[oscillator] * 16) / decay[oscillator]) ;	// 255 - millisecsEnvelope[oscillator] / (decay/16);
		}
		else if (buttonState)
		{
			volume[oscillator] = sustain[oscillator];
			++envelopeStage[oscillator];
			millisecsEnvelope[oscillator] = 0;
		}
		else
		{
			++envelopeStage[oscillator];
			millisecsEnvelope[oscillator] = 0;
		}
		break;
	
		case 2:
		if (!buttonState || (sustain[oscillator] == 0))
		{
			++envelopeStage[oscillator];
			millisecsEnvelope[oscillator] = 0;
		}
		break;
	
		case 3:
		if (volume[oscillator] <= 1)
		{
			volume[oscillator] = 0;
			millisecsEnvelope[oscillator] = 0;
			++envelopeStage[oscillator];
			notePlaying[oscillator] = 0;
		}
		else if (release[oscillator] > 253)
		{
			++envelopeStage[oscillator];
			break;
		}
		else
		{
			if (!volumeRelease[oscillator]) volumeRelease[oscillator] = volume[oscillator];
		
			if (!releaseMode[oscillator])
				if (volumeRelease[oscillator] > ((millisecsEnvelope[oscillator] << 4) / release[oscillator]))
					volume[oscillator] = volumeRelease[oscillator] - ((millisecsEnvelope[oscillator] << 4) / release[oscillator]); //Linear release
				else volume[oscillator] = 0;
			else
				volume[oscillator] = (volumeRelease[oscillator] * release[oscillator]) / ((millisecsEnvelope[oscillator] >> 1) + release[oscillator]) - (millisecsEnvelope[oscillator]/1000); //Non linear release
		}
	}
}

int main()
{
	doEnvelope(0, envButton);
	doEnvelope(1, envButton);
}

I know that execution enters each button action because I can see A,B,C and D being outputted from serial for each button. If I change the declaration to:


bool&		envButton = buttonState[1];		//The button envelope tracks for releasing

It works as intended. But it seems that the assignments in the buttonAction function doesn't work. I tried making buttonState and envButton volatiles.. it still didn't work. Am I missing somehting?

This topic has a solution.

TO THE FINDER... THE ISLE OF KOHOLINT, IS BUT AN ILLUSION... HUMAN, MONSTER, SEA, SKY... A SCENE ON THE LID OF A SLEEPER'S EYE... AWAKE THE DREAMER, AND KOHOLINT WILL VANISH MUCH LIKE A BUBBLE ON A NEEDLE... CAST-AWAY, YOU SHOULD KNOW THE TRUTH!

Last Edited: Sun. Oct 18, 2020 - 05:38 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bool& is surely a reference? So it expects a pointer but you assign buttonState[0] to it so exactly what type us the buttonState array? Or to put it another way did you really mean to use '&'?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh my God. I was expecting the bool& to be reinitialized to refer a new buttonState[] every time. I just read that reference reinitialization is not a thing and I need to use a pointer. *Facepalms*

Thanks again clawson!

TO THE FINDER... THE ISLE OF KOHOLINT, IS BUT AN ILLUSION... HUMAN, MONSTER, SEA, SKY... A SCENE ON THE LID OF A SLEEPER'S EYE... AWAKE THE DREAMER, AND KOHOLINT WILL VANISH MUCH LIKE A BUBBLE ON A NEEDLE... CAST-AWAY, YOU SHOULD KNOW THE TRUTH!