Skip to content

Commit 1e6d66f

Browse files
committed
moved logic to audio driver controller
1 parent 3273c98 commit 1e6d66f

File tree

12 files changed

+175
-369
lines changed

12 files changed

+175
-369
lines changed

src/appshell/view/preferences/commonaudioapiconfigurationmodel.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void CommonAudioApiConfigurationModel::load()
4141
audioDriverController()->currentAudioApiChanged().onNotify(this, [this]() {
4242
emit deviceListChanged();
4343
emit currentDeviceIdChanged();
44+
emit sampleRateListChanged();
4445
emit sampleRateChanged();
4546
emit bufferSizeListChanged();
4647
emit bufferSizeChanged();
@@ -53,6 +54,7 @@ void CommonAudioApiConfigurationModel::load()
5354

5455
audioDriverController()->outputDeviceChanged().onNotify(this, [this]() {
5556
emit currentDeviceIdChanged();
57+
emit sampleRateListChanged();
5658
emit sampleRateChanged();
5759
emit bufferSizeListChanged();
5860
emit bufferSizeChanged();
@@ -69,7 +71,8 @@ void CommonAudioApiConfigurationModel::load()
6971

7072
QString CommonAudioApiConfigurationModel::currentDeviceId() const
7173
{
72-
return QString::fromStdString(audioDriverController()->outputDevice());
74+
AudioDeviceID device = audioDriverController()->outputDevice();
75+
return QString::fromStdString(device);
7376
}
7477

7578
QVariantList CommonAudioApiConfigurationModel::deviceList() const
@@ -106,10 +109,10 @@ unsigned int CommonAudioApiConfigurationModel::bufferSize() const
106109
QList<unsigned int> CommonAudioApiConfigurationModel::bufferSizeList() const
107110
{
108111
QList<unsigned int> result;
109-
std::vector<unsigned int> bufferSizes = audioDriverController()->availableOutputDeviceBufferSizes();
112+
std::vector<samples_t> bufferSizes = audioDriverController()->availableOutputDeviceBufferSizes();
110113

111-
for (unsigned int bufferSize : bufferSizes) {
112-
result << bufferSize;
114+
for (samples_t bufferSize : bufferSizes) {
115+
result << static_cast<unsigned int>(bufferSize);
113116
}
114117

115118
return result;
@@ -128,10 +131,10 @@ unsigned int CommonAudioApiConfigurationModel::sampleRate() const
128131
QList<unsigned int> CommonAudioApiConfigurationModel::sampleRateList() const
129132
{
130133
QList<unsigned int> result;
131-
std::vector<unsigned int> sampleRates = audioDriverController()->availableOutputDeviceSampleRates();
134+
std::vector<sample_rate_t> sampleRates = audioDriverController()->availableOutputDeviceSampleRates();
132135

133-
for (unsigned int sampleRate : sampleRates) {
134-
result << sampleRate;
136+
for (sample_rate_t sampleRate : sampleRates) {
137+
result << static_cast<unsigned int>(sampleRate);
135138
}
136139

137140
return result;

src/framework/audio/driver/audio_driver.cmake

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ if (OS_IS_WIN)
2727
#${CMAKE_CURRENT_LIST_DIR}/platform/win/winmmdriver.h
2828
#${CMAKE_CURRENT_LIST_DIR}/platform/win/wincoreaudiodriver.cpp
2929
#${CMAKE_CURRENT_LIST_DIR}/platform/win/wincoreaudiodriver.h
30-
${CMAKE_CURRENT_LIST_DIR}/platform/win/wasapiaudioclient.cpp
31-
${CMAKE_CURRENT_LIST_DIR}/platform/win/wasapiaudioclient.h
32-
${CMAKE_CURRENT_LIST_DIR}/platform/win/wasapitypes.h
33-
${CMAKE_CURRENT_LIST_DIR}/platform/win/wasapiaudiodriver.cpp
34-
${CMAKE_CURRENT_LIST_DIR}/platform/win/wasapiaudiodriver.h
35-
${CMAKE_CURRENT_LIST_DIR}/platform/win/audiodeviceslistener.cpp
36-
${CMAKE_CURRENT_LIST_DIR}/platform/win/audiodeviceslistener.h
30+
# ${CMAKE_CURRENT_LIST_DIR}/platform/win/wasapiaudioclient.cpp
31+
# ${CMAKE_CURRENT_LIST_DIR}/platform/win/wasapiaudioclient.h
32+
# ${CMAKE_CURRENT_LIST_DIR}/platform/win/wasapitypes.h
33+
# ${CMAKE_CURRENT_LIST_DIR}/platform/win/wasapiaudiodriver.cpp
34+
# ${CMAKE_CURRENT_LIST_DIR}/platform/win/wasapiaudiodriver.h
35+
# ${CMAKE_CURRENT_LIST_DIR}/platform/win/audiodeviceslistener.cpp
36+
# ${CMAKE_CURRENT_LIST_DIR}/platform/win/audiodeviceslistener.h
3737
${CMAKE_CURRENT_LIST_DIR}/platform/win/wasapiaudiodriver2.cpp
3838
${CMAKE_CURRENT_LIST_DIR}/platform/win/wasapiaudiodriver2.h
3939
)

src/framework/audio/driver/platform/win/asio/asioaudiodriver.cpp

Lines changed: 17 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static void s_bufferSwitch(long index, ASIOBool /*processNow*/)
125125
}
126126

127127
uint8_t* stream = reinterpret_cast<uint8_t*>(&proc_buf[0]);
128-
active.callback(nullptr, stream, (int)(procSamplesTotal * sizeof(float)));
128+
active.callback(stream, (int)(procSamplesTotal * sizeof(float)));
129129

130130
constexpr float MAX_S16 = 32767.0f;
131131
constexpr float MAX_S18 = 131071.0f;
@@ -397,23 +397,23 @@ static ASIOError create_asio_buffers(long bufferSize, long outputChannels, long
397397
return ASE_OK;
398398
}
399399

400-
bool AsioAudioDriver::open(const Spec& spec, Spec* activeSpec)
400+
AudioDeviceID AsioAudioDriver::defaultDevice() const
401401
{
402-
if (m_deviceId.empty()) {
403-
AudioDeviceList devices = availableOutputDevices();
404-
if (devices.empty()) {
405-
return false;
406-
}
407-
m_deviceId = devices.at(0).id;
402+
AudioDeviceList devices = availableOutputDevices();
403+
if (devices.empty()) {
404+
return AudioDeviceID();
408405
}
409-
410-
return doOpen(m_deviceId, spec, activeSpec);
406+
return devices.at(0).id;
411407
}
412408

413-
bool AsioAudioDriver::doOpen(const AudioDeviceID& device, const Spec& spec, Spec* activeSpec)
409+
bool AsioAudioDriver::open(const Spec& spec, Spec* activeSpec)
414410
{
415-
LOGI() << "try open: " << device;
416-
const char* name = device.c_str();
411+
LOGI() << "try open: " << spec.deviceId;
412+
IF_ASSERT_FAILED(!spec.deviceId.empty()) {
413+
return false;
414+
}
415+
416+
const char* name = spec.deviceId.c_str();
417417
bool ok = s_adata.drivers.loadDriver(const_cast<char*>(name));
418418
if (!ok) {
419419
LOGE() << "failed load driver: " << name;
@@ -579,53 +579,7 @@ async::Channel<AsioAudioDriver::Spec> AsioAudioDriver::activeSpecChanged() const
579579
return m_activeSpecChanged;
580580
}
581581

582-
bool AsioAudioDriver::setOutputDeviceBufferSize(unsigned int bufferSize)
583-
{
584-
bool result = true;
585-
586-
if (isOpened()) {
587-
close();
588-
Spec spec = s_adata.activeSpec;
589-
spec.output.samplesPerChannel = bufferSize;
590-
result = open(spec, nullptr);
591-
}
592-
593-
if (result) {
594-
m_outputDeviceBufferSizeChanged.notify();
595-
}
596-
597-
return result;
598-
}
599-
600-
async::Notification AsioAudioDriver::outputDeviceBufferSizeChanged() const
601-
{
602-
return m_outputDeviceBufferSizeChanged;
603-
}
604-
605-
bool AsioAudioDriver::setOutputDeviceSampleRate(unsigned int sampleRate)
606-
{
607-
bool result = true;
608-
609-
if (isOpened()) {
610-
close();
611-
Spec spec = s_adata.activeSpec;
612-
spec.output.sampleRate = sampleRate;
613-
result = open(spec, nullptr);
614-
}
615-
616-
if (result) {
617-
m_outputDeviceSampleRateChanged.notify();
618-
}
619-
620-
return result;
621-
}
622-
623-
async::Notification AsioAudioDriver::outputDeviceSampleRateChanged() const
624-
{
625-
return m_outputDeviceSampleRateChanged;
626-
}
627-
628-
std::vector<unsigned int> AsioAudioDriver::availableOutputDeviceBufferSizes() const
582+
std::vector<samples_t> AsioAudioDriver::availableOutputDeviceBufferSizes() const
629583
{
630584
if (!isOpened()) {
631585
return {
@@ -637,69 +591,27 @@ std::vector<unsigned int> AsioAudioDriver::availableOutputDeviceBufferSizes() co
637591
};
638592
}
639593

640-
std::vector<unsigned int> result;
594+
std::vector<samples_t> result;
641595

642596
samples_t n = s_adata.deviceMetrics.maxSize;
643597
samples_t min = s_adata.deviceMetrics.minSize;
644598

645599
while (n >= min) {
646-
result.push_back(static_cast<unsigned int>(n));
600+
result.push_back(n);
647601
n /= 2;
648602
}
649603

650604
return result;
651605
}
652606

653-
std::vector<unsigned int> AsioAudioDriver::availableOutputDeviceSampleRates() const
607+
std::vector<sample_rate_t> AsioAudioDriver::availableOutputDeviceSampleRates() const
654608
{
655609
return {
656610
44100,
657611
48000
658612
};
659613
}
660614

661-
AudioDeviceID AsioAudioDriver::outputDevice() const
662-
{
663-
return m_deviceId;
664-
}
665-
666-
bool AsioAudioDriver::selectOutputDevice(const AudioDeviceID& id)
667-
{
668-
bool result = true;
669-
670-
if (m_deviceId == id) {
671-
return result;
672-
}
673-
674-
m_deviceId = id;
675-
676-
if (isOpened()) {
677-
close();
678-
Spec spec = s_adata.activeSpec;
679-
680-
//! NOTE We are trying to open a new device with the default value;
681-
//! it is not known what it was before.
682-
spec.output.samplesPerChannel = DEFAULT_BUFFER_SIZE;
683-
result = open(spec, nullptr);
684-
}
685-
686-
if (result) {
687-
m_outputDeviceChanged.notify();
688-
}
689-
690-
return result;
691-
}
692-
693-
bool AsioAudioDriver::resetToDefaultOutputDevice()
694-
{
695-
return selectOutputDevice(AudioDeviceID());
696-
}
697-
698-
async::Notification AsioAudioDriver::outputDeviceChanged() const
699-
{
700-
return m_outputDeviceChanged;
701-
}
702-
703615
AudioDeviceList AsioAudioDriver::availableOutputDevices() const
704616
{
705617
char names[16][32];
@@ -727,11 +639,3 @@ async::Notification AsioAudioDriver::availableOutputDevicesChanged() const
727639
{
728640
return m_availableOutputDevicesChanged;
729641
}
730-
731-
void AsioAudioDriver::resume()
732-
{
733-
}
734-
735-
void AsioAudioDriver::suspend()
736-
{
737-
}

src/framework/audio/driver/platform/win/asio/asioaudiodriver.h

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,46 +37,29 @@ class AsioAudioDriver : public IAudioDriver, public async::Asyncable
3737
void init() override;
3838

3939
std::string name() const override;
40+
41+
AudioDeviceID defaultDevice() const override;
42+
4043
bool open(const Spec& spec, Spec* activeSpec) override;
4144
void close() override;
4245
bool isOpened() const override;
4346

4447
const Spec& activeSpec() const override;
4548
async::Channel<Spec> activeSpecChanged() const override;
4649

47-
bool setOutputDeviceBufferSize(unsigned int bufferSize) override;
48-
async::Notification outputDeviceBufferSizeChanged() const override;
49-
bool setOutputDeviceSampleRate(unsigned int sampleRate) override;
50-
async::Notification outputDeviceSampleRateChanged() const override;
51-
std::vector<unsigned int> availableOutputDeviceBufferSizes() const override;
52-
std::vector<unsigned int> availableOutputDeviceSampleRates() const override;
53-
54-
AudioDeviceID outputDevice() const override;
55-
bool selectOutputDevice(const AudioDeviceID& id) override;
56-
bool resetToDefaultOutputDevice() override;
57-
async::Notification outputDeviceChanged() const override;
58-
50+
std::vector<samples_t> availableOutputDeviceBufferSizes() const override;
51+
std::vector<sample_rate_t> availableOutputDeviceSampleRates() const override;
5952
AudioDeviceList availableOutputDevices() const override;
6053
async::Notification availableOutputDevicesChanged() const override;
6154

62-
void resume() override;
63-
void suspend() override;
64-
6555
private:
6656

67-
bool doOpen(const AudioDeviceID& device, const Spec& spec, Spec* activeSpec);
6857
void reset();
6958

7059
std::thread m_thread;
7160
std::atomic<bool> m_running = false;
7261

73-
AudioDeviceID m_deviceId;
74-
async::Notification m_outputDeviceChanged;
75-
async::Notification m_availableOutputDevicesChanged;
76-
7762
async::Channel<Spec> m_activeSpecChanged;
78-
79-
async::Notification m_outputDeviceBufferSizeChanged;
80-
async::Notification m_outputDeviceSampleRateChanged;
63+
async::Notification m_availableOutputDevicesChanged;
8164
};
8265
}

0 commit comments

Comments
 (0)