PHP: preg_match_all_callback

There are several PCRE functions available, but today I looked for one that just wasn’t there: preg_match_all_callback().

Could’ve maybe used preg_replace_callback(), but felt wrong since I didn’t actually want to do any replacing. I just needed my function to be called for each match.

So I wrote it myself. Noting it here, in case I (or someone else) needs it again.

<?php
/**
 * Perform a global regular expression match
 * and calls the callback for each match.
 */

function preg_match_all_callback(
        string $pattern,
        string $subject,
        callable $callback)
{
    $r = preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
    foreach($matches ?? [] as $match)
        $callback($match);
    return $r;
}

And, in case someone reads this post and knows it actually does exist… and if that someone is you, please do leave a comment!

And, yes, I could’ve just written those 3 lines where I needed them, but what’s the fun in that? And besides, the shorter the code where it counts, the easier what counts is to read.

Usage

preg_match_all_callback('/(\w)\w*/', 'Hello World', 'var_dump');
array (size=2)
  0 => string 'Hello' (length=5)
  1 => string 'H' (length=1)

array (size=2)
  0 => string 'World' (length=5)
  1 => string 'W' (length=1)

PHP: Get headers with actual HEAD request

PHP has a function called get_headers which, as you’d guess, gives you the headers returned from an HTTP request. However it actually uses a GET, rather than HEAD, request.

Figured out you can change this by setting a stream context, so wrapped it in a function. And posting it here in case I need it again.

Also added a cleanup of the returned array, as I found it a bit ugly when the request included redirects. See difference below code.

Note: I silence the get_headers call because it throws several warnings, e.g. if the hostname fails lookup, and I’m not really interested in why it fails.

function get_head(string $url, array $opts = [])
{
    // Store previous default context
    $prev = stream_context_get_options(stream_context_get_default());

    // Set new one with head and a small timeout
    stream_context_set_default(['http' => $opts +
        [
            'method' => 'HEAD',
            'timeout' => 2,
        ]]);

    // Do the head request
    $req = @get_headers($url, true);
    if( ! $req)
        return false;

    // Make more sane response
    foreach($req as $h => $v)
    {
        if(is_int($h))
            $headers[$h]['Status'] = $v;
        else
        {
            if(is_string($v))
                $headers[0][$h] = $v;
            else
                foreach($v as $x => $y)
                    $headers[$x][$h] = $y;
        }

    }

    // Restore previous default context and return
    stream_context_set_default($prev);
    return $headers;
}

Example response:

<?php get_head('http://geekality.net');

array (size=2)
  0 =>
    array (size=8)
      'Status' => string 'HTTP/1.1 301 Moved Permanently' (length=30)
      'Date' => string 'Mon, 06 Feb 2017 01:20:48 GMT' (length=29)
      'Server' => string 'Apache' (length=6)
      'Location' => string 'http://www.geekality.net/' (length=25)
      'Vary' => string 'Accept-Encoding' (length=15)
      'Connection' => string 'close' (length=5)
      'Content-Type' => string 'text/html; charset=iso-8859-1' (length=29)
      'Link' => string '<http://www.geekality.net/wp-json/>; rel="https://api.w.org/"' (length=61)
  1 =>
    array (size=6)
      'Date' => string 'Mon, 06 Feb 2017 01:20:48 GMT' (length=29)
      'Server' => string 'Apache' (length=6)
      'Vary' => string 'Accept-Encoding' (length=15)
      'Connection' => string 'close' (length=5)
      'Content-Type' => string 'text/html; charset=UTF-8' (length=24)
      'Status' => string 'HTTP/1.1 200 OK' (length=15)

Example response without my cleanup:

<?php get_head('http://geekality.net');

array (size=9)
  0 => string 'HTTP/1.1 301 Moved Permanently' (length=30)
  'Date' =>
    array (size=2)
      0 => string 'Mon, 06 Feb 2017 01:14:00 GMT' (length=29)
      1 => string 'Mon, 06 Feb 2017 01:14:01 GMT' (length=29)
  'Server' =>
    array (size=2)
      0 => string 'Apache' (length=6)
      1 => string 'Apache' (length=6)
  'Location' => string 'http://www.geekality.net/' (length=25)
  'Vary' =>
    array (size=2)
      0 => string 'Accept-Encoding' (length=15)
      1 => string 'Accept-Encoding' (length=15)
  'Connection' =>
    array (size=2)
      0 => string 'close' (length=5)
      1 => string 'close' (length=5)
  'Content-Type' =>
    array (size=2)
      0 => string 'text/html; charset=iso-8859-1' (length=29)
      1 => string 'text/html; charset=UTF-8' (length=24)
  1 => string 'HTTP/1.1 200 OK' (length=15)
  'Link' => string '<http://www.geekality.net/wp-json/>; rel="https://api.w.org/"' (length=61)

PHP: Convert RGB to hex and back

Just another note to self, in case I need it again:

/**
 * #rrggbb or #rgb to [r, g, b]
 */

function hex2rgb(string $hex): array
{
    $hex = ltrim($hex, '#');

    if(strlen($hex) == 3)
        return [
            hexdec($hex[0].$hex[0]),
            hexdec($hex[1].$hex[1]),
            hexdec($hex[2].$hex[2]),
        ];
    else
        return [
            hexdec($hex[0].$hex[1]),
            hexdec($hex[2].$hex[3]),
            hexdec($hex[4].$hex[5]),
        ];
}


/**
 * [r, g, b] to #rrggbb
 */

function rgb2hex(array $rgb): string
{
    return '#'
        . sprintf('%02x', $rgb[0])
        . sprintf('%02x', $rgb[1])
        . sprintf('%02x', $rgb[2]);
}

Enabling query logging on MariaDB/MySQL

When developing it can sometimes be very useful to see exactly what queries are sent to the database.

Found out there’s something called a General Query Log, and enabling it was really easy. Just add the following to your my.{ini,cnf} file:

[mysqld]
general-log=1
general-log-file=queries.log
log-output=file

Restart the server and you should now find the log in your data directory (unless no queries done yet).

If you don’t know where your data directory is, just run this query:

SHOW VARIABLES WHERE variable_name = 'datadir'

Choir practicing home alone with Musescore

Here’s a simple, hopefully helpful, guide for those who are in a choir, and maybe just got an email with a link to a bunch of Musescore-files sent to them from that weird nerd in the back row.

This is what you do.

0. Download Musescore

Go to musescore.org, download Musescore (big green button), and install it as you would any other program.

Note: If you struggle with this, and/or the next step, just ask that nerd, or a friendly one in your neighbourhood, for help 🙂

1. Get a file onto your computer

Download one of the Musescore files. You can identify them by having the extension “.mscz”, and once on your computer they might have the icon seen below. Here seen next to a PDF file, which might be more familiar to you. But yeah, you want the Musescore file now 🙂

musescore-icons

Note: If you see files whose name starts with a dot, for example “.Celtic Advent Carol.mscz”, these are temporary files that doesn’t work, so ignore them and find the one that doesn’t start with a dot 🙂

2. Open the file in Musescore and start practicing!

Once you’ve opened the file in Musescore (either via Explorer or Finder, or via File/Open inside Musescore, like you would in any other application, like Word, Pages, etc), you should have a view similar to this:

main

There might be some extra panels open, but they are mostly useful for entering sheet music. For playback, very few things are needed.

The View menu, that you see open in that image, is where you turn on and off all those various panels, including the essential Play Panel and Mixer, which we come to shortly.

Simple playback

At the middle top, you have the play-button, and a jump-back-to-the-very-start-button.

If you want to start playback from a certain place, simply click on a note to select it, and then press the play-button.

Playback with the Play Panel

In the View menu, you can open the Play Panel. It looks like this:

play

On the right you can adjust the tempo and the main volume.

The tempo can be useful in tricky parts (or if the tempo is just wrong compared to what you remember from last choir practice).

The Volume slider is mostly useful if the song has a lot of unison parts, which can sometimes result in a bit distorted sound because all the sound gets added on top of each other and ends up being too loud to function. If that happens, just nudge this one down a bit. Otherwise, just use the computer volume as you normally would.

At the bottom middle, you find the same play buttons as in the main window, but you also have some loop buttons you can play around with if you feel like it. I rarely use them, but found it useful a few times when I really struggle to find a certain note. Then I’ve set the playback to loop over that section over and over again until it sticks.

The mixer!

In the View menu you also find the mixer, which is the most important thing. It looks like this:

mixer

As you can see, you have a number of controls repeated down, one for each staff (note line) in the score. And you can fiddle with these both when playback is stopped, and while you’re listening.

Use Mute to turn one off. Use Solo to turn everything else off. Use the volume dial, by clicking on it with your mouse and dragging up/down, for more control over the volume of the various voices. You can also change panning (for example put your voice in the right speaker, and the rest in the left), or change instruments used to playback and such, but I rarely bother with that. If you double-click a dial, it resets to its default value, which is useful.

Either way, I usually do something along the lines of this:

  1. I start by turning my voice up to max.
  2. And and the rest down to a quarter or a half, depending on how secure I already am. That way it’s easy to hear what I’m supposed to sing, but I still have the others in the background.
  3. Practice until getting comfortable.
  4. Then I start turning the other voices back up towards their default value. (remember: double-click the dial to reset it).
  5. If I then have trouble finding my notes, I go back to step 1.
  6. If I’m still finding my notes ok, I start turning down my own sound to make sure I can still find my notes without leaning on what I hear.
  7. If not, practice more.
  8. And when I can get through the whole song, with basically no volume on my own voice, then I’m good to go 🙂

Which in my case usually means: Go, lay down on the couch, and memorize lyrics for an hour or two… because that’s what I personally really struggle with learning 😛

Happy practicing!

Hope this helps someone. I use it for literally every new song I get in the choir, and it helps me a lot. Especially with those tricky bits… that one note I just can’t quite find… that place with a bit of an off beat making it difficult to articulate properly without “getting it”… that crazy section where everyone sings something different and I’m not sure when to actually start and/or stop singing…

Using Musescore for choir practice at home

TL;DR Get Musescore. Manually enter all the parts, each on a separate staff (unless maybe someone else in your choir already did). Then play it back. Use the Mixer to emphasize your own part at first, then blend it increasingly with the rest to make sure you still know it without getting confused.

When I first started singing in the choir Triangelos, I suddenly had to learn a, to me, impossible number of songs in just a few weeks before our first performance. Everyone else more or less knew them already, and I’m not the bravest at speaking up and demanding stuff to be repeated just for my benefit, so I found myself in kind of a bind.

The solution, of course, was to practice at home, but as I didn’t have any sort of musical instruments and couldn’t really read sheet music anyways… what was a computer geek like me supposed to do? I went online hunting for digital help of course…

Musescore

The quest led me to the free sheet music program Musescore. Not only is it free, but it’s also open-source and available to both Windows, Mac OS and Linux. There’s even an app for your Android or iOS devices.

But, musescore is of course of no use on its own. You need digital sheet music. We get ours on paper.

Looked into OCR stuff, but seemed expensive and/or messy, so I’ve ended up doing it manually. Went slowly at first… a lot of line counting and alphabet walking from the only two notes I really knew where was, the G and the F… trips through Wikipedia and the Musescore manual when weird symbols and lines sometimes pops up, to figure out what they mean and how to enter them in Musescore… Anyways, through perseverance and curiosity it went quicker and quicker, and although I’m definitely no professional, I can now do it fast enough to definitely feel worth it. Especially since I also share the finished product with the rest of my choir in case they also want to use them to practice.

A note about staffs and the mixer

The most important feature to me in Musescore (other than the regular playback of course) is the Mixer. This allows me to, among other things, mute and control the volume of different staffs. More about that later, but the important thing to note is that if you enter multiple voices on the same staff, you won’t be able to control them separately.

Often you only have two staffs, one for soprano and alto, and one for tenor and bass. Compact and nice on paper, but if you enter it this way in Musescore, you won’t be able to for example listen to only the alto voice.

So, when entering the score into Musescore, I make sure to give each voice its own separate staff. If there for example is a lot of 1st and 2nd soprano variation as well, I like to split those up too. If there’s only one note difference between the 1st and 2nd bass, I maybe keep them as one staff… but either way, it all boils down to whether you want/need to emphasize a certain note when playing it back or not. If yes, you need them in separate staffs. And if yes, copy, paste and adjust can save you a lot of time. 😉

A note about dynamics

I mostly use Musescore to learn what notes to sing and when to sing them, so I tend to not bother entering all the various dynamics and such. Crescendos and decrescendos, mf, pp, fff, etc. Saves a lot of time, and in my experience it might not even be right anyways because the conductor sometimes has their own small variations and such. So, better to spend time at home learning the part and then paying proper attention to the conductor for the dynamics during practice. 🙂

Sharing is caring

I mainly do all this for my own benefit, but as part of a choir it’s good to share, and since I’m very against repeating things unnecessary, I keep all the Musescore-files I produce in a OneDrive folder I share with the others in my choir. Just them though, because copyrights and such… But yeah, I’ve found doing this has helped others as well. Who are maybe not prepared to spend some hours manually entering a score (or know how to), but would very much like to get some help with that tricky part in that certain song. More practice, better choir, more fun for me as well 😀

How I use this for practicing

This thing turned into a bit more than what I had first planned. A simple, hopefully helpful, guide to how you can use Musescore for practicing singing in a choir, when you already have that digital sheet music, maybe sent to you from a helpful nerd in your choir 😛

So, that’ll go in a separate post to hopefully make it as tidy and to the point as possible…

With a hint of Social Ineptitude