2016-03-08 07:09:58 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <list>
|
2016-05-20 22:57:34 +00:00
|
|
|
#include <thread>
|
2016-03-24 00:01:57 +00:00
|
|
|
#include "AudioVoiceEngine.hpp"
|
2016-03-04 23:02:18 +00:00
|
|
|
#include "logvisor/logvisor.hpp"
|
2016-01-28 23:53:51 +00:00
|
|
|
|
2016-01-29 01:17:19 +00:00
|
|
|
#include <alsa/asoundlib.h>
|
2016-05-21 22:43:43 +00:00
|
|
|
#include <signal.h>
|
2016-01-29 01:17:19 +00:00
|
|
|
|
2016-01-28 23:53:51 +00:00
|
|
|
namespace boo
|
|
|
|
{
|
2016-03-04 23:02:18 +00:00
|
|
|
static logvisor::Module Log("boo::ALSA");
|
2016-01-28 23:53:51 +00:00
|
|
|
|
2016-03-25 02:19:34 +00:00
|
|
|
static const uint64_t StereoChans = (1 << SND_CHMAP_FL) |
|
|
|
|
(1 << SND_CHMAP_FR);
|
|
|
|
|
|
|
|
static const uint64_t QuadChans = (1 << SND_CHMAP_FL) |
|
|
|
|
(1 << SND_CHMAP_FR) |
|
|
|
|
(1 << SND_CHMAP_RL) |
|
|
|
|
(1 << SND_CHMAP_RR);
|
|
|
|
|
|
|
|
static const uint64_t S51Chans = (1 << SND_CHMAP_FL) |
|
|
|
|
(1 << SND_CHMAP_FR) |
|
|
|
|
(1 << SND_CHMAP_RL) |
|
|
|
|
(1 << SND_CHMAP_RR) |
|
|
|
|
(1 << SND_CHMAP_FC) |
|
|
|
|
(1 << SND_CHMAP_LFE);
|
|
|
|
|
|
|
|
static const uint64_t S71Chans = (1 << SND_CHMAP_FL) |
|
|
|
|
(1 << SND_CHMAP_FR) |
|
|
|
|
(1 << SND_CHMAP_RL) |
|
|
|
|
(1 << SND_CHMAP_RR) |
|
|
|
|
(1 << SND_CHMAP_FC) |
|
|
|
|
(1 << SND_CHMAP_LFE) |
|
|
|
|
(1 << SND_CHMAP_SL) |
|
|
|
|
(1 << SND_CHMAP_SR);
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
struct ALSAAudioVoiceEngine : BaseAudioVoiceEngine
|
2016-01-28 23:53:51 +00:00
|
|
|
{
|
2016-03-24 00:01:57 +00:00
|
|
|
snd_pcm_t* m_pcm;
|
2016-01-28 23:53:51 +00:00
|
|
|
snd_pcm_uframes_t m_bufSize;
|
|
|
|
snd_pcm_uframes_t m_periodSize;
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
std::vector<int16_t> m_final16;
|
|
|
|
std::vector<int32_t> m_final32;
|
|
|
|
std::vector<float> m_finalFlt;
|
2016-01-28 23:53:51 +00:00
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
~ALSAAudioVoiceEngine()
|
2016-01-28 23:53:51 +00:00
|
|
|
{
|
2016-03-24 00:01:57 +00:00
|
|
|
snd_pcm_drain(m_pcm);
|
|
|
|
snd_pcm_close(m_pcm);
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelSet _getAvailableSet()
|
|
|
|
{
|
|
|
|
snd_pcm_chmap_query_t** chmaps = snd_pcm_query_chmaps(m_pcm);
|
|
|
|
if (!chmaps)
|
|
|
|
return AudioChannelSet::Stereo;
|
|
|
|
|
|
|
|
static const std::array<AudioChannelSet, 4> testSets =
|
2016-05-20 22:57:34 +00:00
|
|
|
{{AudioChannelSet::Surround71, AudioChannelSet::Surround51,
|
|
|
|
AudioChannelSet::Quad, AudioChannelSet::Stereo}};
|
2016-03-24 00:01:57 +00:00
|
|
|
for (AudioChannelSet set : testSets)
|
|
|
|
{
|
|
|
|
for (snd_pcm_chmap_query_t** chmap = chmaps ; *chmap != nullptr ; ++chmap)
|
|
|
|
{
|
|
|
|
snd_pcm_chmap_t* chm = &(*chmap)->map;
|
|
|
|
uint64_t chBits = 0;
|
2016-05-20 22:57:34 +00:00
|
|
|
for (unsigned c=0 ; c<chm->channels ; ++c)
|
2016-03-24 00:01:57 +00:00
|
|
|
chBits |= 1 << chm->pos[c];
|
|
|
|
|
|
|
|
switch (set)
|
|
|
|
{
|
|
|
|
case AudioChannelSet::Stereo:
|
2016-03-25 02:19:34 +00:00
|
|
|
{
|
|
|
|
if ((chBits & StereoChans) == StereoChans)
|
2016-03-24 00:01:57 +00:00
|
|
|
{
|
|
|
|
snd_pcm_free_chmaps(chmaps);
|
|
|
|
return AudioChannelSet::Stereo;
|
|
|
|
}
|
|
|
|
break;
|
2016-03-25 02:19:34 +00:00
|
|
|
}
|
2016-03-24 00:01:57 +00:00
|
|
|
case AudioChannelSet::Quad:
|
2016-03-25 02:19:34 +00:00
|
|
|
{
|
|
|
|
if ((chBits & QuadChans) == QuadChans)
|
2016-03-24 00:01:57 +00:00
|
|
|
{
|
|
|
|
snd_pcm_free_chmaps(chmaps);
|
|
|
|
return AudioChannelSet::Quad;
|
|
|
|
}
|
|
|
|
break;
|
2016-03-25 02:19:34 +00:00
|
|
|
}
|
2016-03-24 00:01:57 +00:00
|
|
|
case AudioChannelSet::Surround51:
|
2016-03-25 02:19:34 +00:00
|
|
|
{
|
|
|
|
if ((chBits & S51Chans) == S51Chans)
|
2016-03-24 00:01:57 +00:00
|
|
|
{
|
|
|
|
snd_pcm_free_chmaps(chmaps);
|
|
|
|
return AudioChannelSet::Surround51;
|
|
|
|
}
|
|
|
|
break;
|
2016-03-25 02:19:34 +00:00
|
|
|
}
|
2016-03-24 00:01:57 +00:00
|
|
|
case AudioChannelSet::Surround71:
|
2016-03-25 02:19:34 +00:00
|
|
|
{
|
|
|
|
if ((chBits & S71Chans) == S71Chans)
|
2016-03-24 00:01:57 +00:00
|
|
|
{
|
|
|
|
snd_pcm_free_chmaps(chmaps);
|
|
|
|
return AudioChannelSet::Surround71;
|
|
|
|
}
|
|
|
|
break;
|
2016-03-25 02:19:34 +00:00
|
|
|
}
|
2016-03-24 00:01:57 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_free_chmaps(chmaps);
|
|
|
|
return AudioChannelSet::Unknown;
|
|
|
|
}
|
|
|
|
|
2016-03-24 01:55:39 +00:00
|
|
|
ALSAAudioVoiceEngine()
|
2016-03-24 00:01:57 +00:00
|
|
|
{
|
|
|
|
if (snd_pcm_open(&m_pcm, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0)
|
2016-01-28 23:53:51 +00:00
|
|
|
{
|
2016-03-04 23:02:18 +00:00
|
|
|
Log.report(logvisor::Error, "unable to allocate ALSA voice");
|
2016-03-24 01:55:39 +00:00
|
|
|
return;
|
2016-01-28 23:53:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
/* Query audio card for best supported format amd sample-rate */
|
|
|
|
snd_pcm_hw_params_t* hwParams;
|
|
|
|
snd_pcm_hw_params_malloc(&hwParams);
|
|
|
|
snd_pcm_hw_params_any(m_pcm, hwParams);
|
|
|
|
|
|
|
|
snd_pcm_format_t bestFmt;
|
|
|
|
if (!snd_pcm_hw_params_test_format(m_pcm, hwParams, SND_PCM_FORMAT_S32))
|
|
|
|
{
|
|
|
|
bestFmt = SND_PCM_FORMAT_S32;
|
2016-03-24 01:55:39 +00:00
|
|
|
m_mixInfo.m_sampleFormat = SOXR_INT32_I;
|
|
|
|
m_mixInfo.m_bitsPerSample = 32;
|
2016-03-24 00:01:57 +00:00
|
|
|
}
|
|
|
|
else if (!snd_pcm_hw_params_test_format(m_pcm, hwParams, SND_PCM_FORMAT_S16))
|
|
|
|
{
|
|
|
|
bestFmt = SND_PCM_FORMAT_S16;
|
2016-03-24 01:55:39 +00:00
|
|
|
m_mixInfo.m_sampleFormat = SOXR_INT16_I;
|
|
|
|
m_mixInfo.m_bitsPerSample = 16;
|
2016-03-24 00:01:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snd_pcm_close(m_pcm);
|
|
|
|
m_pcm = nullptr;
|
|
|
|
Log.report(logvisor::Fatal, "unsupported audio formats on default ALSA device");
|
2016-03-24 01:55:39 +00:00
|
|
|
return;
|
2016-03-24 00:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int bestRate;
|
|
|
|
if (!snd_pcm_hw_params_test_rate(m_pcm, hwParams, 96000, 0))
|
|
|
|
{
|
|
|
|
bestRate = 96000;
|
2016-03-24 01:55:39 +00:00
|
|
|
m_mixInfo.m_sampleRate = 96000.0;
|
2016-05-19 10:14:21 +00:00
|
|
|
m_5msFrames = 96000 * 5 / 1000;
|
2016-03-24 00:01:57 +00:00
|
|
|
}
|
|
|
|
else if (!snd_pcm_hw_params_test_rate(m_pcm, hwParams, 48000, 0))
|
|
|
|
{
|
|
|
|
bestRate = 48000;
|
2016-03-24 01:55:39 +00:00
|
|
|
m_mixInfo.m_sampleRate = 48000.0;
|
2016-05-19 10:14:21 +00:00
|
|
|
m_5msFrames = 48000 * 5 / 1000;
|
2016-03-24 00:01:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snd_pcm_close(m_pcm);
|
|
|
|
m_pcm = nullptr;
|
|
|
|
Log.report(logvisor::Fatal, "unsupported audio sample rates on default ALSA device");
|
2016-03-24 01:55:39 +00:00
|
|
|
return;
|
2016-03-24 00:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_hw_params_free(hwParams);
|
|
|
|
|
|
|
|
/* Query audio card for channel map */
|
2016-03-24 01:55:39 +00:00
|
|
|
m_mixInfo.m_channels = _getAvailableSet();
|
2016-03-24 00:01:57 +00:00
|
|
|
|
|
|
|
/* Populate channel map */
|
2016-03-24 01:55:39 +00:00
|
|
|
unsigned chCount = ChannelCount(m_mixInfo.m_channels);
|
2016-01-28 23:53:51 +00:00
|
|
|
int err;
|
2016-03-24 00:01:57 +00:00
|
|
|
while ((err = snd_pcm_set_params(m_pcm, bestFmt, SND_PCM_ACCESS_RW_INTERLEAVED,
|
|
|
|
chCount, bestRate, 0, 100000)) < 0)
|
2016-01-28 23:53:51 +00:00
|
|
|
{
|
2016-03-24 01:55:39 +00:00
|
|
|
if (m_mixInfo.m_channels == AudioChannelSet::Stereo)
|
2016-01-28 23:53:51 +00:00
|
|
|
break;
|
2016-03-24 01:55:39 +00:00
|
|
|
m_mixInfo.m_channels = AudioChannelSet(int(m_mixInfo.m_channels) - 1);
|
|
|
|
chCount = ChannelCount(m_mixInfo.m_channels);
|
2016-01-28 23:53:51 +00:00
|
|
|
}
|
|
|
|
if (err < 0)
|
|
|
|
{
|
|
|
|
snd_pcm_close(m_pcm);
|
|
|
|
m_pcm = nullptr;
|
2016-03-04 23:02:18 +00:00
|
|
|
Log.report(logvisor::Error, "unable to set ALSA voice params");
|
2016-03-24 01:55:39 +00:00
|
|
|
return;
|
2016-01-28 23:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_chmap_query_t** chmaps = snd_pcm_query_chmaps(m_pcm);
|
2016-03-24 01:55:39 +00:00
|
|
|
ChannelMap& chmapOut = m_mixInfo.m_channelMap;
|
2016-03-08 07:09:58 +00:00
|
|
|
if (chmaps)
|
2016-01-28 23:53:51 +00:00
|
|
|
{
|
2016-03-08 07:09:58 +00:00
|
|
|
snd_pcm_chmap_t* foundChmap = nullptr;
|
|
|
|
for (snd_pcm_chmap_query_t** chmap = chmaps ; *chmap != nullptr ; ++chmap)
|
|
|
|
{
|
|
|
|
if ((*chmap)->map.channels == chCount)
|
|
|
|
{
|
|
|
|
snd_pcm_chmap_t* chm = &(*chmap)->map;
|
|
|
|
uint64_t chBits = 0;
|
2016-05-20 22:57:34 +00:00
|
|
|
for (unsigned c=0 ; c<chm->channels ; ++c)
|
2016-03-08 07:09:58 +00:00
|
|
|
chBits |= 1 << chm->pos[c];
|
|
|
|
|
|
|
|
bool good = false;
|
2016-03-24 01:55:39 +00:00
|
|
|
switch (m_mixInfo.m_channels)
|
2016-03-08 07:09:58 +00:00
|
|
|
{
|
|
|
|
case AudioChannelSet::Stereo:
|
2016-03-25 02:19:34 +00:00
|
|
|
if ((chBits & StereoChans) == StereoChans)
|
2016-03-08 07:09:58 +00:00
|
|
|
good = true;
|
|
|
|
break;
|
|
|
|
case AudioChannelSet::Quad:
|
2016-03-25 02:19:34 +00:00
|
|
|
if ((chBits & QuadChans) == QuadChans)
|
2016-03-08 07:09:58 +00:00
|
|
|
good = true;
|
|
|
|
break;
|
|
|
|
case AudioChannelSet::Surround51:
|
2016-03-25 02:19:34 +00:00
|
|
|
if ((chBits & S51Chans) == S51Chans)
|
2016-03-08 07:09:58 +00:00
|
|
|
good = true;
|
|
|
|
break;
|
|
|
|
case AudioChannelSet::Surround71:
|
2016-03-25 02:19:34 +00:00
|
|
|
if ((chBits & S71Chans) == S71Chans)
|
2016-03-08 07:09:58 +00:00
|
|
|
good = true;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (good)
|
|
|
|
{
|
|
|
|
foundChmap = chm;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundChmap)
|
|
|
|
{
|
|
|
|
snd_pcm_close(m_pcm);
|
|
|
|
m_pcm = nullptr;
|
|
|
|
snd_pcm_free_chmaps(chmaps);
|
|
|
|
Log.report(logvisor::Error, "unable to find matching ALSA voice chmap");
|
2016-03-24 01:55:39 +00:00
|
|
|
return;
|
2016-03-08 07:09:58 +00:00
|
|
|
}
|
2016-03-24 00:01:57 +00:00
|
|
|
chmapOut.m_channelCount = chCount;
|
2016-05-20 22:57:34 +00:00
|
|
|
for (unsigned c=0 ; c<foundChmap->channels ; ++c)
|
2016-03-24 00:01:57 +00:00
|
|
|
chmapOut.m_channels[c] = AudioChannel(foundChmap->pos[c] - 3);
|
2016-03-08 07:09:58 +00:00
|
|
|
snd_pcm_set_chmap(m_pcm, foundChmap);
|
|
|
|
snd_pcm_free_chmaps(chmaps);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-24 00:01:57 +00:00
|
|
|
chmapOut.m_channelCount = 2;
|
|
|
|
chmapOut.m_channels[0] = AudioChannel::FrontLeft;
|
|
|
|
chmapOut.m_channels[1] = AudioChannel::FrontRight;
|
2016-03-08 07:09:58 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
snd_pcm_get_params(m_pcm, &m_bufSize, &m_periodSize);
|
|
|
|
snd_pcm_prepare(m_pcm);
|
|
|
|
m_mixInfo.m_periodFrames = m_periodSize;
|
2016-03-08 07:09:58 +00:00
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
/* Allocate master mix space */
|
|
|
|
switch (m_mixInfo.m_sampleFormat)
|
|
|
|
{
|
|
|
|
case SOXR_INT16_I:
|
|
|
|
m_final16.resize(m_periodSize * m_mixInfo.m_channelMap.m_channelCount);
|
|
|
|
break;
|
|
|
|
case SOXR_INT32_I:
|
|
|
|
m_final32.resize(m_periodSize * m_mixInfo.m_channelMap.m_channelCount);
|
|
|
|
break;
|
|
|
|
case SOXR_FLOAT32_I:
|
|
|
|
m_finalFlt.resize(m_periodSize * m_mixInfo.m_channelMap.m_channelCount);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-03-08 07:09:58 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
void pumpAndMixVoices()
|
2016-03-08 07:09:58 +00:00
|
|
|
{
|
2016-03-24 00:01:57 +00:00
|
|
|
snd_pcm_sframes_t frames = snd_pcm_avail_update(m_pcm);
|
2016-03-08 07:09:58 +00:00
|
|
|
if (frames < 0)
|
|
|
|
{
|
|
|
|
snd_pcm_state_t st = snd_pcm_state(m_pcm);
|
|
|
|
if (st == SND_PCM_STATE_XRUN)
|
|
|
|
{
|
|
|
|
snd_pcm_prepare(m_pcm);
|
2016-03-24 00:01:57 +00:00
|
|
|
frames = snd_pcm_avail_update(m_pcm);
|
|
|
|
Log.report(logvisor::Warning, "ALSA underrun %ld frames", frames);
|
2016-03-08 07:09:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (frames < 0)
|
2016-01-28 23:53:51 +00:00
|
|
|
return;
|
2016-03-24 00:01:57 +00:00
|
|
|
|
2016-03-08 07:09:58 +00:00
|
|
|
snd_pcm_sframes_t buffers = frames / m_periodSize;
|
|
|
|
for (snd_pcm_sframes_t b=0 ; b<buffers ; ++b)
|
|
|
|
{
|
2016-03-24 00:01:57 +00:00
|
|
|
switch (m_mixInfo.m_sampleFormat)
|
2016-01-28 23:53:51 +00:00
|
|
|
{
|
2016-03-24 00:01:57 +00:00
|
|
|
case SOXR_INT16_I:
|
|
|
|
_pumpAndMixVoices(m_periodSize, m_final16.data());
|
|
|
|
snd_pcm_writei(m_pcm, m_final16.data(), m_periodSize);
|
|
|
|
break;
|
|
|
|
case SOXR_INT32_I:
|
|
|
|
_pumpAndMixVoices(m_periodSize, m_final32.data());
|
|
|
|
snd_pcm_writei(m_pcm, m_final32.data(), m_periodSize);
|
|
|
|
break;
|
|
|
|
case SOXR_FLOAT32_I:
|
|
|
|
_pumpAndMixVoices(m_periodSize, m_finalFlt.data());
|
|
|
|
snd_pcm_writei(m_pcm, m_finalFlt.data(), m_periodSize);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-01-28 23:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|
2016-05-19 02:18:52 +00:00
|
|
|
|
2016-05-20 06:16:07 +00:00
|
|
|
std::vector<std::pair<std::string, std::string>> enumerateMIDIDevices() const
|
2016-05-19 02:18:52 +00:00
|
|
|
{
|
2016-05-20 06:16:07 +00:00
|
|
|
std::vector<std::pair<std::string, std::string>> ret;
|
2016-05-19 02:18:52 +00:00
|
|
|
int status;
|
|
|
|
int card = -1; /* use -1 to prime the pump of iterating through card list */
|
|
|
|
|
|
|
|
if ((status = snd_card_next(&card)) < 0)
|
|
|
|
return {};
|
|
|
|
if (card < 0)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
while (card >= 0)
|
|
|
|
{
|
|
|
|
snd_ctl_t *ctl;
|
|
|
|
char name[32];
|
|
|
|
int device = -1;
|
|
|
|
int status;
|
|
|
|
sprintf(name, "hw:%d", card);
|
|
|
|
if ((status = snd_ctl_open(&ctl, name, 0)) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
do {
|
|
|
|
status = snd_ctl_rawmidi_next_device(ctl, &device);
|
|
|
|
if (status < 0)
|
|
|
|
break;
|
|
|
|
if (device >= 0)
|
|
|
|
{
|
|
|
|
snd_rawmidi_info_t *info;
|
|
|
|
snd_rawmidi_info_alloca(&info);
|
|
|
|
snd_rawmidi_info_set_device(info, device);
|
2016-05-21 01:16:28 +00:00
|
|
|
sprintf(name + strlen(name), ",%d", device);
|
|
|
|
ret.push_back(std::make_pair(name, snd_rawmidi_info_get_name(info)));
|
2016-05-19 02:18:52 +00:00
|
|
|
}
|
|
|
|
} while (device >= 0);
|
|
|
|
|
|
|
|
snd_ctl_close(ctl);
|
|
|
|
|
|
|
|
if ((status = snd_card_next(&card)) < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-20 22:57:34 +00:00
|
|
|
static void MIDIReceiveProc(snd_rawmidi_t* midi, const ReceiveFunctor& receiver, bool& running)
|
|
|
|
{
|
|
|
|
uint8_t buf[512];
|
|
|
|
while (running)
|
|
|
|
{
|
|
|
|
int rdBytes = snd_rawmidi_read(midi, buf, 512);
|
|
|
|
if (rdBytes < 0)
|
|
|
|
{
|
|
|
|
Log.report(logvisor::Error, "MIDI connection lost");
|
|
|
|
running = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver(std::vector<uint8_t>(std::cbegin(buf), std::cbegin(buf) + rdBytes));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 02:18:52 +00:00
|
|
|
struct MIDIIn : public IMIDIIn
|
|
|
|
{
|
2016-05-20 22:57:34 +00:00
|
|
|
bool m_midiRunning = true;
|
2016-05-19 02:18:52 +00:00
|
|
|
snd_rawmidi_t* m_midi;
|
2016-05-20 22:57:34 +00:00
|
|
|
std::thread m_midiThread;
|
|
|
|
|
|
|
|
MIDIIn(snd_rawmidi_t* midi, bool virt, ReceiveFunctor&& receiver)
|
|
|
|
: IMIDIIn(virt, std::move(receiver)), m_midi(midi),
|
|
|
|
m_midiThread(std::bind(MIDIReceiveProc, m_midi, m_receiver, m_midiRunning)) {}
|
|
|
|
|
|
|
|
~MIDIIn()
|
|
|
|
{
|
|
|
|
m_midiRunning = false;
|
2016-05-21 22:43:43 +00:00
|
|
|
pthread_kill(m_midiThread.native_handle(), SIGTERM);
|
2016-05-20 22:57:34 +00:00
|
|
|
if (m_midiThread.joinable())
|
|
|
|
m_midiThread.join();
|
|
|
|
snd_rawmidi_close(m_midi);
|
|
|
|
}
|
2016-05-19 02:18:52 +00:00
|
|
|
|
|
|
|
std::string description() const
|
|
|
|
{
|
|
|
|
snd_rawmidi_info_t* info;
|
|
|
|
snd_rawmidi_info_alloca(&info);
|
|
|
|
snd_rawmidi_info(m_midi, info);
|
|
|
|
std::string ret = snd_rawmidi_info_get_name(info);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-20 06:16:07 +00:00
|
|
|
struct MIDIOut : public IMIDIOut
|
|
|
|
{
|
|
|
|
snd_rawmidi_t* m_midi;
|
|
|
|
MIDIOut(snd_rawmidi_t* midi, bool virt)
|
2016-05-20 22:57:34 +00:00
|
|
|
: IMIDIOut(virt), m_midi(midi) {}
|
2016-05-20 06:16:07 +00:00
|
|
|
|
|
|
|
~MIDIOut() {snd_rawmidi_close(m_midi);}
|
2016-05-20 22:57:34 +00:00
|
|
|
|
2016-05-20 06:16:07 +00:00
|
|
|
std::string description() const
|
|
|
|
{
|
|
|
|
snd_rawmidi_info_t* info;
|
|
|
|
snd_rawmidi_info_alloca(&info);
|
|
|
|
snd_rawmidi_info(m_midi, info);
|
|
|
|
std::string ret = snd_rawmidi_info_get_name(info);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-05-20 22:57:34 +00:00
|
|
|
|
2016-05-20 06:16:07 +00:00
|
|
|
size_t send(const void* buf, size_t len) const
|
|
|
|
{
|
2016-05-20 22:57:34 +00:00
|
|
|
return size_t(std::max(0l, snd_rawmidi_write(m_midi, buf, len)));
|
2016-05-20 06:16:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MIDIInOut : public IMIDIInOut
|
|
|
|
{
|
2016-05-20 22:57:34 +00:00
|
|
|
bool m_midiRunning = true;
|
2016-05-20 06:16:07 +00:00
|
|
|
snd_rawmidi_t* m_midiIn;
|
|
|
|
snd_rawmidi_t* m_midiOut;
|
2016-05-20 22:57:34 +00:00
|
|
|
std::thread m_midiThread;
|
|
|
|
|
|
|
|
MIDIInOut(snd_rawmidi_t* midiIn, snd_rawmidi_t* midiOut, bool virt, ReceiveFunctor&& receiver)
|
|
|
|
: IMIDIInOut(virt, std::move(receiver)), m_midiIn(midiIn), m_midiOut(midiOut),
|
|
|
|
m_midiThread(std::bind(MIDIReceiveProc, m_midiIn, m_receiver, m_midiRunning)) {}
|
2016-05-20 06:16:07 +00:00
|
|
|
|
|
|
|
~MIDIInOut()
|
|
|
|
{
|
2016-05-20 22:57:34 +00:00
|
|
|
m_midiRunning = false;
|
2016-05-21 22:43:43 +00:00
|
|
|
pthread_kill(m_midiThread.native_handle(), SIGTERM);
|
2016-05-20 22:57:34 +00:00
|
|
|
if (m_midiThread.joinable())
|
|
|
|
m_midiThread.join();
|
2016-05-20 06:16:07 +00:00
|
|
|
snd_rawmidi_close(m_midiIn);
|
|
|
|
snd_rawmidi_close(m_midiOut);
|
|
|
|
}
|
2016-05-20 22:57:34 +00:00
|
|
|
|
2016-05-20 06:16:07 +00:00
|
|
|
std::string description() const
|
|
|
|
{
|
|
|
|
snd_rawmidi_info_t* info;
|
|
|
|
snd_rawmidi_info_alloca(&info);
|
|
|
|
snd_rawmidi_info(m_midiIn, info);
|
|
|
|
std::string ret = snd_rawmidi_info_get_name(info);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-05-20 22:57:34 +00:00
|
|
|
|
2016-05-20 06:16:07 +00:00
|
|
|
size_t send(const void* buf, size_t len) const
|
|
|
|
{
|
2016-05-20 22:57:34 +00:00
|
|
|
return size_t(std::max(0l, snd_rawmidi_write(m_midiOut, buf, len)));
|
2016-05-20 06:16:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-20 22:57:34 +00:00
|
|
|
std::unique_ptr<IMIDIIn> newVirtualMIDIIn(ReceiveFunctor&& receiver)
|
2016-05-19 02:18:52 +00:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
snd_rawmidi_t* midi;
|
2016-05-20 22:57:34 +00:00
|
|
|
status = snd_rawmidi_open(&midi, nullptr, "virtual", 0);
|
2016-05-19 02:18:52 +00:00
|
|
|
if (status)
|
|
|
|
return {};
|
2016-05-20 22:57:34 +00:00
|
|
|
return std::make_unique<MIDIIn>(midi, true, std::move(receiver));
|
2016-05-19 02:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IMIDIOut> newVirtualMIDIOut()
|
|
|
|
{
|
2016-05-20 06:16:07 +00:00
|
|
|
int status;
|
|
|
|
snd_rawmidi_t* midi;
|
2016-05-20 22:57:34 +00:00
|
|
|
status = snd_rawmidi_open(nullptr, &midi, "virtual", 0);
|
2016-05-20 06:16:07 +00:00
|
|
|
if (status)
|
|
|
|
return {};
|
|
|
|
return std::make_unique<MIDIOut>(midi, true);
|
2016-05-19 02:18:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 22:57:34 +00:00
|
|
|
std::unique_ptr<IMIDIInOut> newVirtualMIDIInOut(ReceiveFunctor&& receiver)
|
2016-05-19 02:18:52 +00:00
|
|
|
{
|
2016-05-20 06:16:07 +00:00
|
|
|
int status;
|
|
|
|
snd_rawmidi_t* midiIn;
|
|
|
|
snd_rawmidi_t* midiOut;
|
2016-05-20 22:57:34 +00:00
|
|
|
status = snd_rawmidi_open(&midiIn, &midiOut, "virtual", 0);
|
2016-05-20 06:16:07 +00:00
|
|
|
if (status)
|
|
|
|
return {};
|
2016-05-20 22:57:34 +00:00
|
|
|
return std::make_unique<MIDIInOut>(midiIn, midiOut, true, std::move(receiver));
|
2016-05-19 02:18:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 22:57:34 +00:00
|
|
|
std::unique_ptr<IMIDIIn> newRealMIDIIn(const char* name, ReceiveFunctor&& receiver)
|
2016-05-19 02:18:52 +00:00
|
|
|
{
|
2016-05-20 06:16:07 +00:00
|
|
|
snd_rawmidi_t* midi;
|
2016-05-21 01:16:28 +00:00
|
|
|
int status = snd_rawmidi_open(&midi, nullptr, name, 0);
|
2016-05-20 06:16:07 +00:00
|
|
|
if (status)
|
|
|
|
return {};
|
2016-05-20 22:57:34 +00:00
|
|
|
return std::make_unique<MIDIIn>(midi, true, std::move(receiver));
|
2016-05-19 02:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IMIDIOut> newRealMIDIOut(const char* name)
|
|
|
|
{
|
2016-05-20 06:16:07 +00:00
|
|
|
snd_rawmidi_t* midi;
|
2016-05-21 01:16:28 +00:00
|
|
|
int status = snd_rawmidi_open(nullptr, &midi, name, 0);
|
2016-05-20 06:16:07 +00:00
|
|
|
if (status)
|
|
|
|
return {};
|
|
|
|
return std::make_unique<MIDIOut>(midi, true);
|
2016-05-19 02:18:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 22:57:34 +00:00
|
|
|
std::unique_ptr<IMIDIInOut> newRealMIDIInOut(const char* name, ReceiveFunctor&& receiver)
|
2016-05-19 02:18:52 +00:00
|
|
|
{
|
2016-05-20 06:16:07 +00:00
|
|
|
snd_rawmidi_t* midiIn;
|
|
|
|
snd_rawmidi_t* midiOut;
|
2016-05-21 01:16:28 +00:00
|
|
|
int status = snd_rawmidi_open(&midiIn, &midiOut, name, 0);
|
2016-05-20 06:16:07 +00:00
|
|
|
if (status)
|
|
|
|
return {};
|
2016-05-20 22:57:34 +00:00
|
|
|
return std::make_unique<MIDIInOut>(midiIn, midiOut, true, std::move(receiver));
|
2016-05-19 02:18:52 +00:00
|
|
|
}
|
2016-01-28 23:53:51 +00:00
|
|
|
};
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
std::unique_ptr<IAudioVoiceEngine> NewAudioVoiceEngine()
|
2016-03-08 07:09:58 +00:00
|
|
|
{
|
2016-03-24 00:01:57 +00:00
|
|
|
std::unique_ptr<IAudioVoiceEngine> ret = std::make_unique<ALSAAudioVoiceEngine>();
|
|
|
|
if (!static_cast<ALSAAudioVoiceEngine&>(*ret).m_pcm)
|
|
|
|
return {};
|
|
|
|
return ret;
|
2016-03-08 07:09:58 +00:00
|
|
|
}
|
2016-01-28 23:53:51 +00:00
|
|
|
|
|
|
|
}
|