Skip to content

Sound updates - bugfix and enhance#358

Merged
vadi2 merged 4 commits intoMudlet:developmentfrom
Nyyrazzilyss:sound-updates
Apr 19, 2017
Merged

Sound updates - bugfix and enhance#358
vadi2 merged 4 commits intoMudlet:developmentfrom
Nyyrazzilyss:sound-updates

Conversation

@Nyyrazzilyss
Copy link
Copy Markdown
Contributor

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.

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.
@Nyyrazzilyss
Copy link
Copy Markdown
Contributor Author

Nyyrazzilyss commented Dec 6, 2016

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.

Copy link
Copy Markdown
Member

@SlySven SlySven left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ 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 the if(...).
  • try to always include braces {, } around the code for single lines following if(...)s or else- 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...}

Edited to substitute for tabs
{
sound.replace('\\', "/");
}
mudlet::self()->playSound( sound, soundVolume );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just change the return on the command to a 'true' instead of nothing?

Copy link
Copy Markdown
Member

@SlySven SlySven Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 QMediaPlayers if 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" );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go into the errors view.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 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...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also applies/ed to #359.

@SlySven
Copy link
Copy Markdown
Member

SlySven commented Dec 16, 2016

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 playSoundFile(...) command has evaporated, and possibly other things as well... 😖

The fact that we have conflicts here also looks suspicious!

@Nyyrazzilyss
Copy link
Copy Markdown
Contributor Author

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();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}

@SlySven
Copy link
Copy Markdown
Member

SlySven commented Dec 21, 2016

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. 😕

@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Feb 25, 2017

Where are we at with this PR?

@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Feb 25, 2017

By the way, any formatting should be done using clang-format - it will apply the agreed-upon rules file in src/.clang-format.

@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Feb 27, 2017

Oh also this is already in release_30 version: a10fbba

@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Apr 1, 2017

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.

@SlySven
Copy link
Copy Markdown
Member

SlySven commented Apr 1, 2017

I'd hold off on further event planning, I would like to investigate QSoundEffect for faster responding (with the cost of pre-loading/caching the files and only being able to use a smaller number of audio file types - not .mp3 for instance) sound "effects" and combine that with enhancements to make use of the ability to allow the stacking of media files in a playlist and/or looping for continuous playback and/or allow allocation of media files to a particular "player" although this PR does do away with that to a certain extent. {Reminds me, I do need to check whether the pulse-audio infrastructure on my GNU/Linux machines allows different volume levels on different "players" (I think it does) - though they do disappear from the mixer widget when idle}...

@SlySven
Copy link
Copy Markdown
Member

SlySven commented Apr 1, 2017

Approved this on the basis that any formatting issues will come out in a subsequent general clean-up!

@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Apr 2, 2017

OK - remember about backwards compatibility though, breaking existing scripts that use .mp3's won't be a feature.

@SlySven
Copy link
Copy Markdown
Member

SlySven commented Apr 5, 2017

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...

@SlySven SlySven mentioned this pull request Apr 5, 2017
@vadi2 vadi2 merged commit d1fcca3 into Mudlet:development Apr 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants