Linux MIDI: A Brief Survey, Part 4
The venerable Csound music and sound programming language has long supported MIDI input as MIDI files for play by Csound instruments or by a MIDI device for playing a Csound instrument by way of external hardware--keyboard, sequencer, wind controller. Thanks primarily to the work of developer Gabriel Maldonado, Csound also is a useful programming language for real-time MIDI output.
Playing a Csound instrument, an orc in Csound-speak, through an external MIDI device is a fairly simple procedure. The following orc file prepares a MIDI-sensitive instrument :
instr 1
inum notnum ; receive MIDI note number
iamp ampmidi inum*50 ; use it to set amplitude scaling
kfreq cpsmidib ; convert MIDI note number to Hertz
frequency value
a1 oscil iamp,kfreq,1 ; three detuned oscillators, each using a
different waveform (see score below)
a2 oscil iamp,kfreq*1.003,2
a3 oscil iamp,kfreq*.997,3
asig = a1+a2+a3 ; audio signal equals sum of three oscillators
kenv linenr 1,.07,.11,.01 ; MIDI-controlled envelope for scaling output
out asig*kenv ; output equals oscillators shaped by envelope
endin
Notice that once MIDI data is captured by Csound, it can be utilized for any purpose within an instrument design.
This code provides the Csound score file, a sco in Csound-speak, for this instrument :
f1 0 8192 10 .1 0 .2 0 0 .4 0 0 0 0 .8 ; function tables for oscillator waveforms f2 0 8192 10 1 0 .9 0 0 .7 0 0 0 .4 f3 0 8192 10 .5 0 .6 0 0 .3 0 0 0 .9 f0 10000 ; placeholder to activate instrument for 10000 seconds e ; end score
In classic Csound you would run this orc/sco like so:
csound -o devaudio -M /dev/midi -dm6
where -o determines the output target, -M selects a MIDI input device and -dm6 sets the graphics and messaging levels. Instrument, score and launch options all can be rolled into a single CSD file, a format that unifies Csound's necessary components into one handy form.
Designing a Csound instrument involves the definition and connection of components called opcodes. A Csound opcode could be an oscillator (oscil), an envelope generator (linenr, linseg), a mathematic operator (=), a MIDI capture function (ampmid, cpsmidib) or any of the hundreds of other functions and capabilities available as Csound opcodes.
The following CSD-formatted Csound code demonstrates the moscil opcode, a MIDI output function. The instrument, instr 1, transmits a major scale as a series of MIDI note messages starting from a given note-number, 48, and rising according to the envelope curve described by the linseg opcode.
<CsoundSynthesizer>
<CsOptions>
;;; The -Q flag selects a MIDI output port.
;;; An audio output device is required.
-Q0 -o devaudio -dm6
</CsOptions>
<CsInstruments>
sr=44100 ; audio sampling rate, behaves as a
; tempo control for MIDI output
kr=44100 ; signal control rate (equals sr
; for MIDI output best results)
ksmps=1 ; samples per control period
nchnls=1 ; number of audio channels
instr 1
ival = 48
kchn = 0
knum linseg ival,1,ival+2,1,ival+4,1,ival+5,1,ival+7,1,ival+9,1,
ival+11,1,ival+12
kdur = .8
kpause = .2
moscil kchn,knum,44,kdur,kpause ; There is no audio
output stage.
endin
</CsInstruments>
<CsScore>
i1 0 8
e
</CsScore>
</CsoundSynthesizer>
The linseg envelope generator creates a multistage envelope in which each step corresponds to a scale degree. When this file is run with csound moscil-test.csd, a C major scale is played by whatever instrument is receiving data on the MIDI interface selected by the -Q option.
Csound has many opcodes dedicated to the reception, transmission and alteration of MIDI messages. The possibilities are intriguing: simultaneous MIDI input/output, given two physical ports; MIDI control of synthesis parameters; instruments with combined audio/MIDI output; and even MIDI control by way of Csound's FLTK-based GUI opcodes. With Csound, your imagination is the limit.
In my next column, I'll finish this MIDI tour with a look at some GUI-based experimental MIDI applications, including Tim Thompson's KeyKit, Jeffrey Putnam's Grammidity and Elody from GRAME, the MidiShare people. See you next month!
Dave Phillips is a musician, teacher and writer living in Findlay, Ohio. He has been an active member of the Linux audio community since his first contact with Linux in 1995. He is the author of The Book of Linux Music & Sound, as well as numerous articles in Linux Journal.
Similis sum folio de quo ludunt venti.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- I once had a better way I
5 hours 19 min ago - Not only you I too assumed
5 hours 37 min ago - another very interesting
7 hours 30 min ago - Reply to comment | Linux Journal
9 hours 23 min ago - Reply to comment | Linux Journal
16 hours 17 min ago - Reply to comment | Linux Journal
16 hours 34 min ago - Favorite (and easily brute-forced) pw's
18 hours 25 min ago - Have you tried Boxen? It's a
1 day 17 min ago - seo services in india
1 day 4 hours ago - For KDE install kio-mtp
1 day 4 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
Will Part 5 be available soon
Will Part 5 be available soon?
Which card should I buy?
Hi,
Thanks for great articles! I have a USB Midisport 2x2, but it doesn't seem to be easy to setup in linux. I need a new sound card anyway, so I'll buy one with MIDI support as well. In an earlier comment, it was mentioned that not all vendors provide specifications. Who are the good guys? Which cards should I consider for MIDI and audio? :)
regards, aw
I just got a midisport 1x1 go
I just got a midisport 1x1 going last night on my laptop. The way to do it is described here:
http://usb-midi-fw.sourceforge.net/
I made my son laugh this morning - I went to wake him up and told him I had learned some new pieces on the electric piano last night. I dashed back to the music room, hit play in kmid and a beautiful Bach sonata thundered out. As I usually struggle with anything beyond "Twinkle Twinkle" he was impressed !
Rosegarden had compiled while I slept and we then successfully used the piano to input a couple of scales into Rosegarden turned them into notation, played them back as a trumpet on the computer.
So your midisport will work!