Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio fixes #262

Merged
merged 11 commits into from
Aug 19, 2020
18 changes: 17 additions & 1 deletion include/aoapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#include "datatypes.h"
#include "discord_rich_presence.h"

#if defined(BASSAUDIO)
#include "bass.h"
#include "bassopus.h"
#elif defined(QTAUDIO)
#include <QMediaPlayer>
#endif

#include <QApplication>
#include <QFile>
#include <QSettings>
Expand Down Expand Up @@ -202,7 +209,8 @@ class AOApplication : public QApplication {

// Returns the value of whether custom chatboxes should be a thing.
// from the config.ini.
// I am increasingly maddened by the lack of dynamic auto-generation system for settings.
// I am increasingly maddened by the lack of dynamic auto-generation system
// for settings.
bool is_customchat_enabled();

// Returns the value of the maximum amount of lines the IC chatlog
Expand Down Expand Up @@ -433,6 +441,14 @@ class AOApplication : public QApplication {
// The file name of the log file in base/logs.
QString log_filename;

void initBASS();
static void load_bass_opus_plugin();
#ifdef BASSAUDIO
static void CALLBACK BASSreset(HSTREAM handle, DWORD channel, DWORD data,
void *user);
#endif
static void doBASSreset();

private:
const int RELEASE = 2;
const int MAJOR_VERSION = 8;
Expand Down
2 changes: 0 additions & 2 deletions include/courtroom.h
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,6 @@ private slots:
void on_casing_clicked();

void ping_server();

void load_bass_opus_plugin();
};

#endif // COURTROOM_H
68 changes: 68 additions & 0 deletions src/aoapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,71 @@ void AOApplication::call_announce_menu(Courtroom *court)
AOCaseAnnouncerDialog announcer(nullptr, this, court);
announcer.exec();
}

#ifdef BASSAUDIO
// Callback for when BASS device is lost
void CALLBACK AOApplication::BASSreset(HSTREAM handle, DWORD channel,
DWORD data, void *user)
{
doBASSreset();
}

void AOApplication::doBASSreset()
{
BASS_Free();
BASS_Init(-1, 48000, BASS_DEVICE_LATENCY, nullptr, nullptr);
load_bass_opus_plugin();
}

void AOApplication::initBASS()
{
BASS_Free();
// Change the default audio output device to be the one the user has given
// in his config.ini file for now.
unsigned int a = 0;
BASS_DEVICEINFO info;

if (get_audio_output_device() == "default") {
BASS_Init(-1, 48000, BASS_DEVICE_LATENCY, nullptr, nullptr);
load_bass_opus_plugin();
}
else {
for (a = 0; BASS_GetDeviceInfo(a, &info); a++) {
if (get_audio_output_device() == info.name) {
BASS_SetDevice(a);
BASS_Init(static_cast<int>(a), 48000, BASS_DEVICE_LATENCY, nullptr,
nullptr);
load_bass_opus_plugin();
qDebug() << info.name << "was set as the default audio output device.";
return;
}
}
BASS_Init(-1, 48000, BASS_DEVICE_LATENCY, nullptr, nullptr);
load_bass_opus_plugin();
}
}
#endif
#if (defined(_WIN32) || defined(_WIN64))
void AOApplication::load_bass_opus_plugin()
{
#ifdef BASSAUDIO
BASS_PluginLoad("bassopus.dll", 0);
#endif
}
#elif (defined(LINUX) || defined(__linux__))
void AOApplication::load_bass_opus_plugin()
{
#ifdef BASSAUDIO
BASS_PluginLoad("libbassopus.so", 0);
#endif
}
#elif defined __APPLE__
void AOApplication::load_bass_opus_plugin()
{
#ifdef BASSAUDIO
BASS_PluginLoad("libbassopus.dylib", 0);
#endif
}
#else
#error This operating system is unsupported for bass plugins.
#endif
8 changes: 7 additions & 1 deletion src/aoblipplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ void AOBlipPlayer::blip_tick()
m_cycle = 0;

HSTREAM f_stream = m_stream_list[f_cycle];
if (ao_app->get_audio_output_device() != "default")

BASS_ChannelSetDevice(f_stream, BASS_GetDevice());
int f_bass_error = BASS_ErrorGetCode();
if (f_bass_error == BASS_ERROR_DEVICE) {
ao_app->doBASSreset();
BASS_ChannelSetDevice(f_stream, BASS_GetDevice());
}

BASS_ChannelPlay(f_stream, false);
}

Expand Down
34 changes: 19 additions & 15 deletions src/aomusicplayer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "aomusicplayer.h"


AOMusicPlayer::AOMusicPlayer(QWidget *parent, AOApplication *p_ao_app)
{
m_parent = parent;
Expand Down Expand Up @@ -111,8 +110,11 @@ void AOMusicPlayer::play(QString p_song, int channel, bool loop,
else
this->set_volume(m_volume[channel], channel);

BASS_ChannelSetSync(m_stream_list[channel], BASS_SYNC_DEV_FAIL, 0,
ao_app->BASSreset, 0);

this->set_looping(loop, channel); // Have to do this here due to any
// crossfading-related changes, etc.
// crossfading-related changes, etc.
}

void AOMusicPlayer::stop(int channel)
Expand Down Expand Up @@ -164,20 +166,23 @@ void AOMusicPlayer::set_looping(bool toggle, int channel)
}
if (loop_start[channel] > 0) {
if (loop_end[channel] == 0)
loop_end[channel] = BASS_ChannelGetLength(m_stream_list[channel], BASS_POS_BYTE);
if (loop_end[channel] > 0) // Don't loop zero length songs even if we're asked to
loop_end[channel] =
BASS_ChannelGetLength(m_stream_list[channel], BASS_POS_BYTE);
if (loop_end[channel] >
0) // Don't loop zero length songs even if we're asked to
loop_sync[channel] = BASS_ChannelSetSync(
m_stream_list[channel], BASS_SYNC_POS | BASS_SYNC_MIXTIME, loop_end[channel],
loopProc, &loop_start[channel]);
m_stream_list[channel], BASS_SYNC_POS | BASS_SYNC_MIXTIME,
loop_end[channel], loopProc, &loop_start[channel]);
}
}
}
#elif defined(QTAUDIO)

AOMusicPlayer::~AOMusicPlayer() {
for (int n_stream = 0; n_stream < m_channelmax; ++n_stream) {
m_stream_list[n_stream].stop();
}
AOMusicPlayer::~AOMusicPlayer()
{
for (int n_stream = 0; n_stream < m_channelmax; ++n_stream) {
m_stream_list[n_stream].stop();
}
}

void AOMusicPlayer::play(QString p_song, int channel, bool loop,
Expand All @@ -197,10 +202,7 @@ void AOMusicPlayer::play(QString p_song, int channel, bool loop,
m_stream_list[channel].play();
}

void AOMusicPlayer::stop(int channel)
{
m_stream_list[channel].stop();
}
void AOMusicPlayer::stop(int channel) { m_stream_list[channel].stop(); }

void AOMusicPlayer::set_volume(int p_value, int channel)
{
Expand All @@ -213,7 +215,9 @@ void AOMusicPlayer::set_volume(int p_value, int channel)
AOMusicPlayer::~AOMusicPlayer() {}

void AOMusicPlayer::play(QString p_song, int channel, bool loop,
int effect_flags) {}
int effect_flags)
{
}

void AOMusicPlayer::stop(int channel) {}

Expand Down
3 changes: 3 additions & 0 deletions src/aooptionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,9 @@ void AOOptionsDialog::save_pressed()
configini->setValue("casing_can_host_cases",
ui_casing_cm_cases_textbox->text());

#ifdef BASSAUDIO
ao_app->initBASS();
#endif
callwordsini->close();
done(0);
}
Expand Down
14 changes: 11 additions & 3 deletions src/aosfxplayer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "aosfxplayer.h"
#include "file_functions.h"


AOSfxPlayer::AOSfxPlayer(QWidget *parent, AOApplication *p_ao_app)
{
m_parent = parent;
Expand Down Expand Up @@ -68,9 +67,16 @@ void AOSfxPlayer::play(QString p_sfx, QString p_char, QString shout,

set_volume_internal(m_volume);

if (ao_app->get_audio_output_device() != "default")
BASS_ChannelSetDevice(m_stream_list[m_channel], BASS_GetDevice());
int f_bass_error = BASS_ErrorGetCode();
if (f_bass_error == BASS_ERROR_DEVICE) {
ao_app->doBASSreset();
BASS_ChannelSetDevice(m_stream_list[m_channel], BASS_GetDevice());
}

BASS_ChannelPlay(m_stream_list[m_channel], false);
BASS_ChannelSetSync(m_stream_list[channel], BASS_SYNC_DEV_FAIL, 0,
ao_app->BASSreset, 0);
}

void AOSfxPlayer::stop(int channel)
Expand Down Expand Up @@ -199,7 +205,9 @@ void AOSfxPlayer::clear() {}
void AOSfxPlayer::loop_clear() {}

void AOSfxPlayer::play(QString p_sfx, QString p_char, QString shout,
int channel) {}
int channel)
{
}

void AOSfxPlayer::stop(int channel) {}

Expand Down
47 changes: 1 addition & 46 deletions src/courtroom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,7 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow()
{
ao_app = p_ao_app;
#ifdef BASSAUDIO
// Change the default audio output device to be the one the user has given
// in his config.ini file for now.
unsigned int a = 0;
BASS_DEVICEINFO info;

if (ao_app->get_audio_output_device() == "default") {
BASS_Init(-1, 48000, BASS_DEVICE_LATENCY, nullptr, nullptr);
load_bass_opus_plugin();
}
else {
for (a = 0; BASS_GetDeviceInfo(a, &info); a++) {
if (ao_app->get_audio_output_device() == info.name) {
BASS_SetDevice(a);
BASS_Init(static_cast<int>(a), 48000, BASS_DEVICE_LATENCY, nullptr,
nullptr);
load_bass_opus_plugin();
qDebug() << info.name << "was set as the default audio output device.";
break;
}
}
}
ao_app->initBASS();
#elif defined QTAUDIO

if (ao_app->get_audio_output_device() != "default") {
Expand Down Expand Up @@ -4761,28 +4741,3 @@ Courtroom::~Courtroom()
delete objection_player;
delete blip_player;
}

#if (defined(_WIN32) || defined(_WIN64))
void Courtroom::load_bass_opus_plugin()
{
#ifdef BASSAUDIO
BASS_PluginLoad("bassopus.dll", 0);
#endif
}
#elif (defined(LINUX) || defined(__linux__))
void Courtroom::load_bass_opus_plugin()
{
#ifdef BASSAUDIO
BASS_PluginLoad("libbassopus.so", 0);
#endif
}
#elif defined __APPLE__
void Courtroom::load_bass_opus_plugin()
{
#ifdef BASSAUDIO
BASS_PluginLoad("libbassopus.dylib", 0);
#endif
}
#else
#error This operating system is unsupported for bass plugins.
#endif