diff options
Diffstat (limited to 'src/aomusicplayer.cpp')
| -rw-r--r-- | src/aomusicplayer.cpp | 160 |
1 files changed, 88 insertions, 72 deletions
diff --git a/src/aomusicplayer.cpp b/src/aomusicplayer.cpp index b9e6ab98..4265a463 100644 --- a/src/aomusicplayer.cpp +++ b/src/aomusicplayer.cpp @@ -9,48 +9,52 @@ #include <QFuture> #include <QWidget> -AOMusicPlayer::AOMusicPlayer(AOApplication *p_ao_app) - : ao_app(p_ao_app) +AOMusicPlayer::AOMusicPlayer(AOApplication *ao_app) + : ao_app(ao_app) {} AOMusicPlayer::~AOMusicPlayer() { - for (int n_stream = 0; n_stream < CHANNEL_COUNT; ++n_stream) + for (int n_stream = 0; n_stream < STREAM_COUNT; ++n_stream) { BASS_ChannelStop(m_stream_list[n_stream]); } } -QString AOMusicPlayer::play(QString p_song, int channel, bool loop, int effect_flags) +QString AOMusicPlayer::playStream(QString song, int streamId, bool loopEnabled, int effectFlags) { - channel = channel % CHANNEL_COUNT; - if (channel < 0) // wtf? + if (!ensureValidStreamId(streamId)) { return "[ERROR] Invalid Channel"; } - unsigned int flags = BASS_STREAM_PRESCAN | BASS_STREAM_AUTOFREE | BASS_UNICODE | BASS_ASYNCFILE; - unsigned int streaming_flags = BASS_STREAM_AUTOFREE; - if (loop) + + quint32 flags = BASS_STREAM_AUTOFREE; + if (loopEnabled) { flags |= BASS_SAMPLE_LOOP; - streaming_flags |= BASS_SAMPLE_LOOP; } - QString f_path = p_song; + + QString f_path = song; DWORD newstream; if (f_path.startsWith("http")) { if (!Options::getInstance().streamingEnabled()) { - BASS_ChannelStop(m_stream_list[channel]); + BASS_ChannelStop(m_stream_list[streamId]); return QObject::tr("[MISSING] Streaming disabled."); } QUrl l_url = QUrl(f_path); - newstream = BASS_StreamCreateURL(l_url.toEncoded().toStdString().c_str(), 0, streaming_flags, nullptr, 0); + newstream = BASS_StreamCreateURL(l_url.toEncoded().toStdString().c_str(), 0, flags, nullptr, 0); } else { - f_path = ao_app->get_real_path(ao_app->get_music_path(p_song)); - if (f_path.endsWith(".mo3") || f_path.endsWith(".xm") || f_path.endsWith(".mod") || f_path.endsWith(".s3m") || f_path.endsWith(".it") || f_path.endsWith(".mtm") || f_path.endsWith(".umx")) + flags |= BASS_STREAM_PRESCAN | BASS_UNICODE | BASS_ASYNCFILE; + + f_path = ao_app->get_real_path(ao_app->get_music_path(song)); + + QString extension = f_path.split('.').last(); + static const QStringList VALID_EXTENSION_LIST{"mo3", "xm", "mod", "s3m", "it", "mtm", "umx"}; + if (VALID_EXTENSION_LIST.contains(extension, Qt::CaseInsensitive)) { newstream = BASS_MusicLoad(FALSE, f_path.utf16(), 0, 0, flags, 1); } @@ -60,18 +64,17 @@ QString AOMusicPlayer::play(QString p_song, int channel, bool loop, int effect_f } } - int error_code = BASS_ErrorGetCode(); - + int error = BASS_ErrorGetCode(); if (Options::getInstance().audioOutputDevice() != "default") { - BASS_ChannelSetDevice(m_stream_list[channel], BASS_GetDevice()); + BASS_ChannelSetDevice(m_stream_list[streamId], BASS_GetDevice()); } - QString d_path = f_path + ".txt"; + m_loop_start[streamId] = 0; + m_loop_end[streamId] = 0; - m_loop_start[channel] = 0; - m_loop_end[channel] = 0; - if (loop && file_exists(d_path)) // Contains loop/etc. information file + QString d_path = f_path + ".txt"; + if (loopEnabled && file_exists(d_path)) // Contains loop/etc. information file { QStringList lines = ao_app->read_file(d_path).split("\n"); bool seconds_mode = false; @@ -90,6 +93,7 @@ QString AOMusicPlayer::play(QString p_song, int channel, bool loop, int effect_f seconds_mode = true; // Use new epic behavior continue; } + continue; } @@ -114,25 +118,25 @@ QString AOMusicPlayer::play(QString p_song, int channel, bool loop, int effect_f } if (arg == "loop_start") { - m_loop_start[channel] = bytes; + m_loop_start[streamId] = bytes; } else if (arg == "loop_length") { - m_loop_end[channel] = m_loop_start[channel] + bytes; + m_loop_end[streamId] = m_loop_start[streamId] + bytes; } else if (arg == "loop_end") { - m_loop_end[channel] = bytes; + m_loop_end[streamId] = bytes; } } - qDebug() << "Found data file for song" << p_song << "length" << BASS_ChannelGetLength(newstream, BASS_POS_BYTE) << "loop start" << m_loop_start[channel] << "loop end" << m_loop_end[channel]; + qDebug() << "Found data file for song" << song << "length" << BASS_ChannelGetLength(newstream, BASS_POS_BYTE) << "loop start" << m_loop_start[streamId] << "loop end" << m_loop_end[streamId]; } - if (BASS_ChannelIsActive(m_stream_list[channel]) == BASS_ACTIVE_PLAYING) + if (BASS_ChannelIsActive(m_stream_list[streamId]) == BASS_ACTIVE_PLAYING) { - DWORD oldstream = m_stream_list[channel]; + DWORD oldstream = m_stream_list[streamId]; - if (effect_flags & SYNC_POS) + if (effectFlags & SYNC_POS) { BASS_ChannelLock(oldstream, true); // Sync it with the new sample @@ -140,7 +144,7 @@ QString AOMusicPlayer::play(QString p_song, int channel, bool loop, int effect_f BASS_ChannelLock(oldstream, false); } - if ((effect_flags & FADE_OUT) && m_volume[channel] > 0) + if ((effectFlags & FADE_OUT) && m_volume[streamId] > 0) { // Fade out the other sample and stop it (due to -1) BASS_ChannelSlideAttribute(oldstream, BASS_ATTRIB_VOL | BASS_SLIDE_LOG, -1, 4000); @@ -152,47 +156,47 @@ QString AOMusicPlayer::play(QString p_song, int channel, bool loop, int effect_f } else { - BASS_ChannelStop(m_stream_list[channel]); + BASS_ChannelStop(m_stream_list[streamId]); } - m_stream_list[channel] = newstream; + m_stream_list[streamId] = newstream; BASS_ChannelPlay(newstream, false); - if (effect_flags & FADE_IN) + if (effectFlags & FADE_IN) { // Fade in our sample BASS_ChannelSetAttribute(newstream, BASS_ATTRIB_VOL, 0); - BASS_ChannelSlideAttribute(newstream, BASS_ATTRIB_VOL, static_cast<float>(m_volume[channel] / 100.0f), 1000); + BASS_ChannelSlideAttribute(newstream, BASS_ATTRIB_VOL, static_cast<float>(m_volume[streamId] / 100.0f), 1000); } else { - this->set_volume(m_volume[channel], channel); + this->setStreamVolume(m_volume[streamId], streamId); } BASS_ChannelSetSync(newstream, 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. + this->setStreamLooping(loopEnabled, streamId); // Have to do this here due to any + // crossfading-related changes, etc. - bool is_stop = (p_song == "~stop.mp3"); - QString p_song_clear = QUrl(p_song).fileName(); + bool is_stop = (song == "~stop.mp3"); + QString p_song_clear = QUrl(song).fileName(); p_song_clear = p_song_clear.left(p_song_clear.lastIndexOf('.')); - if (is_stop && channel == 0) + if (is_stop && streamId == 0) { // don't send text on channels besides 0 return QObject::tr("None"); } - if (error_code == BASS_ERROR_HANDLE) + if (error == BASS_ERROR_HANDLE) { // Cheap hack to see if file missing return QObject::tr("[MISSING] %1").arg(p_song_clear); } - if (p_song.startsWith("http") && channel == 0) + if (song.startsWith("http") && streamId == 0) { return QObject::tr("[STREAM] %1").arg(p_song_clear); } - if (channel == 0) + if (streamId == 0) { return p_song_clear; } @@ -200,36 +204,37 @@ QString AOMusicPlayer::play(QString p_song, int channel, bool loop, int effect_f return ""; } -void AOMusicPlayer::stop(int channel) -{ - BASS_ChannelStop(m_stream_list[channel]); -} - -void AOMusicPlayer::set_muted(bool toggle) +void AOMusicPlayer::setMuted(bool enabled) { - m_muted = toggle; + m_muted = enabled; // Update all volume based on the mute setting - for (int n_stream = 0; n_stream < CHANNEL_COUNT; ++n_stream) + for (int n_stream = 0; n_stream < STREAM_COUNT; ++n_stream) { - set_volume(m_volume[n_stream], n_stream); + setStreamVolume(m_volume[n_stream], n_stream); } } -void AOMusicPlayer::set_volume(int p_value, int channel) +void AOMusicPlayer::setStreamVolume(int value, int streamId) { - m_volume[channel] = p_value; + if (!ensureValidStreamId(streamId)) + { + qWarning().noquote() << QObject::tr("Invalid stream ID '%2'").arg(streamId); + return; + } + + m_volume[streamId] = value; // If muted, volume will always be 0 - float volume = (m_volume[channel] / 100.0f) * !m_muted; - if (channel < 0) + float volume = (m_volume[streamId] / 100.0f) * !m_muted; + if (streamId < 0) { - for (int n_stream = 0; n_stream < CHANNEL_COUNT; ++n_stream) + for (int n_stream = 0; n_stream < STREAM_COUNT; ++n_stream) { BASS_ChannelSetAttribute(m_stream_list[n_stream], BASS_ATTRIB_VOL, volume); } } else { - BASS_ChannelSetAttribute(m_stream_list[channel], BASS_ATTRIB_VOL, volume); + BASS_ChannelSetAttribute(m_stream_list[streamId], BASS_ATTRIB_VOL, volume); } } @@ -243,37 +248,48 @@ void CALLBACK loopProc(HSYNC handle, DWORD channel, DWORD data, void *user) BASS_ChannelLock(channel, false); } -void AOMusicPlayer::set_looping(bool loop_song, int channel) +void AOMusicPlayer::setStreamLooping(bool enabled, int streamId) { - if (!loop_song) + if (!ensureValidStreamId(streamId)) { - if (BASS_ChannelFlags(m_stream_list[channel], 0, 0) & BASS_SAMPLE_LOOP) + qWarning().noquote() << QObject::tr("Invalid stream ID '%2'").arg(streamId); + return; + } + + if (!enabled) + { + if (BASS_ChannelFlags(m_stream_list[streamId], 0, 0) & BASS_SAMPLE_LOOP) { - BASS_ChannelFlags(m_stream_list[channel], 0, + BASS_ChannelFlags(m_stream_list[streamId], 0, BASS_SAMPLE_LOOP); // remove the LOOP flag } - BASS_ChannelRemoveSync(m_stream_list[channel], m_loop_sync[channel]); - m_loop_sync[channel] = 0; + BASS_ChannelRemoveSync(m_stream_list[streamId], m_loop_sync[streamId]); + m_loop_sync[streamId] = 0; return; } - BASS_ChannelFlags(m_stream_list[channel], BASS_SAMPLE_LOOP, + BASS_ChannelFlags(m_stream_list[streamId], BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); // set the LOOP flag - if (m_loop_sync[channel] != 0) + if (m_loop_sync[streamId] != 0) { - BASS_ChannelRemoveSync(m_stream_list[channel], - m_loop_sync[channel]); // remove the sync - m_loop_sync[channel] = 0; + BASS_ChannelRemoveSync(m_stream_list[streamId], + m_loop_sync[streamId]); // remove the sync + m_loop_sync[streamId] = 0; } - if (m_loop_start[channel] < m_loop_end[channel]) + if (m_loop_start[streamId] < m_loop_end[streamId]) { // Loop when the endpoint is reached. - m_loop_sync[channel] = BASS_ChannelSetSync(m_stream_list[channel], BASS_SYNC_POS | BASS_SYNC_MIXTIME, m_loop_end[channel], loopProc, &m_loop_start[channel]); + m_loop_sync[streamId] = BASS_ChannelSetSync(m_stream_list[streamId], BASS_SYNC_POS | BASS_SYNC_MIXTIME, m_loop_end[streamId], loopProc, &m_loop_start[streamId]); } else { // Loop when the end of the file is reached. - m_loop_sync[channel] = BASS_ChannelSetSync(m_stream_list[channel], BASS_SYNC_END | BASS_SYNC_MIXTIME, 0, loopProc, &m_loop_start[channel]); + m_loop_sync[streamId] = BASS_ChannelSetSync(m_stream_list[streamId], BASS_SYNC_END | BASS_SYNC_MIXTIME, 0, loopProc, &m_loop_start[streamId]); } } + +bool AOMusicPlayer::ensureValidStreamId(int streamId) +{ + return (streamId >= 0 && streamId < STREAM_COUNT); +} |
