Friday, 31 January 2014

Code Highlighting/Beautifying

I'm sure this is old hat to everyone, but I wanted to get it down here if only as a reminder to me. Looked at a number of code highlighters/beautifiers, and settled on HiLite.me as one that works for me.

 Paste in, check it looks all ok (I'm using the autumn theme).  Select the language type - important as I'll be posting C and C#/Sliverlight and bits of XML and markup.

The only manual adjustment I'm making is to add in an instruction (font-size:small;) to use a small font so that the lines do not wrap with the blogger layout I have.

<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow:auto;width:auto;
border:solid gray;border-width:.1em .1em .1em .8em;
padding:.2em .6em;font-size:small;">

Thursday, 30 January 2014

Silverlight in Blogger

Did a bit of reading at lunchtime and a bit more playing this evening to get this to work. As most of my coding is in C#/Siverlight I thought it would be neat to be able to publish up some of the smaller ideas on blogger with some comments, get some opinions etc... there are quite a few posts on how to do this, it's just a matter of working through them and finding something that works.

I decided to host on drop-box as it is free and works without too much trouble. The biggest challenge is that most of the examples refer to Public folders which drop box removed recently. There are some useful posts how to get this re-enabled. I did consider and look into using GoogleAppEngine (GAE) as an alternative. I liked the idea of the common Google infrastructure, unfortunately this requires quite a bit more messing around and also limits to 10 applications. I probably want to post more than that, but only simple examples.

Finally, it would have been nice to be able to do this with Google docs, but it looks like the limitation on this like many other file hosting sites is sending the correct MIME type for the XAP files.

Lastly the reason for dropbox vs. some of the smaller file engines. Simply, most of the others are blocked at work and I thought that some of the little apps I put up might be useful to reference at work.

So, here, goes, following tradition a hello world Silverlight application embedded in Blogger. More interesting ideas to come:


And, little note to myself... don't forget to get the right URL.... here's the article.

Tuesday, 28 January 2014

Arduino MIDI Synth No.3

And moving rapidly on with following the Gordophone's work, Marmonizer v2 which adds chords.

This is really neat, taking the MIDI in note, a chord is formed from adding notes below the pitch to form a chord. In this case the drop is -4 and -9 from the original note (quoting Gordophone this this is a major triad in second inversion - a 6/4 chord).

Here's my code version:

///////////////////////////////////////////////////////////////////////
//
// HondrouMidifier
//
// Initial testing using Gordon Good's Marmonizer as a base
// http://gordophone.blogspot.co.uk/2010/01/marmonizer-v2.html
// Gordon Good (velo27 <at> yahoo <dot> com)
//
// This uses the MIDI library from FortySevenEffects
// http://fortyseveneffects.github.io/arduino_midi_library/
//
// 
// implements a simple MIDI harmonizer. The harmonizarion ia simple; the
// input note is the top voice of a triad in second inversion (e.g. if the
// input note is E4, then the voices below are G3 and C4. For discussion of
// what those note names mean, see http://en.wikipedia.org/wiki/C_%28musical_note%29
// The transposition pot is retained.
//
// Limitations: the algorithm always "maps down" so we may roll notes off the
// deep end of the MIDI spec.

#include <SoftwareSerial.h>
#include <Midi.h>

SoftwareSerial MusicSerial(2, 3); //Soft TX on 3, we don't use RX in this code

// defines for MIDI Shield components only
#define KNOB1  0
#define KNOB2  1

#define STAT1  7
#define STAT2  6

// Music shield pins - both digital
#define MUSIC_PIN_MIDI_IN  3 // soft serial TX -> VS1053 RX
#define MUSIC_PIN_RESET  4   // VS1053 RESET

#define HARM_NOTE_1_OFFSET -4
#define HARM_NOTE_2_OFFSET -9


int trPotPin = KNOB1; // Analog pin for reading the transposition potentiometer
int ledPin = STAT1;  // LED pin to blink for debugging

int transposition = 0; // number of semitones to transpose (negative = transpose down)

void noteOnCallback(byte channel, byte pitch, byte velocity) 
{  
  digitalWrite(ledPin, HIGH);

  noteOn(channel, pitch + transposition, velocity);
  noteOn(channel, pitch + transposition + HARM_NOTE_1_OFFSET, velocity);
  noteOn(channel, pitch + transposition + HARM_NOTE_2_OFFSET, velocity);
}

void noteOffCallback(byte channel, byte pitch, byte velocity) 
{
  digitalWrite(ledPin, LOW);
  
  noteOff(channel, pitch + transposition, velocity);  
  noteOff(channel, pitch + transposition + HARM_NOTE_1_OFFSET, velocity);  
  noteOff(channel, pitch + transposition + HARM_NOTE_2_OFFSET, velocity);  
}

/////////////////////////////////////////////////////////////////////////////
//  Setup routine             
/////////////////////////////////////////////////////////////////////////////
void setup() 
{
  pinMode(STAT1,OUTPUT);   
  pinMode(STAT2,OUTPUT);  
  
  // Initiate MIDI communications, listen to all channels
  MIDI.begin(MIDI_CHANNEL_OMNI);    
  
  // Connect the HandleNoteOn function to the library, so it is called upon reception of 
  // a NoteOn.
  MIDI.setHandleNoteOn(noteOnCallback);    
  MIDI.setHandleNoteOff(noteOffCallback);  

  pinMode(trPotPin, INPUT);
  digitalWrite(trPotPin, HIGH);
  
  // VS1053
  //start serial with midi baudrate 31250
  MusicSerial.begin(31250);      
  
  // Reset the VS1053
  pinMode(MUSIC_PIN_RESET, OUTPUT);
  digitalWrite(MUSIC_PIN_RESET, LOW);
  delay(100);
  digitalWrite(MUSIC_PIN_RESET, HIGH);
  delay(100);
  talkMIDI(0xB0, 0x07, 120); //0xB0 is channel message, set channel volume to near max (127)
  
  digitalWrite(STAT1,LOW);
  digitalWrite(STAT2,LOW);
}

/////////////////////////////////////////////////////////////////////////////
//  Main loop             
/////////////////////////////////////////////////////////////////////////////

void loop() 
{
    // Read the transposition pot, and map the value to a + or - one octave transposition
    transposition = map(analogRead(trPotPin), 0, 1023, -12, 12);
    
    // Call MIDI.read the fastest you can for real-time performance.
    MIDI.read();
  
    // There is no need to check if there are messages incoming if they are bound to a Callback 
    // function.
}

/////////////////////////////////////////////////////////////////////////////
//  Functions             
/////////////////////////////////////////////////////////////////////////////

//Send a MIDI note-on message.  Like pressing a piano key
//channel ranges from 0-15
void noteOn(byte channel, byte note, byte attack_velocity) 
{
  talkMIDI( (0x90 | channel), note, attack_velocity);
}

//Send a MIDI note-off message.  Like releasing a piano key
void noteOff(byte channel, byte note, byte release_velocity) 
{
  talkMIDI( (0x80 | channel), note, release_velocity);
}

//Plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that data values are less than 127
void talkMIDI(byte cmd, byte data1, byte data2) 
{
  digitalWrite(STAT1,HIGH);

  MusicSerial.write(cmd);
  MusicSerial.write(data1);

  //Some commands only have one data byte. All cmds less than 0xBn have 2 data bytes 
  //(sort of: http://253.ccarh.org/handout/midiprotocol/)
  if( (cmd & 0xF0) <= 0xB0)
  {
    MusicSerial.write(data2);
  }
  digitalWrite(STAT1,LOW);
}

Arduino MIDI Synth No.2

Well, finally some more time this evening to continue the investigations and now have some basic MIDI handling and manipulation. Basically this tests that I can get MIDI callbacks and do something with them before passing onto the Real-Time MIDI on the VS1053.

Have followed Gordophone's lead again but instead of using the Ruin + Wesen MIDI library, I've used the library from fortyseveneffects. The class library description is here. I liked this library as it's only a set of midi.cpp and midi.h files that are quite well laid out and described and pretty easy to follow. Certainly gives me some comfort that if I need to jump in there and start twiddling with the code I've got a good basis to step on from.

BTW, for those of you on a Mac, adding libraries to Arduino installs requires finding the Arduino app, showing the package contents (in Finder click the cog wheel, select Show Package Contents) and then navigate to Contents/Resources/Java/libraries. I put the midi.cpp and midi.h into a new folder called Midi.

Here's the code... basically a reworking of Marmonizer v1 to read the MIDI input and transpose the note up or down an octave. The pot input is read with an analogRead, mapped to +/-12 and then the variable transposition is added to the incoming midiOn messages.

Note - ref to the MIDI numbers, note names and frequencies for me to find when I read this again. One octave is mapped to 12 semitones. In MIDI Middle C (C4 = 60).


///////////////////////////////////////////////////////////////////////
//
// HondrouMidifier
//
// Initial testing using Gordon Good's Marmonizer as a base
// http://gordophone.blogspot.co.uk/2010/01/marmonizer-v1.html
// Gordon Good (velo27 <at> yahoo <dot> com)
//
// Have used the MIDI library from FortySevenEffects instead of the Ruin+Wesen
// library.
// http://fortyseveneffects.github.io/arduino_midi_library/
// To prove some basic assumptions, the very first version is a simple MIDI 
// transposer. The input note is transposed up or down, and the amount of 
// transposition is controlled by a voltage applied to analog input 0, 
// e.g. with a potentiometer.
//
// This proves:
// - That we can do the transposition with reasonable latency
// - That we've got the Midi library working properly
//
// Note: this will probably leave dangling notes if the transposition is
// changed between a note on and the corresponding note off. The final
// code will have to account for user knob-twisting while playing, and
// make sure it turns off the right notes. Probably some sort of a map
// that relates a received note to all the note on messages it spawned.
//


#include <SoftwareSerial.h>
#include <Midi.h>

SoftwareSerial MusicSerial(2, 3); //Soft TX on 3, we don't use RX in this code

// defines for MIDI Shield components only
#define KNOB1  0
#define KNOB2  1

#define STAT1  7
#define STAT2  6

// Music shield pins - both digital
#define MUSIC_PIN_MIDI_IN  3 // soft serial TX -> VS1053 RX
#define MUSIC_PIN_RESET  4   // VS1053 RESET


int trPotPin = KNOB1; // Analog pin for reading the transposition potentiometer
int ledPin = STAT1;  // LED pin to blink for debugging

int transposition = 0; // number of semitones to transpose (negative = transpose down)

void noteOnCallback(byte channel, byte pitch, byte velocity) 
{  
  digitalWrite(ledPin, HIGH);

  noteOn(channel, pitch + transposition, velocity);
}

void noteOffCallback(byte channel, byte pitch, byte velocity) 
{
  digitalWrite(ledPin, LOW);
  
  noteOff(channel, pitch + transposition, velocity);  
}

/////////////////////////////////////////////////////////////////////////////
//  Setup routine             
/////////////////////////////////////////////////////////////////////////////
void setup() 
{
  pinMode(STAT1,OUTPUT);   
  pinMode(STAT2,OUTPUT);  
  
  // Initiate MIDI communications, listen to all channels
  MIDI.begin(MIDI_CHANNEL_OMNI);    
  
  // Connect the HandleNoteOn function to the library, so it is called upon reception of 
  // a NoteOn.
  MIDI.setHandleNoteOn(noteOnCallback);    
  MIDI.setHandleNoteOff(noteOffCallback);  

  pinMode(trPotPin, INPUT);
  digitalWrite(trPotPin, HIGH);
  
  // VS1053
  //start serial with midi baudrate 31250
  MusicSerial.begin(31250);      
  
  // Reset the VS1053
  pinMode(MUSIC_PIN_RESET, OUTPUT);
  digitalWrite(MUSIC_PIN_RESET, LOW);
  delay(100);
  digitalWrite(MUSIC_PIN_RESET, HIGH);
  delay(100);
  talkMIDI(0xB0, 0x07, 120); //0xB0 is channel message, set channel volume to near max (127)
  
  digitalWrite(STAT1,LOW);
  digitalWrite(STAT2,LOW);
}

/////////////////////////////////////////////////////////////////////////////
//  Main loop             
/////////////////////////////////////////////////////////////////////////////

void loop() 
{
    // Read the transposition pot, and map the value to a + or - one octave transposition
    transposition = map(analogRead(trPotPin), 0, 1023, -12, 12);
    
    // Call MIDI.read the fastest you can for real-time performance.
    MIDI.read();
  
    // There is no need to check if there are messages incoming if they are bound to a Callback 
    // function.
}

/////////////////////////////////////////////////////////////////////////////
//  Functions             
/////////////////////////////////////////////////////////////////////////////

//Send a MIDI note-on message.  Like pressing a piano key
//channel ranges from 0-15
void noteOn(byte channel, byte note, byte attack_velocity) 
{
  talkMIDI( (0x90 | channel), note, attack_velocity);
}

//Send a MIDI note-off message.  Like releasing a piano key
void noteOff(byte channel, byte note, byte release_velocity) 
{
  talkMIDI( (0x80 | channel), note, release_velocity);
}

//Plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that data values are less than 127
void talkMIDI(byte cmd, byte data1, byte data2) 
{
  digitalWrite(STAT1,HIGH);

  MusicSerial.write(cmd);
  MusicSerial.write(data1);

  //Some commands only have one data byte. All cmds less than 0xBn have 2 data bytes 
  //(sort of: http://253.ccarh.org/handout/midiprotocol/)
  if( (cmd & 0xF0) <= 0xB0)
  {
    MusicSerial.write(data2);
  }
  digitalWrite(STAT1,LOW);
}

Sunday, 26 January 2014

Arduino MIDI Synth No.1

It isn't particularly elegant, cheap or clever, but it is a start. Done hundreds of other places as well, but it's my start. Arduino MIDI Synth starting position.

I've simply assembled the SparkFun MIDI Shield (without buttons) on top of the SparkFun Musical Instrument Shield which then sits on top of a Seeeduino. The Musical Instrument Shield has been soldered up with through headers (from Proto-PIC).

The input is from a Roland PC-300 keyboard that I picked up for a fiver on e-bay. This makes simple sounds, but now allows the keyboard to do something powering both from a dual USB wall-wart. The output is into headphones direct from the shield.

What it also allows me is a base to start playing around with some things (like the previously mentioned  Marmonizer) with just a few hours of evening assembly. 

The buttons were not wired up due to the well commented oversight from SparkFun of wiring the buttons to pins D3 + D4. Button D2 could have been wired up, but a single button is not all that exciting really. Shame that this was not thought of as the two make obvious companions. This can be worked around with a bit of hard wiring as show below from naught101. As he says, makes a nice hack-able synth for 60quid. 




I used naught101's code which he kindly posted up to get started with a few mods as Serial1 was not available. Please respect the Beerware license and by Nathan a beer if you see him.

I've included this for comprehensiveness and as I expect to use it as a basis for building future ideas.

BTW, I know the convention is to use K&R styling on the brackets for compactness etc... , but after having this beaten out of me during my industrial placement year over 20 years ago I still find it hard to write any code this way myself.

/////////////////////////////////////////////////////////////////////////////
// Code released under the Beerware license. Public domain, but if you meet
// any of the authors, feel free to buy them a drink.
//
// Authors:
//   Nathan Seidle: Spark Fun Music instrument shield example code,
//     https://www.sparkfun.com/products/10587
//   Unknown: Spark Fun MIDI shield, https://www.sparkfun.com/products/9595
//   naught101: arduino_midi_box,
//     https://bitbucket.org/naught101/arduino_midi_box
//   hondrou: modified to work with my basic setup
//     hondrouthoughts.blogspot.com 
//
// The code is desiged to work with the Spark Fun Music instrument shield
// (https://www.sparkfun.com/products/10587), and the Spark Fun MIDI shield
// (https://www.sparkfun.com/products/9595). The two shields have a couple of
// conflicting pins (digital 3 and 4, used for MIDI shield buttons, and Music
// shield MIDI in and reset). To get around this, the code assumes that the 
// buttons have not been wired up 
//
/////////////////////////////////////////////////////////////////////////////

// Standard Arduino Pins
#define PIN_LED 13
#define PIN_RX 0
#define PIN_TX 1

// defines pin setups for MIDI Shield components only
// knobs are analogue
#define MIDI_PIN_KNOB1  0
#define MIDI_PIN_KNOB2  1

// status lights
#define MIDI_PIN_STAT1  7
#define MIDI_PIN_STAT2  6

// Music shield pins - both digital
#define MUSIC_PIN_MIDI_IN  3 // soft serial TX -> VS1053 RX
#define MUSIC_PIN_RESET  4   // VS1053 RESET

// MIDI status
#define MIDI_STATUS_NOTE_ON 0x80
#define MIDI_STATUS_NOTE_OFF 0x90
#define MIDI_STATUS_CONTROL_CHANGE 0xB0
#define MIDI_STATUS_PROGRAM_CHANGE 0xC0

#include <SoftwareSerial.h>

SoftwareSerial MusicSerial(2, 3); //Soft TX on 3, we don't use RX in this code

/////////////////////////////////////////////////////////////////////////////
//  Setup routine             
/////////////////////////////////////////////////////////////////////////////

void setup() 
{
  pinMode(MIDI_PIN_STAT1, OUTPUT);
  pinMode(MIDI_PIN_STAT2, OUTPUT);

  for(int i = 0; i < 3; i++) // flash MIDI Sheild LED's on startup
  {
    digitalWrite(MIDI_PIN_STAT1, HIGH);
    digitalWrite(MIDI_PIN_STAT2, LOW);
    delay(100);
    digitalWrite(MIDI_PIN_STAT1, LOW);
    digitalWrite(MIDI_PIN_STAT2, HIGH);
    delay(100);
  }
  digitalWrite(MIDI_PIN_STAT1, HIGH);
  digitalWrite(MIDI_PIN_STAT2, HIGH);

  // start hardware serial with midi baudrate 31250
  Serial.begin(31250);

  // Setup soft serial for MIDI control of Music Shield
  MusicSerial.begin(31250);

  // Reset the VS1053
  pinMode(MUSIC_PIN_RESET, OUTPUT);
  digitalWrite(MUSIC_PIN_RESET, LOW);
  delay(100);
  digitalWrite(MUSIC_PIN_RESET, HIGH);
  delay(100);
}

/////////////////////////////////////////////////////////////////////////////
//  Main loop             
/////////////////////////////////////////////////////////////////////////////

void loop () 

{


  // MIDI to Music
  int byte_count = Serial.available();
  if (byte_count > 0) 
  {
    // If we have serial data incoming, record it.
    byte midi_bytes[byte_count];
    for (int i = 0; i < byte_count; i++) 
    {
      midi_bytes[i] = Serial.read();
    }
    
    //Send straight to the sound module:
    music_write_midi(midi_bytes, byte_count);
  }

}

/////////////////////////////////////////////////////////////////////////////
//  Functions             
/////////////////////////////////////////////////////////////////////////////



// Sends MIDI data to Music Instrument Sheild, and flashes the LED
void music_write_midi(byte* midi_bytes, int byte_count) 
{
  digitalWrite(MIDI_PIN_STAT1, LOW);
  for(int i = 0; i < byte_count; i++)
  {
     MusicSerial.write(midi_bytes[i]);
  }
  digitalWrite(MIDI_PIN_STAT1, HIGH);
}


Unfortunately the debugging to the Serial port is not possible with the Seeeduino I have so I had to remove those lines of code although very helpful.

When uploading this and running remember that the Tx/Rx pins are being used for the MIDI In/Out in the shield. There is a switch for toggling Prog mode and Run mode. Switch to Prog when uploading the program and Run obviously when running.


Now the playground is in place I'm ready for more experiments later in the week ....

Thursday, 23 January 2014

Gordophone and Gotharman

Read through all the posts on the Gordophone blog earlier in the week. Excellent introduction to DIY Wind Controllers and certainly helped me thinking through some of the MIDI paramters that need tweaking to make a project work.

I also really liked the idea of the Marmonizer project and am going to knock one of these up myself to have a play around with.

Googling around it seems like there are quite a few little MIDI-bending boxes around so there are some good possibilities for some fun little projects. I particularly like the Note Randomizer box made by Gotharman.

The description of the MKII from Gotharman is as follows:

Gotharman’s note randomizer MKII are a MIDI effect, that treats incoming MIDI note on and off's. When it receives note data, it takes a look at the adjustable probability parameter, and the velocity value, if Velo to Prob is other than zero, and randomly decides if it will play that note or not. If it decides to play the note, it will have a look at the adjustable random velocity parameter, and if this is higher than zero, it will randomize the note velocity value with the adjusted amount. 

If the Time parameter is turned up, it will also create a random time delay, before it puts the note-on or off thru to the output.

This effect is very useful for creating acoustic-like "strumms" out of chords, for making "sterile" computer timing more alive, or just for creating variations in otherwise repeating patterns.




Seeeduino

I'm posting this to admit to a 'doh' moment. I have had a Seeeduino sitting the draw for a year now and have been scratching my head with various little Arduino projects frustrated that the standard Arduino board is not plugable into prototyping boards yet liking the ease of plugging some pre-made sheilds in for a quick make. The obvious reason for wanting to plug into the proto-boards is to make all the other wiring neat and easy to fit rather than cramming onto another shield.

What I failed to spot is the Seeeduino does a very nice job of having the upper plugs in the shield pattern, but offsetting the holes for the matching pins so they can fit onto protoboard. Genius! I'm sure everyone else in the community spotted this, I just didn't make the connection.


See the offset on the AREF - D8 pins at the top, this means it will fit onto a protoboard.

With this in mind I've ordered another couple of boards for some projects I have in mind and hopefully will get round to posting up.


I'm also liking the idea of the Stalker that I only noticed today.... hmmm, have some ideas for that too...



Friday, 17 January 2014

Melodeons and Accordions

Driven by my wonderful wife's love of the accordion I've been looking for some time at a project to build a similar type of electronic device.

First of all, I needed to do a bit of basic learning, so for the benefit of everyone:

Accordions are the general term, with the ones with the piano keys on the side usually referred to as accordions or piano accordions.

Accordion.jpg


Melodeons are the ones with buttons and go with with the general name 'diatonic button accordions'.

Accordéon diatonique.jpg

Personally I prefer the look of melodeons and from the Make point of view the keys are appealing to be able to put into a case and wire up.










New Year - new blog

Been meaning to do this for a long time and the turn of the New Year is the prompt to start.

I've been looking for some time at electronic musical devices and the cool Makes that are online and decided to resurrect some of the projects that have been on a backburner for a while waiting for the cold, dark winter months.

Will be trying to keep up posting the things I've been looking at with Ardunio projects, code and ideas to capture them online in addition to the set of Moleskines that I'm carrying around. We'll see if I can also start writing up some of the many notes on audio, video, software from the old notebooks.