Monday, July 8, 2013

Breath control revisited

I have been using my breath control and openpipe breakout for two months now and it makes for a really enjoyable instrument. There are some things to develop, first the hardware pipe and pressure sensor compartment should be redesigned, the current is a quick hack. So here comes some notes on designing a new mouthpiece. In some weeks time I hope to be able to add some sound examples and also describe some of the programming.

Designing a mouthpiece

The mouthpiece should allow some air to pass through while playing to make breathing more natural, but also stop the airflow enough for a clearly measurable pressure to build up. To avoid moisture on the sensor board i place the sensor in a compartment after the exhaust hole so that the air stream does not pass directly over the sensor board. Closing the exhaust hole and just using the pressure makes the end of notes sound bad since the pressure doesn't drop cleanly when you stop blowing. The best option is probably to make the size of the exhaust hole adjustable and to let the player decide.

The air pressure in a recorder mouthpiece varies between 200 and 1000Pa depending on the note played with high notes having more pressure. The difference in pressure between pp and ff (loud and quiet) is about 200Pa, these numbers can be found in Modeling of Gesture-Sound Relationships in Recorder Playing: A Study of Blowing Pressure, a master thesis by Leny Vinceslas.
An exhaust hole with 3-4mm diameter gives this kind of pressure on the sensor and feels quite nice to play. I will test more with different sized exhaust holes, how hard to blow and how the pressure varies on the sensor. 

Here is my design sketch for the next version of breath sensor mouthpiece.  I have found very cheap nylon tubing used for electrical installation work that fits snugly around the Open Pipe. I am fairly confident this can built at home with simple tools, the only remaining part is the silicone rubber film. It can be bought 0.3 mm thick 50x50 cm from Germany for 90 euros, a bit much money but its probably enough for more than 600 such mouthpieces  ( I might find some use for a lot of silicone rubber film :) ).

The sensors

Reading both temperature and pressure and calculating the calibrated values takes around 11ms, this time is mostly spent waiting for the chip to complete a conversion.  With careful programming other calculations and sampling of the touch sensors can be done during this wait time. A breakout board can be found for around $15

This sensor seems to have as good or better performance than the BMP085 with faster sampling rate. The calibration and temperature compensation is done in the sensor ASIC and the convoluted calculations needed for the BMP085 are not needed. I have ordered a breakout board for testing.

A further enhancement would be to use a very open mouthpiece and sense both pressure in the middle of  the airstream and total flow, this would more correspond to playing a flute. Not sure what sensors to use for this and how to mount them.

Relation between pressure, tone height and volume

Using the data in L Vinceslas work I set up a table of the normal pressure used to to play the different notes at medium volume.  This value is used as baseline for the note, corresponding to midi volume 64. This means that like in a real flute or recorder, in order to keep a constant volume, the pressure must increase as we play higher notes.

    int volume;
    int midpressure = note_pressure[note-60];

    volume = 64 + ((pressure - midpressure)*psensitivity)/128;
    if (volume < 0) volume = 0;
    if (volume > 127) volume = 127;

This code fragment shows the midi volume calculation, the psensitiviy gives the sensitivity to pressure variation around the standard note_pressure from the table. A value of around 15-20 seems to work quite well. In my test sketch I have assigned this value to a CC controller so it can be changed dynamically while playing.

This has been tested and its easy to dynamically control the expression of the sound.

Using the pressure to control the octave of the note played

If the pressure is more than 2/3 of the pressure difference to the note one octave higher than the one fingered then scale is shifted one octave up and later if it is below 2/3 of the difference down to the original note the scale is shifted back.  This code is still in planning.

Detecting the start of a note

The program recognises the start of a note when the pressure has been more than 50 Pa above ambient for three sample periods (30ms). This is the number of samples needed for the pressure to reach its peak value so that the midi note volume can be calculated. If aftertouch, channel pressure or the expression continuous controller is active then this may be decreased at the risk of losing the initial attack.

Thursday, July 4, 2013

Adventures with the Terasic DE0 Nano

I have for a long time been fascinated by the idea of programmable logic as a complement to standard MCU's. Ideas like running 32 pwm channels and as many quadrature detectors on one chip for servo control is definitely beyond todays MCU's, powerful as they are.

I have previously played a bit with the Terasic Trac C1 and the Dallas Logic Quickgate EP2C8 Cyclone II boards, trying to learn VHDL and how to build things like an audio synthesizer with them.  So when I saw the Terasic DE0 Nano I simply couldn't resist the urge to buy one. At €74 from Mouser it is not dirt cheap, but for an FPGA board of this kind it is very good value.

Designing FPGA logic is quite different from ordinary C/C++ microprocessor programming. The best book I have found to help me is "Rtl Hardware Design using VHDL" by Pong P. Chau.

So after reviving some old VHDL projects I started to install the Quartus software on my Fedora 18 system. Quartus 13 refused to run without frequent crashes even after I changed and added several system libraries to conform to the ones coded into the Quartus 13 executables. After this I tried installing Quartus Free Web Edition 11, and it seems to run perfectly,  this might be because of the changes done to make Q 13 run, or not, but at the moment it works. Older Quartus versions can be found at

Most of the get started manuals for complex systems like this tells you to install some precoded development package and just click menu boxes in a specified sequence without giving the logic for that. For me this is not really learning a new tool. So I try to build small things from scratch to see what happens before using the heavyweight preprogrammed IP in the component libraries.

Right now I have a Serial Port echo running on the DE0 Nano that displays incoming serial bytes on the 8 LED's and then echoes them back, the small chip is a Teensy 3 that acts as a Serial-USB bridge. Its not very advanced yet but writing the logic from scratch is fun and rewarding.  Next step is SPI and some PWM.

Thursday, May 9, 2013

OpenPipe and breath control

I have been playing around with the OpenPipe Breakout, the electronic pipe/flute control, for a few weeks now, trying to revive some old and mostly forgottens skills on how to play a flute or Irish tinwhistle.

The pipe is connected through a I2C interface to a Maple clone, the Olimexino STM32 and then with MIDI to Garageband on my iMac. Its a fun instrument but I find it a bit hard to balance, holding it and playing some fast fingering at the same time, using a thumb for note on/off is also a bit unusal.

So I decided to try and make a breath control so that the pipe can be played almost like a real flute.

The breath control sensor is a BMP085 breakout board, this atmospheric pressure sensor
connects to the Maple board over I2C. The mouthpiece is made from two pieces of nylon tubing. A cork from a bottle of good Italian wine holds things in place. The sensor is placed inside the tube and the end is sealed with the cork, a small ventilation hole lets some air pass thrugh the mouthpiece.

The sketch reads the BMP085 and the touch sensor in the OpenPipe Breakout and starts a note if the pressure is more than 50Pa above ambient. Some early tests shows that the basic setup works but theres a lot more to do before the sound can be controlled by breath like in a real flute.

Selecting a pressure sensor

BMP085 is an absolute pressure sensor accessed using the I2C protocol. No extra components are needed. The drawbacks are that the breath only represents a small fraction of the sensors range and the baseline pressure, ambient pressure, must me calibrated for.. Price is ___

The other major type of pressure sensor is a MEMS bridge giving a small voltage representing the difference between measured pressure and ambient. The problem here is that the small sensor output must be amplified before the signal is input to a AD converter. No calibration for changing ambient temperature is needed.

Saturday, April 6, 2013

MIDI USB Class for the Maple board

I got myself an OpenPipe breakout board and want to use a Maple board to connect it to a soft synth on my computer or a hardware synth. For this I want the Maple to implement a MIDI USB class device.

The Maple has as standard a USB serial device that gets setup and loaded as part of building a sketch and its then available as SerialUSB object. The MIDI USB will replace the Serial USB, and register the device as a MIDI class compliant device. The Maple bootloader is not affected, but the remote reset into bootloader is not implemented, so a manual reset is needed to get into the bootloader, I can live with that.

The MIDI USB needs a few things to setup

  • USB Setup and handling of Control Requests
  • A MIDI USB device descriptor to present itself to a host computer as a MIDI USB device
  • Bulk IN and OUT endpoints for MIDI USB packets, 32 bit/4 byte blocks of data
  • Code that interprets the MIDI USB packets as standard MIDI events.

Building the MIDI USB class as a variant of the existing USB serial code, the first and third parts are almost identical for MIDI and Serial, actually easier for MIDI since no modem control line handling is necessary and no management endpoint is needed.
The device descriptor is bit harder, but its a static datastructure and just following the MIDI USB documentation carefully will get you through this.
The USB MIDI package handling is standard MIDI code, and does not depend on the details of the USB transport layer.  

The code has been tested and registers as a MIDI device both under OSX and Android, and seems to be working.

A git repository can be found at

The MIDI USB is built from the following files:
High level device object, Wirish style, replaces usb_serial.cpp
  • usb_midi.cpp
  • include/wirish/usb_midi.h
Low level USB driver, replaces usb_cdcacm.c
  • stm32f1/usb_midi_device.c
  • include/libmaple/usb_midi_device.h
The process of setting up a sketch to use MIDI instead of Serial is still clumsy and needs some manual editing of the boards.h file.

The development is done on a modified Maple-IDE that uses a current arm toolchain and a libmaple layout that is closer to the present libmaple layout so the files are placed in different locations than the standard Maple-IDE file layout.

UPDATE 2013/0412

The descriptor definitions have been factored out of usb_midi_device and placed into usb_midi_descr.c/h . A working copy of the libmaple git repository with the midi usb files placed in their proper place in the hierarchy can be found at .