From 1139bf5cd0473817ba223ac8a9fe9d193575206a Mon Sep 17 00:00:00 2001 From: iamgoofball Date: Sat, 19 Jan 2019 21:01:19 -0800 Subject: Bass.dll functionality-based clientside music looping system by using channel loopable flags (no use of QTimer required) Implement Goofball's AOV loopable music server message where any value that's not -1 when the length of the handle_message packet is longer than 3 will not loop the music (still confused about this but w/e) --- src/aomusicplayer.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/aomusicplayer.cpp') diff --git a/src/aomusicplayer.cpp b/src/aomusicplayer.cpp index 2791809..781a90c 100644 --- a/src/aomusicplayer.cpp +++ b/src/aomusicplayer.cpp @@ -17,22 +17,42 @@ void AOMusicPlayer::play(QString p_song) BASS_ChannelStop(m_stream); QString f_path = ao_app->get_music_path(p_song); - - m_stream = BASS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, BASS_STREAM_AUTOFREE | BASS_UNICODE | BASS_ASYNCFILE); + unsigned int flags = BASS_STREAM_AUTOFREE | BASS_UNICODE | BASS_ASYNCFILE; + m_stream = BASS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); this->set_volume(m_volume); - + this->set_looping(m_looping); if (ao_app->get_audio_output_device() != "default") BASS_ChannelSetDevice(m_stream, BASS_GetDevice()); BASS_ChannelPlay(m_stream, false); } +void AOMusicPlayer::stop() +{ + BASS_ChannelStop(m_stream); +} + void AOMusicPlayer::set_volume(int p_value) { m_volume = p_value; float volume = m_volume / 100.0f; BASS_ChannelSetAttribute(m_stream, BASS_ATTRIB_VOL, volume); } + +void AOMusicPlayer::set_looping(bool toggle) +{ + m_looping = toggle; + if (BASS_ChannelFlags(m_stream, 0, 0) & BASS_SAMPLE_LOOP) + { + if (m_looping == false) + BASS_ChannelFlags(m_stream, 0, BASS_SAMPLE_LOOP); // remove the LOOP flag + } + else + { + if (m_looping == true) + BASS_ChannelFlags(m_stream, BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); // set the LOOP flag + } +} #elif defined(QTAUDIO) AOMusicPlayer::AOMusicPlayer(QWidget *parent, AOApplication *p_ao_app) { -- cgit From 3899dbe0bd82875214ebd676130692120f89a412 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Fri, 20 Sep 2019 05:30:07 +0300 Subject: I dunno what the fuck was I doing for the past 4 hours but I made crossfading music work. Music packets can receive channel to play the song in and the crossfading option too. --- src/aomusicplayer.cpp | 94 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 19 deletions(-) (limited to 'src/aomusicplayer.cpp') diff --git a/src/aomusicplayer.cpp b/src/aomusicplayer.cpp index 781a90c..f6bcb17 100644 --- a/src/aomusicplayer.cpp +++ b/src/aomusicplayer.cpp @@ -1,6 +1,6 @@ #include "aomusicplayer.h" -#if defined(BASSAUDIO) +#ifdef BASSAUDIO AOMusicPlayer::AOMusicPlayer(QWidget *parent, AOApplication *p_ao_app) { m_parent = parent; @@ -9,48 +9,104 @@ AOMusicPlayer::AOMusicPlayer(QWidget *parent, AOApplication *p_ao_app) AOMusicPlayer::~AOMusicPlayer() { - BASS_ChannelStop(m_stream); + for (int n_stream = 0 ; n_stream < m_channelmax ; ++n_stream) + { + BASS_ChannelStop(m_stream_list[n_stream]); + } } -void AOMusicPlayer::play(QString p_song) +void AOMusicPlayer::play(QString p_song, int channel, bool crossfade) { - BASS_ChannelStop(m_stream); - + channel = channel % m_channelmax; QString f_path = ao_app->get_music_path(p_song); - unsigned int flags = BASS_STREAM_AUTOFREE | BASS_UNICODE | BASS_ASYNCFILE; - m_stream = BASS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); +// QString d_path = f_path + ".txt"; + +// if (file_exists(d_path)) //Contains loop/etc. information file +// { +// QStringList lines = ao_app->read_file(d_path).split("\n"); +// foreach (QString line, lines) +// { +// QStringList args = line.split("="); +// if (args.size() < 2) +// continue; +// QString arg = args[0].trimmed(); +// if (arg == "loop_start") +// loop_start = args[1].trimmed().toUInt(); +// else if (arg == "loop_end") +// loop_end = args[1].trimmed().toUInt(); +// } +// qDebug() << "Found data file for song" << p_song << loop_start << loop_end; +// } + + unsigned int flags = BASS_STREAM_PRESCAN | BASS_STREAM_AUTOFREE | BASS_UNICODE | BASS_ASYNCFILE; + DWORD newstream = BASS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); - this->set_volume(m_volume); - this->set_looping(m_looping); if (ao_app->get_audio_output_device() != "default") - BASS_ChannelSetDevice(m_stream, BASS_GetDevice()); - BASS_ChannelPlay(m_stream, false); + BASS_ChannelSetDevice(m_stream_list[channel], BASS_GetDevice()); + + if (BASS_ChannelIsActive(m_stream_list[channel]) == BASS_ACTIVE_PLAYING && crossfade) + { + DWORD oldstream = m_stream_list[channel]; + + //Fade out the other sample and stop it + BASS_ChannelSlideAttribute(oldstream, BASS_ATTRIB_VOL|BASS_SLIDE_LOG, -1, 5000); + BASS_ChannelLock(oldstream, true); + //Mute the new sample + BASS_ChannelSetAttribute(newstream, BASS_ATTRIB_VOL, 0); + //Sync it with the new sample + BASS_ChannelSetPosition(newstream, BASS_ChannelGetPosition(oldstream, BASS_POS_BYTE), BASS_POS_BYTE); + //Start it + BASS_ChannelPlay(newstream, false); + //Fade in our sample + BASS_ChannelSlideAttribute(newstream, BASS_ATTRIB_VOL, static_cast(m_volume / 100.0f), 1000); + BASS_ChannelLock(oldstream, false); + m_stream_list[channel] = newstream; + } + else + { + BASS_ChannelStop(m_stream_list[channel]); + m_stream_list[channel] = newstream; + BASS_ChannelPlay(m_stream_list[channel], false); + this->set_volume(m_volume); + } + + this->set_looping(m_looping); //Have to do this here due to any crossfading-related changes, etc. } -void AOMusicPlayer::stop() +void AOMusicPlayer::stop(int channel) { - BASS_ChannelStop(m_stream); + BASS_ChannelStop(m_stream_list[channel]); } -void AOMusicPlayer::set_volume(int p_value) +void AOMusicPlayer::set_volume(int p_value, int channel) { m_volume = p_value; float volume = m_volume / 100.0f; - BASS_ChannelSetAttribute(m_stream, BASS_ATTRIB_VOL, volume); + BASS_ChannelSetAttribute(m_stream_list[channel], BASS_ATTRIB_VOL, volume); } -void AOMusicPlayer::set_looping(bool toggle) +//void CALLBACK loopProc(HSYNC handle, DWORD channel, DWORD data, void *user) +//{ +// AOMusicPlayer *self= static_cast(user); +// qDebug() << BASS_ChannelGetPosition(channel, BASS_POS_BYTE); +// BASS_ChannelSetPosition(channel, self->loop_start, BASS_POS_BYTE); +//} + +void AOMusicPlayer::set_looping(bool toggle, int channel) { m_looping = toggle; - if (BASS_ChannelFlags(m_stream, 0, 0) & BASS_SAMPLE_LOOP) + if (BASS_ChannelFlags(m_stream_list[channel], 0, 0) & BASS_SAMPLE_LOOP) { if (m_looping == false) - BASS_ChannelFlags(m_stream, 0, BASS_SAMPLE_LOOP); // remove the LOOP flag + BASS_ChannelFlags(m_stream_list[channel], 0, BASS_SAMPLE_LOOP); // remove the LOOP flag +// BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync); } else { if (m_looping == true) - BASS_ChannelFlags(m_stream, BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); // set the LOOP flag + BASS_ChannelFlags(m_stream_list[channel], BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); // set the LOOP flag +// if (loop_end > 0 && loop_end < BASS_ChannelGetLength(m_stream_list[channel], BASS_POS_BYTE)) +// loop_sync = BASS_ChannelSetSync(m_stream_list[channel], BASS_SYNC_POS | BASS_SYNC_MIXTIME, loop_end, loopProc, this); } } #elif defined(QTAUDIO) -- cgit From 842b829bee8b840149c0042bb7adfc3bda6eec9d Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Fri, 20 Sep 2019 22:11:37 +0300 Subject: Tweak the ambience/crosfade layer to fade in regardless of another sample being there Fix music not being looped sometimes Prevent channel less than 0 from being passed Set up a new music display element with an attached music name, it displays a scrolling text of the currently playing music Fix music packet processing issues Make SFX slider responsible for all music channels besides 0 (actual music) scrolltext.cpp code recipe was taken from https://stackoverflow.com/questions/10651514/text-scrolling-marquee-in-qlabel - thanks to leemes for that one, I only adapted it for newer C++ version and tweaked some stuff. --- src/aomusicplayer.cpp | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'src/aomusicplayer.cpp') diff --git a/src/aomusicplayer.cpp b/src/aomusicplayer.cpp index f6bcb17..c845948 100644 --- a/src/aomusicplayer.cpp +++ b/src/aomusicplayer.cpp @@ -18,6 +18,8 @@ AOMusicPlayer::~AOMusicPlayer() void AOMusicPlayer::play(QString p_song, int channel, bool crossfade) { channel = channel % m_channelmax; + if (channel < 0) //wtf? + return; QString f_path = ao_app->get_music_path(p_song); // QString d_path = f_path + ".txt"; @@ -39,27 +41,33 @@ void AOMusicPlayer::play(QString p_song, int channel, bool crossfade) // } unsigned int flags = BASS_STREAM_PRESCAN | BASS_STREAM_AUTOFREE | BASS_UNICODE | BASS_ASYNCFILE; + if (m_looping) + flags |= BASS_SAMPLE_LOOP; DWORD newstream = BASS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); if (ao_app->get_audio_output_device() != "default") BASS_ChannelSetDevice(m_stream_list[channel], BASS_GetDevice()); - if (BASS_ChannelIsActive(m_stream_list[channel]) == BASS_ACTIVE_PLAYING && crossfade) + if (crossfade) { DWORD oldstream = m_stream_list[channel]; - - //Fade out the other sample and stop it - BASS_ChannelSlideAttribute(oldstream, BASS_ATTRIB_VOL|BASS_SLIDE_LOG, -1, 5000); - BASS_ChannelLock(oldstream, true); //Mute the new sample BASS_ChannelSetAttribute(newstream, BASS_ATTRIB_VOL, 0); - //Sync it with the new sample - BASS_ChannelSetPosition(newstream, BASS_ChannelGetPosition(oldstream, BASS_POS_BYTE), BASS_POS_BYTE); + //Crossfade time + if (BASS_ChannelIsActive(oldstream) == BASS_ACTIVE_PLAYING) + { + //Fade out the other sample and stop it + BASS_ChannelSlideAttribute(oldstream, BASS_ATTRIB_VOL|BASS_SLIDE_LOG, -1, 5000); + BASS_ChannelLock(oldstream, true); + //Sync it with the new sample + BASS_ChannelSetPosition(newstream, BASS_ChannelGetPosition(oldstream, BASS_POS_BYTE), BASS_POS_BYTE); + BASS_ChannelLock(oldstream, false); + } //Start it BASS_ChannelPlay(newstream, false); //Fade in our sample BASS_ChannelSlideAttribute(newstream, BASS_ATTRIB_VOL, static_cast(m_volume / 100.0f), 1000); - BASS_ChannelLock(oldstream, false); + m_stream_list[channel] = newstream; } else @@ -67,7 +75,7 @@ void AOMusicPlayer::play(QString p_song, int channel, bool crossfade) BASS_ChannelStop(m_stream_list[channel]); m_stream_list[channel] = newstream; BASS_ChannelPlay(m_stream_list[channel], false); - this->set_volume(m_volume); + this->set_volume(m_volume, channel); } this->set_looping(m_looping); //Have to do this here due to any crossfading-related changes, etc. @@ -82,7 +90,17 @@ void AOMusicPlayer::set_volume(int p_value, int channel) { m_volume = p_value; float volume = m_volume / 100.0f; - BASS_ChannelSetAttribute(m_stream_list[channel], BASS_ATTRIB_VOL, volume); + if (channel < 0) + { + for (int n_stream = 0 ; n_stream < m_channelmax ; ++n_stream) + { + BASS_ChannelSetAttribute(m_stream_list[n_stream], BASS_ATTRIB_VOL, volume); + } + } + else + { + BASS_ChannelSetAttribute(m_stream_list[channel], BASS_ATTRIB_VOL, volume); + } } //void CALLBACK loopProc(HSYNC handle, DWORD channel, DWORD data, void *user) -- cgit From 292c425c7895afcd8a545afa40dffefea803aabc Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 1 Oct 2019 04:58:57 +0300 Subject: Fix music volume being weird due to m_volume being shared by ambience and music at the same time Fix version= being wrong --- src/aomusicplayer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/aomusicplayer.cpp') diff --git a/src/aomusicplayer.cpp b/src/aomusicplayer.cpp index c845948..b44e2e2 100644 --- a/src/aomusicplayer.cpp +++ b/src/aomusicplayer.cpp @@ -66,7 +66,7 @@ void AOMusicPlayer::play(QString p_song, int channel, bool crossfade) //Start it BASS_ChannelPlay(newstream, false); //Fade in our sample - BASS_ChannelSlideAttribute(newstream, BASS_ATTRIB_VOL, static_cast(m_volume / 100.0f), 1000); + BASS_ChannelSlideAttribute(newstream, BASS_ATTRIB_VOL, static_cast(m_volume[channel] / 100.0f), 1000); m_stream_list[channel] = newstream; } @@ -75,7 +75,7 @@ void AOMusicPlayer::play(QString p_song, int channel, bool crossfade) BASS_ChannelStop(m_stream_list[channel]); m_stream_list[channel] = newstream; BASS_ChannelPlay(m_stream_list[channel], false); - this->set_volume(m_volume, channel); + this->set_volume(m_volume[channel], channel); } this->set_looping(m_looping); //Have to do this here due to any crossfading-related changes, etc. @@ -88,8 +88,8 @@ void AOMusicPlayer::stop(int channel) void AOMusicPlayer::set_volume(int p_value, int channel) { - m_volume = p_value; - float volume = m_volume / 100.0f; + m_volume[channel] = p_value; + float volume = m_volume[channel] / 100.0f; if (channel < 0) { for (int n_stream = 0 ; n_stream < m_channelmax ; ++n_stream) -- cgit From 65332f209c9344e1d3f643ebca32a23dd6f31bdf Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Fri, 4 Oct 2019 14:41:33 +0300 Subject: fucking hell it's not accurate and doesn't match the absolute quality when it's played back in audacity jesus christ will the torture ever end --- src/aomusicplayer.cpp | 80 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 33 deletions(-) (limited to 'src/aomusicplayer.cpp') diff --git a/src/aomusicplayer.cpp b/src/aomusicplayer.cpp index b44e2e2..8fe6d15 100644 --- a/src/aomusicplayer.cpp +++ b/src/aomusicplayer.cpp @@ -21,24 +21,6 @@ void AOMusicPlayer::play(QString p_song, int channel, bool crossfade) if (channel < 0) //wtf? return; QString f_path = ao_app->get_music_path(p_song); -// QString d_path = f_path + ".txt"; - -// if (file_exists(d_path)) //Contains loop/etc. information file -// { -// QStringList lines = ao_app->read_file(d_path).split("\n"); -// foreach (QString line, lines) -// { -// QStringList args = line.split("="); -// if (args.size() < 2) -// continue; -// QString arg = args[0].trimmed(); -// if (arg == "loop_start") -// loop_start = args[1].trimmed().toUInt(); -// else if (arg == "loop_end") -// loop_end = args[1].trimmed().toUInt(); -// } -// qDebug() << "Found data file for song" << p_song << loop_start << loop_end; -// } unsigned int flags = BASS_STREAM_PRESCAN | BASS_STREAM_AUTOFREE | BASS_UNICODE | BASS_ASYNCFILE; if (m_looping) @@ -48,6 +30,34 @@ void AOMusicPlayer::play(QString p_song, int channel, bool crossfade) if (ao_app->get_audio_output_device() != "default") BASS_ChannelSetDevice(m_stream_list[channel], BASS_GetDevice()); + QString d_path = f_path + ".txt"; + + loop_start = 0; + loop_end = 0; + if (m_looping && file_exists(d_path)) //Contains loop/etc. information file + { + QStringList lines = ao_app->read_file(d_path).split("\n"); + foreach (QString line, lines) + { + QStringList args = line.split("="); + if (args.size() < 2) + continue; + QString arg = args[0].trimmed(); + + float sample_rate; + BASS_ChannelGetAttribute(newstream, BASS_ATTRIB_FREQ, &sample_rate); + qDebug() << sample_rate << args[1].trimmed(); + + if (arg == "loop_start") + loop_start = BASS_ChannelSeconds2Bytes(newstream, args[1].trimmed().toDouble() / static_cast(sample_rate)); + else if (arg == "loop_length") + loop_end = loop_start + BASS_ChannelSeconds2Bytes(newstream, args[1].trimmed().toDouble() / static_cast(sample_rate)); + else if (arg == "loop_end") + loop_end = BASS_ChannelSeconds2Bytes(newstream, args[1].trimmed().toDouble() / static_cast(sample_rate)); + } + qDebug() << "Found data file for song" << p_song << "length" << BASS_ChannelGetLength(newstream, BASS_POS_BYTE) << "loop start" << loop_start << "loop end" << loop_end; + } + if (crossfade) { DWORD oldstream = m_stream_list[channel]; @@ -103,28 +113,32 @@ void AOMusicPlayer::set_volume(int p_value, int channel) } } -//void CALLBACK loopProc(HSYNC handle, DWORD channel, DWORD data, void *user) -//{ -// AOMusicPlayer *self= static_cast(user); -// qDebug() << BASS_ChannelGetPosition(channel, BASS_POS_BYTE); -// BASS_ChannelSetPosition(channel, self->loop_start, BASS_POS_BYTE); -//} +void CALLBACK loopProc(HSYNC handle, DWORD channel, DWORD data, void *user) +{ + AOMusicPlayer *self= static_cast(user); + qDebug() << BASS_ChannelGetPosition(channel, BASS_POS_BYTE); + BASS_ChannelSetPosition(channel, self->loop_start, BASS_POS_BYTE); +} void AOMusicPlayer::set_looping(bool toggle, int channel) { m_looping = toggle; - if (BASS_ChannelFlags(m_stream_list[channel], 0, 0) & BASS_SAMPLE_LOOP) + qDebug() << "looping" << m_looping; + if (m_looping == false && BASS_ChannelFlags(m_stream_list[channel], 0, 0) & BASS_SAMPLE_LOOP) { - if (m_looping == false) - BASS_ChannelFlags(m_stream_list[channel], 0, BASS_SAMPLE_LOOP); // remove the LOOP flag -// BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync); + BASS_ChannelFlags(m_stream_list[channel], 0, BASS_SAMPLE_LOOP); // remove the LOOP flag + BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync[channel]); } - else + else if (m_looping == true) { - if (m_looping == true) - BASS_ChannelFlags(m_stream_list[channel], BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); // set the LOOP flag -// if (loop_end > 0 && loop_end < BASS_ChannelGetLength(m_stream_list[channel], BASS_POS_BYTE)) -// loop_sync = BASS_ChannelSetSync(m_stream_list[channel], BASS_SYNC_POS | BASS_SYNC_MIXTIME, loop_end, loopProc, this); + BASS_ChannelFlags(m_stream_list[channel], BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); // set the LOOP flag + BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync[channel]); + qDebug() << loop_end; + if (loop_end > 0) + { + loop_sync[channel] = BASS_ChannelSetSync(m_stream_list[channel], BASS_SYNC_POS | BASS_SYNC_MIXTIME, loop_end, loopProc, this); + qDebug() << "Started loop sync"; + } } } #elif defined(QTAUDIO) -- cgit From 9f543f9ef7109340142134124e8bda6c9f750a18 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Sat, 5 Oct 2019 20:32:19 +0300 Subject: I HAVE CONQUERED BASS HELL BEHOLD, LOOP_START AND LOOP_END MUSIC POINTS! It reads the songname.mp3.txt file, looking for loop_start, loop_length and loop_end in samples MUSIC EFFECTS SYSTEM THAT CAN ***COMMUNICATE WITH THE SERVER***, WOAHHHHHHHHH! RIGHT-CLICK CONTEXT MENUS TO ENABLE/DISABLE SPECIFIC MUSIC EFFECTS MUSIC EFFECTS ENUMS Fix an issue with music looping --- src/aomusicplayer.cpp | 93 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 37 deletions(-) (limited to 'src/aomusicplayer.cpp') diff --git a/src/aomusicplayer.cpp b/src/aomusicplayer.cpp index 8fe6d15..49cf089 100644 --- a/src/aomusicplayer.cpp +++ b/src/aomusicplayer.cpp @@ -15,7 +15,7 @@ AOMusicPlayer::~AOMusicPlayer() } } -void AOMusicPlayer::play(QString p_song, int channel, bool crossfade) +void AOMusicPlayer::play(QString p_song, int channel, bool loop, int effect_flags) { channel = channel % m_channelmax; if (channel < 0) //wtf? @@ -23,8 +23,9 @@ void AOMusicPlayer::play(QString p_song, int channel, bool crossfade) QString f_path = ao_app->get_music_path(p_song); unsigned int flags = BASS_STREAM_PRESCAN | BASS_STREAM_AUTOFREE | BASS_UNICODE | BASS_ASYNCFILE; - if (m_looping) + if (loop) flags |= BASS_SAMPLE_LOOP; + DWORD newstream = BASS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); if (ao_app->get_audio_output_device() != "default") @@ -33,8 +34,8 @@ void AOMusicPlayer::play(QString p_song, int channel, bool crossfade) QString d_path = f_path + ".txt"; loop_start = 0; - loop_end = 0; - if (m_looping && file_exists(d_path)) //Contains loop/etc. information file + loop_end = BASS_ChannelGetLength(newstream, BASS_POS_BYTE); + if (loop && file_exists(d_path)) //Contains loop/etc. information file { QStringList lines = ao_app->read_file(d_path).split("\n"); foreach (QString line, lines) @@ -46,49 +47,60 @@ void AOMusicPlayer::play(QString p_song, int channel, bool crossfade) float sample_rate; BASS_ChannelGetAttribute(newstream, BASS_ATTRIB_FREQ, &sample_rate); - qDebug() << sample_rate << args[1].trimmed(); + //Grab number of bytes for sample size + int sample_size = 16/8; + + //number of channels (stereo/mono) + int num_channels = 2; + + //Calculate the bytes for loop_start/loop_end to use with the sync proc + QWORD bytes = static_cast(args[1].trimmed().toFloat() * sample_size * num_channels); if (arg == "loop_start") - loop_start = BASS_ChannelSeconds2Bytes(newstream, args[1].trimmed().toDouble() / static_cast(sample_rate)); + loop_start = bytes; else if (arg == "loop_length") - loop_end = loop_start + BASS_ChannelSeconds2Bytes(newstream, args[1].trimmed().toDouble() / static_cast(sample_rate)); + loop_end = loop_start + bytes; else if (arg == "loop_end") - loop_end = BASS_ChannelSeconds2Bytes(newstream, args[1].trimmed().toDouble() / static_cast(sample_rate)); + loop_end = bytes; } qDebug() << "Found data file for song" << p_song << "length" << BASS_ChannelGetLength(newstream, BASS_POS_BYTE) << "loop start" << loop_start << "loop end" << loop_end; } - if (crossfade) + if (BASS_ChannelIsActive(m_stream_list[channel]) == BASS_ACTIVE_PLAYING) { DWORD oldstream = m_stream_list[channel]; - //Mute the new sample - BASS_ChannelSetAttribute(newstream, BASS_ATTRIB_VOL, 0); - //Crossfade time - if (BASS_ChannelIsActive(oldstream) == BASS_ACTIVE_PLAYING) + + if (effect_flags & SYNC_POS) { - //Fade out the other sample and stop it - BASS_ChannelSlideAttribute(oldstream, BASS_ATTRIB_VOL|BASS_SLIDE_LOG, -1, 5000); BASS_ChannelLock(oldstream, true); //Sync it with the new sample BASS_ChannelSetPosition(newstream, BASS_ChannelGetPosition(oldstream, BASS_POS_BYTE), BASS_POS_BYTE); BASS_ChannelLock(oldstream, false); } - //Start it - BASS_ChannelPlay(newstream, false); - //Fade in our sample - BASS_ChannelSlideAttribute(newstream, BASS_ATTRIB_VOL, static_cast(m_volume[channel] / 100.0f), 1000); - m_stream_list[channel] = newstream; + if (effect_flags & FADE_OUT) + { + //Fade out the other sample and stop it (due to -1) + BASS_ChannelSlideAttribute(oldstream, BASS_ATTRIB_VOL|BASS_SLIDE_LOG, -1, 4000); + } + else + BASS_ChannelStop(oldstream); //Stop the sample since we don't need it anymore } else - { BASS_ChannelStop(m_stream_list[channel]); - m_stream_list[channel] = newstream; - BASS_ChannelPlay(m_stream_list[channel], false); - this->set_volume(m_volume[channel], channel); + + m_stream_list[channel] = newstream; + BASS_ChannelPlay(m_stream_list[channel], false); + if (effect_flags & FADE_IN) + { + //Fade in our sample + BASS_ChannelSetAttribute(newstream, BASS_ATTRIB_VOL, 0); + BASS_ChannelSlideAttribute(newstream, BASS_ATTRIB_VOL, static_cast(m_volume[channel] / 100.0f), 1000); } + else + this->set_volume(m_volume[channel], channel); - this->set_looping(m_looping); //Have to do this here due to any crossfading-related changes, etc. + this->set_looping(loop); //Have to do this here due to any crossfading-related changes, etc. } void AOMusicPlayer::stop(int channel) @@ -115,29 +127,36 @@ void AOMusicPlayer::set_volume(int p_value, int channel) void CALLBACK loopProc(HSYNC handle, DWORD channel, DWORD data, void *user) { - AOMusicPlayer *self= static_cast(user); - qDebug() << BASS_ChannelGetPosition(channel, BASS_POS_BYTE); - BASS_ChannelSetPosition(channel, self->loop_start, BASS_POS_BYTE); + QWORD loop_start = *(static_cast(user)); + BASS_ChannelLock(channel, true); + BASS_ChannelSetPosition(channel, loop_start, BASS_POS_BYTE); + BASS_ChannelLock(channel, false); } void AOMusicPlayer::set_looping(bool toggle, int channel) { m_looping = toggle; - qDebug() << "looping" << m_looping; - if (m_looping == false && BASS_ChannelFlags(m_stream_list[channel], 0, 0) & BASS_SAMPLE_LOOP) + if (!m_looping) { - BASS_ChannelFlags(m_stream_list[channel], 0, BASS_SAMPLE_LOOP); // remove the LOOP flag + if (BASS_ChannelFlags(m_stream_list[channel], 0, 0) & BASS_SAMPLE_LOOP) + BASS_ChannelFlags(m_stream_list[channel], 0, BASS_SAMPLE_LOOP); // remove the LOOP flag BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync[channel]); + loop_sync[channel] = 0; } - else if (m_looping == true) + else { BASS_ChannelFlags(m_stream_list[channel], BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); // set the LOOP flag - BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync[channel]); - qDebug() << loop_end; - if (loop_end > 0) + if (loop_sync[channel] != 0) + { + BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync[channel]); //remove the sync + loop_sync[channel] = 0; + } + if (loop_start > 0) { - loop_sync[channel] = BASS_ChannelSetSync(m_stream_list[channel], BASS_SYNC_POS | BASS_SYNC_MIXTIME, loop_end, loopProc, this); - qDebug() << "Started loop sync"; + if (loop_end == 0) + loop_end = BASS_ChannelGetLength(m_stream_list[channel], BASS_POS_BYTE); + if (loop_end > 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, loopProc, &loop_start); } } } -- cgit From a49c4a503bcdfd09b9bbda3552e1598a4d147f40 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Mon, 4 Nov 2019 16:10:54 +0300 Subject: add .opus support --- src/aomusicplayer.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/aomusicplayer.cpp') diff --git a/src/aomusicplayer.cpp b/src/aomusicplayer.cpp index 49cf089..9a96b3e 100644 --- a/src/aomusicplayer.cpp +++ b/src/aomusicplayer.cpp @@ -26,7 +26,11 @@ void AOMusicPlayer::play(QString p_song, int channel, bool loop, int effect_flag if (loop) flags |= BASS_SAMPLE_LOOP; - DWORD newstream = BASS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); + DWORD newstream; + if (f_path.endsWith(".opus")) + newstream = BASS_OPUS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); + else + newstream = BASS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); if (ao_app->get_audio_output_device() != "default") BASS_ChannelSetDevice(m_stream_list[channel], BASS_GetDevice()); -- cgit From c8e12558cdd3fd0769b81679ad09edf1f29b780f Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Fri, 22 May 2020 01:18:24 +0300 Subject: Clang-ify the code with this styling using Visual Studio Code: { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Stroustrup, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All } (this is the Visual Studio preset with only "BreakBeforeBraces" changed from Allman to Stroustrup) --- src/aomusicplayer.cpp | 262 ++++++++++++++++++++++++-------------------------- 1 file changed, 123 insertions(+), 139 deletions(-) (limited to 'src/aomusicplayer.cpp') diff --git a/src/aomusicplayer.cpp b/src/aomusicplayer.cpp index 9a96b3e..77222e1 100644 --- a/src/aomusicplayer.cpp +++ b/src/aomusicplayer.cpp @@ -3,108 +3,102 @@ #ifdef BASSAUDIO AOMusicPlayer::AOMusicPlayer(QWidget *parent, AOApplication *p_ao_app) { - m_parent = parent; - ao_app = p_ao_app; + m_parent = parent; + ao_app = p_ao_app; } AOMusicPlayer::~AOMusicPlayer() { - for (int n_stream = 0 ; n_stream < m_channelmax ; ++n_stream) - { - BASS_ChannelStop(m_stream_list[n_stream]); - } + for (int n_stream = 0; n_stream < m_channelmax; ++n_stream) { + BASS_ChannelStop(m_stream_list[n_stream]); + } } void AOMusicPlayer::play(QString p_song, int channel, bool loop, int effect_flags) { - channel = channel % m_channelmax; - if (channel < 0) //wtf? - return; - QString f_path = ao_app->get_music_path(p_song); - - unsigned int flags = BASS_STREAM_PRESCAN | BASS_STREAM_AUTOFREE | BASS_UNICODE | BASS_ASYNCFILE; - if (loop) - flags |= BASS_SAMPLE_LOOP; - - DWORD newstream; - if (f_path.endsWith(".opus")) - newstream = BASS_OPUS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); - else - newstream = BASS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); - - if (ao_app->get_audio_output_device() != "default") - BASS_ChannelSetDevice(m_stream_list[channel], BASS_GetDevice()); - - QString d_path = f_path + ".txt"; - - loop_start = 0; - loop_end = BASS_ChannelGetLength(newstream, BASS_POS_BYTE); - if (loop && file_exists(d_path)) //Contains loop/etc. information file - { - QStringList lines = ao_app->read_file(d_path).split("\n"); - foreach (QString line, lines) - { - QStringList args = line.split("="); - if (args.size() < 2) - continue; - QString arg = args[0].trimmed(); - - float sample_rate; - BASS_ChannelGetAttribute(newstream, BASS_ATTRIB_FREQ, &sample_rate); - - //Grab number of bytes for sample size - int sample_size = 16/8; - - //number of channels (stereo/mono) - int num_channels = 2; - - //Calculate the bytes for loop_start/loop_end to use with the sync proc - QWORD bytes = static_cast(args[1].trimmed().toFloat() * sample_size * num_channels); - if (arg == "loop_start") - loop_start = bytes; - else if (arg == "loop_length") - loop_end = loop_start + bytes; - else if (arg == "loop_end") - loop_end = bytes; - } - qDebug() << "Found data file for song" << p_song << "length" << BASS_ChannelGetLength(newstream, BASS_POS_BYTE) << "loop start" << loop_start << "loop end" << loop_end; - } + channel = channel % m_channelmax; + if (channel < 0) //wtf? + return; + QString f_path = ao_app->get_music_path(p_song); + + unsigned int flags = BASS_STREAM_PRESCAN | BASS_STREAM_AUTOFREE | BASS_UNICODE | BASS_ASYNCFILE; + if (loop) + flags |= BASS_SAMPLE_LOOP; + + DWORD newstream; + if (f_path.endsWith(".opus")) + newstream = BASS_OPUS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); + else + newstream = BASS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); - if (BASS_ChannelIsActive(m_stream_list[channel]) == BASS_ACTIVE_PLAYING) - { - DWORD oldstream = m_stream_list[channel]; + if (ao_app->get_audio_output_device() != "default") + BASS_ChannelSetDevice(m_stream_list[channel], BASS_GetDevice()); - if (effect_flags & SYNC_POS) + QString d_path = f_path + ".txt"; + + loop_start = 0; + loop_end = BASS_ChannelGetLength(newstream, BASS_POS_BYTE); + if (loop && file_exists(d_path)) //Contains loop/etc. information file { - BASS_ChannelLock(oldstream, true); - //Sync it with the new sample - BASS_ChannelSetPosition(newstream, BASS_ChannelGetPosition(oldstream, BASS_POS_BYTE), BASS_POS_BYTE); - BASS_ChannelLock(oldstream, false); + QStringList lines = ao_app->read_file(d_path).split("\n"); + foreach (QString line, lines) { + QStringList args = line.split("="); + if (args.size() < 2) + continue; + QString arg = args[0].trimmed(); + + float sample_rate; + BASS_ChannelGetAttribute(newstream, BASS_ATTRIB_FREQ, &sample_rate); + + //Grab number of bytes for sample size + int sample_size = 16 / 8; + + //number of channels (stereo/mono) + int num_channels = 2; + + //Calculate the bytes for loop_start/loop_end to use with the sync proc + QWORD bytes = static_cast(args[1].trimmed().toFloat() * sample_size * num_channels); + if (arg == "loop_start") + loop_start = bytes; + else if (arg == "loop_length") + loop_end = loop_start + bytes; + else if (arg == "loop_end") + loop_end = bytes; + } + qDebug() << "Found data file for song" << p_song << "length" << BASS_ChannelGetLength(newstream, BASS_POS_BYTE) << "loop start" << loop_start << "loop end" << loop_end; } - if (effect_flags & FADE_OUT) - { - //Fade out the other sample and stop it (due to -1) - BASS_ChannelSlideAttribute(oldstream, BASS_ATTRIB_VOL|BASS_SLIDE_LOG, -1, 4000); + if (BASS_ChannelIsActive(m_stream_list[channel]) == BASS_ACTIVE_PLAYING) { + DWORD oldstream = m_stream_list[channel]; + + if (effect_flags & SYNC_POS) { + BASS_ChannelLock(oldstream, true); + //Sync it with the new sample + BASS_ChannelSetPosition(newstream, BASS_ChannelGetPosition(oldstream, BASS_POS_BYTE), BASS_POS_BYTE); + BASS_ChannelLock(oldstream, false); + } + + if (effect_flags & FADE_OUT) { + //Fade out the other sample and stop it (due to -1) + BASS_ChannelSlideAttribute(oldstream, BASS_ATTRIB_VOL | BASS_SLIDE_LOG, -1, 4000); + } + else + BASS_ChannelStop(oldstream); //Stop the sample since we don't need it anymore } else - BASS_ChannelStop(oldstream); //Stop the sample since we don't need it anymore - } - else - BASS_ChannelStop(m_stream_list[channel]); + BASS_ChannelStop(m_stream_list[channel]); + + m_stream_list[channel] = newstream; + BASS_ChannelPlay(m_stream_list[channel], false); + if (effect_flags & FADE_IN) { + //Fade in our sample + BASS_ChannelSetAttribute(newstream, BASS_ATTRIB_VOL, 0); + BASS_ChannelSlideAttribute(newstream, BASS_ATTRIB_VOL, static_cast(m_volume[channel] / 100.0f), 1000); + } + else + this->set_volume(m_volume[channel], channel); - m_stream_list[channel] = newstream; - BASS_ChannelPlay(m_stream_list[channel], false); - if (effect_flags & FADE_IN) - { - //Fade in our sample - BASS_ChannelSetAttribute(newstream, BASS_ATTRIB_VOL, 0); - BASS_ChannelSlideAttribute(newstream, BASS_ATTRIB_VOL, static_cast(m_volume[channel] / 100.0f), 1000); - } - else - this->set_volume(m_volume[channel], channel); - - this->set_looping(loop); //Have to do this here due to any crossfading-related changes, etc. + this->set_looping(loop); //Have to do this here due to any crossfading-related changes, etc. } void AOMusicPlayer::stop(int channel) @@ -114,105 +108,95 @@ void AOMusicPlayer::stop(int channel) void AOMusicPlayer::set_volume(int p_value, int channel) { - m_volume[channel] = p_value; - float volume = m_volume[channel] / 100.0f; - if (channel < 0) - { - for (int n_stream = 0 ; n_stream < m_channelmax ; ++n_stream) - { - BASS_ChannelSetAttribute(m_stream_list[n_stream], BASS_ATTRIB_VOL, volume); + m_volume[channel] = p_value; + float volume = m_volume[channel] / 100.0f; + if (channel < 0) { + for (int n_stream = 0; n_stream < m_channelmax; ++n_stream) { + BASS_ChannelSetAttribute(m_stream_list[n_stream], BASS_ATTRIB_VOL, volume); + } + } + else { + BASS_ChannelSetAttribute(m_stream_list[channel], BASS_ATTRIB_VOL, volume); } - } - else - { - BASS_ChannelSetAttribute(m_stream_list[channel], BASS_ATTRIB_VOL, volume); - } } void CALLBACK loopProc(HSYNC handle, DWORD channel, DWORD data, void *user) { - QWORD loop_start = *(static_cast(user)); - BASS_ChannelLock(channel, true); - BASS_ChannelSetPosition(channel, loop_start, BASS_POS_BYTE); - BASS_ChannelLock(channel, false); + QWORD loop_start = *(static_cast(user)); + BASS_ChannelLock(channel, true); + BASS_ChannelSetPosition(channel, loop_start, BASS_POS_BYTE); + BASS_ChannelLock(channel, false); } void AOMusicPlayer::set_looping(bool toggle, int channel) { - m_looping = toggle; - if (!m_looping) - { - if (BASS_ChannelFlags(m_stream_list[channel], 0, 0) & BASS_SAMPLE_LOOP) - BASS_ChannelFlags(m_stream_list[channel], 0, BASS_SAMPLE_LOOP); // remove the LOOP flag - BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync[channel]); - loop_sync[channel] = 0; - } - else - { - BASS_ChannelFlags(m_stream_list[channel], BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); // set the LOOP flag - if (loop_sync[channel] != 0) - { - BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync[channel]); //remove the sync - loop_sync[channel] = 0; + m_looping = toggle; + if (!m_looping) { + if (BASS_ChannelFlags(m_stream_list[channel], 0, 0) & BASS_SAMPLE_LOOP) + BASS_ChannelFlags(m_stream_list[channel], 0, BASS_SAMPLE_LOOP); // remove the LOOP flag + BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync[channel]); + loop_sync[channel] = 0; } - if (loop_start > 0) - { - if (loop_end == 0) - loop_end = BASS_ChannelGetLength(m_stream_list[channel], BASS_POS_BYTE); - if (loop_end > 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, loopProc, &loop_start); + else { + BASS_ChannelFlags(m_stream_list[channel], BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); // set the LOOP flag + if (loop_sync[channel] != 0) { + BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync[channel]); //remove the sync + loop_sync[channel] = 0; + } + if (loop_start > 0) { + if (loop_end == 0) + loop_end = BASS_ChannelGetLength(m_stream_list[channel], BASS_POS_BYTE); + if (loop_end > 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, loopProc, &loop_start); + } } - } } #elif defined(QTAUDIO) AOMusicPlayer::AOMusicPlayer(QWidget *parent, AOApplication *p_ao_app) { - m_parent = parent; - ao_app = p_ao_app; + m_parent = parent; + ao_app = p_ao_app; } AOMusicPlayer::~AOMusicPlayer() { - m_player.stop(); + m_player.stop(); } void AOMusicPlayer::play(QString p_song) { - m_player.stop(); + m_player.stop(); - QString f_path = ao_app->get_music_path(p_song); + QString f_path = ao_app->get_music_path(p_song); - m_player.setMedia(QUrl::fromLocalFile(f_path)); + m_player.setMedia(QUrl::fromLocalFile(f_path)); - this->set_volume(m_volume); + this->set_volume(m_volume); - m_player.play(); + m_player.play(); } void AOMusicPlayer::set_volume(int p_value) { - m_volume = p_value; - m_player.setVolume(m_volume); + m_volume = p_value; + m_player.setVolume(m_volume); } #else AOMusicPlayer::AOMusicPlayer(QWidget *parent, AOApplication *p_ao_app) { - m_parent = parent; - ao_app = p_ao_app; + m_parent = parent; + ao_app = p_ao_app; } AOMusicPlayer::~AOMusicPlayer() { - } void AOMusicPlayer::play(QString p_song) { - } void AOMusicPlayer::set_volume(int p_value) { - } #endif -- cgit From 88de4cde0433ef5e606f38a2f1e6041f0d24a87e Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Fri, 22 May 2020 02:14:54 +0300 Subject: clang 2 electric boogaloo { BasedOnStyle: LLVM, BreakBeforeBraces: Stroustrup} --- src/aomusicplayer.cpp | 289 ++++++++++++++++++++++++++------------------------ 1 file changed, 149 insertions(+), 140 deletions(-) (limited to 'src/aomusicplayer.cpp') diff --git a/src/aomusicplayer.cpp b/src/aomusicplayer.cpp index 77222e1..bc1ab23 100644 --- a/src/aomusicplayer.cpp +++ b/src/aomusicplayer.cpp @@ -3,200 +3,209 @@ #ifdef BASSAUDIO AOMusicPlayer::AOMusicPlayer(QWidget *parent, AOApplication *p_ao_app) { - m_parent = parent; - ao_app = p_ao_app; + m_parent = parent; + ao_app = p_ao_app; } AOMusicPlayer::~AOMusicPlayer() { - for (int n_stream = 0; n_stream < m_channelmax; ++n_stream) { - BASS_ChannelStop(m_stream_list[n_stream]); - } + for (int n_stream = 0; n_stream < m_channelmax; ++n_stream) { + BASS_ChannelStop(m_stream_list[n_stream]); + } } -void AOMusicPlayer::play(QString p_song, int channel, bool loop, int effect_flags) +void AOMusicPlayer::play(QString p_song, int channel, bool loop, + int effect_flags) { - channel = channel % m_channelmax; - if (channel < 0) //wtf? - return; - QString f_path = ao_app->get_music_path(p_song); - - unsigned int flags = BASS_STREAM_PRESCAN | BASS_STREAM_AUTOFREE | BASS_UNICODE | BASS_ASYNCFILE; - if (loop) - flags |= BASS_SAMPLE_LOOP; - - DWORD newstream; - if (f_path.endsWith(".opus")) - newstream = BASS_OPUS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); - else - newstream = BASS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); - - if (ao_app->get_audio_output_device() != "default") - BASS_ChannelSetDevice(m_stream_list[channel], BASS_GetDevice()); - - QString d_path = f_path + ".txt"; - - loop_start = 0; - loop_end = BASS_ChannelGetLength(newstream, BASS_POS_BYTE); - if (loop && file_exists(d_path)) //Contains loop/etc. information file - { - QStringList lines = ao_app->read_file(d_path).split("\n"); - foreach (QString line, lines) { - QStringList args = line.split("="); - if (args.size() < 2) - continue; - QString arg = args[0].trimmed(); - - float sample_rate; - BASS_ChannelGetAttribute(newstream, BASS_ATTRIB_FREQ, &sample_rate); - - //Grab number of bytes for sample size - int sample_size = 16 / 8; - - //number of channels (stereo/mono) - int num_channels = 2; - - //Calculate the bytes for loop_start/loop_end to use with the sync proc - QWORD bytes = static_cast(args[1].trimmed().toFloat() * sample_size * num_channels); - if (arg == "loop_start") - loop_start = bytes; - else if (arg == "loop_length") - loop_end = loop_start + bytes; - else if (arg == "loop_end") - loop_end = bytes; - } - qDebug() << "Found data file for song" << p_song << "length" << BASS_ChannelGetLength(newstream, BASS_POS_BYTE) << "loop start" << loop_start << "loop end" << loop_end; + channel = channel % m_channelmax; + if (channel < 0) // wtf? + return; + QString f_path = ao_app->get_music_path(p_song); + + unsigned int flags = BASS_STREAM_PRESCAN | BASS_STREAM_AUTOFREE | + BASS_UNICODE | BASS_ASYNCFILE; + if (loop) + flags |= BASS_SAMPLE_LOOP; + + DWORD newstream; + if (f_path.endsWith(".opus")) + newstream = BASS_OPUS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); + else + newstream = BASS_StreamCreateFile(FALSE, f_path.utf16(), 0, 0, flags); + + if (ao_app->get_audio_output_device() != "default") + BASS_ChannelSetDevice(m_stream_list[channel], BASS_GetDevice()); + + QString d_path = f_path + ".txt"; + + loop_start = 0; + loop_end = BASS_ChannelGetLength(newstream, BASS_POS_BYTE); + if (loop && file_exists(d_path)) // Contains loop/etc. information file + { + QStringList lines = ao_app->read_file(d_path).split("\n"); + foreach (QString line, lines) { + QStringList args = line.split("="); + if (args.size() < 2) + continue; + QString arg = args[0].trimmed(); + + float sample_rate; + BASS_ChannelGetAttribute(newstream, BASS_ATTRIB_FREQ, &sample_rate); + + // Grab number of bytes for sample size + int sample_size = 16 / 8; + + // number of channels (stereo/mono) + int num_channels = 2; + + // Calculate the bytes for loop_start/loop_end to use with the sync proc + QWORD bytes = static_cast(args[1].trimmed().toFloat() * + sample_size * num_channels); + if (arg == "loop_start") + loop_start = bytes; + else if (arg == "loop_length") + loop_end = loop_start + bytes; + else if (arg == "loop_end") + loop_end = bytes; } - - if (BASS_ChannelIsActive(m_stream_list[channel]) == BASS_ACTIVE_PLAYING) { - DWORD oldstream = m_stream_list[channel]; - - if (effect_flags & SYNC_POS) { - BASS_ChannelLock(oldstream, true); - //Sync it with the new sample - BASS_ChannelSetPosition(newstream, BASS_ChannelGetPosition(oldstream, BASS_POS_BYTE), BASS_POS_BYTE); - BASS_ChannelLock(oldstream, false); - } - - if (effect_flags & FADE_OUT) { - //Fade out the other sample and stop it (due to -1) - BASS_ChannelSlideAttribute(oldstream, BASS_ATTRIB_VOL | BASS_SLIDE_LOG, -1, 4000); - } - else - BASS_ChannelStop(oldstream); //Stop the sample since we don't need it anymore + qDebug() << "Found data file for song" << p_song << "length" + << BASS_ChannelGetLength(newstream, BASS_POS_BYTE) << "loop start" + << loop_start << "loop end" << loop_end; + } + + if (BASS_ChannelIsActive(m_stream_list[channel]) == BASS_ACTIVE_PLAYING) { + DWORD oldstream = m_stream_list[channel]; + + if (effect_flags & SYNC_POS) { + BASS_ChannelLock(oldstream, true); + // Sync it with the new sample + BASS_ChannelSetPosition(newstream, + BASS_ChannelGetPosition(oldstream, BASS_POS_BYTE), + BASS_POS_BYTE); + BASS_ChannelLock(oldstream, false); } - else - BASS_ChannelStop(m_stream_list[channel]); - - m_stream_list[channel] = newstream; - BASS_ChannelPlay(m_stream_list[channel], false); - if (effect_flags & FADE_IN) { - //Fade in our sample - BASS_ChannelSetAttribute(newstream, BASS_ATTRIB_VOL, 0); - BASS_ChannelSlideAttribute(newstream, BASS_ATTRIB_VOL, static_cast(m_volume[channel] / 100.0f), 1000); + + if (effect_flags & FADE_OUT) { + // Fade out the other sample and stop it (due to -1) + BASS_ChannelSlideAttribute(oldstream, BASS_ATTRIB_VOL | BASS_SLIDE_LOG, + -1, 4000); } else - this->set_volume(m_volume[channel], channel); + BASS_ChannelStop( + oldstream); // Stop the sample since we don't need it anymore + } + else + BASS_ChannelStop(m_stream_list[channel]); - this->set_looping(loop); //Have to do this here due to any crossfading-related changes, etc. + m_stream_list[channel] = newstream; + BASS_ChannelPlay(m_stream_list[channel], false); + if (effect_flags & FADE_IN) { + // Fade in our sample + BASS_ChannelSetAttribute(newstream, BASS_ATTRIB_VOL, 0); + BASS_ChannelSlideAttribute(newstream, BASS_ATTRIB_VOL, + static_cast(m_volume[channel] / 100.0f), + 1000); + } + else + this->set_volume(m_volume[channel], channel); + + this->set_looping(loop); // Have to do this here due to any + // crossfading-related changes, etc. } void AOMusicPlayer::stop(int channel) { - BASS_ChannelStop(m_stream_list[channel]); + BASS_ChannelStop(m_stream_list[channel]); } void AOMusicPlayer::set_volume(int p_value, int channel) { - m_volume[channel] = p_value; - float volume = m_volume[channel] / 100.0f; - if (channel < 0) { - for (int n_stream = 0; n_stream < m_channelmax; ++n_stream) { - BASS_ChannelSetAttribute(m_stream_list[n_stream], BASS_ATTRIB_VOL, volume); - } - } - else { - BASS_ChannelSetAttribute(m_stream_list[channel], BASS_ATTRIB_VOL, volume); + m_volume[channel] = p_value; + float volume = m_volume[channel] / 100.0f; + if (channel < 0) { + for (int n_stream = 0; n_stream < m_channelmax; ++n_stream) { + BASS_ChannelSetAttribute(m_stream_list[n_stream], BASS_ATTRIB_VOL, + volume); } + } + else { + BASS_ChannelSetAttribute(m_stream_list[channel], BASS_ATTRIB_VOL, volume); + } } void CALLBACK loopProc(HSYNC handle, DWORD channel, DWORD data, void *user) { - QWORD loop_start = *(static_cast(user)); - BASS_ChannelLock(channel, true); - BASS_ChannelSetPosition(channel, loop_start, BASS_POS_BYTE); - BASS_ChannelLock(channel, false); + QWORD loop_start = *(static_cast(user)); + BASS_ChannelLock(channel, true); + BASS_ChannelSetPosition(channel, loop_start, BASS_POS_BYTE); + BASS_ChannelLock(channel, false); } void AOMusicPlayer::set_looping(bool toggle, int channel) { - m_looping = toggle; - if (!m_looping) { - if (BASS_ChannelFlags(m_stream_list[channel], 0, 0) & BASS_SAMPLE_LOOP) - BASS_ChannelFlags(m_stream_list[channel], 0, BASS_SAMPLE_LOOP); // remove the LOOP flag - BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync[channel]); - loop_sync[channel] = 0; + m_looping = toggle; + if (!m_looping) { + if (BASS_ChannelFlags(m_stream_list[channel], 0, 0) & BASS_SAMPLE_LOOP) + BASS_ChannelFlags(m_stream_list[channel], 0, + BASS_SAMPLE_LOOP); // remove the LOOP flag + BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync[channel]); + loop_sync[channel] = 0; + } + else { + BASS_ChannelFlags(m_stream_list[channel], BASS_SAMPLE_LOOP, + BASS_SAMPLE_LOOP); // set the LOOP flag + if (loop_sync[channel] != 0) { + BASS_ChannelRemoveSync(m_stream_list[channel], + loop_sync[channel]); // remove the sync + loop_sync[channel] = 0; } - else { - BASS_ChannelFlags(m_stream_list[channel], BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); // set the LOOP flag - if (loop_sync[channel] != 0) { - BASS_ChannelRemoveSync(m_stream_list[channel], loop_sync[channel]); //remove the sync - loop_sync[channel] = 0; - } - if (loop_start > 0) { - if (loop_end == 0) - loop_end = BASS_ChannelGetLength(m_stream_list[channel], BASS_POS_BYTE); - if (loop_end > 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, loopProc, &loop_start); - } + if (loop_start > 0) { + if (loop_end == 0) + loop_end = BASS_ChannelGetLength(m_stream_list[channel], BASS_POS_BYTE); + if (loop_end > 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, + loopProc, &loop_start); } + } } #elif defined(QTAUDIO) AOMusicPlayer::AOMusicPlayer(QWidget *parent, AOApplication *p_ao_app) { - m_parent = parent; - ao_app = p_ao_app; + m_parent = parent; + ao_app = p_ao_app; } -AOMusicPlayer::~AOMusicPlayer() -{ - m_player.stop(); -} +AOMusicPlayer::~AOMusicPlayer() { m_player.stop(); } void AOMusicPlayer::play(QString p_song) { - m_player.stop(); + m_player.stop(); - QString f_path = ao_app->get_music_path(p_song); + QString f_path = ao_app->get_music_path(p_song); - m_player.setMedia(QUrl::fromLocalFile(f_path)); + m_player.setMedia(QUrl::fromLocalFile(f_path)); - this->set_volume(m_volume); + this->set_volume(m_volume); - m_player.play(); + m_player.play(); } void AOMusicPlayer::set_volume(int p_value) { - m_volume = p_value; - m_player.setVolume(m_volume); + m_volume = p_value; + m_player.setVolume(m_volume); } #else AOMusicPlayer::AOMusicPlayer(QWidget *parent, AOApplication *p_ao_app) { - m_parent = parent; - ao_app = p_ao_app; + m_parent = parent; + ao_app = p_ao_app; } -AOMusicPlayer::~AOMusicPlayer() -{ -} +AOMusicPlayer::~AOMusicPlayer() {} -void AOMusicPlayer::play(QString p_song) -{ -} +void AOMusicPlayer::play(QString p_song) {} -void AOMusicPlayer::set_volume(int p_value) -{ -} +void AOMusicPlayer::set_volume(int p_value) {} #endif -- cgit