Sound updates - bugfix and enhance#358
Conversation
This moves sound from 4 static players into an unlimited QList, fixing https://bugs.launchpad.net/mudlet/+bug/1645064 It also adds volume (0-100) to the LUA playSoundFile command, with 100 (maximum) substituted if not present for backwards compatability.
|
I've no idea what the actual limit on this would be/ or if there is one. I tried playing a couple hundred sounds simultaneously, and it worked/didn't crash - Sounded like noise, though. |
SlySven
left a comment
There was a problem hiding this comment.
🛠️ You have used the wrong indentation type - you have used hard tabs instead of groups of 4 spaces for each level of indent and this can mess things up in the source code (not fatally - it just makes it harder to read and may confuse git difference detection if it is not set to ignore white-space!) 🛠️
As it happens we have not yet been through and cleaned up the coding style so it is uniform across even the C++ source code - as will be apparent if you compare, say the VarUnit class with the dlgRoomExits one...! So for the moment try and imitate the style around where you are inserting your code with the following exceptions:
- don't use the single line
if (...) simple/compound statement;form - always put the simple/compound on a new line - it avoids an observer confusing a separate following line with the code that is subject to theif(...). - try to always include braces
{,}around the code for single lines followingif(...)s orelse- the exact position of the opening{does vary in different parts of the code-base but please include them in new code! {Yes, they are missing in some places, but they should be put in eventually...}
src/TLuaInterpreter.cpp
Outdated
| { | ||
| sound.replace('\\', "/"); | ||
| } | ||
| mudlet::self()->playSound( sound, soundVolume ); |
There was a problem hiding this comment.
I'd be tempted to make mudlet::playSound(...) return a boolean indicating whether that was successful; however given that we do not test for the sound file's actual existence (actually IIRC it could be a remote resource on the Internet so that is a whole different kettle of worms!) I'd fall back to issuing an unconditional:
lua_pushboolean( L, true );
return 1;
instead of nothing with the following:
return 0;
because returning less arguments then the user has coded for will give them a nil for the missing value(s). If the end user has got into the habit of checking for a nil + error message for runtime value errors this means they will get a nil + nil pair of values on success (as far as this function believes) instead of true + nil. On the other hand I think rain is wet so who am I to say...
There was a problem hiding this comment.
I'm not sure if QMediaPlayer::Error would have populated yet since the return-to-lua would occur the next line immediately following the playSound request. It would likely still be in the process of finding/opening the sound file/ codecs.
There was a problem hiding this comment.
So just change the return on the command to a 'true' instead of nothing?
There was a problem hiding this comment.
Yep. In most cases the user/scripter won't pay any attention, but if you are using the command from the "command line" it is nice (IMHO) to get confirmation that the command went through Okay. It also work better (also IMHO) if you ARE checking for errors by checking for NOT a nil + error message because the absence of any wanted return value from a Llua command is a nil or at least that is what the variable waiting for a value gets. e.g. :
function someRandomFunction()
-- stuff
local x = 1
local y = "two"
-- stuff that doesn't change x or y
return x,y
end
then if we use this:
local x,y,z = someRandomFunction()
then x will be 1, y will be "two" and z will be nil as I understand Lua {which is not as good as some others 😉} and getting both a nil when things work and a nil when they don't is a little user unfriendly 😈.
As for a failure to find/play the sound file given; that might be a suitable case for a new "sysSoundEvent" TEvent type which might include other "callback" type features such as:
- notification of reaching the end (or some %age point in the sound playback) of a media file
- a time-out related change in the number of
QMediaPlayersif we decide to cull them {as this series of commits only ever increases them with no ability to get rid of them if not needed} - notifications of QMediaPlayer errors that arise after the request was made to play a file
These are just outline thoughts and not something for immediate action, more likely the "let's think about this at some idle moment" pile...
| Host * pH = getActiveHost(); | ||
|
|
||
| if (pH) { | ||
| pH->postMessage( "\n[ ERROR ] - Unable to create new QMediaPlayer object\n" ); |
There was a problem hiding this comment.
This should go into the errors view.
There was a problem hiding this comment.
🐛 Also, a leading new-line will break the code that detects the type of message that was posted and applies the colour coding to it...
There was a problem hiding this comment.
This error should actually never be displayed, it's really only there for a just in case thing. I'd had to reverse the if statement to test it, at which point it printed out at the end of the line that triggered the error. That's why I added the leading newline, though I will remove it. This also wouldn't be an issue though it was going to the correct window (errors, not pH). I'll take a look, but could someone post a snippet demonstrating use of the the errors window?
There was a problem hiding this comment.
Take a look though the map related classs e.g. TMap, TRoomDB, TArea, TRoom {which is an area where I have been particularly active 😊 } besides which, as you have poked around in the cTelnet::postMessage(...) you might see that the code looks for the "[ something ] - " tag on the first line. FTR once the code detects this type of string the code aligns all following lines to the same indentation as the position of the first character after the ending "... - " of the prefix so I try to ensure that all messages have the same width in the portion "[ something ] - " so that they all align on screen in the console; you may notice the in source code formatting of such messages try to replicate this alignment. I also try to avoid individual message lines including indentation to be over 80 characters so that they don't trip the line wrapping that the main console text display stuff does. This doesn't always work - especially when you have %1...%10 type variable replacements by strings...
|
Is it my imagination or has someone been rebasing - I think history has been rewritten and amongst other things I feel that a comment I made about handling the optional 2nd argument to the Lua The fact that we have conflicts here also looks suspicious! |
|
Sorry, just saw these comments - I don't use git command line: just the windows gui right now. You have to explicity add the rebase command back in to the menu to use it (I haven't). Not sure if I did something else though that might have developed issues. |
| /* set volume and play sound */ | ||
| pPlayer->setMedia( QUrl::fromLocalFile( s ) ); | ||
| pPlayer->setVolume(soundVolume); | ||
| pPlayer->play(); |
There was a problem hiding this comment.
Might I suggest this replacement for the above that better fits our current "standards" for Lua commands with better error reporting?
int TLuaInterpreter::playSoundFile( lua_State * L )
{
Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
if( ! pHost ) {
lua_pushnil( L );
lua_pushstring( L, tr( "playSoundFile: NULL Host pointer - something is wrong!" )
.toUtf8().constData() );
return 2;
}
QString sound;
if( ! lua_isstring( L, 1 ) ) {
lua_pushstring( L, tr( "playSoundFile: bad argument #1 type (sound pathAndFilename as string expected,\n"
"got %1!)" )
.arg( luaL_typename( L, 1) )
.toUtf8().constData() );
lua_error( L );
return 1;
}
else {
// Windows platforms, at least, can have non-ASCII characters in path/filenames:
sound = QString::fromUtf8( lua_tostring( L, 1 );
if( sound.isEmpty() {
lua_pushnil( L );
lua_pushstring( L, tr( "playSoundFile: bad argument #1 value (sound pathAndFilename cannot be empty.)" )
.toUtf8().constData() );
return 2;
}
#ifdef Q_OS_WIN32
sound.replace( QLatin1Char( '\\' ), QLatin1Char( '/' ) );
#endif
}
qint64 soundVolume = 100;
if( lua_gettop( L ) > 1 ) {
if( ! lua_isnumber( L, 2 ) {
lua_pushstring( L, tr( "playSoundFile: bad argument #2 type (sound volume percentage as number is\n"
"optional {default = 100 percent if ommitted}, got %1!)"
.arg( luaL_typename( L, 2 ) )
.toUtf8().constData() );
lua_error( L );
return 1;
}
soundVolume = lua_tointeger( L, 2 );
if( soundVolume < 0 || soundVolume > 100 ) {
lua_pushnil( L );
lua_pushstring( L, tr( "setAreaName: bad argument #2 value (sound volume %1 is not in range"
"{0 = silent; 100 = full, default if omitted}.)" )
.arg( soundVolume )
.toUtf8().constData() );
return 2;
}
}
mudlet::self()->playSound( sound, soundVolume );
lua_pushboolean( L, 1 );
return 1;
}
|
Apparently I was able to "fix" the conflicts that prevented merging which were three places where there were simultaneous changes to copyright lines where other PRs have gone into the development tree and they clashed with similar but different changes in this PR. In using the "on-line" resolve-conflicts command I have "Merge branch 'development' into sound-updates" as c0eb6b8 - I hope that this does not mess things up for you @Nyyrazzilyss and I'm not entirely sure how this will affect your local repository but it seems to have modified your Github one - so you might want to check that they are in sync before committing any further changes on your local copy. 😕 |
|
Where are we at with this PR? |
|
By the way, any formatting should be done using clang-format - it will apply the agreed-upon rules file in src/.clang-format. |
|
Oh also this is already in release_30 version: a10fbba |
|
I'll build on this PR to finish it up as I'd like to add an event for when the sound stops playing, and this PR changes the sound system a bit. |
|
I'd hold off on further event planning, I would like to investigate |
|
Approved this on the basis that any formatting issues will come out in a subsequent general clean-up! |
|
OK - remember about backwards compatibility though, breaking existing scripts that use .mp3's won't be a feature. |
|
Won't be an issue as we'd need a new interface/system to pre-load "SFX"s and then play them anyhow - nothing to be backward compatible with... |
This moves sound from 4 static players into an unlimited QList,
fixing https://bugs.launchpad.net/mudlet/+bug/1645064
It also adds volume (0-100) to the LUA playSoundFile command, with 100
(maximum) substituted if not present for backwards compatability.