A MIDI Pedalboard Encoder
Home Up Other Publications Contact Me About the Author About this Website " Prog Organ"

 

A MIDI Pedalboard Encoder

by Colin Pykett

January  2004; last revised April 2007.

Copyright © C E Pykett 2004-2007

Abstract.  Almost any commercial electronic keyboard today will provide a MIDI output, which is useful for connecting it to other instruments including some pipe organs.  However MIDI pedalboards are much rarer, which among other things makes it difficult to put together a simple home practice facility.  Therefore this article  shows how the appropriate MIDI signals can be generated from the closure or opening of a simple key contact.  Full details are included for a MIDI encoder suitable for a standard 30 or 32 note pedalboard; the encoder uses only a handful of standard integrated circuits, thus removing the need to program microprocessors or read-only memories for which few have the knowledge or facilities.  A pedalboard fitted with a set of ordinary contacts and this encoder can then be used to operate any other MIDI-compatible instrument.  Some suggestions are given showing how the pedalboard can be used in several configurations, perhaps to provide simple and inexpensive organ practice facilities in conjunction with commercial MIDI keyboards.  The material may prove useful to schools and colleges, as well as to individuals who possess basic electronic skills.

 

Introduction

It was decided to write a detailed article about a MIDI encoder for a pedal keyboard because the availability of such units seems to fluctuate – sometimes there are several alternatives on the market yet at others none.  Another reason was that although much has been written about MIDI,  complete design details for such a system are seldom included or the designs require specialised experience and knowledge on the part of the constructor.  This article also includes technical information about MIDI which makes it a companion to another on this website, which was written at a non-technical level for organists [7].

Without doubt, the most economical and flexible way to proceed is to use a microprocessor controlled system, and PIC microcontrollers [6] are particularly attractive on account of their low cost and ready availability.  However the use of PICs or similar devices requires some level of experience in their hardware and software aspects which, although not beyond the wit of man, is decidedly non-trivial.  Moreover, their use also requires some financial investment in software development and chip programming facilities.  Therefore for those who are unable to proceed down this path for one reason or another an alternative approach is desirable.  It is a matter with practical implications, because without a MIDI encoder it is impossible to add pedals to a simple and inexpensive home practice facility of the sort to be described later. 

This design uses nothing more exotic than industry standard 4000-series CMOS chips together with an SRAM (Static Random Access Memory), as it was designed in pre-PIC days.  Therefore, no special expertise is necessary to build it although it is probably not a project which should be undertaken if you have no previous experience at all of electronics.  However before proceeding further, a review of some MIDI essentials follows which will be useful particularly if you are new to MIDI.

 

  MIDI Essentials

This section canters through some aspects of MIDI to enable readers who may be unfamiliar with it to better comprehend the material later on.  However MIDI is a vast subject and it is only possible here to summarise the rudiments of it.  Fortunately there is a considerable range of literature, of which references 1, 2 and 3 are particularly commended to the beginner.  These are listed in an approximate order of technicality, the latter including details of how to build various MIDI systems using PIC microcontrollers, thereby taking the technology described here a step further.  

MIDI stands for Musical Instrument Digital Interface and it came into being in the early 1980’s when the major electronic musical instrument manufacturers got together to agree a common control standard for their products.  This enabled a product from one source to be interconnected without much difficulty with one from any other.  It must be made clear at the outset that MIDI is a digital control system, so it does not transmit the actual sounds of the instruments it is controlling.  This is a matter which causes confusion in view of the existence of a set of 128 "instruments" and effects inappropriately called General MIDI (GM) [4] which appear on virtually all electronic keyboards, synthesisers and computer sound cards.  These sounds are generated internally in the equipment mentioned and they differ in detail from one manufacturer to another, but MIDI itself only selects which of the instruments is heard when notes are keyed. 

Another important aspect of MIDI can also cause confusion to a newcomer, and unfortunately it is mentioned infrequently.  It is that no MIDI signals are sent unless something changes [8].  Thus while no notes are being played, no signals are sent.  This might seem obvious, but it is not true of many other non-MIDI multiplex transmission systems.  Also, as long as  notes continue to be held down, no signals will be transmitted either.  Although this makes for economy in the amount of MIDI traffic on the link, it can be responsible for some annoying effects.  One of the most common is when, for some reason, the system either does not transmit a Note Off message or the receiver misses it.  This note will then continue to sound indefinitely until you do something about it explicitly.  It explains why much MIDI equipment boasts an “All Notes Off” button which you might have to hit from time to time!  There are also other annoying features of MIDI which most users discover and come to terms with in due course.  They are simply aspects of the way the system was designed and they cannot be surmounted easily.

Some Basic Definitions

Definitions of a few MIDI terms follow which will be useful later.  It is by no means an exhaustive list.  The terms are explained in more detail later on:   

 

bullet

Preset (also called instruments, voices, patches or programs).

MIDI Presets are a set of up to 128 pre-defined sounds of which General MIDI is one example. 

   

bullet

 Bank

A Bank is a set of 128 Presets.  There can be up to 16384 different Banks.

   

bullet

 Channel  

There are 16 transmit channels available in MIDI and a commercial keyboard can be set to transmit on any of them.  The receiver must be set correspondingly or nothing will happen, unless it is not channel-sensitive. 

Basic Hardware Issues

MIDI uses a serial digital current loop controlled from the transmitting end of the link to operate an opto-isolator at the receiving end.  Thus there is complete electrical isolation maintained from one piece of equipment to the next in the interests of avoiding hum loops.  This isolation must never be compromised, and it is always prudent to check with an ohmmeter that there is no continuity between the screens of MIDI cables and any other equipment (including chassis, earth lines, audio inputs and outputs etc) in the system. 

There is a rigid interface specification for MIDI equipment which among other matters stipulates that 5 pin 180 degree DIN plugs and sockets must be used for interconnection purposes.  Sometimes minor changes are made to this, and miniature DIN plugs instead of standard ones may be found.  In these cases an interface lead is often provided so that the equipment will connect properly to ordinary MIDI cables.  Normally only three pins of the DIN connector need to be used, and these must be wired correctly because polarity is important for several reasons.  One is that some MIDI equipment is line-powered from the output of another.   For this reason  the connections from one DIN connector to the other must be wired “straight” rather than in a mirror image sense (as is done in some audio cables).  Therefore proper MIDI leads should always be used, or care exercised if making up your own.

Figure 1.  Standard MIDI Output Circuit

Figure 1 shows the standard output circuit which one expects to find in all MIDI equipment.  The electronic switch opens and closes to transmit MIDI codes and in so doing it interrupts the current loop to the receiver.  A straightforward twin screened cable is sufficient to interconnect the two pieces of equipment, and the screen is connected to pin 2 of the DIN socket.  It should be connected at both ends because of the need to use it sometimes as the return path for line powered equipment which draws its power from pin 4.

MIDI Messages

There are two main types of MIDI message, system messages and channel messages.  The former exist in a vast variety of forms, many of which are specific to particular manufacturers, and it is partly for this reason we shall not discuss them further here.  Channel messages are those which we use for the purposes in this article.  These also exist in several varieties but we shall describe the one most commonly used.  It consists of 3 data bytes (a byte is 8 bits) sent one after the other.  Each byte is preceded by a start bit and it ends with a stop bit.  Thus there are 30 bits in the message.  The system uses a variant of the RS232 serial interface protocol in which it sends bits at a rate of 31.25 kbaud (31.25 thousand bits per second).  Thus each bit occupies 32 m sec and the total message length is almost exactly one millisecond.  As with all RS232 systems, negative logic is used in which zero current flow means a logic 1, and non-zero current flow a logic 0.

 

Figure 2. MIDI 3-byte Message Structure

Figure 2  illustrates the bit stream for a 3-byte channel message.  Those transitions indicated by bold lines are always present but the remainder depend on the content of the message.  Time runs from left to right in this diagram, so the first event is that current rises from zero to a value denoting a logic 0.  This signals the start bit of the first byte to the receiving equipment.  Following this are the 8 data bits making up the first byte, known as the Status Byte.  Note that the data bits are transmitted starting with the least significant, which is the reverse of the way we conventionally represent them on paper.  For example, a byte with a value of  1 would be written in binary notation as 00000001, whereas this pattern would have to be reversed to fit into the bit positions of the diagram above.  Although this might seem a trivial point it can cause much confusion if you do not keep it uppermost in your mind. 

Status Byte:  Channel Number

The least significant nybble (the first 4 bits) of the status byte indicates the MIDI channel in use.  Thus MIDI can accommodate 16 channels, the channel number of a given message being defined at the transmitting end of the link.  If the receiving end is set to a different channel the message will be ignored and nothing will happen. Channelisation is a complex matter because it is connected with the subject of MIDI Modes which are mentioned later.  Whereas the channel is defined at the transmitting end of the link, the mode is defined by the receiver, and as just mentioned the transmitter and receiver have to be set appropriately to make things work.   

Status Byte: Message Type

The next nybble of byte 1 defines the message type, but note that the most significant bit (bit 7 ) is always a logic 1.  This differs from the MSB’s of the following bytes which are always 0.  This convention is said to facilitate the receiver distinguishing a status byte from any other sort, but from all other points of view it must be one of the daftest design decisions ever taken in electronics.  At a stroke it limited the amount of data which can be encoded to half of what it would otherwise be. 

The important message types for our purposes are in the table below.  There are other types but we make no use of them here. 

Message Type

Binary Code

Note On

1001

Note Off

1000

Program Change

1100

Control Change

1011

Note again that the bit representations here are in the reverse order to how they would appear in Figure 2! 

The “Note On” and “Note Off” messages are self-explanatory, but the system needs to know which note has been switched on or off.  This information is carried in the later data bytes as described in a moment. 

“Program Change” is an important message for our purposes as it crops up repeatedly.  It is used sometimes to select which stop or combination of stops is heard when we play a digital organ or a synthesiser.  In the latter case it selects an "instrument" to be played from the General MIDI set, for example.  Yet again, remember these bits only define the message type – the data defining the stop to be used follows in successive bytes. 

“Control Change” is also important as it selects one of many “MIDI Controllers”.  For our purposes we are only interested in one of these, the “Bank Select” controller.  MIDI Banks will also be described later. 

Data Bytes 1 and 2

The information carried by the two data bytes depends on which type of message is being transmitted, as follows:  

 

 Note On or Note Off

 

For either of these messages bits 0 – 6 of data byte 1 hold the number of the note in question,  from 0 to 127 (bit 7 is always 0).  Thus the MIDI system can accommodate 128 notes or about 10 ½ octaves.  0 represents the bottom note of this range;  127 the top note.   Conventionally, middle C is note number 60.

 

Data byte 2 holds the velocity of the note when there is a Note On message, and it is ignored by the receiver for a Note Off.  Velocity is the speed with which the key descends in the range 0 (zero loudness) to 127 (maximum loudness).  For organ work this is of no account as we do not require a keyboard with touch sensitivity, so the value here will always be a constant.  Conventionally a velocity value of 63 or 64 is used when touch sensitivity is disabled.

 

Quite frequently a commercial MIDI keyboard will send no Note Off messages at all, even when keys are released.  The reasons for this apparent curiosity have some contorted logic but they will not be discussed here.  When this convention is used, key release is signalled by using a Note On message but with zero velocity [9].

   

 Program Change

 

When this message is sent there is usually only one following data byte rather than two, and it holds the number of the desired “program”.  Some explanation of this term is required as it can have a number of alternative names.

   

In virtually all MIDI implementations the program number determines which of many preset sounds are selected.  Again the number range is from 0 to 127, enabling up to 128 sounds to be called up (in fact the actual number is vastly more than this if we invoke the concept of MIDI Banks as described shortly).  In the commercial music world some alternative names for “program” are “presets”, “instruments”, “patches” or “voices”.

 

 Control Change

 

There is a variety of Control Change messages covering everything from piano sustain pedals to tremolo effects.  However we are only concerned here with how to change MIDI Banks, and these will now be explained.

   

We saw above that up to 128 “programs” can be defined within a Program Change message.  In the MIDI system these all occupy what is called a Bank (i.e. a bank of 128 different presettable sounds).  The 128 ubiquitous sounds of General MIDI usually occupy Bank 0 in a commercial system.  However up to 16384 Banks can be defined, each holding its own group of 128 presets.   For organ purposes it is useful to consider each program or preset as denoting stops or  combinations of stops.  In the latter case we can go further and think of a combination of stops as equivalent to a general piston or a sequencer setting.  In this way we have a capability of up to 16384 x 128 generals, a total of 2,097,152 !  By transmitting Bank Select (i.e. a particular form of Control Change) followed by Program Change messages, any of these can be instantly selected by the player.

 

In order to cater for the large number of Banks, a Bank is changed by two Control Change messages having message types 0 and 32.  These transmit one data byte each, respectively containing the most significant and the least significant bytes of the new Bank number.  It is not recommended that you worry too much about the details of the Bank Select message structure in a MIDI system at this stage. 

MIDI Modes

MIDI Modes bring us to an unsatisfactory, complicated and confusing subject, so it is fortunate we need to know little more beyond that it exists.  The naming convention of the various modes goes back to the days of monophonic synthesisers, as some of the modes switch between monophonic and polyphonic operation.  Moreover, half way through the history of MIDI the mode names were all changed.  Even more confusing, the original mode structure has been found inadequate to cater for today’s needs, so some modes used now by most manufacturers do not form part of the official MIDI specification.

 

bullet

A good account of modes appears in [2] , and the bare minimum of basic facts appears below:

 

bullet

 Modes are defined by the receiver, not the transmitter.

 

bullet

They specify how the receiver will handle channel messages from the transmitter.

 

bullet

 A very important aspect concerns channelisation – a unit in mode 1 will respond to messages on any channel whereas in mode 3 it will respond only to those of a specific channel.  

 

The MIDI Pedalboard Encoder - Principle of Operation

Outline Description

The general principles of operation of the encoder are as follows.  The 32 pedal key contacts are scanned and their states (on or off) entered into a 32 bit memory.  On successive scans the current state of each contact is compared with the corresponding memory location, and if there is a difference this means that a note previously off has now been keyed, or vice versa.  When such a difference is detected a MIDI code is transmitted containing the address of the note in question and whether it has been switched on or off.  When the transmission is complete the memory location for this note is updated to reflect the new status of the note, and then the key scan continues. 

  Figure 3.  Block diagram of the MIDI encoder 

Detailed Description

A block diagram of the system is shown in Figure 3.  Before addressing the detail it is important to point out that the system must work at 5 volts.  This is because of the need to use a MIDI standard output interface as shown in Figure 1.  So if you decide to use other voltages for the logic inside the system you must ensure that only 5 volts appears at the output, otherwise MIDI receiving equipment could be damaged.  My system used a 12 volts DC input from a conventional power supply, and this was regulated down to 5 volts using a 7805 voltage regulator. 

First let us assume that no keys are pressed.  Under these conditions the clock generator supplies pulses at 3.9 kHz through the clk inhibit box to the keyboard scanner address generator (thus the clock is not in fact inhibited at this stage – this occurs later).  This repeatedly generates a cycle of addresses from 0 to 31 on a 5 bit bus, and these are supplied to the key contact scanner.  This distributes pulses in sequence to the 32 pedal contacts.  A scan frequency of 3.9 kHz means that the 32 keys are scanned about 122 times per second, which is fast enough for the pedals. 

The same address lines are also connected to an SRAM memory chip.  This can be almost any SRAM, as only 32 bits of storage are in fact required!  I used a 6116 2K by 8 chip as it happened to be to hand, and moreover it is still easily obtainable at the time of writing [11].  Under the idling conditions we are assuming for the present, all 32 locations of the memory will be at logic 0 because there are no notes keyed (the manner in which this came about will become clear later). 

For each address, the status comparator compares the input from the key contact array on line B and the output from the memory on line A.  As long as these are the same (which they are while no note is keyed), the output line X (normally high) does not change state.  This is true whether A and B are both high or both low.  Only when a difference exists does the output line change state. 

Now consider what happens when a note is keyed.  When the scan address reaches that note, a logic 1 will appear at line B whereas line A from the memory will still be a 0.  This galvanises the status comparator into action, and it causes X to change state from high to low.  Simultaneously Z goes high, indicating a new Note On has been detected. 

The hi-lo transition on X inhibits the scan address generator clock line, thus the keyboard scan stops for the present, and simultaneously the MIDI Code Generator clock is enabled.  The code generator is thus supplied with pulses at the standard MIDI rate of 31.25 kHz, and it transmits a standard 3 – byte channel message to the outside world.  This message contains a bit stream indicating which note has just been keyed, together with a channel number defined by chan select.  The note address on lines A0 – A4 is added to a base address supplied from base addr select.  Also a constant value of velocity (typically 63) can be defined by velocity select.  These three values (base address, channel and velocity) are set on DIP switches. 

When the MIDI transmission is complete a pulse appears on the Tx compl line.  This is supplied to the memory write circuit which is also aware (via the high on B) that a Note On state has just been detected.  Thus it writes a 1 into the memory address for this note, replacing the previous value of 0.  As soon as this happens line A goes high (because the memory output is now high), and this means that the two lines A and B are both high.  (Until the contents of the memory was refreshed, A was low and B was high).  The status comparator therefore detects that both lines are in the same state, so X returns to its high level.  This re-starts the scan clock, and the keyboard scan commences again. 

Now consider what happens when the note is released.  As before, the status comparator will detect this and X goes low.  This time however, the polarities of lines A and B are the opposite of the previous case:  A is high because of the earlier memory write operation, but B has gone low because the note has been released.  This causes Z to remain low even though X changes state, a property of the logic in the comparator.  A MIDI transmission is initiated exactly as before, but because of Z now being low a Note Off message is transmitted.  All other bits in the message remain the same as previously. 

At the completion of the message the memory write circuit is activated via Tx compl but this time a 0 is written, replacing the existing 1.  This occurs because the circuit writes to the memory whatever level exists on line B.  Following the write, lines A and B are now the same (low in both cases) so X goes high again and the keyboard scan re-commences. 

It should now be clear how the memory contents is first written when the circuit is switched on.  At this instant the memory will in general contain some random bit pattern. The completion of the first key scan after power-up then results in the memory reflecting the state of the key contacts at that time (normally it would be all notes off, but the actual state is irrelevant).  During this initial scan there will therefore be some meaningless MIDI codes transmitted as the correct memory contents is established, but a time delay circuit in series with the output prevents them reaching the outside world.

Circuit Details

The circuit of the encoder appears in Figures 4 and 5.  Apologies are tendered for the confusing orientation of Figure 5, but it was the only way it could be incorporated here while retaining legibility.

The circuit uses 16 standard CMOS circuits together with an SRAM chip. Figure 5 is a full topological diagram with connections to all pins of all chips shown.  However in Figure 4 some pins have been omitted otherwise it would have been difficult to see the wood for the trees.  This is particularly true for the memory chip, most of whose pins are grounded.  However such details are completed later in this narrative.  Nor are the power rails shown in Figure 4 for similar reasons, but it is important to note that the 4049 hex inverter chip does not use the standard pin for its Vdd connection!

Figure 4.  MIDI Encoder circuit (1) - above

Figure 5.  MIDI Encoder circuit (2) - above  

System Clock (Figure 5)

The clock uses the convenient 4060 chip which is a combined oscillator and divider.  It is imperative that a crystal controlled clock is used to ensure that the MIDI receiver (which works asynchronously with respect to the transmitter) has a high probability of synchronising itself within one half of a clock period to an incoming transmission.  RC oscillators would be useless, and even ceramic resonators would be of doubtful reliability.  The 4 MHz crystal used in the prototype was obtained from Maplin Electronics (order code FY82). 

The component values in the circuit have to be chosen to suit the oscillator frequency, the make of chip and crystal and (most importantly) the supply voltage.  Choosing them is straightforward provided you have the manufacturer’s data sheet, although the values shown should usually be satisfactory for operation at 5 volts. 

The clock generates two frequencies: one at 3.9 kHz is used to drive the keyboard scanner.  The other at 31.25 kHz drives the MIDI code generator. 

Keyboard Scanner (Figure 4)

The scanner is driven at 3.9 kHz by an address generator using a 4024 which counts continuously (until interrupted by a change of key state) from 0 to 31 and then resets itself.  This arrangement accommodates up to 32 pedal notes.  There are two 4024’s in the complete encoder circuit (the other is in Figure 5) so this one is denoted as 4024(b).  The 5 bit address from the 4024 is used to scan the key contacts, to address the contents of the SRAM, and to provide an (inverted) note address to the MIDI code generator. 

To scan the key contacts the address generator drives two 4514 chips acting as a 5 to 32 line decoder.  Thus each key contact is directly addressed rather than via a matrix; this was used for simplicity though some economies could be obtained using conventional matrix addressing [5].   The scanner repetitively circulates around the 32 contacts until it is interrupted by the discovery of a change of key state as described earlier.  Each 5 bit address from the address generator identifies a particular contact, address 0 corresponding to bottom C on the pedal board and 31 to top G.  Chip 4514(a) addresses notes 0 to 15 and 4514(b) notes 16 to 31.  The connections to the two decoder chips are in the tables below: 

Keyboard Scanner – Connections for 4514(a)  

Pin no. of 4514(a)

11

9

10

8

7

6

5

4

18

17

20

19

14

13

16

15

MIDI Note Address

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Note

C1

C#1

D1

D#1

E1

F1

F#1

G1

G#1

A1

A#1

B1

C2

C#2

D2

D#2

 

Keyboard Scanner – Connections for 4514(b) 

Pin no. of 4514(b)

11

9

10

8

7

6

5

4

18

17

20

19

14

13

16

15

MIDI Note Address

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

Note

E2

F2

F#2

G2

G#2

A2

A#2