Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Title formatting strings: A few more functions (Read 1064 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Title formatting strings: A few more functions

I developed the following query in title formatting format for the foo_skip component, so differently rated tracks are played with different probabilities:

Code: [Select]
$if($not(
// Only play the following.
$and(
$not($meta_test(duplicate)),
$or(
// Play unrated.
$not($meta_test(rating)),

// (Don't play anything rated lower than 3.)

$and(
// Play rating 3 with 50% probability.
$ifequal(%rating%,3,$not($not()),0),
$greater($muldiv(4294967295,50,100),$rand())
),
$and(
// Play rating 4 with 90% probability.
$ifequal(%rating%,4,$not($not()),0),
$greater($muldiv(4294967295,90,100),$rand())
),
$and(
// Play rating 5 with 95% probability.
$ifequal(%rating%,5,$not($not()),0),
$greater($muldiv(4294967295,95,100),$rand())
)
)
)
),1,0)

As you can see, I had to use the hack "$not($not())" to get boolean true, because $and() expects a boolean and "1" is considered false. But this is in itself unlogical, because, when an empty string is false, one negation of this should return true.

The following new functions would make one's life easier when developing such title formatting strings:

  • These should follow in the footsteps of $greater() (2 arguments, returns boolean): $equal() and $less()
  • Like $ifgreater() and $ifequal() (4 arguments): $ifless() (this also is a de-facto $ifgreaterequal() when swapping arguments). These $if...() functions should also attach boolean true to the respective value the user provides.
  • $fromhex(). It should support upper- and lowercase hex digits as well as spaces between digits. Example usage: $fromhex(ffff ffff)
  • $true() and $false() also wouldn't hurt for unforeseen cases. They would have an empty string as non-boolean output.

Re: Title formatting strings: A few more functions

Reply #1
This is pretty neat, going to grab this and do some alterations for my own use case. Thanks.

Re: Title formatting strings: A few more functions

Reply #2
(See also here for practical usage.)

--------------------------

$true() and $false() should actually just pass their argument on, which is an empty string by default, and attaching their respective boolean value. If somebody needs "1" and "0" along with the boolean value, they can get it.

Inline comments would also be very useful for title formatting and query strings. Regular expressions have inline comments of the form "(?#comment text)". In title formatting strings, it could perhaps be "$(comment text)". You would, e.g., benefit from comments in the foo_skip drop-down box with one-line textbox that provides a history of queries used in the past. Comments at the start of your entries would allow for quicker assessment of what the queries actually do, so you can switch between them more hassle-free.

Re: Title formatting strings: A few more functions

Reply #3
$char() should not only support decimal numbers, but also hexidecimal strings (up to 5 digits), starting with U+ (case-insensitive). Example: $char(U+202F). Much better "interoperability" with outside information and tools than $char(8239).

Or are you willing to support hexadecimal numbers starting with 0x or in some other notation? Would be shorter than $fromhex() for other use cases and kill two birds with one stone.

Re: Title formatting strings: A few more functions

Reply #4
I cannot give you new features in the title formatting language, but I can provide you with some ideas on make the best of what is there.

Comments: A comment is essentially a piece of code which does not affect the output. The normal syntax rules should be relaxed or removed so we can write more naturally. Ideally a comment should have no negative performance impact.

In the title formatting language an expression must have the following properties to not affect the output: It must evaluate to the empty string and the boolean value false. (If we use it at the top level, the boolean value might be negligible.) It also must not have side effects. The performance only really matters, if the script is evaluated quickly for lots of tracks, e.g. in album list or a playlist.

Here are some of the functions I found with these properties:
Code: [Select]
# using "write-only" variables
$puts(COMMENT,this is a comment)
$puts(REM,for fans of batch scripts)
$puts(,the variable with the empty name)
# using string functions
$left(this is a comment,0)
$left(same but a little shorter,)
# using conditional blocks
[a comment without special characters in it]
['a comment with some special characters (,)%[]
and a line break']
[$(this is actually an unknown function error but the square brackets swallow it)]

The $puts version may be the most obvious comment, but it is the worst performance-wise (it has a side effect). Square brackets with quoted strings would be may favorite style-wise. $left with literal arguments will be optimized away by the title formatting compiler, so it should be optimal performance-wise. I don't remember whether the same optimization is applied for square brackets.

This optimization actually has a bug. The compiler replaces function calls with literal arguments by the value of the function call. Unfortunately it discards the boolean value. :( This is also what is happening with $not($not()). There are several ways around this. We could use variable access to produce true and false like so:

Code: [Select]
# initialize true at the start of the script
$puts(true,)
# $get returns false for undefined variables
$get(false)
# $get returns true for defined variables (and their string value)
$get(true)

However this feels rather clunky. Maybe we don't even need boolean literals. You used the pattern $and($if(A,true),B) in your script. That is actually the same as $if(A,B). You also do a case distinction on a numeric value in the range 1 to 5. We could use $select for this. The foo_skip plugin only cares about the string value we produce and will play the track, if it is "0" or the empty string. (Actually, the boolean value is not even accessible outside the title formatting script itself.) We could use this to make the script more readable. If we do all that you script could like like this:

Code: [Select]
$if($meta_test(duplicate),skip,
$if($meta_test(rating),
$ifgreater($select(%rating%,0,0,50,90,95),$mod($rand(),100),skip,[play]),
[play]))

I hope I could provide some ideas to make writing title formatting scripts more satisfying!