Skip to content

Commit

Permalink
linuxaudiodriver runs jack+alsa
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrra committed Sep 2, 2023
1 parent 1af624d commit 22128db
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 259 deletions.
23 changes: 11 additions & 12 deletions src/framework/audio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,22 @@ if (OS_IS_WIN)
${CMAKE_CURRENT_LIST_DIR}/internal/platform/win/audiodeviceslistener.h
)
elseif(OS_IS_LIN OR OS_IS_FBSD)
set(JACK_SRC "")
if (MUE_ENABLE_AUDIO_JACK)
set(DRIVER_SRC
set(JACK_SRC
${CMAKE_CURRENT_LIST_DIR}/internal/platform/jack/jackaudiodriver.cpp
${CMAKE_CURRENT_LIST_DIR}/internal/platform/jack/jackaudiodriver.h
${CMAKE_CURRENT_LIST_DIR}/internal/platform/jack/audiodeviceslistener.cpp
${CMAKE_CURRENT_LIST_DIR}/internal/platform/jack/audiodeviceslistener.h
)
else (MUE_ENABLE_AUDIO_JACK)
set(DRIVER_SRC
${CMAKE_CURRENT_LIST_DIR}/internal/platform/alsa/alsaaudiodriver.cpp
${CMAKE_CURRENT_LIST_DIR}/internal/platform/alsa/alsaaudiodriver.h
${CMAKE_CURRENT_LIST_DIR}/internal/platform/lin/linuxaudiodriver.cpp
${CMAKE_CURRENT_LIST_DIR}/internal/platform/lin/linuxaudiodriver.h
${CMAKE_CURRENT_LIST_DIR}/internal/platform/lin/audiodeviceslistener.cpp
${CMAKE_CURRENT_LIST_DIR}/internal/platform/lin/audiodeviceslistener.h
)
endif()
set(DRIVER_SRC
${CMAKE_CURRENT_LIST_DIR}/internal/platform/lin/linuxaudiodriver.cpp
${CMAKE_CURRENT_LIST_DIR}/internal/platform/lin/linuxaudiodriver.h
${CMAKE_CURRENT_LIST_DIR}/internal/platform/lin/audiodeviceslistener.cpp
${CMAKE_CURRENT_LIST_DIR}/internal/platform/lin/audiodeviceslistener.h
${CMAKE_CURRENT_LIST_DIR}/internal/platform/alsa/alsaaudiodriver.cpp
${CMAKE_CURRENT_LIST_DIR}/internal/platform/alsa/alsaaudiodriver.h
${JACK_SRC}
)
elseif(OS_IS_MAC)
set(DRIVER_SRC
${CMAKE_CURRENT_LIST_DIR}/internal/platform/osx/osxaudiodriver.mm
Expand Down
10 changes: 0 additions & 10 deletions src/framework/audio/audiomodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ using namespace mu::audio;
using namespace mu::audio::synth;
using namespace mu::audio::fx;

#ifdef JACK_AUDIO
#include "internal/platform/jack/jackaudiodriver.h"
#endif

#ifdef Q_OS_LINUX
#include "internal/platform/lin/linuxaudiodriver.h"
#endif
Expand Down Expand Up @@ -112,10 +108,6 @@ void AudioModule::registerExports()
m_soundFontRepository = std::make_shared<SoundFontRepository>();
m_registerAudioPluginsScenario = std::make_shared<RegisterAudioPluginsScenario>();

#if defined(JACK_AUDIO)
m_audioDriver = std::shared_ptr<IAudioDriver>(new JackAudioDriver());
#else

#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD)
m_audioDriver = std::shared_ptr<IAudioDriver>(new LinuxAudioDriver());
#endif
Expand All @@ -134,8 +126,6 @@ void AudioModule::registerExports()
m_audioDriver = std::shared_ptr<IAudioDriver>(new WebAudioDriver());
#endif

#endif // JACK_AUDIO

ioc()->registerExport<IAudioConfiguration>(moduleName(), m_configuration);
ioc()->registerExport<IAudioThreadSecurer>(moduleName(), std::make_shared<AudioThreadSecurer>());
ioc()->registerExport<IAudioDriver>(moduleName(), m_audioDriver);
Expand Down
33 changes: 24 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,17 @@ static void* alsaThread(void* aParam)
return nullptr;
}

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

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

void AlsaDriverState::alsaCleanup()
{
m_audioProcessingDone = true;
Expand All @@ -76,23 +87,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 +117,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 Expand Up @@ -138,10 +151,12 @@ bool AlsaDriverState::open(const IAudioDriver::Spec& spec, IAudioDriver::Spec* a

snd_pcm_hw_params_get_rate(params, &val, &dir);
aSamplerate = val;
m_spec.sampleRate = aSamplerate;

if (m_buffer != nullptr) {
LOGW() << "open before close";
delete[] m_buffer;
m_buffer = nullptr;
}

m_buffer = new float[m_spec.samples * m_spec.channels];
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
Loading

0 comments on commit 22128db

Please sign in to comment.