Thursday, April 17, 2014

Android + Galaxy S4 + Touch OSC + Max + OS X



A simple demonstration of using TouchOSC on Android with Max via UDP and Route. Note that the external OSC namespace library is not used.


Download Max patch here: http://milkcrate.com.au/_other/downloads/max_patches/TouchOSC_Android.maxpat

Tuesday, April 15, 2014

notes2multiCCramps - M4L Device


Download here: http://milkcrate.com.au/_other/downloads/M4L/little-scale.note2multiCCramps.amxd



gen~: First 32 Primes Modulo at Audio Rate

RedBoard Tutorial 3: Simple MIDI Note Button Controller

Introduction
This tutorial series is intended for people that attended the Arduino / RedBoard Beginner's Workshop at Square Sounds Festival in Melbourne in April, 2014 (though it may be of use to others). This particular tutorial deals with how to make a very simple MIDI controller that generates a note by pressing a button. 




Hardware Setup
The hardware setup for this is relatively straightforward. However, it should be noted that this example will be using "pull up resistors" that are internal to the Red Board - and that these resistors can be externalised (thus making the programming slightly simpler but the physical circuit more complex). The button will be read via a digital input pin, which can be either HIGH or LOW (i.e. 1 or 0).

Let's start with a blank breadboard and a blank Red Board!

This is a simple push button. Normally, there is no connection between the two pairs of legs. When you push the button, a connection is made allowing current and voltage to pass through. We can use this to test whether or not the button has been pushed.

Place the button on the breadboard.


Add a connection between Arduino Red Board digital pin 2 and one leg of the button.



Add another connection between ground and the other leg of the button.



Pull Up Resistor
The above diagram represents the situation of a pull up resistor. The button element has two states - either connected (when the points marked "1" and "2" are joined i.e. the button is pushed down) or disconnected (when the points marked "1" and "2" are not joined i.e. the button is not pushed down).

If the button is up (i.e. disconnected), then the digital input pin is connected to the 5V signal via the 10k resistor. The digital input pin reads a HIGH state (which somewhat paradoxically corresponds to the button NOT being pressed).

If the button is down (i.e. connected), then the digital input pin will take the path of least resistance, and will be connected to the ground instead - as there is no resistor in place. The digital input pin reads a LOW state (which somewhat paradoxically corresponds to the button BEING pressed).

Without the 10k resistor and 5V path, the digital input pin would simply pick up noise whenever the button is disconnected - hence, the 10k resistors pulls up the digital input pin to 5V whenever there is no connection to ground via the button.

We could have built this circuit on the breadboard using the 10k resistor from the previous exercise. However, the Red Board has the pull up resistor and the 5V connection already on board - internally. There is no need to complicate the hardware setup.

Hence, the hardware setup is very straightforward.



Programming and Code
To actually read the value of the button, the Red Board must be programmed in some way. Let's summarise what the program should do:
• Read the current value of the button
• If the value is different than the previous time the button was read, send a musical note event
• If the button has gone from being de-pressed to being pressed, the musical note event should be a "note on" event, signifying the start of a note
• If the button has gone from being pressed to being de-pressed, the musical note event should be a "note off" event, signifying the end of a note

Although there are a few methods for achieving this, a (hopefully) easy to follow method is as follows:

Let's go through this code structure by structure and line by line.

The first two lines create two variables that are called "current_button_state" and "previous_button_state". This means that we are reserving two bytes within our working memory (RAM) that can be referred to by these names anywhere in our program.

These bytes can be written to, like so:
 current_button_state = 1;

And these bytes can be read from, like so:
some_other_variable = current_button_state;

However, in order to achieve any of that, they had to be defined. Because we've defined them before the setup() function, they are then available to us throughout the program structure.

In setup(), we first open up a serial port just like in RedBoard Tutorial 2 in the line Serial.begin(57600);. pinMode(2, INPUT) sets up digital pin two as a digital input pin.

The final line of setup() curiously performs a "digitalWrite" command - even though pin 2 is actually an input. This line activates the internal pull up resistor, as discussed previously.

The first line of current_button_state reads the value of the digital pin 2 (our button) as a data value. But there's a twist. The code is:
current_button_state = 1 - digitalRead(2);

Now, keep in mind that if the button is pressed, digitalRead(2) will equal 0 and if the button is not pressed, digitalRead(2) will equal 1. Therefore, we can subtract digitalRead(2) from 1 to flip around that logic.

If the button is pressed:
current_button_state = 1 - digitalRead(2) = 1 - 0 = 1

If the button is not pressed:
current_button_state = 1 - digitalRead(2) = 1 - 1 = 0

This makes more sense to us logically, and will be useful later on.

The next line of code is a conditional IF statement, and means that if the current state of the button is not the same as the previous state, execute some block of code that is contained within the curly brackets { and }.

The next five lines only occur if there is a change in the state of the button. Otherwise we would be sending continuous note on / note off events!

previous_button_state = current_button_state;
Serial.write(0x90);
Serial.write(60);
Serial.write(current_button_state * 127);
delay(5);

The first line out of these five is very important. It stores the current value of the button into the previous value. Thus, when the loop() code runs again, the previous value will be stored and a valid comparison to the (future) current value can be made.

The next three lines deal with generating a note on or note off message. Once again, details about the MIDI protocol can be found here, however a note message has three main components:

• A channel from 1 - 16
• A note (pitch) number from 0 - 127 where 60 is equal to middle C
• Each increment in pitch number is traditionally equal to a semitone
• A velocity value from 0 - 127 where 0 is off (silence / note-off) and 127 is maximum velocity
• Velocity may correspond to volume, tone colour and / or a number of other attributes

The format for this type of message is as follows:
byte 1: the value 144 plus the channel number from 0 to 15 (note: 144 as hexadecimal is 0x90)
byte 2: the pitch value from 0 to 127
byte 3: the velocity value from 0 to 127


Let's re-examine the three Serial.write statements:
Serial.write(0x90);
Serial.write(60);
Serial.write(current_button_state * 127);

Each note event will have:
• A channel number of 1
• A pitch value of 60 (middle C)
• A velocity that is determined by the current_button_state multiplied by 127.

The current button state is either 1 or 0, depending on if the button is pressed or de-pressed. If the current button state is pressed, then the formatted message will be: a note-on event with a channel of 1, a pitch of 60 and a velocity of 127. If the current button state is de-pressed, then the formatted message will be: a note-off event with a channel of 1, a pitch of 60 and a velocity of 0 .

A short delay ensures that mechanical false positive triggering of the button does not cause unwanted note-on or note-off events. This delay after a mechanical button or switch is called "de-bouncing".




Software Setup and Mapping
Once the code has been uploaded to the Red Board, make sure to launch and set up the SerialThing app. A note-on or note-off event will be sent for every press / de-press of the button that occurs. Watch the video below for a brief demonstration. Make sure that the virtual bus to where SerialThing is routed is also set as an input device in the music software.









RedBoard Tutorial 2: Simple Continuous MIDI Controller

Introduction
This tutorial series is intended for people that attended the Arduino / RedBoard Beginner's Workshop at Square Sounds Festival in Melbourne in April, 2014 (though it may be of use to others). This particular tutorial deals with how to make a very simple continuous MIDI controller.



Red Board Hardware


This is the Red Board. Different pins are marked as having different functions - including 5V and ground for power, analog in (for sensors) and digital (for buttons, LEDs, anything that goes ON and OFF).

The Red Board is programmed using the Arduino IDE, which can be downloaded from http://arduino.cc/.

In this tutorial, a sensor (potentiometer, flex, light and touch) is connected to an analog in. The analog in can take a voltage between 0V(ground) and 5V, and converts this to a number between 0 and 1023 (relative to the voltage). The resulting data is sent to a music program to control a parameter. Let's build the hardware first, and then set up and program the Red Board.



Connecting up the Hardware






This is a bread board. The breadboard has two columns (which in the above image are horizontal), divided up into many rows per column. Each row has five holes. Each row of five holes are electrically connected, so that placing the leg of one component (or a breadboard jumper wire) in one hole connects that component to the four other holes in that row.

This is a pot. The pot can turn approximately 270˚. The pot has three legs.

Place the pot onto the breadboard, so that each leg of the pot occupies a different, separate row.

Connect the ground pin of the Red Board to one of the outside legs of the pot, by putting it in the appropriate row.

Connect the 5V pin of the Red Board to the other outside leg of the pot.








Voltage Divider
Connect analog in 0 to the middle leg of the pot. This type of configuration is called a voltage divider, and is a very common circuit. Two resistive components sit in-between a voltage rail (in this case 5V0 and ground. At the junction where the two resistive components meets, the output voltage will be equal to the ratio of one resistive component compared to the other in respect to the voltage rail.



For example, if resistive component A is 3kΩ and resistive component B is 2kΩ, then the output voltage where A and B meet will be 2V. if A is 4kΩ and B is 16kΩ, then the voltage output will be 4V.

A number of different components act as a resistor in this scenario. A common one is the pot. Essentially, from one outer leg to the middle leg is resistive component A, and from the middle leg to the other outer leg is resistive component B.




Red Board Code
To actually read the value of the pot, the Red Board must be programmed in some way. Let's summarise what the program should do:
• Read the value of the analog input
• Send the value of the analog input as a message that a music program can understand (MIDI)
• Repeat indefinitely

The most straightforward way of achieving this is with the following code, or something similar:


Let's go through this code line by line, structure by structure. Every Arduino program (a "sketch") is made up of at least two main structures - a setup() function and a loop() function.

The setup() function is block of code that is executed once when the Arduino is powered up and turned on.

The loop() function is  block of code that is repeated on loop forever until the Arduino is powered down and turned off. Of course, conditioning branches and structures can be written as part of loop() to create complex programs and actions, depending on various factors.

The only line in the setup() function is:
Serial.begin(57600);

This line of code opens up a serial port - in this case via the USB bus - using a baud rate (speed) of 57600.

The loop() function is a set of four lines:
Serial.write(0xb0);
Serial.write(0x01);
Serial.write(analogRead(0) / 8);
delay(5);

The Serial.write() command sends a single byte of data to the serial port. Keep in mind that the data needs to be formatted in a way that our music software will eventually understand. To this end, the data from the analog input is formatted as a continuous controller MIDI message.

This type of message is typically used for faders, knobs and buttons on a USB MIDI controller. If you are unfamiliar with MIDI and continuous controllers, it is suffice to say that:
• Part of MIDI is a language, with "room" for 16 channels with notes, volumes, etc etc
• A continuous controller message is one type of MIDI message that can be sent
• A continuous controller message will have a channel number from 1 - 16
• A continuous controller message will be one of 128 different controller path ways for that particular channel
• A continuous controller message will have a value of 0 - 127

One way of thinking about continuous controller messages is to imagine a theoretical MIDI console that has 128 different physical faders. Each fader needs its own "pathway" to send data along - and thus, a continuous controller message can be used to send the value of a particular fader to a computer. More information about MIDI can be found here.

The format for this type of message is as follows:

byte 1: the value 176 plus the channel number from 0 to 15 (note: 176 as hexadecimal is 0xb0)
byte 2: the controller number (i.e. which "fader" is it that is being controlled) from 0 to 127
byte 3: the controller value (i.e. what is the value of the fader as defined in byte 2) from 0 to 127

Back to our Red Board Arduino code, the first two lines of the loop() function are:
Serial.write(0xb0);
Serial.write(0x01);

These two lines correspond directly with byte 1 and byte 2 of the continuous controller message. Serial.write(0xb0) indicates that we are sending a continuous controller message via channel 1. Serial.write(0x01); indicates that we are sending a continuous controller message via controller number 1.

The third line of code is:
Serial.write(analogRead(0) / 8);

Here, the voltage that is present at analog input pin 0 is read as a data value. The number that is returned is somewhere between 0 and 1023. As the value range of a continuous controller message is limited to 0 to 127, the range 0 - 1023 is divided by 8 to always guarantee a value between 0 - 127. Serial.write(analogRead(0) / 8); indicates that we are sending a continuous controller value between 0 - 127, depending on the position of the pot.

The final line of the loop() function is delay(5);, which is simply a short delay of five milliseconds before the loop is repeated. 





Software Setup and Mapping
Once the code has been uploaded to the Red Board, make sure to launch and set up the SerialThing app. A stream of controller values will be sent out of the app and can be used to control a parameter in music software, as shown below.



The basic software setup is:
• Upload code to Red Board
• Set up SerialThing
• Open music software and map controller 1 channel 1 to a parameter
• The method for the step above is different for various music software packages

In Ableton Live, the process is simple:
• Enter MIDI mapping mode by pressing command M
• Click on the onscreen parameter that is to be mapped
• Move the pot
• Confirm the channel and continuous controller number
• Exit MIDI mapping mode by pressing command M
• Move the pot to confirm that the mapping works, and that the pot is controlling the onscreen parameter





Component Replacement and Experimentation
Note that the exact same code and circuit can be used with other components, as shown below:




Light dependent resistor and 10kΩ resistor. The light dependent resistor goes to 5V and analog in. The 10kΩ resistor goes to analog in and ground.







Flex sensor and 10k resistor. The flex sensor goes to 5V and analog in. The 10kΩ resistor goes to analog in and ground.






Touch slider. The three legs of the touch slider go to 5V, analog in and ground respectively (equivalent to a potentiometer).