Skip to content

Commit

Permalink
connect linuxdriver to alsadriver
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrra committed Sep 1, 2023
1 parent 1af624d commit 7638762
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
30 changes: 21 additions & 9 deletions src/framework/audio/internal/platform/alsa/alsaaudiodriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ static void* alsaThread(void* aParam)
return nullptr;
}

AlsaDriverState::AlsaDriverState()
{
m_deviceId = "alsa";
}

AlsaDriverState::~AlsaDriverState()
{
alsaCleanup();
}

void AlsaDriverState::alsaCleanup()
{
m_audioProcessingDone = true;
Expand All @@ -76,23 +86,25 @@ void AlsaDriverState::alsaCleanup()
snd_pcm_close(aDevice);
m_alsaDeviceHandle = nullptr;
}

delete[] m_buffer;
if (m_buffer) {
delete[] m_buffer;
}
m_buffer = nullptr;
}

AlsaDriverState::AlsaDriverState()
std::string AlsaDriverState::name() const
{
m_deviceId = "alsa";
return "alsa";
}

AlsaDriverState::~AlsaDriverState()
std::string AlsaDriverState::deviceName() const
{
alsaCleanup();
return m_deviceName;
}

std::string AlsaDriverState::name() const
void AlsaDriverState::deviceName(const std::string newDeviceName)
{
return "MUAUDIO(ALSA)";
m_deviceName = newDeviceName;
}

bool AlsaDriverState::open(const IAudioDriver::Spec& spec, IAudioDriver::Spec* activeSpec)
Expand All @@ -104,7 +116,7 @@ bool AlsaDriverState::open(const IAudioDriver::Spec& spec, IAudioDriver::Spec* a

int rc;
snd_pcm_t* handle;
rc = snd_pcm_open(&handle, name().c_str(), SND_PCM_STREAM_PLAYBACK, 0);
rc = snd_pcm_open(&handle, m_deviceName.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions src/framework/audio/internal/platform/alsa/alsaaudiodriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ class AlsaDriverState : public AudioDriverState
bool open(const IAudioDriver::Spec& spec, IAudioDriver::Spec* activeSpec) override;
void close() override;
bool isOpened() const override;
std::string deviceName() const;
void deviceName(const std::string newDeviceName);

void* m_alsaDeviceHandle = nullptr;

float* m_buffer = nullptr;
bool m_audioProcessingDone = false;
pthread_t m_threadHandle = 0;
private:
std::string m_deviceName = "default";
void alsaCleanup();
};
}
Expand Down
24 changes: 19 additions & 5 deletions src/framework/audio/internal/platform/lin/linuxaudiodriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ using namespace mu::audio;

LinuxAudioDriver::LinuxAudioDriver()
{
// default to open the Alsa device
m_current_audioDriverState = std::make_shared<AlsaDriverState>();
}

Expand Down Expand Up @@ -87,16 +88,29 @@ bool LinuxAudioDriver::selectOutputDevice(const AudioDeviceID& deviceId)
if (m_current_audioDriverState->name() == deviceId) {
return true;
}
LOGW() << "current device: " << m_current_audioDriverState->name()
<< " new device: " << deviceId;
LOGW() << "Changing device is not implemented";

//FIX: no, we need to create the new device conditioned on the deviceId
bool reopen = m_current_audioDriverState->isOpened();
LOGW() << "current device is opened: " << reopen;
IAudioDriver::Spec spec(m_current_audioDriverState->m_spec);
m_current_audioDriverState->close();

bool ok = true;
// close current device
if (reopen) {
ok = m_current_audioDriverState->open(spec, &spec);
m_current_audioDriverState->close();
}
// select the new device driver
if (deviceId == "alsa") {
m_current_audioDriverState = std::make_shared<AlsaDriverState>();
} else if (deviceId == "jack") {
// m_current_audioDriverState = std::make_shared<JackDriverState>();
} else {
LOGE() << "Unknown device name: " << deviceId;
return false;
}
ok = m_current_audioDriverState->open(spec, &spec);
m_current_audioDriverState->m_spec = spec;

if (ok) {
Expand Down Expand Up @@ -147,7 +161,7 @@ bool LinuxAudioDriver::setOutputDeviceBufferSize(unsigned int bufferSize)

bool ok = true;
if (reopen) {
// FIX:
LOGW() << "reopen the device is not implemented";
// FIX:
//ok = open(m_current_audioDriverState->m_spec, &m_current_audioDriverState->m_spec);
}
Expand All @@ -168,7 +182,7 @@ std::vector<unsigned int> LinuxAudioDriver::availableOutputDeviceBufferSizes() c
{
std::vector<unsigned int> result;

unsigned int n = 4096;
unsigned int n = 4096; // FIX: magic number
while (n >= MINIMUM_BUFFER_SIZE) {
result.push_back(n);
n /= 2;
Expand Down

0 comments on commit 7638762

Please sign in to comment.