Skip to content

Instantly share code, notes, and snippets.

@eed3si9n
Created May 21, 2011 23:09
Show Gist options
  • Save eed3si9n/984985 to your computer and use it in GitHub Desktop.
Save eed3si9n/984985 to your computer and use it in GitHub Desktop.
package com.eed3si9n.pikopiko;
import com.eed3si9n.pikopiko.R;
import android.content.Context;
import android.graphics.*;
import android.media.*;
import android.graphics.Paint.Style;
public class PikoPikoEngine {
private static final int LOCAL_WIDTH = 6;
private static final int LOCAL_HEIGHT = 8;
private static final int k_defaultPriority = 1;
private static final int k_noLoop = 0;
private static final float k_defaultRate = 1.0f;
private static final float k_defaultVolume = 0.8f;
private static final int k_unit = 50;
private static final int k_margin = 20;
// http://en.wikipedia.org/wiki/Just_intonation
private static final int SA_CENT = 0;
private static final int RE_CENT = 204;
private static final int GA_CENT = 386;
private static final int MA_CENT = 498;
private static final int PA_CENT = 702;
private static final int DHA_CENT = 906;
private static final int NI_CENT = 1088;
private static Paint s_circlePaint;
private Context m_context;
private SoundPool m_soundPool;
private int m_geId;
private int m_naId;
private boolean m_initialized;
private int m_geStreamId;
private int m_naStreamId;
private int [] m_geCents;
private static Paint getCirclePaint() {
if (s_circlePaint == null) {
s_circlePaint = new Paint();
s_circlePaint.setAntiAlias(true);
s_circlePaint.setARGB(100, 100, 100, 100);
s_circlePaint.setStyle(Style.FILL);
} // if
return s_circlePaint;
}
public PikoPikoEngine(Context a_context) {
m_context = a_context;
m_initialized = false;
m_geCents = new int[7];
m_geCents[0] = SA_CENT;
m_geCents[1] = RE_CENT;
m_geCents[2] = GA_CENT;
m_geCents[3] = MA_CENT;
m_geCents[4] = PA_CENT;
m_geCents[5] = DHA_CENT;
m_geCents[6] = NI_CENT;
}
public void initialize() {
if (m_initialized) {
return;
} // if
m_initialized = true;
m_soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
m_naId = m_soundPool.load(m_context, R.raw.na1, k_defaultPriority);
m_geId = m_soundPool.load(m_context, R.raw.ge1, k_defaultPriority);
}
public void playGe() {
playGe(0f);
}
public void playGe(float a_rate) {
initialize();
if (m_geStreamId > 0) {
m_soundPool.stop(m_geStreamId);
} // if
m_geStreamId = m_soundPool.play(m_geId, k_defaultVolume, k_defaultVolume,
k_defaultPriority, k_noLoop, a_rate);
}
public void modulateGe() {
initialize();
if (m_geStreamId == 0) {
return;
} // if
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // try-catch
float f = 1.0f;
for (int i = 0; i < 70; i++) {
f += 0.01f;
m_soundPool.setRate(m_geStreamId, f);
try {
Thread.sleep(3);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // for i
m_soundPool.stop(m_geStreamId);
}
public void playNa() {
initialize();
m_naStreamId = m_soundPool.play(m_naId, k_defaultVolume, k_defaultVolume,
k_defaultPriority, k_noLoop, k_defaultRate);
}
private float convertLocalToScreen(int a_value) {
return a_value * k_unit + k_margin;
}
private int convertScreenToLocal(int a_value) {
return (a_value - k_margin) / k_unit;
}
public void draw(Canvas a_canvas, int a_width, int height) {
for (int x = 0; x < LOCAL_WIDTH; x++) {
for (int y = 0; y < LOCAL_HEIGHT; y++) {
a_canvas.drawRect(convertLocalToScreen(x),
convertLocalToScreen(y),
convertLocalToScreen(x) + k_unit - 1,
convertLocalToScreen(y) + k_unit - 1,
getCirclePaint());
} // y
} // for x
}
private float centToRate(int a_value) {
return 1.0f + (a_value / 1200.0f);
}
private int toPadNumber(int a_xScreen, int a_yScreen) {
int retval = -1;
int x = convertScreenToLocal(a_xScreen);
int y = convertScreenToLocal(a_yScreen);
if ((x < 0) || (x >= LOCAL_WIDTH)
|| (y < 0) || (y >= LOCAL_HEIGHT)) {
return retval;
} // if
retval = y * LOCAL_HEIGHT + x;
return retval;
}
public void addMove(int a_xScreen, int a_yScreen,
int a_oldXScreen, int a_oldYScreen) {
int oldNum = toPadNumber(a_oldXScreen, a_oldYScreen);
int num = toPadNumber(a_xScreen, a_yScreen);
if (oldNum == num) {
return;
} // if
if (m_geStreamId == 0) {
return;
} // if
if ((oldNum >= 0) && (oldNum < 4)
&& isGe(num)) {
float rate = centToRate(toGeCent(num));
m_soundPool.setRate(m_geStreamId, rate);
} // if
}
private boolean isGe(int a_padNumber) {
return ((a_padNumber >= 0) && (a_padNumber <= 4));
}
private int toGeCent(int a_padNumber) {
if (isGe(a_padNumber)) {
return m_geCents[a_padNumber];
} else {
return SA_CENT;
} // if-else
}
public void addTouch(int a_xScreen, int a_yScreen) {
int padNumber = toPadNumber(a_xScreen, a_yScreen);
if (isGe(padNumber)) {
playGe(centToRate(toGeCent(padNumber)));
} // if
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment