Thanks for the reply.
I've had a stab at the wave pattern channel after reading that doc. It isnt sounding right so im unsure if it is a bug in my code or a misunderstanding with how the wave channel works.
This is the algorithm I use for calculating the wave.
Code:
int CalculateWave ( int numCycles )
{
// dac power, master on off and length counter (if length counter is enabled)
if (channelIsNotEnabled)
return 0;
bool timeToUpdate = frequency <= numCycles ;
frequency -= numCycles ;
if (timeToUpdate)
{
// reset the frequency
int value = (set5 << 8) | set4 ;
value &= 0x7FF ;
frequency = (2048 - value) * 2 ;
// move on to next wave pos
wavePos++ ;
wavePos = wavePos % 32 ;
}
int volumeControl = set3 >> 5;
volumeControl &= 3;
int shift = 0 ;
switch(volumeControl)
{
case 0: shift = 4 ; break ;
case 1: shift = 0 ; break ;
case 2: shift = 1 ; break ;
case 3: shift = 2 ; break ;
}
return GetDACOutput (wave[wavePos] >> shift)
}
This is just pretty much pseudo code of what im doing. Basically it decreases the frequency by the num of clock cycles the last opcode took. If the frequency becomes less than 0 then it reloads the frequency and moves onto the next wave pos.
The input value given to the DAC is the current sample shifted by the volume control.
This is what the GetDACOutput does
Code:
int GetDACOutput(int volume)
{
static double analog[] = { -1, -0.8667, -0.7334, -0.6, -0.4668, -0.3335, -0.2, -0.067, 0.0664, 0.2, 0.333, 0.4668,0.6,0.7334,0.8667,1 } ;
return static_cast<int>(8000 * analog[volume] ) ;
}
The reason why im doing 8000 multiply the analog value is because SDL is using AUDIO_S16SYS which gives a range of approx -32000 to 32000 and as there are 4 channels 8000 will fit in range.
Also just to confirm something with the other square wave channels, if the current polarity is 0 then it always returns a value of 0, correct? However if the value is 1 then it passes its current volume into GetDACOutput which will return -1 to 1?
Thanks for any help