Skip to content

Commit

Permalink
Move RPi code from Synth_Dexed to MiniDexed, thanks @rsta2
Browse files Browse the repository at this point in the history
Move the Raspberry Pi specific code from the classes AudioSynthDexed* from the Synth_Dexed project to the MiniDexed project
probonopd/MiniDexed#8 (comment)
  • Loading branch information
probonopd committed Feb 23, 2022
1 parent cdaf1f6 commit 86dba98
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 413 deletions.
304 changes: 0 additions & 304 deletions src/synth_dexed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1783,310 +1783,6 @@ void AudioSynthDexed::update(void)

in_update = false;
};
#elif defined(__circle__)
const TNoteInfo AudioSynthDexed::s_Keys[] =
{
{',', 72}, // C4
{'M', 71}, // B4
{'J', 70}, // A#4
{'N', 69}, // A4
{'H', 68}, // G#3
{'B', 67}, // G3
{'G', 66}, // F#3
{'V', 65}, // F3
{'C', 64}, // E3
{'D', 63}, // D#3
{'X', 62}, // D3
{'S', 61}, // C#3
{'Z', 60} // C3
};

AudioSynthDexed *AudioSynthDexed::s_pThis = 0;

bool AudioSynthDexed::Initialize (void)
{
if (!m_Serial.Initialize(31250))
{
return false;
}

m_bUseSerial = true;

activate();

return true;
}

void AudioSynthDexed::Process(boolean bPlugAndPlayUpdated)
{
if (m_pMIDIDevice != 0)
{
return;
}

if (bPlugAndPlayUpdated)
{
m_pMIDIDevice =
(CUSBMIDIDevice *) CDeviceNameService::Get ()->GetDevice ("umidi1", FALSE);
if (m_pMIDIDevice != 0)
{
m_pMIDIDevice->RegisterRemovedHandler (USBDeviceRemovedHandler);
m_pMIDIDevice->RegisterPacketHandler (MIDIPacketHandler);

return;
}
}

if (m_pKeyboard != 0)
{
return;
}

if (bPlugAndPlayUpdated)
{
m_pKeyboard =
(CUSBKeyboardDevice *) CDeviceNameService::Get ()->GetDevice ("ukbd1", FALSE);
if (m_pKeyboard != 0)
{
m_pKeyboard->RegisterRemovedHandler (USBDeviceRemovedHandler);
m_pKeyboard->RegisterKeyStatusHandlerRaw (KeyStatusHandlerRaw);

return;
}
}

if (!m_bUseSerial)
{
return;
}

// Read serial MIDI data
u8 Buffer[20];
int nResult = m_Serial.Read (Buffer, sizeof Buffer);
if (nResult <= 0)
{
return;
}

// Process MIDI messages
// See: https://www.midi.org/specifications/item/table-1-summary-of-midi-message
for (int i = 0; i < nResult; i++)
{
u8 uchData = Buffer[i];

switch (m_nSerialState)
{
case 0:
MIDIRestart:
if ((uchData & 0xE0) == 0x80) // Note on or off, all channels
{
m_SerialMessage[m_nSerialState++] = uchData;
}
break;

case 1:
case 2:
if (uchData & 0x80) // got status when parameter expected
{
m_nSerialState = 0;

goto MIDIRestart;
}

m_SerialMessage[m_nSerialState++] = uchData;

if (m_nSerialState == 3) // message is complete
{
MIDIPacketHandler (0, m_SerialMessage, sizeof m_SerialMessage);

m_nSerialState = 0;
}
break;

default:
assert (0);
break;
}
}
}

void AudioSynthDexed::MIDIPacketHandler (unsigned nCable, u8 *pPacket, unsigned nLength)
{
assert (s_pThis != 0);

// The packet contents are just normal MIDI data - see
// https://www.midi.org/specifications/item/table-1-summary-of-midi-message

if (nLength < 3)
{
return;
}

u8 ucStatus = pPacket[0];
//u8 ucChannel = ucStatus & 0x0F;
u8 ucType = ucStatus >> 4;
u8 ucKeyNumber = pPacket[1];
u8 ucVelocity = pPacket[2];

if (ucType == MIDI_NOTE_ON)
{
s_pThis->keydown(ucKeyNumber,ucVelocity);
}
else if (ucType == MIDI_NOTE_OFF)
{
s_pThis->keyup(ucKeyNumber);
}
}

void AudioSynthDexed::KeyStatusHandlerRaw (unsigned char ucModifiers, const unsigned char RawKeys[6])
{
assert (s_pThis != 0);

// find the key code of a pressed key
char chKey = '\0';
for (unsigned i = 0; i <= 5; i++)
{
u8 ucKeyCode = RawKeys[i];
if (ucKeyCode != 0)
{
if (0x04 <= ucKeyCode && ucKeyCode <= 0x1D)
{
chKey = RawKeys[i]-0x04+'A'; // key code of 'A' is 0x04
}
else if (ucKeyCode == 0x36)
{
chKey = ','; // key code of ',' is 0x36
}

break;
}
}

if (chKey != '\0')
{
// find the pressed key in the key table and set its frequency
for (unsigned i = 0; i < sizeof(s_Keys)/sizeof(s_Keys[0]); i++)
{
if (s_Keys[i].Key == chKey)
{
u8 ucKeyNumber = s_Keys[i].KeyNumber;

s_pThis->keydown(ucKeyNumber,100);

return;
}
}
}

// TODO: s_pThis->keyup(ucKeyNumber);
}

void AudioSynthDexed::USBDeviceRemovedHandler (CDevice *pDevice, void *pContext)
{
if (s_pThis->m_pMIDIDevice == (CUSBMIDIDevice *) pDevice)
{
s_pThis->m_pMIDIDevice = 0;
}
else if (s_pThis->m_pKeyboard == (CUSBKeyboardDevice *) pDevice)
{
s_pThis->m_pKeyboard = 0;
}
}

bool AudioSynthDexedPWM::Initialize (void)
{
if (!AudioSynthDexed::Initialize())
{
return false;
}

return Start ();
}

unsigned AudioSynthDexedPWM::GetChunk(u32 *pBuffer, unsigned nChunkSize)
{
unsigned nResult = nChunkSize;

int16_t int16_buf[nChunkSize/2];

getSamples(nChunkSize/2, int16_buf);

for (unsigned i = 0; nChunkSize > 0; nChunkSize -= 2) // fill the whole buffer
{
s32 nSample = int16_buf[i++];
nSample += 32768;
nSample *= GetRangeMax()/2;
nSample /= 32768;

*pBuffer++ = nSample; // 2 stereo channels
*pBuffer++ = nSample;
}
return(nResult);
};

bool AudioSynthDexedI2S::Initialize (void)
{
if (!AudioSynthDexed::Initialize())
{
return false;
}

return Start ();
}

unsigned AudioSynthDexedI2S::GetChunk(u32 *pBuffer, unsigned nChunkSize)
{
unsigned nResult = nChunkSize;

int16_t int16_buf[nChunkSize/2];

getSamples(nChunkSize/2, int16_buf);

for (unsigned i = 0; nChunkSize > 0; nChunkSize -= 2) // fill the whole buffer
{
s32 nSample = int16_buf[i++];
nSample <<= 8;

*pBuffer++ = nSample; // 2 stereo channels
*pBuffer++ = nSample;
}
return(nResult);
};

bool AudioSynthDexedHDMI::Initialize (void)
{
if (!AudioSynthDexed::Initialize())
{
return false;
}

return Start ();
}

unsigned AudioSynthDexedHDMI::GetChunk(u32 *pBuffer, unsigned nChunkSize)
{
unsigned nResult = nChunkSize;

int16_t int16_buf[nChunkSize/2];
unsigned nFrame = 0;

getSamples(nChunkSize/2, int16_buf);

for (unsigned i = 0; nChunkSize > 0; nChunkSize -= 2) // fill the whole buffer
{
s32 nSample = int16_buf[i++];
nSample <<= 8;

nSample = ConvertIEC958Sample (nSample, nFrame);

if (++nFrame == IEC958_FRAMES_PER_BLOCK)
nFrame = 0;

*pBuffer++ = nSample; // 2 stereo channels
*pBuffer++ = nSample;
}
return(nResult);
};
#endif

/*
Expand Down

0 comments on commit 86dba98

Please sign in to comment.