Hello,

I've been looking all over the internet but I cant figure out what I did 
wrong. So I'm trying to graphicly show the audio recording. So it has to be 
realtime. I checked all my points and they are normal. But I get a lot of 
lines (if I use the GL10.GL_LINES) or a lot of static points (when using 
GL10.GL_POINTS). Now I don't know where the lines come from and why they're 
not removed. Anyone that has an idea? Or a great example with a refreshing 
of the vertex array every ondraw method.

I added my renderer class and a screenshot from my phone when testing (made 
with GL_LINES, points is a bit hard to see). 

Any help would be great.
Thanks,
Karl

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

<<attachment: device-2012-10-24-155523.png>>

package android.renderer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.application.HeartMonitorMain;
import android.listeners.OnNewDataAvailableListener;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.util.Log;

public class VisualizerRenderer implements Renderer, OnNewDataAvailableListener {
	private static final String TAG = "VisualizerRenderer";

	private static final int bufferSize = 2878;
	private VisualizerView main;
	private FloatBuffer buffer = FloatBuffer.allocate(bufferSize);
	private int counter = 0;
	private float cameraDistance = 10f;
	private float openGlViewWidth = 10f;
	private float amplitude = 0;
	public boolean autoAmplitude = true;
	private long previousFrame;

	public VisualizerRenderer(VisualizerView main) {
		if (main == null) {
			throw new IllegalArgumentException(
					"We need a view to retrieve data!");
		}
		this.main = main;
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		previousFrame = System.currentTimeMillis();
		gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
		gl.glClearDepthf(Math.abs(cameraDistance) + 1);
		gl.glViewport(0, 0, main.getWidth(), main.getHeight());
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		Log.d(TAG + ".onSurfaceChanged", "Changed size (" + width + "," + height + ")");
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glLineWidth(5f);
		float ratio = (float) width / height;
		gl.glClearDepthf(Math.abs(cameraDistance) + 1f);
		gl.glFrustumf(-ratio, ratio, -1f, 1f, 1f, Math.abs(cameraDistance) + 1);
		
		ByteBuffer bBuffer = ByteBuffer.allocateDirect(bufferSize * 4);
		bBuffer.order(ByteOrder.nativeOrder());
		buffer = bBuffer.asFloatBuffer();
		openGlViewWidth = Math.abs(cameraDistance) * 2f * ratio;
		buffer.put(new float[] { -openGlViewWidth / 2f, 0,
				openGlViewWidth / 2f, 0 });
		counter = 4;
	}

	@Override
	public void onDrawFrame(GL10 gl) {
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();

		GLU.gluLookAt(gl, 0f, 0f, cameraDistance, 0f, 0f, 0f, 0f, 1f, 0f);
		gl.glFrontFace(GL10.GL_CW);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glColor4f(1f, 0f, 0f, 0f);
		if (counter != 4) {
			gl.glLineWidth(5f);
		} else {
			gl.glLineWidth(0.1f);
		}
		
		buffer.position(0);
		gl.glVertexPointer(2, GL10.GL_FLOAT, 0, buffer);
		gl.glDrawArrays(GL10.GL_POINTS, 0, counter);

		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glFlush();
		synchronized(this){
			gl.glFinish();
		}
		int error = gl.glGetError();
		if (error > 0) {
			Log.e(TAG + ".onDrawFrame", "OpenGL end error = " + error);
		}
		long fps = (System.currentTimeMillis() - previousFrame);
		if(fps > 0){
			HeartMonitorMain.getInstance().setFps((int) Math.floor(1000/fps));
		}
		HeartMonitorMain.getInstance().setAmplitude();
		previousFrame = System.currentTimeMillis();
	}

	@Override
	public void newDataAvailable(byte[] buffer) {
		Thread t = new Thread(new VisualizeDataMonoRunnable(buffer));
		t.start();
	}

	@Override
	public void endOfDataInput() {
		main.onPause();
		ByteBuffer bBuffer = ByteBuffer.allocateDirect(bufferSize * 4);
		bBuffer.order(ByteOrder.nativeOrder());
		buffer = bBuffer.asFloatBuffer();

		buffer.clear();
		buffer.position(0);
		buffer.put(new float[] { -openGlViewWidth / 2f, 0,
				openGlViewWidth / 2f, 0 });
		counter = 4;
	}

	@Override
	public void startOfDataInput() {
		main.onResume();
	}

	private class VisualizeDataMonoRunnable implements Runnable {

		private byte[] buffer;

		public VisualizeDataMonoRunnable(byte[] buffer) {
			super();
			if (buffer == null) {
				throw new IllegalArgumentException("No data");
			}
			this.buffer = buffer;
		}

		@Override
		public void run() {
			FloatBuffer localBuffer = FloatBuffer
					.allocate(buffer.length > bufferSize ? bufferSize : buffer.length);
			int localCounter = localBuffer.capacity();
			if (localCounter > 0) {
				short[] shorts = new short[(localCounter) / 2];
				ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN)
						.asShortBuffer().get(shorts);

				if (autoAmplitude) {
					short maxAmpl = 0;
					for (short s : shorts) {
						if (maxAmpl < Math.abs(s)) {
							maxAmpl = (short) Math.abs(s);
						}
					}
					if (maxAmpl > amplitude) {
						amplitude = maxAmpl;
					}
				}

				float ratio = Math.abs(cameraDistance)
						* main.getWidth() / main.getHeight();
				float sampleSize = (ratio * 4f) / (localCounter);
				float currentSample = -ratio + (sampleSize / 2f);
				localBuffer.position(0);
				boolean notFirst = false;
				for (short s : shorts) {
					float f = (s / amplitude) * ratio;
					localBuffer.put(currentSample);
					localBuffer.put(f);
					if(notFirst){
						localBuffer.put(currentSample);
						localBuffer.put(f);
					}else{
						notFirst = false;
					}
					currentSample += sampleSize;
				}
				synchronized (VisualizerRenderer.this) {
					localBuffer.position(0);
					VisualizerRenderer.this.buffer.position(0);
					VisualizerRenderer.this.buffer.put(localBuffer);
					VisualizerRenderer.this.counter = localCounter;
				}
			} else {
				synchronized (this) {
					VisualizerRenderer.this.buffer.position(0);
					VisualizerRenderer.this.buffer.put(new float[] {
							-openGlViewWidth / 2f, 0,
							openGlViewWidth / 2f, 0 });
					VisualizerRenderer.this.counter = 4;
				}
			}
		}
	}

	public short getAmplitude() {
		return (short) amplitude;
	}

	public void setAmplitude(float amplitude) {
		setAmplitude((short) amplitude);
	}

	public void setAmplitude(int amplitude) {
		this.setAmplitude((short) amplitude);
	}

	public void setAmplitude(short amplitude) {
		if (amplitude > 0) {
			this.amplitude = amplitude;
		}
	}
}

Reply via email to