[sci.electronics] MIDI

jojo@crystal.UUCP (02/17/87)

   I own a pc-clone and will soon be buying a MIDI interface so I can hook
up my guitar, drum machine, etc up to my pc.  Thing is I don't know much about
the MIDI interface or how it works.  

   I also hear a lot of talk about fourier transforms being used on sampled
tracks.  Could someone explain this stuff to me or point me to references?


--j
jojo@crys.wisc.edu

cs161fhn@sdcc10.ucsd.edu (Dennis Lou) (11/12/90)

On my room mate's Amiga, he plugs a little box into his serial port
and voila! he's got MIDI in, out, and thru.  Looking at the
schematic, the box consists of a hex inverter and an optoisolator.
On the RS-232 end, it only uses pins 2,3,7 and a +5V line (which is
routed through 19 or 21).

Why can't you do this on an IBM?  What does the Roland MPU-401 
have that a modified serial interface doesn't?  What does the 
Amiga serial interface have that the IBM's doesn't?  Can you 
roll your own sequencer/notation package using the serial port adapter?

(please, let's not start a processor/architecture war here :-)

-- 
Dennis Lou               | "But Yossarian, what if everyone thought that way?"
dlou@ucsd.edu            | "Then I'd be crazy to think any other way!"
[backbone]!ucsd!dlou     +----------------------------------------------------
dlou@ucsd.BITNET cs161fhn@sdcc10.ucsd.edu        | Woz went to my high school.

brian@ucsd.Edu (Brian Kantor) (11/12/90)

>From ucsd!brian Wed Sep 21 07:44:52 PDT 1988

If you're not a hardware type, this article may only be a bad
temptation for you....

Recently I got interested in building a MIDI interface for an IBM
PClone.  A few years ago I'd have done for an S-100 system, but
since PClones are the current hardware-hacker's junk computer, it
seemed easiest to use one of them.  Besides, they're cheap and easy
to fix when you fry one.  Parts are everywhere.

Brief review of MIDI:  Midi uses a serial asychronous protocol,
running at 31.25kbit (31,250 baud).  It sends a 10-bit frame
consisting of a start bit, 8 bits of data (from LSB to MSB), and
a stop bit.  It uses an optically-isolated receiver and a 5ma
current-sourcing transmitter in a current-loop configuration.
Circuits are simplex (i.e., each pair of wires has a defined
transmitter end [MIDI OUT/THRU], and a defined receiver end [MIDI
IN]).  Unlike every current-loop interface ever designed since
Samuel F.B. Morse invented the land-line telegraph, MIDI idles in
the marking ("1","on") state with no current flowing, and indicates
a spacing ("0", "off") state by flowing current.  (Yes, that means
that it's inverted from everybody else's concept of current loop
interfaces.  Arrgh!)  It uses 5-pin DIN jacks with the cable being
twisted pair inside a shield.  DIN pin 2 is the shield, pin 4 the 
+ side of the source, and pin 5 the - side.  Pins 1 and 3 aren't
used, generally.  The 8+2 bit frame is nearly identical to the
serial interface used for personal computer data communications,
although the electrical interface is quite different.

Being of un-sound mind (PUN!) and thin wallet, I decided to see how
I could build a MIDI interface for real cheap.  Turns out that
PClone serial interface adapter boards seem to be a drug on the
market: there are lots of them out there for real cheap.  So how
to adapt one to MIDI? Watch:

MIDI runs at 31,250 baud.  You can't set a PClone async board to
that baud rate - the closest you can get is 38,400 or 28,800.
That's because the UART chip generates its baud rate by dividing
a 1.8432MHz clock signal by a sixteen-bit number [in a register on
the chip] to provide the baud rate clock at 16 times the actual
baud rate. (For example, 38400*3*16=1843200, or 28800*4*16=1843200).
Some Taiwan versions of the serial interface board use 18,432MHz
crystals and a divide-by-ten chip to get the 1.8432MHz clock, while
others just use a 1.8432 MHz crystal in the first place.  18MHz
crystals are cheaper or easier to get, I guess.

But if we change the baud rate generator clock to 2MHz, we can
divide by 4 to get 500KHz, which is just right - because it's 16
times the MIDI baud rate.  So: rip out the 1.8432 MHz [18.432]
crystal and put in a 2.0 MHz [20 MHz] crystal.  No problem: crystals
are about $2 at the electro-junk store, and those hybrid oscillator
modules that look like oversize tin chips are about $6.  Real cheap.
All you have to do is know how to un- and re-solder.  The 8250 UART
chip won't mind - its maximum baud rate generator clock frequency
is spec'd at 3.1MHz.

Ok, we've taken care of the baud rate.  Now, the current loop.
Early PClone serial cards had a current loop interface available
by just reversing the large jumper header on the card (the small
one changed it from COM1 to COM2 when you reversed it), but it's
inverted from what MIDI wants, and later versions don't have current
loop anyway.  So we'll have to build an adaptor.

The MIDI-OUT part is a breeze.  RS232 swings from -12 to +12 on
most PClone cards, since those voltages are available inside the
machine.  If we connect the - side of our PC-MIDI-OUT cable to
signal ground (pin 7 on the 25-pin connector), and stuff a diode
and a resistor in series between data out (pin 2) of the connector
and the + side of the PC-MIDI-OUT cable, we've got it.  The plus
side of the diode (the end with the stripe on it) goes towards the
cable so that current flows in the MIDI-OUT cable only when the
port is at +12 volts, trying to send a zero.  The diode is some
el-cheapo silicon type like a 1N914 or 1N4148, and the resistor is
470 to 680 ohms -- it's just there to limit the current to around
5 to 10 ma like a good MIDI interface should.  Put a 5-pin DIN plug
on the other end of the cable, with the + wire going to pin 4, - wire
to pin 5, and shield to pin 2, and connect the shield on the PC end to 
pin 1 of the DB-25, and you've got it.  Voila! MIDI-OUT for your PC.

MIDI-IN is a bit more complicated, but only slightly.  First, you
have to make sure that the serial card manufacturer provides + and
-12 volts on pins 9 and 10 of the 25-pin connector.  Most do.  If
not, they're certainly available on the card and you can install
some wires (use wire-wrap wire) to route them to the connector.
That taken care of, you need an opto-isolator chip.  About $1 at
the electo-junk emporium.  I used a 4N37 chip because I had one,
and it's a good choice, but almost any type with a phototransistor
output should work fine.  Connect a 4.7Kohm resistor between pin
10 (- voltage) and pin 3 (data in) of the 25-pin plug on the serial
card.  Connect the emitter lead (pin 4) of the chip to pin 3 on
the DB-25, and the collector lead (pin 5) of the chip to pin 9 
(+ voltage) of the DB-25.  Connect the + wire of the PC-MIDI-IN cable
to the anode (pin 1) of the opto-isolator chip, and the - wire of
the cable to the cathode (pin 2) of the chip.  The way this works
is that the resistor keeps the serial card data in (pin 3) at -12
volts normally, which corresponds to an idle ("1") line.  When current
flows in the MIDI-IN circuit, the LED in the opto-isolator lights
up, shines on the phototransistor, which switches on and provides
+12 volts to the data in of the serial card, which reads it as a 
zero bit.

Wrap the whole thing in electrical tape or something to make it
sturdy, and you're done.

If you are one of those souls blessed with a 9-pin instead
of a 25-pin serial port, you'll have to figure out the pins for
yourself, and find some way to get + and - 12 volts out the back.
Maybe you could drill a hole in the card mounting plate and run
the wires out.  Note that shorting the 12 volt supplies to ground
or to each other will probably fry something. Be careful!

Software is something I'm still playing with, so I'll not include any
here, except to show you how to set the baud rate to MIDI rate.  Note
that BASIC isn't fast enough; you'll have to use some compiled or
assembled language that lets you frob with the ports directly.  You've
only got ~320 microseconds to grab an incoming MIDI byte, so you can't
waste time.  Note also that PC interrupts are really SLOW, so you may be
better off with polled port I/O.

This fragment of C code assumes that your interface lives at COM2 at
address 2F8 in the PClone.  Clearly you could put it elsewhere.

	outportb(0x2fb, 0x83);	/* select baud rate divider latch */
	outportb(0x2f8, 4);	/* low byte of divider = 4 */
	outportb(0x2f9, 0);	/* hi byte of divider = 0 */
	outportb(0x2fb, 3);	/* deselect baud; 8-bit no parity mode */

>From this point on, you can read port 2FD for the serial port status,
and read/write port 2F8 to receive and send MIDI data.  The rest is up
to you.  Don't fry anything expensive.  Enjoy!

	Brian Kantor	UC San Diego

   "There is more harmony in films than in life."
	- Francois Truffaut


P.S.: it's virtually certain that commercial MIDI software packages
can't use this interface, since it'll be at the wrong address at least.
Sorry 'bout that.

dgold@basso.actrix.co.nz (Dale Gold) (11/13/90)

Quoted from - cs161fhn@sdcc10.ucsd.edu (Dennis Lou):
> On my room mate's Amiga, he plugs a little box into his serial port
> and voila! he's got MIDI in, out, and thru.
> [...]
> Why can't you do this on an IBM?  What does the Roland MPU-401 
> have that a modified serial interface doesn't?  

A friend of mine has built an IBM interface based roughly on my homemade
Amiga box. The problem is that all (?most?) of the good IBM software
is designed for the MPU-401, which has a lot of built-in features. Don't
ask me what they are! :-) My friend is going to write his own s/ware, so
for him, it's not a problem.

> What does the Amiga serial interface have that the IBM's doesn't? 

Basically, nothing. My old A1000 has a source of power through the serial 
port - I think this was eliminated in the 500-3000 range. Otherwise, 
there's nothing unusual in the Amiga port.

> (please, let's not start a processor/architecture war here :-)
> 
No way, mate!

Cheers, Dale 
--
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  |                              |    Critics can't even make     |
  |  dgold@basso.actrix.co.nz    |  music by rubbing their back   |
  |                              |  legs together. - Mel Brooks   |
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

wayned@wddami.spoami.com (Wayne Diener) (11/14/90)

>In article <4840.tnews@basso.actrix.co.nz> dgold@basso.actrix.co.nz (Dale Gold) writes:
>Quoted from - cs161fhn@sdcc10.ucsd.edu (Dennis Lou):
>> On my room mate's Amiga, he plugs a little box into his serial port
>> and voila! he's got MIDI in, out, and thru.
>> [...]
>> Why can't you do this on an IBM?  What does the Roland MPU-401 
>> have that a modified serial interface doesn't?  
>
>A friend of mine has built an IBM interface based roughly on my homemade
>Amiga box. The problem is that all (?most?) of the good IBM software
>is designed for the MPU-401, which has a lot of built-in features. Don't
>ask me what they are! :-) My friend is going to write his own s/ware, so
>for him, it's not a problem.
>
>> What does the Amiga serial interface have that the IBM's doesn't? 
>
>Basically, nothing. My old A1000 has a source of power through the serial 
>port - I think this was eliminated in the 500-3000 range. Otherwise, 
>there's nothing unusual in the Amiga port.
>

Not quite true.  The A1000 is capable of generating the 31.25 baud signal
for midi.  Most clone serial ports are not capable of this.

--
|---------------------------------------------------------------|
|       //                  Wayne D. Diener                     |
|      //                   Spokane, WA                         |
|  \\ //     E-mail reply to:                                   |   
|   \X/      To: isc-br!hawk!wddami!wayned@uunet.uu.net         |
|---------------------------------------------------------------|     

rzi@philpav.tds.philips.se (Roman Zielinski) (11/14/90)

In article <4840.tnews@basso.actrix.co.nz>, dgold@basso.actrix.co.nz (Dale Gold) writes:
> Quoted from - cs161fhn@sdcc10.ucsd.edu (Dennis Lou):
> > [...]
> > Why can't you do this on an IBM?  What does the Roland MPU-401 
> > have that a modified serial interface doesn't?  
> > [...]

A simplest MIDI-interface can be built as an UART plus opto-isolator
working in 31kb/s speed.
Normal UARTs in PCs can work up to 19.2kb/s (You can force them to higher 
speeds, but.... what happens if your CPU will be too busy to handle all
interrupts from UART?)

MPU401 is a Motorola 6800-based system (not just an UART shifting out/in data
to/from the line). It is very clever card performing all timing work. It also
keeps track on measures, tempos, metronome bit, etc

MPU and application use a handshaking protocol so you can transfer
data and events in both directions simultanously.

For example, from application you send events tagged with time-to-next event,
like:
	time-to-next-event <event> <event-data>

	05	ch=1 tone-on tone velocity
	25	ch=2 tone-on tone velocity
	36	ch=5 program-change prog
	55	ch=1 tone-off tone
		measure
		measure
	01	ch=2 tone-off tone

Due the timing facilities the application can work quite asyncronously to
the hardware, and it cannot be done with a pure UART system.

Roland has a techn spec for $5-10, it is about 70-80 pages...

+-------------------------------------------+           _--~--_
| Roman M. Zielinski                        |   ----   /       \   ----
| Philips Tele & Data System AB             |  ----   (  |^^^|  )   ----
| S-115 84 Stockholm, Sweden                |   ----   \  \ /  /   ---
| tel +46 8 782 1373                        |           |=====|
+-------------------------------------------+           |=====|
| NET ADDR:  rzi@pav.tds.philips.se         |           |=====|
+-------------------------------------------+            ~~U~~

helin@vehka.uta.fi (Mikko Helin) (11/14/90)

In article <505@philpav.tds.philips.se> rzi@philpav.tds.philips.se (Roman Zielinski) writes:

>
>MPU401 is a Motorola 6800-based system (not just an UART shifting out/in data
>to/from the line). It is very clever card performing all timing work. It also
>keeps track on measures, tempos, metronome bit, etc
>

MPU401 can't handle Song Position Pointers (SPPs), so the only way is to
use MPU in UART mode. All sequencer software that is claimed to understand
SPPs does this. In my opinion, Voyetras VAPI is better standard than MPU401
in IBM compatibles (I mean software drivers). 

By the way, has anybody there  written VAPI driver for modified 
RS-232 card or knows something about it? Plese tell us others!

>
>Due the timing facilities the application can work quite asyncronously to
>the hardware, and it cannot be done with a pure UART system.
>

It has been done anyway.

--------------------------------------------------------------
MIKKO HELIN
FINLAND

INET: helin@vehka.uta.fi
--------------------------------------------------------------

jones@sdd.hp.com (Christopher Jones) (11/15/90)

In article <4840.tnews@basso.actrix.co.nz> dgold@basso.actrix.co.nz (Dale Gold) writes:
>Quoted from - cs161fhn@sdcc10.ucsd.edu (Dennis Lou):
>> On my room mate's Amiga, he plugs a little box into his serial port
>> and voila! he's got MIDI in, out, and thru.
>> [...]
>> Why can't you do this on an IBM?  What does the Roland MPU-401 
>> have that a modified serial interface doesn't?  
>
>A friend of mine has built an IBM interface based roughly on my homemade
>Amiga box. The problem is that all (?most?) of the good IBM software
>is designed for the MPU-401, which has a lot of built-in features. Don't
>ask me what they are! :-) My friend is going to write his own s/ware, so
>for him, it's not a problem.
>
>
>Cheers, Dale 


I've also designed and built an IBM-Midi interface which 
"uses" the serial port to operate.  Uses in quotes, because
I'm not using the IBM serial port's hardware, but rather
its COM2 port address and protocol.  The interface card
connects directly to the XT bus, and employs a 12MHz 
8051 microcontroller to UART and timestamp MIDI data.  

I realize that this isn't exactly what Dennis was talking 
about, but in order to accomplish that (as someone has mentioned),
you'd have to change the oscillator on the IBM serial port's
hardware, or build an intelligent serial<--->serial converter,
perhaps with the MCS-51 chip.



/ctj

pete@wlbr.IMSD.CONTEL.COM (Pete Lyall) (11/15/90)

Actually, I use Sequencer Plus MKIII with an OP-4001 and it syncs very
nicely to Song Position Pointers. I understand that it cannot digest
MTC (Midi Time Code.. an addition to the MIDI SPEC that MIDI-fies
SMPTE frame data)...

Pete

-- 
Pete Lyall                                                   Contel Corporation
Compuserve: 76703,4230              OS9_Net: (805) 375-1401 (24hr 300/1200/2400)
Internet: pete@wlbr.imsd.contel.com     UUCP: {hacgate,jplgodo,voder}!wlbr!pete 

drfoam@watcsc.waterloo.edu (Dennis Henry) (11/18/90)

Thanks to all those who responded to my question.  Here is a summary of
the Email I got (less the article from Brian Kantor which has already
been posted).
 
One other note.  A couple of people have mentioned a BYTE project for
a MIDI interface.  I have located it (JUNE, 1986) and also a MIDI recorder
program for the Roland MPU-401 in the BYTE IBM Special Issue Volume 10, 
Number 11, 1985.
 
Dennis Henry
 
==========================================================================
 
From maytag!sprite.Berkeley.EDU!shirriff Tue Nov  6 20:49:14 1990
Date: Tue, 6 Nov 90 16:01:59 PST
From: maytag!sprite.Berkeley.EDU!shirriff (Ken Shirriff)
To: drfoam@watcsc.waterloo.edu
Subject: Re: MIDI Interface
Newsgroups: sci.electronics
In-Reply-To: <1990Nov6.120535.10850@watcsc.waterloo.edu>
Organization: University of California, Berkeley
 
In article <1990Nov6.120535.10850@watcsc.waterloo.edu> you write:
>A friend of mine has embarked on a project to interface his computer
>to some MIDI instruments.  He wishes to build the hardware and to
>write the software.
 
Try asking in rec.music.synth.  You should be able to get lots of advice
there.
 
Ken Shirriff		shirriff@sprite.Berkeley.EDU
"There are 2 basic truths, never fight a land war in Asia and never
go against a Sicilian if death is on the line."
 
 
 
 
From maytag!unix.cis.pitt.edu!fmgst Wed Nov  7 00:34:44 1990
Date: Wed, 7 Nov 90 00:14:58 -0500
From: Filip Gieszczykiewicz <maytag!unix.cis.pitt.edu!fmgst>
To: drfoam@watcsc.waterloo.edu
Subject: Re: MIDI Interface
Newsgroups: sci.electronics
In-Reply-To: <1990Nov6.120535.10850@watcsc.waterloo.edu>
Organization: The Last Jedi
 
 
	Greetings. Look in Byte. I CLEARLY remember (maybe even have)
	an article about MIDI, how to build a card for IBM, and
	sample software listings. It has diagrams for the board,
	in all not a bad project. Listings were in basic I think.
 
	If your friend is doing this for IBM, e-mail me for more
	info...
 
	Take care.
-- 
_______________________________________________________________________________
"The Force will be with you, always." It _is_ with me and has been for 10 years
Filip Gieszczykiewicz  "... a Jedi does it with a mind trick... " ;-)
FMGST@PITTVMS  or  fmgst@unix.cis.pitt.edu "My ideas. ALL MINE!!"
 
 
 
From maytag!oasys.dt.navy.mil!curt Wed Nov  7 11:29:17 1990
Date: Wed, 7 Nov 90 10:35:18 EST
Newsgroups: sci.electronics
References: <1990Nov6.120535.10850@watcsc.waterloo.edu>
From: maytag!oasys.dt.navy.mil!curt (Curt Welch)
Subject: Re: MIDI Interface
To: drfoam@watcsc.waterloo.edu (Dennis Henry)
Cc: curt@oasys.dt.navy.mil
 
In article <1990Nov6.120535.10850@watcsc.waterloo.edu> drfoam@watcsc.waterloo.edu (Dennis Henry) writes:
>A friend of mine has embarked on a project to interface his computer
>to some MIDI instruments.  He wishes to build the hardware and to
>write the software.
 
They talk a lot about this in rec.music.synth and rec.music.makers.
 
You might try posting your question their also.
 
You should also specify what type of computer your friend has.
 
>Any information or references to information on the hardware interface
>and the data formats would be much appreciated.
 
Your friend should get a copy of the MIDI spec.  The people in
rec.music.synth can give you the address to write to.  There is an
organization in CA (West Hollywood I think) that is responsible for
the MIDI spec.
 
Curt Welch
curt@oasys.dt.navy.mil
Code 3531
David Taylor Research Center (A Navy Lab)
Bethesda, MD
(301) 227-1428
 
 
 
From maytag!flute.cs.uiuc.edu!totty Wed Nov  7 21:45:08 1990
Date: Wed, 7 Nov 90 20:02:59 CST
From: maytag!flute.cs.uiuc.edu!totty (Brian Totty)
To: drfoam@watcsc.waterloo.edu
 
Subject: Re: MIDI Interface
 
 
In article <1990Nov6.120535.10850@watcsc.waterloo.edu> you write:
> A friend of mine has embarked on a project to interface his computer
> to some MIDI instruments.  He wishes to build the hardware and to
> write the software.
 
	I just built an RS232 -> MIDI interface for my Yamaha synth.  I
	can provide some info about my very sub-optimal hardware and a
	bit about the MIDI protocol if you send some specific questions.
 
   /                      Brian Totty              o o
  /__  __  o       1304 W. Springfield Avenue       o  
 /  / /   /             Urbana, IL 61801           \_/  "We have corn in
/__/ /   /             totty@cs.uiuc.edu                Massachusetts too!"
 
 
 
 
From maytag!VAX1.Mankato.MSUS.EDU!MARAUDER Sat Nov 10 15:00:07 1990
Date: Sat, 10 Nov 90 10:57 CST
From: Joseph Arlanda Enterprises <maytag!CUNYVM.CUNY.EDU!MARAUDER%VAX1.Mankato.MSUS.EDU>
Subject: Midi interface
To: drfoam@watcsc.waterloo.edu
X-Envelope-To: drfoam@watcsc.waterloo.edu
X-Vms-To: IN%"drfoam@watcsc.waterloo.edu"
 
 
There is a post script file in the sumex-aim archives that is a one-instrument
midi interface. It is under info-mac/sounds/program @ sumex-aim.stanford.edu.
Hope this information helps.
 
-Todd Hartman
 
 
==========================================================================
 
 
drfoam@watcsc		(Dennis Henry)