60beat GamePad

## Input

* 13× Digital Buttons
	+ Face: 1,2,3,4
	+ Shoulder: L1, R1, L2, R2
	+ Other: MODE, SELECT, START, L3, R3
* Digital Directional Pad
* 2× Analog Sticks

## Ports

* 3.5mm Phone TRRS 🔌 🔈

The audio carrier rate is 44,100 Hz.

Ring buffer size for processing is 2^11/44100 seconds, or about 46.4 ms.

In testing, the data comes in 3 pulses: the first pulse is empty, the second has data, and the third is truncated due to likely powerloss.

The Int16 stream must first be smoothed and cleaned. This is the method the original 60beats logic used to process the waveform but might be better served with a more modern and powerful reconstruction filter.

```
static Int16 FirstPassFilter(Int16 amplitude)
{
 // possbly a 2-wide box filter
 Int16 currentAmplitude = amplitude;
 amplitude = (Int16)((previousPreviousAmplitude + (previousAmplitude \* 2) + currentAmplitude) / 4);
 previousPreviousAmplitude = previousAmplitude;
 previousAmplitude = currentAmplitude;

 return amplitude;
}

```

The Int16 value is then stored in a ring-buffer of 40 bytes size which is used to make room for repairing damaged waveforms. The Int16 16 samples ahead is read, which is from the prior loop of the ring buffer and is initially 0 and checked for peak/valley truncation.

```
previousValues.Push(amplitude);

// correct clipping at 255 or -256 using a parabala to synthesize the lost peak/vally
if (previousValues[16] == 255 || previousValues[16] == -256)
{
 int weightedPrevious = previousValues[16 - 1]; // prev[15] - (prev[16] - prev[15]) \* (iter+1)
 int divisor = 0; // terations \* (iterations + 1)
 int iterations = 0;
 for (; iterations < 6; iterations++)
 {
 divisor += (iterations \* 2);
 weightedPrevious += previousValues[16 - 1] + (previousValues[16] - previousValues[16 - 1]);

 // break if we go out of range early
 if (previousValues[16] == 255 && previousValues[16 + 1 + iterations] >= 255)
 break;
 if (previousValues[16] == -256 && previousValues[16 + 1 + iterations] <= -256)
 break;
 }
 if (iterations > 2)
 {
 float a = (float)(previousValues[16 + 1 + iterations] - weightedPrevious) / (float)divisor; // (prev[17+iter] - prev[16] \* (iter+1) + prev[15] \* iter) / (iter \* (iter+1))
 float b = (float)(previousValues[16] - previousValues[16 - 1]) - a;
 float c = (float)previousValues[16 - 1];
 for (int v13 = iterations, v9Index = 16 - 1, x = 2; v13 > 3; v13--, v9Index++, x++)
 {
 previousValues[v9Index] = (Int16)(float)Math.Round((a \* x \* x) + (b \* x) + c);
 }
 }
}

```

The maximum absolute amplitude is stored in another 40 entry ring-buffer. This buffer is checked to find the maximum amplitude in the sample window.

```
// get the max absolute amplitude of the last BUFFER frames
previousAbsValues.Push((Int16)Math.Abs((previousValues[16] == Int16.MinValue) ? Int16.MaxValue : previousValues[16]));
int maxAbsInWindow = 0;
for (int i = 0; i < previousAbsValues.Size; i++)
{
 Int16 newValue = previousAbsValues[i];
 if (newValue > maxAbsInWindow)
 maxAbsInWindow = newValue;
}

```

Gather the min and max amplitude for the next 9 frames (after frame 18 rather than) and use it to find the true middle point of the waveform.

```
// get the min and max for the 9 frames at the read index
Int16 findMin = 0;
Int16 findMax = 0;
for (int i = 0; i < 9; i++)
{
 Int16 newValue = previousValues[16 + 2 + i];
 if (newValue > findMax)
 findMax = newValue;
 if (newValue < findMin)
 findMin = newValue;
}

int rangeTop = Math.Max((int)findMax, 100);
int rangeBottom = Math.Min((int)findMin, -100);
int rangeMiddle = (rangeBottom + rangeTop) / 2;

Int16 rangedValue = (Int16)Math.Min(Math.Max(previousValues[16] - rangeMiddle, Int16.MinValue), Int16.MaxValue);

```

If the amplitude has gotten somewhat quiet, start to process the frames in the ring buffer. Attempt to process the half-save width counts stored so far. Note we haven't reached the code that stores these yet so this buffer will be empty initially. This test attempts to use multiple different dividing lines between the length for a long and a short half-wave looking for one that generates a valid bitstream.

```
if (maxAbsInWindow < 101)
//if (maxAbsInWindow < 101 || (countsBufferIndex > 0 && countsBuffer[countsBufferIndex-1] > 1000))
//if (maxAbsInWindow < 101 || (countsBufferIndex > 0 && countsBuffer[countsBufferIndex-1] > 100))
{
 //if (countsBufferIndex != 0)
 if (countsBufferIndex > 0)
 {
 // process counts data with various possible thresholds
 rx\_buffer\_len = 0;
 currentBitIndex = 0;
 currentByte = 0;
 self\_checksumOK = false;
 if (!processCountsWithThreshold(63))
 {
 for (int v60\_ = 1; v60\_ < 10; v60\_++) // shouldn't this start at 1 as the above already tried?
 {
 rx\_buffer\_len = 0;
 currentBitIndex = 0;
 currentByte = 0;
 if (processCountsWithThreshold(63 + v60\_))
 {
 if (self\_checksumOK)
 break;
 }
 rx\_buffer\_len = 0;
 currentBitIndex = 0;
 currentByte = 0;
 if (processCountsWithThreshold(63 - v60\_))
 {
 if (self\_checksumOK)
 break;
 }
 }
 }

 if (self\_checksumOK)
 {
 //Console.WriteLine(TimeSinceGood);
 //TimeSinceGood = 0;

 ++self\_checksumSuccessCount;
 //finishedPacket();
 }
 else if ((uint)countsBufferIndex > 20)
 {
 ++self\_checksumErrorCount;
 Console.Title = $"Checksum Success: {self\_checksumSuccessCount} Checksum Errors: {self\_checksumErrorCount} self\_checksumErrorCount, Error Ratio: {(1.0f \* self\_checksumErrorCount / (self\_checksumErrorCount + self\_checksumSuccessCount))}";
 }
 countsBufferIndex = 0;
 }
 //return;
}//else

```

After handling good but older data, we're still in the middle of processing new data. Count the time between crosses of the adjusted 0 line (accounting for any waves that are way too short suggesting signal damage).

```
if (rangedValue > 1)
{
 \_MergedGlobals\_currentValue = 1;
}
else if (rangedValue < -1)
{
 \_MergedGlobals\_currentValue = 0;
}
{
 \_MergedGlobals\_countsSinceLastTransition += 10;
 if (\_MergedGlobals\_currentValue != \_MergedGlobals\_lastValue)
 {
 int stepsAgoCrossedZero = 10 \* Math.Abs((int)rangedValue) / Math.Abs((int)rangedValue - (int)\_MergedGlobals\_previousValue);
 \_MergedGlobals\_countsSinceLastTransition -= stepsAgoCrossedZero;
 if (rangedValue > 1 || countsBufferIndex != 0)
 {
 if (countsBufferIndex == 0)
 \_MergedGlobals\_countsSinceLastTransition = stepsAgoCrossedZero;
 countsBuffer[countsBufferIndex] = \_MergedGlobals\_countsSinceLastTransition;

 //TimeSinceGood = TimeSinceGood + (uint)\_MergedGlobals\_countsSinceLastTransition;

 // nielk1 attempt to fix noise by merging overly-short inversion, another method might be detecting slope instead of time
 if (countsBufferIndex > 4 && \_MergedGlobals\_countsSinceLastTransition < 35)
 {
 int total = countsBuffer[countsBufferIndex - 0] + countsBuffer[countsBufferIndex - 1] + countsBuffer[countsBufferIndex - 2];
 if (total < 100)
 {
 countsBufferIndex -= 2;
 countsBuffer[countsBufferIndex] = total;
 }
 }

 if (countsBufferIndex < 4000)
 countsBufferIndex++;
 }
 \_MergedGlobals\_countsSinceLastTransition = stepsAgoCrossedZero;
 }
 \_MergedGlobals\_lastValue = \_MergedGlobals\_currentValue;
 \_MergedGlobals\_previousValue = rangedValue;
}

```

Below is the bitstream check that attempts to find a valid bitstream from the length of the data.

```
static bool processCountsWithThreshold(int threshold, int countsOffset = 0)
{
 bool flipFlop = false;
 bool doingFlip = false;

 //handleBit(true);
 bool WholeArray = false;
 for (int i = countsOffset; i < countsBufferIndex; i++)
 {
 if (countsBuffer[i] >= (uint)threshold) // over writes current flipFlop value
 {
 if (doingFlip) // if we under-over, the data is bad since unders must be paired
 {
 return false;
 }
 WholeArray = handleBit(flipFlop);
 }
 else if (doingFlip) // 2nd under writes current flipFlop value
 {
 WholeArray = handleBit(flipFlop);
 doingFlip = false;
 }
 else // first under skips write and just flipsFlops
 {
 doingFlip = true;
 }
 flipFlop = !flipFlop;
 if (WholeArray)
 break;
 }

 //Console.WriteLine();
 return true;
}

```

```
static bool handleBit(bool bit)
{
 int bit\_ = bit ? 1 : 0;
 /\*{
 Console.ForegroundColor = rx\_buffer\_len % 2 == 0 ? ConsoleColor.Gray : ConsoleColor.DarkGray;
 Console.Write(bit\_);
 }\*/
 currentByte = (byte)((currentByte << 1) | bit\_);

 currentBitIndex++;
 if (currentBitIndex == 8)
 {
 currentBitIndex = 0;
 \_MergedGlobals\_\_rx\_buffer[rx\_buffer\_len] = currentByte;
 rx\_buffer\_len++;
 currentByte = 0;
 }
 if (rx\_buffer\_len == 7 && currentBitIndex == 1)
 {
 \_MergedGlobals\_\_rx\_buffer[7] = currentByte;

 verifyChecksum();
 return true;
 }
 return false;
}

```

Further documentation pending attempting an alternate reconstruction filter.