Processing Android – Playing Sounds

How to play sounds on Android, using Processing Android.

The minim library doesn’t work on Android, so use APWidgets instead.

Example:

import apwidgets.*;
APMediaPlayer player;

void setup()
{
  player = new APMediaPlayer(this);

  // SOUND FILE IS LOCATED IN THE '/data' FOLDER
  player.setMediaFile("sound.mp3");
}

void keyPressed()
{
  if (key == 's') player.start();
}

Resources:
http://forum.processing.org/one/topic/audio-for-android.html
https://code.google.com/p/apwidgets/


Calculations – MIDI Calculations

For use with the MIDI (Musical Instrument Digital Interface) standard, a frequency mapping is defined by:

p = 69 + 12 \times \log_2{f \over 440 \,\text{Hz}}

In Processing:

// log2(x) = log(x) / log(2);
final static float LOG2 = log(2);

int freqToPitch(float freq)
{
  return int(69+12*log(freq/440)/LOG2);
} // freqToPitch()

Where p is the MIDI note number. And in the opposite direction, to obtain the frequency from a MIDI note p, the formula is defined as:

f=2^{(p-69)/12} \times 440\,\text{Hz}

In Processing:

float pitch2Freq (int p)
{
  return pow(2,(float)(p-69)/12) * 440;
} // pitch2Freq()

For notes in an A440 equal temperament, this formula delivers the standard MIDI note number (p). Any other frequencies fill the space between the whole numbers evenly. This allows MIDI instruments to be tuned very accurately in any microtuning scale, including non-western traditional tunings.

midinotes

Calculate the note name from a note pitch (in Processing):

final static String[] names = {
  "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"
};

void setup()
{
  println(pitch2Name(90));
} // setup()

String pitch2Name (int pitch)
{
  return names[pitch%12]+((pitch/12)-1);
} // pitch2Name()

=> "Gb6"

Instrument groups:

  1. Piano
  2. Chromatic Percussion
  3. Organ
  4. Guitar
  5. Bass
  6. String
  7. Ensemble
  8. Brass
  9. Reed
  10. Pipe
  11. Synth Lead
  12. Synth Pad
  13. Synth Effects
  14. Ethnic
  15. Percussive
  16. Sound Effects

Resources:
http://en.wikipedia.org/wiki/Note

MIDI Note Number to Frequency Conversion Charts:
http://subsynth.sourceforge.net/midinote2freq.html
http://www.phys.unsw.edu.au/jw/notes.html

MIDI Instrument Map:
http://en.wikipedia.org/wiki/General_MIDI

MIDI Android Library:
https://code.google.com/p/android-midi-lib/