Showing posts with label Inverting. Show all posts
Showing posts with label Inverting. Show all posts

Sunday, 10 February 2019

Scales and inverted keyboards

Some time ago, I looked at the 'factory' Ableton Live MaxForLive device called 'Scale', and in particular the 'Inverted and Useless' preset, which is both a good and a bad description. Behind the scenes, I was working on my own 'scale' device, and this has recently been updated to the point at which it is probably releasable. Yes, I know that I often seem to release things too early, but sometimes things that I don't release go through slow and tortuous development behind the scenes (Waverne 2 is one example), and there are also devices where the beta testing takes longer than expected. Anyway, NoteScalery is now at the point where I think it is ready for people to play with, and it will be followed by a number of other related devices that depend on having some way of controlling the mode/scale of the notes that they produce. All of the data spreadsheets that I produced in order to create NoteScalery will be made freely available for download.

Scales

So what's a scale (in the genre of conventional 'Western' music that MIDI 1.n kind of assumes)? For MIDI notes then it can be thought of as a mapping between all of the possible notes (128 of them, numbered from 0-127) and a smaller set of notes that follow a (normally) musical rule. So example rules might be along these lines: 'All of the white notes', or 'The notes in the key of C Major', or All notes except sharps or flats'...

Now because of the way that notes have an interesting property related to multiples of pitch, then, if you go up in semitones from a given note, after 12 semitones, you end up at the same note, but one octave higher, and this note has double the pitch or frequency of the note where you started. Octaves are very special intervals because of this doubling (or halving if you go down in pitch), and when you combine this with the equal intervals between the semitones (in equal temperament), then this hugely influences the way that people think about the way that notes work together. 12 notes in a familiar black and white pattern, and it repeats every octave, so everything must repeat every octave, yes?

Unfortunately, it isn't quite as simple as this. You can see this when a piano tuner 'tunes' a piano. Actually, whilst they do 'tune' the piano, they also have to make compromises because those 12 neat semitone notes are only an approximation that sounds reasonable in most key signatures - and the piano tuner is adjusting the notes so that various intervals sound okay 'overall'...

You can also see a problem when you have minor scales that are different when they are ascending or descending, and when you start to invert the notes in a scale then things can go very weird. So the idea that a scale applies to an octave of notes is a nice tidy approximation that works in a lot of cases, but it isn't perfect for all cases.

In musical terminology, the correct word for the mapping of notes is a 'mode', but scale is often used synonymously...

Implementation

So how are MIDI Scale devices typically implemented? Well, they use octaves - they show which notes are in the scale for an octave, and just apply this across the whole the note range. Simple.

And sometimes things really are that simple. If you look inside the MaxForLive PitchScaler device that comes with the M4L examples in Live 9, then you get code that fundamentally exploits octaves:


On the left is the raw Max code. In the middle I have marked up the main sections, and on the right is the block diagram of what is happening. So after the decoding of the incoming MIDI messages, then there is the split into two sets of numbers: Note numbers, and Octave numbers. Note numbers go from 0-11 and indicate which note in the octave has been received. To extract this from the raw MIDI Note Number you just do a modulo 12, which is what the '%12' object does - it just repeatedly subtracts 12 until the result is less than twelve. You probably know this as a 'remainder' - the number that is left when you do a division. The Octave number is interesting, because when you 'repeatedly subtract' in the modulo function to get the remainder, then the number of times that you repeat is the Octave number. So if the MIDI Note number that was received was 25 then subtracting 12 gives 13, and subtracting 12 from that gives 1, so we know that the Note number is 1 (if C=0, then 1 will be C#), and the Octave number is 2, because we had to subtract 12 twice to get a remainder which was less than 12. So the MIDI Note was a C# in the third octave.

Now that there is only one octave's worth of Note numbers, then mapping which input numbers produce which output numbers requires just a 12x12 table. Nothing needs to happen to the Octave numbers - they just pass through to the output. The output stage is shown as an addition with a '+' symbol, but actually there's two extra refinements in the code. First notice that the output of the map table has another modulo function, so if there is any unexpected value in the table, then the only Note numbers we can get out will be less than 12. Second, the Octave number is multiplied by 12, so that we undo all of the repeated subtractions. When we add together the mapped Note number and the multiplied Octave number, then we get the MIDI Note number for the mapped note on the scale - as defined in the map table.

So that's how Ableton's own example does it, and you might be forgiven for thinking that we are at the end. We have a working Scale device!


If you look at the standard 'Scale' device that is included with Live, then you can see the mapping table because Ableton made it part of the user interface (it is all those light grey, dark grey and orange squares!), and it is12x12 squares. So it seems likely that this is coded similarly to the PitchScaler MaxForLive example - and the 'Fold' function limits the output to just one octave, which is exactly what happens if the Octave number is not allowed to the output...

Unfortunately, this 'Split' approach is not perfect. As I have mentioned before, the behaviour of this type of scale mapping device breaks down if you want to invert MIDI notes, so that the high notes play low notes, and vice-versa. It is well worth going back and seeing how things can go very weird... But the main problem is those two 'modulo' functions that are cairned out on the MIDI Note numbers. You remember that these are just repeatedly subtracting 12 until they get a remainder of less than 12? Well, this requires a loop that repeatedly subtracts 12, and inside that loop is a check to see if the remainder is less than 12. For a typical MIDI Note number of say 60, then this is going to require quite a lot of operations to be carried out, and it turns out that whilst a '+' operation can be very quick, the 'modulo' function can be a lot slower. If we think what is happening here, then we have two slow operations being carried out on each and every MIDI Note message, and this is going to delay the start of the note being played by the instrument (synth, sampler...) in that channel inside the Live DAW. Unwanted delay is not good.

To remove the delay, then one method is to remove the modulo functions. This can be achieved by using a larger table - and to cover all of the MIDI Note range then we would need a table of 128x128.


Here's a conceptual diagram that shows the 12x12 tables from the Scale device, mosaic'ed together to produce a 128x128 table, so that we can map any incoming MIDI Note number to any outgoing MIDI Note number, and the only operation that is required is to look up the MIDI Note number and see what the output should be - easy to do, and fast!


Actually, most of the time, the inputs and outputs are going to be quite similar, and so if we look at a 'Chromatic' scale, where each input note maps to the same output note, then actually, most of those 'Scale' tables will not have any orange squares in them at all - the only orange squares are gong to be on the lower left to upper right diagonal. So the 'copy and paste'd Scale tables in the diagram are not a fair reflection of reality.


For an 'Inverse and Useful' mapping, then the diagonal just goes the other way. Unlike the 'Scale' device, this mapping works perfectly - it produces low MIDI note outputs for high MIDI input notes, and vice-verse, and with low delay time. In this screenshot the 'Scale' tables are not shown, and you can see that most of the table is empty. In this case, there will be just 128 orange squares along that diagonal: one for each input note (mapping it to the inverted output).

MIDINoteScalery

And that's what is inside my MIDINoteScalery device - a 128x128 table (mostly empty), plus a few utilities and an interesting way of visualising the scale and the way it maps the input notes to the output notes.


The 'zl lookup' object replaces the 'mapping' object in the previous example, and the inversion, transposing and folding are handled slightly differently, but the 128x128 table does all of the scale mapping with a single look-up operation - there are no modulo arithmetic or integer divisions required any longer... Well, as long as you don't use the 'Fold' feature, but I'm working on removing them from that as well - watch for a future release...)


Ableton's Scale device has the incoming MIDI notes going vertically, and the outputs horizontally. This isn't how MaxForLive normally shows keyboards, and so the table needs to be rotated along the diagonal - the input notes now match to the horizontal axis of the table. The orange squares have been replaced with a 'staircase' diagram that makes it easy to see if the interval between output notes is one semitone, two semitones, three, etc. For the 'C Major' scale shown here, the C# isn't present in the output, and so the interval between the C and the D is 2 semitones, so the 'tread' of the staircase is 2 semitones wide. The next interval is from D to E, which is 2 semitones again, and so the tread is 2 semitones wide. The next interval is from the E to the F, which is only 1 semitone, so the tread is only 1 semitone wide, and you can see this in the 'staircase' diagram.

So how do you make a mapping table? In this post I'm going to show you how to create the table, and all of the spreadsheets that I show will be available to download for free, so you can make your own custom tables, and your own scale/mode mapping devices if you want to.

Types of scale


There are lots of scales, and so I chose two sets: scales starting with capital letters are basic scales, whilst scales starting with lower case letters are more unusual. The scales are deliberately organised in this order, with 'invert' as the final scale.


The basic scales (or more correctly, modes) are shown above with a base key (or Tonic) of C. The 'Chromatic' mode is a 1:1 mapping - the output is the same as the input! The Ionian mode shown here only outputs the white notes; C, D, E, F, G, A, and B.


To be able to create the mapping table, then the notes need to be in a form where MaxForLive can work with them.  Here is the same table, converted to numbers instead of note names. When I did the conversion, I didn't subtract 1 from the numbers, and so you need to do this to get them into the form that is used to represent the notes in the octave: so C is 0, C# is 1, D is 2, etc.


In this table, then the numbers from the previous table have been converted again - this time into a form where the output note number is shown, and so there are always 12 outputs for the 12 note inputs in an octave. So, reading across the 'Chromatic' mode, the outputs are '0 1 2 3 4 5 6 7 8 9 10 and 11'. Reading across the 'Ionian (C Major) mode, the outputs are '0 0 2 2 4 5 5 7 7 9 9 and 11' - which means that and C# inputs will only produce a C output (the '0'), D and D# will only produce a D output (the '2'), and so on. Note that some of the modes only have a small number of output notes - the minor 7th has only 0, 3, 7 and 10, for instance.

Now this table is almost useful! If you use the 'Ionian C Major' mode, then the numbers indicate what the output should be for each input. So the numbers are '0 0 2 2 4 5 5 7 7 9 9 and 11', and this means that if the input is a C or C# (note numbers 0 and 1), then the output will be 0 (C). For the next two note inputs, D and D# (2 and 3) then the output is 2 (D), and so on. These numbers are a simple lookup table where you move horizontally for the input note number, and the number in the mapping table is the output note number.


To read this type of table into a MaxForLive 'coll' object, so that the numbers can be fed into a mapping 'zl lookup' object, then additional formatting is required. This is just a case of concatenating columns in the spreadsheet, as shown above.

The tables so far have only shown a single octave. Using a spreadsheet, it is easy to extend the table to higher numbers - all the way to 128 for all of the possible MIDI Note numbers that can be input and output. All you do is add 12 to each of the values in the first octave to get the values for this second octaves, then ad 12 to those for the third octave, and so on. This gives rows with 128 values: one for each MIDI input note number, and each number in the mapping table is the MIDI note number that will be output:


This table has entries from outputs of 0-127 for each of the 128 MIDI Note input numbers. The full table is in the downloadable spreadsheet, and contains complete tables for all of the basic and unusual modes. Note that there isn't any magic or deep maths in the mapping table - it literally is just horizontal rows of numbers that specify what the output number is for a given input number.

Utilities

The default setting for the MIDI NoteScalery device is a straight-forward mapping of input notes to output notes. The output notes are shown as colour (blue or orange - selectable), and when an accidental (black) note is output then that is shown in colour.



The '+' button (blue) allows the output notes to be transposed. If the transposition goes outside of the MIDI note range then the notes will be folded back into range.


The 'Fold' button (orange) allows the output notes to be restricted to a range of 1, 2, 3... octaves, with an offset. So if you want the output notes to be only in the range of 1 octave, then set the range to 1 and the offset to 0. As I mentioned earlier, the 'Fold' function still uses the modulo function internally, but this should be removed in a future version...


The 'Inv' button (yellow) allows the keyboard to be inverted - perfectly. High input MIDI notes will produce low MIDI note outputs, and vice-versa. This can be done for any of the modes, and it is recommended that the 'Chromatic' mode is used initially. inversion can be set to happen around any note - in the examples shown, a C input will be output as an F.


Summary


MIDI NoteScalery is one of a family of utilities intended for exploring scales and modes. All of the devices will be available from MaxForLive.com for free (and in fact, some of them are already available!).


The two devices that are included by Ableton in Live are 'Scale' and 'PitchScaler'.

Getting MIDINoteScalery

You can download MIDNoteScalery for free from MaxForLive.com.

Here are the instructions for what to do with the .amxd file that you download from MaxforLive.com:

     https://synthesizerwriter.blogspot.co.uk/2017/12/where-do-i-put-downloaded-amxd.html

(In Live 10, you can also just double-click on the .amxd file, but this puts the device in the same folder as all of the factory devices...)

Getting the 'Scales and Modes' spreadsheet...

You can download the 'Scales and Modes' spreadsheet from here. It is stored in various formats to try and maximise availability. Whilst it starts out with just music theory turned into notes, the numbers rapidly become somewhat MAX-oriented, so this isn't a general-purpose 'scale development tool', although the mapping should be reasonably easy to apply to other environments. Note that there are three tabs - people often seem to overlook tabs on spreadsheets...

'Basic' - the first tab contains just the basic scales in a 'coll'-friendly format. Just a 'cover'-screen, really.

'One Octave' - the second tab has all of the data for a single octave for the basic and extended/extra scales, and contains just about all of the notes and numbers that should be useful for making your own scales and mapping tables. The data goes quite a long way across the the right... You can use this tab for developing and testing your own scales (and getting used to the 'CONCATENATE' function that is used all over this spreadsheet, and which is indispensable for assembling data into arcane formats...) and reformatting to suit your own development environment.

'Full Range' - the third tab is the 'One Octave' extended to cover all 128 MIDI Notes. This time the spreadsheet goes a very long way over to the right, and it then continues downwards. The final 'big table' actually starts at row 58, so don't miss it. This tab is where you will produce your final 'big table'...

If you are interested in how to use the Excel 'CONCATENATE' function, then you may find this spreadsheet useful - it is used everywhere to assemble numbers into the format that objects like 'coll' expect.

If/when I get around to recoding the 'fold' code so that it doesn't use the modulo function, then the table development spreadsheets will be made available for free as well.

Modular Equivalents

In terms of modular equivalents, then it depends if you count a 'Scale' module as a 'basic' module, and  once again you rapidly get to the point where you want to step outside of the 'basic modules only' rule and go into more specialist modules. I'm going to add a 'Scale' module to the 'basic' set, and so my estimate is that full functionality is going to require a scale module plus some additional CV processing (for invert and fold) and so this gives a grand total of 3 ME.


Sunday, 6 August 2017

Scale Inversion of MIDI Notes...

Hidden away inside Ableton Live are some interesting comments. One that is in plain sight is in the presets for the 'Scale' MIDI Effect. If you look carefully at all of the sometimes exotically named scales, you will see one that is named 'Inverted and Useless'.

Now that got my interest immediately. Inverted keyboards have always fascinated me, and I had already done a MIDI Effect that could invert a keyboard, and lots more besides: MIDI f(x)_mr. But why would Ableton describe one of their own presets as 'Useless'? Here are the default 'Scale' settings, which pass notes through unchanged:


The mapping of input to output is very straight-forward:


Here is the 'Inverted' version, which takes the diagonal mapping and just reverses it, which intuitively ought to be fine - after all, all that has happened is that the diagonal now goes the other way, so the notes should too...


Unfortunately, when I tired playing the MIDI output of a keyboard through the Scale MIDI Effect using the 'Inverted and Useless' preset and driving a piano instrument, then I quickly learned why it was described as useless. Some arpeggios work ok, but others have sudden jumps of just under an octave, and some chords sounded very weird indeed, even when I deliberately adjusted them for the inversion. Something was definitely wrong... This needed deeper investigation with a specialist setup.

Investigation Setup


First, I created an arpeggio clip stretching over about three octaves, and then I surrounded the 'Scale' MIDI Effect with some display tools: two MIDI HugeNoter_mr 'big' note displays, and a variation of one of my favourite tools, the MIDIKeyMon_mr, which I described in a previous blog entry. (MIDIKeyMonFull_mr shows the whole MIDI keyboard range and has a summary scrolling window showing the notes played.) HugeNoter is great when you need to display notes in a way that is difficult to miss, and is available from MaxForLive.com.

But the important part of this setup is the Ableton 'Scale' MIDI Effect in the middle. As you can see, in the case shown in that screenshot a C3 note input has become a B3 output. So there's obviously some pitch changing going on, but C3 inverting to a B3 seems strange. So I wrote down the input and output of the Ableton 'Scale' MIDI Effect with the 'Inverted and Useless' preset:


After noting down all those inputs and outputs, here's part of what I got:


It isn't easy to see what's actually happening here, so here are plots of the input and output:


So the purple input is a rising series of notes, and appears as a sloping line going up from left to right. The blue-green output does have a series of descending notes, from 71 (B3) to 60 (C3), but either side of that descending series is a sudden jump - the output jumps from 48 (C2) to 71 (B3) when the input changes from 59 (B2) to 60 (C3), and the output changes from 60 (C3) to 83 (B4) when the input changes from 71 (B1) to 72 (C4). Both of these intervals are 13ths, which my head (and ears) don't really think of as an octave plus a semitone - they just sound wrong.


The results show why the 'Scale' effect only shows an octave grid - it is just working on an octave at a time, and mapping the notes in that octave to new values. For any rising scale then this is fine, but when you have an 'Inverted' scale, then you get jumps because each octave map is being transposed to try and do an inversion across the whole range. 

If we look at the whole range, then it looks like this:


And what the 'Inverted and Useless' scale effect preset is trying to do is turn those descending blue-green segments into an inversion of the rising purple line, and the effect is a broken series of invited scales with discontinuities at the ends of each octave:


What is really needed is an inverter that inverts properly over the whole range, of course! The 'Scale' effect is designed to provide a way of mapping rising scales to other rising scales, and it was never designed to invert the complete MIDI keyboard range.

But since I had started investigating, I wondered if there was any way to improve on the existing 'Inverted and Useless' preset. C3 mapping to B3 seemed wrong to me, so I looked at the theory behind inverting MIDI keyboards...

Theory: Inverting MIDI Keyboards

The limits of the MIDI keyboard range are:

Lowest Note. 

The lowest note on a MIDI keyboard is number 0, and this is normally referred to as C-2. (But it can be C-1 or C0 - see the next entry)

Middle Note.

There is some variation on MIDI note octaves (the '-2' in the example above) and the middle of the keyboard, because the original MIDI specification isn't very precise about which pitch is MIDI note 60, and so as a result Middle C can be C3 or C4 or even C5, and may be assigned MIDI Note Number 60, 72 or 84. MIDI equipment tends to use C3 = 60 and so that's what I will use here. (Look up 'Is Middle C C3, C4 or C5' or 'Is MIDI note number 60 = C3?' on your favourite Interweb search engine to see lots of people politely failing to agree...)

Highest Note.

The highest note on a MIDI keyboard is number 127, and this is normally referred to as G8 (or G9 or G10 - see the previous entry)

Keyboard ranges.

Most musical keyboards do not have 128 notes, and so they only provide a sub-set of the available notes. 5 octave, 61 note, C-C keyboards are common for professional synthesisers, but monosynths tend to have less notes (44 F-C on a Minimoog), and top-of-the-range workstations tend to have more (88 or more, and often not C-C). 

Octave numbering.

If you go up from A2, you get A#2, B2, C3, C#3... Octaves change when a new C starts (not when you go from G to A as you might expect!). Because the lowest MIDI note is a C (number 0), and the top note is a G (number 127), then the half way point happens in the 3rd Octave, and is between D#3 and E3. 

if we ignore the notes above 120, and consider MIDI as having a range from 0 to 120 (C-2 to C8), then the middle of these 121 notes is number 60, C3. By using sub-sets of the full 128 notes, then any note can be used for inversion, although maximising the usable keyboard range seems to be a good goal, and inverting around D#3 and E3 gives 127 usable notes, whilst inverting around the D#3/E3 (63/64) boundary gives 128 notes. 

Exploring inverted scales

The 'Inverted and Useless' preset is just the opposite diagonal to the default chromatic diagonal scale grid.


Because the grid used by the Scale effect is just one octave, then the lowest note when the Base is set to C is a C, and the highest note is a B - this can be seen by looking at the second row up on the grid - the pattern of darker notes reflects the 2 and 3 groupings of 'black/enharmonic' keys on a piano keyboard. If you plot one keyboard against another, then you get a useful diagram of the corresponding keys:


Which explains why the C3 gives a B3 output. Note that this has all white keys mapped to white keys, and all the black keys mapped to black keys, except for the F to G# mappings.

However, there is an extra row in the grid, and if you use this, then you can do a different diagonal: shifted one cell upwards. This gives a different output for a C, and a different diagram.



This time, the mapping is almost all opposites (back mapped to white), except for the G, G# and F keys. Here are the two grids, side by side:

                                         Inverted and Useless                                          Scale Invert mr1


When you play these, then the 'Inverted and Useless' is obvious because it shifts the C downwards to a B. Otherwise they are both chromatic inversions over an octave, and both have a jump at each end of the octave. For someone like me, who likes a C to play as a C, then I prefer 'mr1'.

Alternative inversions

Here are some alternative inversions that I experimented with, trying to minimise the discontinuities, or perhaps, trying to spread them out across the octave.

                                                                                Scale Invert mr2


This scale moves the C from the top row to the bottom row, and so the octave jump is between the C and the C#, instead f the B and the C. It's a different feel, but still an inversion.

The interesting results so far then inspired the following explorations.

                                             Scale Invert mr7                                        Scale Invert mr 8n

The above scales put the jump in a different place on the keyboard, but there are now two discontinuities per octave...

                                          Scale Invert mr1m                                         Scale Invert mr1s

The above scales spread the discontinuities wider... Scale Invert 1s puts the  main discontinuity between E and F, whilst Scale Invert 1m  puts it between F and F#. It really depends on what key you are playing in, really. Having a set of different inverts is actually a great way of creating new melodies, chords or chord progressions, because it removes any bias or preference your fingers have for well-worn scales or fingerings. 


                                                 Scale Invert mr1w                                   Scale Invert mr1q 

The above scales spread the discontinuities wider and in different ways.

                                                                                Scale Invert mr1x

And finally, almost not an inverting scale at all!

Now it is possible to input these scale grids into the Scale effect, but that is a lot of work, and so here's a shared 'Scale Invert' folder containing all of the chromatic scales above, plus several extras, and they are all free for you to download: Scale Invert

MIDI HugeNoter_mr 


HugeNoter_mr is just a way to show MIDI notes and note numbers in a big way. I use it when developing utilities or doing investigations, as in this blog. It shows, from top to bottom, the MIDI note number (in dark blue), the note and octave (in white), and the note and octave split, as described in this blog. The three small grey buttons at the top let you set the justification of the displayed note and octave. MIDI_HugeNoter_mr is available from MaxForLive.com.

MIDIKeyMonFull_mr


MIDIKeyMonFull_mr is a variant of the MIDIKeyMon, but adjusted so that you can see the whole of the MIDI note range without needing to select a range. It also has a scrolling display of previous notes played. A useful utility when working on scale effects... MIDIKeyMonFull_mr is available from MaxForLive.com.

Where's the dark version?


Available at MaxForLive.com.